DocsComposantsToggle group
Toggle group
A set of toggles, single or multiple choice.
Alpine.js2 fichiers
<pb-ui.toggle-group type="multiple" value="bold">
<pb-ui.toggle-group-item value="bold" aria-label="Gras"><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-group-item>
<pb-ui.toggle-group-item value="italic" aria-label="Italique"><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"><line x1="19" x2="10" y1="4" y2="4"/><line x1="14" x2="5" y1="20" y2="20"/><line x1="15" x2="9" y1="4" y2="20"/></svg></pb-ui.toggle-group-item>
<pb-ui.toggle-group-item value="underline" aria-label="Souligné"><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 4v6a6 6 0 0 0 12 0V4"/><line x1="4" x2="20" y1="20" y2="20"/></svg></pb-ui.toggle-group-item>
</pb-ui.toggle-group>Installation
pyblade-ui add toggle-group- Installez les dépendances
Alpine.js, chargé dans votre
<head>. - Copiez ces fichiers dans votre projetcomponents/ui/toggle_group.html
@props({'type': 'single', 'value': ''}) <div role="group" data-slot="toggle-group" x-data="{ type: '{{ type }}', value: '{{ value }}'.split(',').filter(Boolean), has(v) { return this.value.includes(v) }, toggle(v) { if (this.type === 'single') { this.value = this.has(v) ? [] : [v]; return } this.value = this.has(v) ? this.value.filter(x => x !== v) : [...this.value, v]; }, }" {{ attributes.cn("flex w-fit items-center rounded-md [&>*]:rounded-none [&>*:first-child]:rounded-l-md [&>*:last-child]:rounded-r-md") }}>{{ slot }}</div>components/ui/toggle_group_item.html@props({'value': '', 'variant': 'default'}) <button type="button" data-slot="toggle-group-item" x-on:click="toggle('{{ value }}')" x-bind:aria-pressed="has('{{ value }}')" x-bind:data-state="has('{{ value }}') ? 'on' : 'off'" {{ attributes.cn( "inline-flex h-9 min-w-9 flex-1 shrink-0 cursor-pointer items-center justify-center gap-2 px-2 text-sm font-medium whitespace-nowrap transition-[color,box-shadow] outline-none hover:bg-muted hover:text-muted-foreground focus:z-10 focus-visible:z-10 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground [&_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 [&:not(:first-child)]:border-l-0'}[variant], ) }}>{{ slot }}</button> - Adaptez le chemin à votre projet
Le dossier
componentsest celui déclaré danspyblade.toml.
Utilisation
Les balises <pb-ui.toggle-group> <pb-ui.toggle-group-item> sont disponibles dans tous vos templates.
<pb-ui.toggle-group type="single" value="center">
<pb-ui.toggle-group-item value="left">Left</pb-ui.toggle-group-item>
<pb-ui.toggle-group-item value="center">Center</pb-ui.toggle-group-item>
<pb-ui.toggle-group-item value="right">Right</pb-ui.toggle-group-item>
</pb-ui.toggle-group>Exemples
Choix unique, outline
<pb-ui.toggle-group type="single" value="month">
<pb-ui.toggle-group-item value="day" variant="outline">Jour</pb-ui.toggle-group-item>
<pb-ui.toggle-group-item value="week" variant="outline">Semaine</pb-ui.toggle-group-item>
<pb-ui.toggle-group-item value="month" variant="outline">Mois</pb-ui.toggle-group-item>
</pb-ui.toggle-group>Props
<pb-ui.toggle-group>
| Prop | Défaut |
|---|---|
| type | 'single' |
| value | '' |
<pb-ui.toggle-group-item>
| Prop | Défaut |
|---|---|
| value | '' |
| variant | 'default' |
Tout autre attribut (class, id, x-on:click, hx-post…) est transmis à l'élément racine ; vos classes Tailwind l'emportent sur celles du composant.