Skip to content
Ginga UI
Ginga UI
日本語

Getting started

  • Overview
  • Quick start
  • Theme generation
  • Custom rendering
  • All components

Forms

  • Button
  • Checkbox
  • Input
  • Radio
  • Questionnaire
  • SegmentedControl
  • Select
  • ComboBox
  • NumberField
  • SearchField
  • Slider
  • Switch
  • FormControl

Display

  • Accordion
  • Alert
  • AlertDialog
  • Badge
  • CodeBlock
  • Card
  • Dialog
  • Drawer
  • Image
  • List
  • Popover
  • Menu
  • Tooltip
  • Table
  • Tab
  • Toast
  • Trace

Typography

  • Heading
  • Paragraph

Navigation

  • Anchor

Layout

  • Box

Resources

  • Changelog
  • GitHub (opens in a new tab)

Logo: Twemoji (opens in a new tab) (CC-BY 4.0)

Switch

A toggle switch for turning something on or off.

View in Storybook (opens in a new tab)

Installation

import { Switch } from "@ginga-ui/core";

Basic usage

A simple switch example.

switch.tsx
import { Switch } from "@ginga-ui/core";

export function BasicExample() {
  return <Switch>Allow notifications</Switch>;
}

Selected

A switch that is on initially.

switch.tsx
import { Switch } from "@ginga-ui/core";

export function SelectedExample() {
  return <Switch selected>On by default</Switch>;
}

Disabled

An example of a disabled switch.

switch.tsx
switch.module.css
import { Switch } from "@ginga-ui/core";
import styles from "./switch.module.css";

export function DisabledExample() {
  return (
    <div className={styles.disabledList}>
      <Switch disabled>Disabled (off)</Switch>
      <Switch disabled selected>
        Disabled (on)
      </Switch>
    </div>
  );
}

Controlled switch

A switch whose state is controlled externally.

switch.tsx
import { Switch } from "@ginga-ui/core";

export function ControlledExample() {
  return (
    <Switch onChange={(isSelected) => console.log("State:", isSelected)}>
      Dark mode
    </Switch>
  );
}

Props

Switch

NameTypeDefaultDescription
childrenReactNode-Required. Label of the switch.
selectedboolean-Whether the switch is on (controlled). Use it with onChange.
disabledbooleanfalseDisables the switch.
readOnlybooleanfalsePrevents the user from changing the value.

Also accepts the props of React Aria's Switch (opens in a new tab).