# Switch API

> Explore the API of Cimpress UI's switch component.

Source: https://ui.cimpress.io/components/switch/api/

## Import

```tsx
import { Switch } from '@cimpress-ui/react';
```

## Controlled/uncontrolled usage

```tsx
import { Stack, Switch } from '@cimpress-ui/react';
import { useState } from 'react';

export default function Demo() {
  const [isSelected, setIsSelected] = useState(false);

  return (
    <Stack gap={32}>
      <Switch isSelected={isSelected} onChange={setIsSelected}>
        I am controlled by React state
      </Switch>

      <Switch>I am uncontrolled and unselected by default</Switch>

      <Switch defaultSelected>I am uncontrolled and selected by default</Switch>
    </Stack>
  );
}
```

## Accessibility notes

The switch state can be changed by pressing Space, similar to a native HTML checkbox.

`Switch` requires a textual label to remain accessible to assistive technologies. See our [accessibility guide](/dev-guides/accessibility#labelling) for more details.

The switch label **must** remain the same regardless of the selection state.

## Forms

Switch can integrate with native HTML forms. See our [forms guide](/dev-guides/forms) to learn how to work with forms.

## API reference

### Switch

Allows users to turn a setting on or off.

See [switch usage guidelines](https://ui.cimpress.io/components/switch/).

- `ref`: `Ref<HTMLLabelElement>`

  The `ref` type for this component.

**Props: `SwitchProps`**

- `children`: `StringLikeChildren`

  The label rendered next to the switch.

- `description`: `string`

  A description for the switch. Provides a hint such as specific requirements for which setting to choose.

- `labelPosition`: `'end' | 'start' | 'top'`

  Controls where the label is rendered relative to the switch.

  Defaults to `'end'`.

- `fullWidth`: `boolean`

  Whether the switch should take up the whole available width. Enabling this option will add space between the switch and its label.

  Defaults to `false`.

- `id`: `string`

  The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `aria-label`: `string`

  Defines a string value that labels the current element.

- `aria-labelledby`: `string`

  Identifies the element (or elements) that labels the current element.

- `aria-describedby`: `string`

  Identifies the element (or elements) that describes the object.

- `aria-details`: `string`

  Identifies the element (or elements) that provide a detailed, extended description for the object.

- `name`: `string`

  The name of the input element, used when submitting an HTML form. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname).

- `form`: `string`

  The `<form>` element to associate the input with.
  The value of this attribute must be the id of a `<form>` in the same document.
  See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form).

- `autoFocus`: `boolean`

  Whether the element should receive focus on render.

- `isDisabled`: `boolean`

  Whether the input is disabled.

- `isReadOnly`: `boolean`

  Whether the input can be selected but not changed by the user.

- `defaultSelected`: `boolean`

  Whether the element should be selected (uncontrolled).

- `isSelected`: `boolean`

  Whether the element should be selected (controlled).

- `onChange`: `(isSelected: boolean) => void`

  Handler that is called when the element's selection state changes.

- `value`: `string`

  The value of the input element, used when submitting an HTML form. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefvalue).

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.
