GitHub

Switch

A toggle switch component for boolean settings, ready for CMS integration.

CMSPayload CMS Integration

Switches can be configured through CMS fields, allowing content editors to customize labels, descriptions, and default states without code changes.

CMS Demo

See how content editors configure switches in a CMS:

Loading...
CMS ConfigurationSimulates Payload CMS switch fields with localization

Payload Form Example

// In your Payload CMS form config
export const NotificationSettings = {
  slug: "notification-settings",
  fields: [
    {
      name: "emailNotifications",
      type: "group",
      fields: [
        { name: "label", type: "text", localized: true, required: true },
        { name: "description", type: "text", localized: true },
        { name: "defaultEnabled", type: "checkbox", defaultValue: true },
      ],
    },
  ],
}

Rendering the Form

import { Switch } from "@mordecai-design-system/ui"

export function NotificationSettings({ data }) {
  const field = data.emailNotifications
  return (
    <div className="flex items-center justify-between">
      <div className="space-y-0.5">
        <label className="text-sm font-medium">{field.label}</label>
        <p className="text-xs text-muted-foreground">{field.description}</p>
      </div>
      <Switch defaultChecked={field.defaultEnabled} />
    </div>
  )
}

Basic Usage

Loading...
SwitchA default switch

Installation

pnpm add @corew500/ui

Usage

import { Switch } from "@mordecai-design-system/ui"
<Switch />

Examples

With Label

<div className="flex items-center gap-2">
  <Switch id="airplane-mode" />
  <label htmlFor="airplane-mode" className="text-sm font-medium">
    Airplane Mode
  </label>
</div>

Sizes

<Switch size="sm" />
<Switch size="default" />

Disabled

<Switch disabled />
<Switch disabled defaultChecked />

Controlled

const [enabled, setEnabled] = useState(false)

<Switch checked={enabled} onCheckedChange={setEnabled} />

Props

PropTypeDefaultDescription
classNameenumCSS class applied to the element, or a function that returns a class based on the component’s state.
defaultCheckedenumfalseWhether the switch is initially active. To render a controlled switch, use the `checked` prop instead.
styleenumStyle applied to the element, or a function that returns a style object based on the component’s state.
idstringThe id of the switch element.
renderenumAllows you to replace the component’s HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.
checkedenumWhether the switch is currently active. To render an uncontrolled switch, use the `defaultChecked` prop instead.
disabledenumfalseWhether the component should ignore user interaction.
inputRefenumA ref to access the hidden `<input>` element.
namestringIdentifies the field when a form is submitted.
onCheckedChange(checked: boolean, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element; }) => voidEvent handler called when the switch is activated or deactivated.
readOnlyenumfalseWhether the user should be unable to activate or deactivate the switch.
requiredenumfalseWhether the user must activate the switch before submitting a form.
valuestringThe value submitted with the form when the switch is on. By default, switch submits the "on" value, matching native checkbox behavior.
uncheckedValuestringThe value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior.
nativeButtonenumfalseWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button.
sizeenum