Skip to content

Toggle ​

Basic Usage ​

The Toggle field renders a switch-style toggle. By default, it stores true when on and false when off:

Example
php
use InertiaUI\Forms\Fields\Toggle;

Toggle::make('notifications')
    ->label('Email notifications')
    ->default(true);

Use Toggle for a single on/off value. For several related choices, use Checkbox Group. For a choice of one option out of many, use Radio.

Custom Values ​

To customize the value that is stored when the toggle is on or off, you may use the onValue and offValue methods:

php
Toggle::make('status')
    ->onValue('active')
    ->offValue('inactive');

Toggle::make('is_public')
    ->onValue(1)
    ->offValue(0);

The values supported across stacks are strings, numbers, booleans, and null. Arrays and objects are rejected during field configuration. On the server, a model or default value that strictly matches either configured value is kept as it is. Any other value is converted using PHP truthiness and then mapped to onValue or offValue. Vue and React also use strict equality to decide whether the switch is on.

For example, this field writes null when the toggle is off:

php
Toggle::make('status')
    ->onValue('enabled')
    ->offValue(null);

Validation ​

The Toggle field validates the submitted value against the configured pair. The default boolean values add Laravel's boolean rule. Custom values add a Rule::in() constraint, and a configured null value also adds the nullable rule. Optional fields that are omitted from the request are not validated.

You may replace this managed constraint with your own boolean, in, or Rule::in() rule:

php
Toggle::make('status')
    ->onValue('enabled')
    ->offValue('disabled')
    ->rule('in:enabled,disabled,archived');

The field doesn't add an acceptance rule automatically, so add the Laravel rule that matches the configured values. The required method only adds Laravel's required rule and exposes aria-required. It doesn't mean the switch must be on. To require an on/off value to be enabled, use the accepted rule.

Display Options ​

The following methods change what the switch shows: text labels beside it, icons inside its thumb, its size and active color, and a loading spinner.

Labels ​

To show text labels next to the toggle, you may use the onLabel and offLabel methods. Labels pair well with custom stored values whose submitted value is not a boolean:

Example
PrivatePublic
php
Toggle::make('visibility')
    ->label('Portal visibility')
    ->onValue('public')
    ->offValue('private')
    ->onLabel('Public')
    ->offLabel('Private')
    ->color('#16a34a')
    ->default('public');

These labels are optional and only describe the visible state. They don't change the accessible name of the switch, which comes from the field label or an explicitly forwarded aria-label.

Icons ​

You may add icons inside the toggle thumb by passing resolver-backed icon names to the onIcon and offIcon methods. See Icons for the resolver setup:

Example
php
Toggle::make('dark_mode')
    ->onIcon('MoonIcon')
    ->offIcon('SunIcon');

The icon resolver only receives the name configured for the active state. Icons are decorative and don't replace the field label.

Size ​

Three sizes are available: sm, md, and lg. The default size is md:

Example
php
Toggle::make('small_toggle')
    ->label('Small')
    ->sm();

Toggle::make('medium_toggle')
    ->label('Medium')
    ->md();

Toggle::make('large_toggle')
    ->label('Large')
    ->lg();

You may also pass the size to the generic size method:

php
Toggle::make('notifications')->size('lg');

Vue and React fall back to the medium styles for any serialized size they don't recognize. The sm, md, and lg methods are the supported public contract.

Color ​

To customize the active color of the toggle, pass any valid CSS color value to the color method:

php
Toggle::make('notifications')->color('#16a34a');
Toggle::make('maintenance_mode')->color('rgb(220 38 38)');

The custom color only applies to the track in the on state. Passing null restores the theme accent color.

Loading ​

The loading method shows a spinner while an update is pending. A loading toggle is busy and disabled, so user interaction may not change it until loading ends:

Example
php
Toggle::make('syncing')->loading();
Toggle::make('locked_setting')->disabled();

Using the Component Directly ​

You may render Toggle with local state, or pass serialized field props during manual form rendering.

vue
<script setup lang="ts">
import { Toggle } from '@inertiaui/form-vue/components'
import { Moon, Sun } from '@lucide/vue'
import { ref } from 'vue'
import type { IconResolverFn } from '@inertiaui/form-vue'

const theme = ref<'dark' | 'light'>('light')
const saving = ref(false)
const canEdit = ref(true)

const icons = { MoonIcon: Moon, SunIcon: Sun }
const iconResolver: IconResolverFn = (icon) => icons[icon as keyof typeof icons] ?? null

const toggleParts = {
  track: 'ring-1 ring-zinc-950/10',
  thumb: 'ring-1 ring-zinc-950/10',
}

const trackBlur = () => {}
</script>

<template>
  <Toggle
    v-model="theme"
    id="theme-mode"
    name="theme_mode"
    label="Theme mode"
    help="Applied to the current workspace."
    on-value="dark"
    off-value="light"
    on-label="Dark"
    off-label="Light"
    on-icon="MoonIcon"
    off-icon="SunIcon"
    size="lg"
    color="#16a34a"
    :loading="saving"
    :disabled="!canEdit"
    :icon-resolver="iconResolver"
    required
    precognitive
    invalid
    error="Choose an available theme mode."
    badge="Workspace"
    badge-class="bg-zinc-100 text-zinc-700"
    label-trailing="Required"
    label-trailing-class="text-zinc-500"
    tooltip="Shown to every member of this workspace."
    help-position="below"
    layout="stacked"
    control-position="end"
    label-class="font-medium"
    help-class="text-zinc-500"
    error-class="text-red-600"
    wrapper-class="max-w-xl"
    control-class="bg-white"
    class="font-medium"
    :part-classes="toggleParts"
    data-testid="theme-toggle"
    aria-label="Theme mode"
    @blur="trackBlur"
  />
</template>
tsx
import { useState } from 'react'
import { MoonIcon, SunIcon } from 'lucide-react'
import { Toggle } from '@inertiaui/form-react/components'
import type { IconResolverFn } from '@inertiaui/form-react'

const icons = { MoonIcon, SunIcon }
const iconResolver: IconResolverFn = (icon) => icons[icon as keyof typeof icons] ?? null

const toggleParts = {
    track: 'ring-1 ring-zinc-950/10',
    thumb: 'ring-1 ring-zinc-950/10',
}

export default function ThemeToggle() {
    const [theme, setTheme] = useState<'dark' | 'light'>('light')
    const [saving] = useState(false)
    const [canEdit] = useState(true)
    const trackBlur = () => {}

    return (
        <Toggle
            value={theme}
            onValueChange={(value) => setTheme(value as 'dark' | 'light')}
            id="theme-mode"
            name="theme_mode"
            label="Theme mode"
            help="Applied to the current workspace."
            onValue="dark"
            offValue="light"
            onLabel="Dark"
            offLabel="Light"
            onIcon="MoonIcon"
            offIcon="SunIcon"
            size="lg"
            color="#16a34a"
            loading={saving}
            disabled={!canEdit}
            iconResolver={iconResolver}
            required
            precognitive
            invalid
            error="Choose an available theme mode."
            badge="Workspace"
            badgeClass="bg-zinc-100 text-zinc-700"
            labelTrailing="Required"
            labelTrailingClass="text-zinc-500"
            tooltip="Shown to every member of this workspace."
            helpPosition="below"
            layout="stacked"
            controlPosition="end"
            labelClass="font-medium"
            helpClass="text-zinc-500"
            errorClass="text-red-600"
            wrapperClass="max-w-xl"
            controlClass="bg-white"
            class="font-medium"
            partClasses={toggleParts}
            data-testid="theme-toggle"
            aria-label="Theme mode"
            onBlur={trackBlur}
        />
    )
}

Vue uses v-model, and React uses a controlled value with onValueChange. Native attributes and listeners target the underlying button. See Native Attributes and Events.

All shared field methods remain available. See Validation, Conditional Visibility, Authorization, Model Binding, Form Class, Icons, and Styling for rules, visibility, authorization, model values, runtime configuration, icon resolvers, labels, help, layout, and classes.