Switch

A toggle control that switches between on and off states.

Loading...
<div class="flex items-center space-x-2">
    <twig:Switch id="airplane-mode" />
    <twig:Label for="airplane-mode">Airplane Mode</twig:Label>
</div>

Installation

Ensure the Symfony UX Toolkit is installed in your Symfony app:

$ composer require --dev symfony/ux-toolkit

Then, run the following command to install the component and its dependencies:

$ bin/console ux:install switch --kit shadcn

The UX Toolkit is not mandatory to install a component. You can install it manually by following the next steps:

  1. Copy the following file(s) into your Symfony app:
    templates/components/Switch.html.twig
    <label class="{{ 'relative inline-flex items-center cursor-pointer ' ~ attributes.render('class')|tailwind_merge }}">
        <input type="checkbox" class="sr-only peer absolute inset-0 w-full h-full cursor-pointer" {{ attributes }}>
        <div class="pointer-events-none w-11 h-6 bg-input rounded-full peer peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-primary peer-disabled:opacity-50 peer-disabled:cursor-not-allowed"></div>
    </label>
    
  2. If necessary, install the following Composer dependencies:
    $ composer require tales-from-a-dev/twig-tailwind-extra:^1.0.0
  3. And the most important, enjoy!

Usage

<div class="flex items-center space-x-2">
    <twig:Switch id="airplane-mode" />
    <twig:Label for="airplane-mode">Airplane Mode</twig:Label>
</div>

Examples

Default

Loading...
<div class="flex items-center space-x-2">
    <twig:Switch id="airplane-mode" />
    <twig:Label for="airplane-mode">Airplane Mode</twig:Label>
</div>

Form

Loading...
<form action="/" method="post">
    <h3 class="mb-4 text-lg font-medium">Email Notifications</h3>
    <div class="flex flex-col gap-3">
        <div class="gap-2 flex flex-row items-center justify-between rounded-lg border p-4">
            <div class="space-y-0.5">
                <twig:Label class="text-base" for="marketing-emails">Marketing emails</twig:Label>
                <p class="text-sm text-muted-foreground">Receive emails about new products, features, and more.</p>
            </div>
            <twig:Switch id="marketing-emails" />
        </div>
        <div class="gap-2 flex flex-row items-center justify-between rounded-lg border p-4">
            <div class="space-y-0.5">
                <twig:Label class="text-base" for="security-emails">Security emails</twig:Label>
                <p class="text-sm text-muted-foreground">Receive emails about your account security.</p>
            </div>
            <twig:Switch id="security-emails" checked disabled />
        </div>
    </div>
</form>