# Slider API

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

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

## Import

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

## Controlled/uncontrolled usage

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

export default function Example() {
  const [value, setValue] = useState<[number, number]>([30, 70]);

  return (
    <Stack gap={16}>
      <Slider label="Price range (controlled)" value={value} onChange={setValue} minValue={0} maxValue={200} />
      <Slider label="Price range (uncontrolled)" defaultValue={[30, 70]} minValue={0} maxValue={200} />
    </Stack>
  );
}
```

## Accessibility notes

Slider follows the [ARIA slider pattern](https://www.w3.org/WAI/ARIA/apg/patterns/slider/). See the linked page for a list of available keyboard interactions.

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

## Value formatting

Slider displays its value(s) as a decimal number by default. This can be overridden using the `formatOptions` prop. This prop has the same syntax as the `options` parameter of the [`Intl.NumberFormat` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options), with the following limitations:

- `notation` property only supports the `'standard'` value

The following example demonstrates a slider that displays a currency value:

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

export default function Example() {
  return (
    <Slider
      label="Price range"
      minValue={0}
      maxValue={500}
      defaultValue={[0, 50]}
      formatOptions={{ style: 'currency', currency: 'EUR' }}
    />
  );
}
```

## Forms

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

## API reference

### Slider

Allows users to select one or more values within a range.

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

- `ref`: `Ref<HTMLDivElement>`

  The `ref` type for this component.

**Props: `SliderProps<T extends number | ([number, number]) = number>`**

- `description`: `string`

  A description for the field. Provides a hint such as specific requirements for what to choose.

- `value`: `T`

  The current value (controlled).

- `defaultValue`: `T`

  The default value (uncontrolled).

- `onChange`: `(value: T) => void`

  Handler that is called when the value changes.

- `onChangeEnd`: `(value: T) => void`

  Handler that is called when the slider stops moving.

- `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).

- `name`: `T extends [number, number] ? [string, string] : string`

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

- `withInput`: `boolean`

  Whether to show input fields for slider values.

  Defaults to `false`.

- `showLabels`: `boolean`

  Whether to show static labels with minimum and maximum values.

  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/).

- `label`: `string`

  The content to display as the label.

- `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.

- `formatOptions`: `NumberFormatOptions`

  The display format of the value label.

- `isDisabled`: `boolean`

  Whether the whole Slider is disabled.

- `minValue`: `number`

  The slider's minimum value.

  Defaults to `0`.

- `maxValue`: `number`

  The slider's maximum value.

  Defaults to `100`.

- `step`: `number`

  The slider's step value.

  Defaults to `1`.

**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`.
