DocsComponentsToggle
Toggle
A two-state button that can be on or off.
Alpine.js1 file
<pb-ui.toggle aria-label="Bold">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8"/></svg>
</pb-ui.toggle>Installation
pyblade-ui add toggle- Install the dependencies
Alpine.js, loaded in your
<head>. - Copy this file into your projectcomponents/ui/toggle.html
@props({'pressed': False, 'variant': 'default', 'size': 'default'}) <button type="button" data-slot="toggle" x-data="{ on: {{ str(bool(pressed)).lower() }} }" x-on:click="on = !on" x-bind:aria-pressed="on" x-bind:data-state="on ? 'on' : 'off'" {{ attributes.cn( "inline-flex cursor-pointer items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-[color,box-shadow] outline-none hover:bg-muted hover:text-muted-foreground disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", {'default': 'bg-transparent', 'outline': 'border border-input bg-transparent shadow-xs hover:bg-accent hover:text-accent-foreground'}[variant], {'default': 'h-9 min-w-9 px-2', 'sm': 'h-8 min-w-8 px-1.5', 'lg': 'h-10 min-w-10 px-2.5'}[size], ) }}>{{ slot }}</button> - Update the path to fit your project
The
componentsfolder is the one set inpyblade.toml.
Usage
The tag <pb-ui.toggle> is available in all your templates.
<pb-ui.toggle variant="outline" aria-label="Bold">B</pb-ui.toggle>Examples
Variants
<div class="flex gap-2">
<pb-ui.toggle>Default</pb-ui.toggle>
<pb-ui.toggle variant="outline" pressed>Outline</pb-ui.toggle>
<pb-ui.toggle variant="outline" size="sm">Small</pb-ui.toggle>
<pb-ui.toggle disabled>Disabled</pb-ui.toggle>
</div>Props
| Prop | Default |
|---|---|
| pressed | False |
| variant | 'default' |
| size | 'default' |
Any other attribute (class, id, x-on:click, hx-post…) is passed to the root element; your Tailwind classes win over the component's.