Skip to main content
Switch
@coinbase/cds-web@8.13.6
A control for toggling between on and off.
Import
import { Switch } from '@coinbase/cds-web/controls/Switch'
SourceView source codeStorybookView StorybookFigmaView Figma
Peer dependencies
  • framer-motion: ^10.18.0
Related components
Loading...
Live Code
function SwitchDemo() {
  const [checked, setChecked] = useState(false);

  return (
    <Switch onChange={() => setChecked((s) => !s)} checked={checked}>
      Dark Mode
    </Switch>
  );
}

Customizing Colors

Like other control components (i.e. Radio, Checkbox), you can customize the color of the Switch by setting the controlColor prop.

Loading...
Live Code
function SwitchDemo() {
  const [checked, setChecked] = useState(false);

  return (
    <Switch controlColor="accentBoldGreen" onChange={() => setChecked((s) => !s)} checked={checked}>
      Custom control color
    </Switch>
  );
}

For more advanced color customization, you can use additional style props like background, borderColor, borderWidth, and color:

Loading...
Live Code
function AdvancedSwitchDemo() {
  const [checked, setChecked] = useState(false);

  return (
    <Switch
      background={checked ? 'accentBoldPurple' : 'bgNegative'}
      borderColor={checked ? 'bgPositive' : 'bgPrimary'}
      borderWidth={200}
      checked={checked}
      color="bgPrimary"
      controlColor="bgPositive"
      onChange={() => setChecked((s) => !s)}
    >
      Advanced styling
    </Switch>
  );
}

Is this page useful?

Coinbase Design is an open-source, adaptable system of guidelines, components, and tools that aid the best practices of user interface design for crypto products.