# CompareSlider
Package: @mantine/core
Import: import { CompareSlider } from '@mantine/core';
Description: Before/after slider to compare two layers of content

## Usage

`CompareSlider` displays two layers in the same box with a draggable divider between them.
It is commonly used to compare two versions of an image. The `before` layer is displayed
on the left/top side of the divider, the `after` layer – on the right/bottom side.

```tsx
import { CompareSlider, Image } from '@mantine/core';

function Demo() {
  return (
    <CompareSlider
      aria-label="Compare images"
      w={{ base: 300, sm: 500 }}
      h={{ base: 192, sm: 320 }}
      before={<Image src="/before.png" alt="Before" h="100%" w="100%" fit="cover" />}
      after={
        <Image
          src="/after.png"
          alt="After"
          h="100%"
          w="100%"
          fit="cover"
          style={{ filter: 'grayscale(1)' }}
        />
      }
    />
  );
}
```


## Compare any content

`before` and `after` accept any React node: videos, maps, charts, code blocks or plain markup.
Both layers fill the container, make sure they have the same dimensions:

```tsx
import { Box, Center, CompareSlider, Stack, Text, Title } from '@mantine/core';

function Panel({ background, color, label, title }) {
  return (
    <Box h="100%" w="100%" bg={background} c={color}>
      <Center h="100%">
        <Stack gap={4} align="center">
          <Text tt="uppercase" fw={700} fz="xs" opacity={0.7}>{label}</Text>
          <Title order={3}>{title}</Title>
        </Stack>
      </Center>
    </Box>
  );
}

function Demo() {
  return (
    <CompareSlider
      aria-label="Compare designs"
      h={{ base: 156, sm: 260 }}
      w={{ base: 300, sm: 500 }}
      before={
        <Panel background="gray.1" color="dark.6" label="Before" title="Old design" />
      }
      after={
        <Panel background="dark.7" color="white" label="After" title="New design" />
      }
    />
  );
}
```


## Vertical orientation

Set `orientation="vertical"` to move the divider vertically:

```tsx
import { CompareSlider, Image } from '@mantine/core';

function Demo() {
  return (
    <CompareSlider
      aria-label="Compare images"
      orientation="vertical"
      h={{ base: 192, sm: 320 }}
      w={{ base: 300, sm: 500 }}
      before={<Image src="/before.png" alt="Before" h="100%" w="100%" fit="cover" />}
      after={
        <Image
          src="/after.png"
          alt="After"
          h="100%"
          w="100%"
          fit="cover"
          style={{ filter: 'grayscale(1)' }}
        />
      }
    />
  );
}
```


## Controlled

Set `position` and `onPositionChange` props to control the divider position.
The position is a number between 0 and 100:

```tsx
import { useState } from 'react';
import { CompareSlider, Image, Slider, Stack, Text } from '@mantine/core';

function Demo() {
  const [position, setPosition] = useState(30);

  return (
    <Stack>
      <CompareSlider
        aria-label="Compare images"
        h={{ base: 192, sm: 320 }}
        w={{ base: 300, sm: 500 }}
        position={position}
        onPositionChange={(value) => setPosition(Math.round(value))}
        before={<Image src="/before.png" alt="Before" h="100%" w="100%" fit="cover" />}
        after={
          <Image
            src="/after.png"
            alt="After"
            h="100%"
            w="100%"
            fit="cover"
            style={{ filter: 'grayscale(1)' }}
          />
        }
      />

      <Text size="sm">Divider position: {position}%</Text>
      <Slider
        thumbLabel="Divider position"
        value={position}
        onChange={setPosition}
        label={(value) => `${value}%`}
      />
    </Stack>
  );
}
```


## Change position on hover

Set `changePositionOnHover` to update the divider position when the pointer moves over the
component:

```tsx
import { CompareSlider, Image } from '@mantine/core';

function Demo() {
  return (
    <CompareSlider
      aria-label="Compare images"
      changePositionOnHover
      h={{ base: 192, sm: 320 }}
      w={{ base: 300, sm: 500 }}
      before={<Image src="/before.png" alt="Before" h="100%" w="100%" fit="cover" />}
      after={
        <Image
          src="/after.png"
          alt="After"
          h="100%"
          w="100%"
          fit="cover"
          style={{ filter: 'grayscale(1)' }}
        />
      }
    />
  );
}
```


## Custom handle

Use the `handle` prop to replace the default divider handle with any React node:

```tsx
import { ArrowsLeftRightIcon } from '@phosphor-icons/react';
import { CompareSlider, Image, ThemeIcon } from '@mantine/core';

function Demo() {
  return (
    <CompareSlider
      aria-label="Compare images"
      h={{ base: 192, sm: 320 }}
      w={{ base: 300, sm: 500 }}
      handle={
        <ThemeIcon radius="xl" size="lg" variant="filled">
          <ArrowsLeftRightIcon size={18} />
        </ThemeIcon>
      }
      before={<Image src="/before.png" alt="Before" h="100%" w="100%" fit="cover" />}
      after={
        <Image
          src="/after.png"
          alt="After"
          h="100%"
          w="100%"
          fit="cover"
          style={{ filter: 'grayscale(1)' }}
        />
      }
    />
  );
}
```


Set `withHandle={false}` to hide the handle. The divider line stays visible and the component
remains interactive:

```tsx
import { CompareSlider, Image } from '@mantine/core';

function Demo() {
  return (
    <CompareSlider
      withHandle={false}
      before={<Image src="/before.png" />}
      after={<Image src="/after.png" />}
    />
  );
}
```

## Container size

`CompareSlider` does not have an intrinsic size. It takes the full width of its parent,
so in most layouts you only need to set a height with `h` prop or an `aspect-ratio`.
If the parent does not constrain the width (for example, a flex container), set `w` or `maw` prop.
Make both layers fill the box:

```tsx
import { CompareSlider, Image } from '@mantine/core';

function Demo() {
  return (
    <CompareSlider
      w={{ base: 300, sm: 500 }}
      h={{ base: 192, sm: 320 }}
      before={<Image src="/before.png" h="100%" w="100%" fit="cover" />}
      after={<Image src="/after.png" h="100%" w="100%" fit="cover" />}
    />
  );
}
```

## Transition

Set `transition` prop to animate position changes made with the keyboard or with a controlled
value. Pass `true` to use the default `150ms` duration or a number to set the duration in
milliseconds. The transition is not applied while the divider is dragged and when
`changePositionOnHover` is set:

```tsx
import { CompareSlider } from '@mantine/core';

function Demo() {
  return <CompareSlider transition={200} before="Before" after="After" />;
}
```

## Disabled

Set the `disabled` prop to lock the divider and prevent user interaction:

```tsx
import { CompareSlider, Image } from '@mantine/core';

function Demo() {
  return (
    <CompareSlider
      aria-label="Compare images"
      disabled
      defaultPosition={40}
      h={{ base: 192, sm: 320 }}
      w={{ base: 300, sm: 500 }}
      before={<Image src="/before.png" alt="Before" h="100%" w="100%" fit="cover" />}
      after={
        <Image
          src="/after.png"
          alt="After"
          h="100%"
          w="100%"
          fit="cover"
          style={{ filter: 'grayscale(1)' }}
        />
      }
    />
  );
}
```


## Accessibility

The divider handle is a `role="slider"` element. To make the component accessible for screen
readers, set the `aria-label` prop:

```tsx
import { CompareSlider } from '@mantine/core';

function Demo() {
  return <CompareSlider aria-label="Compare before and after" before="Before" after="After" />;
}
```

Keyboard interactions when the handle is focused:

For vertical orientation, `ArrowUp`/`ArrowLeft` decrease the position and
`ArrowDown`/`ArrowRight` increase it.


#### Props

**CompareSlider props**

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| after | React.ReactNode | - | Content of the second (after) layer, displayed on the right/bottom side of the divider |
| before | React.ReactNode | - | Content of the first (before) layer, displayed on the left/top side of the divider |
| changePositionOnHover | boolean | - | If set, the divider position follows the pointer on hover, without requiring a drag |
| defaultPosition | number | - | Uncontrolled divider default position, number between 0 and 100 |
| disabled | boolean | - | Sets `data-disabled` attribute, disables interactions |
| handle | React.ReactNode | - | Custom divider handle content |
| keyboardStep | number | - | Arrow keys position increment/decrement in % |
| onPositionChange | (position: number) => void | - | Called when the divider position changes |
| orientation | "horizontal" \| "vertical" | - | Divider direction |
| position | number | - | Controlled divider position, number between 0 and 100 |
| transition | number \| boolean | - | Enables position change animation, number is used as animation duration in ms (`150` when set to `true`). The animation is disabled while the divider is dragged and when `changePositionOnHover` is set. |
| withHandle | boolean | - | If set, the divider handle is displayed |


#### Styles API

CompareSlider component supports Styles API. With Styles API, you can customize styles of any inner element. Follow the documentation to learn how to use CSS modules, CSS variables and inline styles to get full control over component styles.

**CompareSlider selectors**

| Selector | Static selector | Description |
|----------|----------------|-------------|
| root | .mantine-CompareSlider-root | Root element |
| before | .mantine-CompareSlider-before | Wrapper of the `before` content, clipped to the current position |
| after | .mantine-CompareSlider-after | Wrapper of the `after` content |
| divider | .mantine-CompareSlider-divider | Line displayed between `before` and `after` content |

**CompareSlider CSS variables**

| Selector | Variable | Description |
|----------|----------|-------------|
| root | --compare-divider-width | Controls divider `width` (`height` in vertical orientation) |
| root | --compare-divider-color | Controls divider and default handle `background-color` |
| root | --compare-handle-size | Controls default handle `width` and `height` |

**CompareSlider data attributes**

| Selector | Attribute | Condition | Value |
|----------|-----------|-----------|-------|
| root | data-orientation | Determines slider orientation, `horizontal` by default | - |
| root | data-disabled | `disabled` prop is set | - |
| handle | data-hidden | `withHandle` prop is `false` | - |
| handle | data-custom | `handle` prop is set | - |
