• CMSnpm version

    • Overview
    • ContentBuilder
    • InviteUserForm
    • LoginForm
    • RenewPasswordForm
  • UInpm version

    • Overview
    • Accordion
    • AlertBubble
    • AnnouncementBar
    • Avatar
    • AvatarFileInput
    • Badge
    • Button
    • ButtonGroup
    • ButtonList
    • Calendar
    • Checkbox
    • CheckboxButton
    • CheckboxInput
    • CheckboxList
    • Chip
    • ColorRadio
    • ColorRadioGroup
    • Combobox
    • DatePicker
    • DatePickerInput
    • DateRangePicker
    • DateRangePickerInput
    • DatetimePicker
    • DatetimePickerInput
    • Dialog
    • Dropdown
    • Dropzone
    • ErrorMessage
    • FileInput
    • FlashMessages
    • FormComponent
    • Icon
    • IconButton
    • ImageGallery
    • InfoBox
    • Input
    • Label
    • Layout
    • Lightbox
    • ListItem
    • Loader
    • Lozenge
    • Menu
    • Message
    • Modal
    • ✅ ModalDialog
    • ✅ ModalHeader
    • MultiCombobox
    • MultiSelect
    • Pagination
    • Paper
    • Popover
    • Radio
    • RadioGroup
    • RasterImage
    • Select
    • ✅ Tabs
    • TextInput
    • TextLink
    • Textarea
    • TimePicker
    • TimePickerInput
    • Toggle
    • Tooltip
    • Typography
  • Formnpm version

    • Overview
    • AvatarFileInput
    • CheckboxButton
    • CheckboxInput
    • CheckboxList
    • ColorRadioGroup
    • Combobox
    • DatePickerInput
    • DateRangePickerInput
    • DatetimePickerInput
    • Dropzone
    • FileInput
    • Form
    • FormRenderer
    • GpsInput
    • MoneyInput
    • MultiCombobox
    • MultiSelect
    • NumberInput
    • PasswordInput
    • RadioGroup
    • Select
    • TextInput
    • Textarea
    • TimePickerInput
    • Toggle
  • DataGridnpm version

    • Overview
    • DataGrid
    • DataGridCustomExample
    • ExportButton
    • FilterList
    • Filters
    • FiltersButton
    • FulltextInput
    • HiddenColumns
    • HiddenColumnsButton
    • Pagination
    • RowCounts
    • RowsPerPageSelect
    • SelectedRowsToolbar
    • TableV2
    • ToolbarControl
    • ToolbarCustoms
    • ToolbarTabs
  • Wysiwygnpm version

    • Overview
  • Resizernpm version

    • Overview
  • Routernpm version

    • Overview
  • Corenpm version

    • Overview
  • Core-Reactnpm version

    • Overview
  • Stylesnpm version

    • Overview
  • Localizenpm version

    • Overview
  • Analyticsnpm version

    • Overview
  • Datepickernpm version

    • Overview
  • Icons-generatornpm version

    • Overview
  • Smart-addressnpm version

    • Overview
  • E2Enpm version

    • Overview
  • E2E-Playwrightnpm version

    • Overview
View source on GitLab

Toggle

A labelled on/off switch — a controlled boolean input built on Headless UI's Switch.

When to use

Use Toggle for a single binary on/off setting that you drive yourself via value / onChange.

Inside a @uxf/form form use @uxf/form/toggle instead — it registers the field with react-hook-form (value, onChange, validation, disabled/read-only from the form context) and renders this component underneath. @uxf/ui/toggle is the controlled primitive with no form wiring.

Usage

import { Toggle } from "@uxf/ui/toggle";
import { useState } from "react";

function Example() {
    const [isChecked, setIsChecked] = useState(true);

    return <Toggle label="Notifications" name="notifications" onChange={setIsChecked} value={isChecked} />;
}

Props

Extends FormControlProps<boolean | undefined> (from @uxf/ui/types).

Prop Type Default Description
value boolean | undefined — Required. Checked state (controlled).
onChange (value: boolean | undefined, event?) => void — Required. Called with the new checked state.
name string — Required. Field name; emitted as data-name on the wrapper and set on the switch.
label ReactNode — Required. Label rendered next to the switch.
hiddenLabel boolean false Visually hide the label (it stays in the DOM for accessibility).
size ToggleSize "default" Size.
variant ToggleVariant "default" Layout variant.
isDisabled boolean false Disable the switch.
isInvalid boolean false Invalid-state styling.
isReadOnly boolean false Read-only styling.
isRequired boolean false Required-state styling.
id string — id of the switch element.
className string — Extra classes on the wrapper.
style CSSProperties — Inline style on the switch.

onBlur, onFocus and isFocused are part of the inherited FormControlProps type but are not wired up by this primitive — the @uxf/form/toggle twin is what makes use of them.

Variants & states

Built-in values (from theme.ts):

Group Values
variant default, reversed
size sm, default

reversed renders the label before the switch (flex-direction: row-reverse).

ToggleVariants and ToggleSizes are open interfaces, so a project can add its own values via module augmentation:

// toggle.d.ts
declare module "@uxf/ui/toggle/theme" {
    interface ToggleSizes {
        lg: true;
    }
}

Behavioural states: isDisabled, isInvalid, isReadOnly and isRequired toggle the shared state classes on the wrapper and switch; isDisabled also disables the underlying Switch.

Requirements

Import the component stylesheet once in your global CSS:

@import url("@uxf/ui/css/toggle.css");

Also requires the global @uxf/ui token layer, set up once per app — see @uxf/ui setup.

Default
Open in new tab