View as Markdown

Toggle

A two-state button that can be either on or off.

Loading...
<twig:Toggle variant="outline" size="sm" aria-label="Toggle bookmark">
    <twig:ux:icon name="lucide:bookmark" class="group-data-[state=on]/toggle:fill-current" />
    Bookmark
</twig:Toggle>

Installation

php bin/console ux:install toggle --kit shadcn

Install the following Composer dependencies:

composer require twig/extra-bundle twig/html-extra:^3.12.0 tales-from-a-dev/twig-tailwind-extra:^1.0.0

Copy the following file(s) into your app:

import { Controller } from &#039;@hotwired/stimulus&#039;;

export default class extends Controller {
    static values = { pressed: Boolean };

    connect() {
        if (!this.hasPressedValue) {
            this.pressedValue = this.element.getAttribute(&#039;aria-pressed&#039;) === &#039;true&#039;;
        }

        this.updateState();
    }

    toggle() {
        this.pressedValue = !this.pressedValue;
    }

    pressedValueChanged() {
        this.updateState();
    }

    updateState() {
        const pressed = this.pressedValue;
        this.element.setAttribute(&#039;aria-pressed&#039;, String(pressed));
        this.element.dataset.state = pressed ? &#039;on&#039; : &#039;off&#039;;
    }
}
{# @prop variant &#039;default&#039;|&#039;outline&#039; The visual style variant. #}
{# @prop size &#039;default&#039;|&#039;sm&#039;|&#039;lg&#039; The toggle size. #}
{# @prop pressed boolean Whether the toggle is initially pressed. #}
{# @block content The toggle label and/or icon. #}
{%- props variant = &#039;default&#039;, size = &#039;default&#039;, pressed = false -%}
{%- set style = html_cva(
    base: &quot;group/toggle inline-flex items-center justify-center gap-1 rounded-lg text-sm font-medium whitespace-nowrap transition-all outline-none hover:bg-muted hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-pressed:bg-muted data-[state=on]:bg-muted dark:aria-invalid:ring-destructive/40 [&amp;_svg]:pointer-events-none [&amp;_svg]:shrink-0 [&amp;_svg:not([class*=&#039;size-&#039;])]:size-4&quot;,
    variants: {
        variant: {
            default: &#039;bg-transparent&#039;,
            outline: &#039;border border-input bg-transparent hover:bg-muted&#039;,
        },
        size: {
            default: &#039;h-8 min-w-8 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2&#039;,
            sm: &quot;h-7 min-w-7 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&amp;_svg:not([class*=&#039;size-&#039;])]:size-3.5&quot;,
            lg: &#039;h-9 min-w-9 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2&#039;,
        },
    },
    default_variant: {
        variant: &#039;default&#039;,
        size: &#039;default&#039;,
    },
) -%}
&lt;button
    type=&quot;button&quot;
    class=&quot;{{ style.apply({variant: variant, size: size}, attributes.render(&#039;class&#039;))|tailwind_merge }}&quot;
    {{ attributes.defaults({
        &#039;data-slot&#039;: &#039;toggle&#039;,
        &#039;data-controller&#039;: &#039;toggle&#039;,
        &#039;data-action&#039;: &#039;click-&gt;toggle#toggle&#039;,
        &#039;aria-pressed&#039;: pressed ? &#039;true&#039; : &#039;false&#039;,
        &#039;data-state&#039;: pressed ? &#039;on&#039; : &#039;off&#039;,
    }) }}
&gt;
    {%- block content %}{% endblock -%}
&lt;/button&gt;

Usage

<twig:Toggle>
    Toggle
</twig:Toggle>

Examples

Outline

Use variant="outline" for an outline style.

Loading...
<div class="flex flex-wrap items-center gap-2">
    <twig:Toggle variant="outline" aria-label="Toggle italic">
        <twig:ux:icon name="lucide:italic" />
        Italic
    </twig:Toggle>
    <twig:Toggle variant="outline" aria-label="Toggle bold">
        <twig:ux:icon name="lucide:bold" />
        Bold
    </twig:Toggle>
</div>

With Text

Loading...
<twig:Toggle aria-label="Toggle italic">
    <twig:ux:icon name="lucide:italic" />
    Italic
</twig:Toggle>

Size

Use the size prop to change the size of the toggle.

Loading...
<div class="flex flex-wrap items-center gap-2">
    <twig:Toggle variant="outline" aria-label="Toggle small" size="sm">
        Small
    </twig:Toggle>
    <twig:Toggle variant="outline" aria-label="Toggle default" size="default">
        Default
    </twig:Toggle>
    <twig:Toggle variant="outline" aria-label="Toggle large" size="lg">
        Large
    </twig:Toggle>
</div>

Disabled

Loading...
<div class="flex flex-wrap items-center gap-2">
    <twig:Toggle aria-label="Toggle disabled" disabled>
        Disabled
    </twig:Toggle>
    <twig:Toggle variant="outline" aria-label="Toggle disabled outline" disabled>
        Disabled
    </twig:Toggle>
</div>

RTL

To enable RTL support, set the dir="rtl" attribute on the root element.

Loading...
<div class="flex flex-col gap-8">
    {# Arabic #}
    <twig:Toggle variant="outline" size="sm" aria-label="Toggle bookmark" dir="rtl">
        <twig:ux:icon name="lucide:bookmark" class="group-aria-pressed/toggle:fill-foreground" />
        إشارة مرجعية
    </twig:Toggle>

    {# Hebrew #}
    <twig:Toggle variant="outline" size="sm" aria-label="Toggle bookmark" dir="rtl">
        <twig:ux:icon name="lucide:bookmark" class="group-aria-pressed/toggle:fill-foreground" />
        סימנייה
    </twig:Toggle>
</div>

API Reference

<twig:Toggle>

Prop Type Default
variant 
'default'|'outline' 'default'
size 
'default'|'sm'|'lg' 'default'
pressed 
boolean false
Block Description
content The toggle label and/or icon.