BridgeBeta
  • Brand
  • Digital Assets
    Foundations
    Icons
    Components
    Templates
  • Resources
    Design Tokens
    How-tos
    Best Practices
    Libraries
    Accessibility
    Products List
  • Support
    • Overview
    • Actions
      • Overview
      • Button
      • ContextMenu
      • Link
    • Controls
      • Overview
      • Checkbox
      • Tag
      • Toggle
    • Forms
      • Overview
      • DatePicker
      • FileUploader
      • Input
      • NumberInput
      • RadioButton
      • Search
      • Select
      • Slider
      • TextArea
      • TextInput
    • Hooks
      • useIsomorphicLayoutEffect
      • useMediaQuery
      • useSettings
    • Navigation
      • Overview
      • AnchorNavigation
      • BannerNavigation
      • Breadcrumb
      • Footer
      • InfoBar
      • MainNavigation
      • SecondaryNavigation
      • StepNavigation
      • SubNavigation
    • Overlay
      • Overview
      • Credits
      • Modal
      • ModalWrapper
      • Notification
      • Tooltip
    • Structure
      • Overview
      • Accordion
      • AuthLayout
      • Avatar
      • Callout
      • Card
      • ContentSwitcher
      • Empty
      • Hero
      • InlineLoading
      • Item
      • List
      • Loading
      • mdxComponents
      • Module
      • Pagination
      • ReadMore
      • Story
      • Table
      • Tabs
      • Text
      • Unit
      • User
      • WFPCoreProvider
      • Wrapper
Components
Controls
Toggle

Toggle

  • Design
  • Code
  • Props

Toggles are controls that are used to quickly switch between two possible states.

Import statement

import { Toggle } from "@wfp/react"

Usage

Make sure to use the toggle in a controlled context.

Editable Example

<Toggle
  labelText="The Input"
  helperText="Optional helperText"
  labelA="Off"
  labelB="On"
  name="input-example"
/>;

react-hook-form example

This example shows how to use the Toggle component with react-hook-form.

Expand code

Editable Example

const FormExample = () => {
  const [defaultValues, setDefaultValues] = useState(
    {}
  );
  const {
    control,
    register,
    handleSubmit,
    watch,
    reset,
  } = useForm({ defaultValues });
  const [data, setData] = useState({});

  const setDefaultValuesFunc = (e) => {
    console.log(e.target.value);
    try {
      const values = JSON.parse(e.target.value);
      setDefaultValues(values);
    } catch (e) {
      console.log(e);
    }
  };

  const resetInputs = () => {
    reset(defaultValues);
  };
  const currentValues = watch();

  return (
    <>
      <form
        onSubmit={handleSubmit((data) =>
          setData(data)
        )}
      >
        <>
          <Toggle
            labelText="The uncontrolled Input"
            helperText="Optional helperText"
            labelA="Off"
            labelB="On"
            {...register("inputname")}
          />

          <Controller
            name="inputnameB"
            control={control}
            render={({ field }) => {
              return (
                <Toggle
                  labelText="The controlled Input"
                  helperText="Optional helperText"
                  labelA="Off"
                  labelB="On"
                  {...field}
                  onChange={(e) =>
                    field.onChange(e.target.checked)
                  }
                  checked={field.value === true}
                />
              );
            }}
          />
        </>
        <Button type="submit">Submit</Button>{" "}
        <Button onClick={resetInputs} kind="secondary">
          Reset here
        </Button>
        <div className="debug">
          <h4>Submitted form data</h4>
          <pre>{JSON.stringify(data, null, 2)}</pre>

          <h4>Current values</h4>
          <pre>
            {JSON.stringify(currentValues, null, 2)}
          </pre>

          <TextInput
            labelText="Default values (editable)"
            defaultValue={JSON.stringify(
              defaultValues
            )}
            onChange={setDefaultValuesFunc}
          />
        </div>
      </form>
    </>
  );
};

render(<FormExample />);

The Toogle component is wrapped inside the Input component, but can be used without label or helperText:

Editable Example

<Toggle labelA="Off" labelB="On" />;

Reference

  • Input component
  • Carbon Design System Usage for text inputs

On this page

  • Usage
  • react-hook-form example
  • Reference
Storybook
Figma library
Contact us
2025 © World Food Programme