# 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,
for example to compare two versions of an image. The `before` layer is displayed on the left
of the divider, the `after` layer on the right:

```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. 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 }: {
  background: string;
  color: string;
  label: string;
  title: string;
}) {
  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 move the divider with the pointer instead of dragging:

```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 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` takes the full width of its parent and has no intrinsic height. Set the 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` as well. 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` to `true` or a duration in milliseconds to animate keyboard and controlled position changes.
Dragging and `changePositionOnHover` move the divider immediately. Click the button to see the transition:

```tsx
import { useState } from 'react';
import { Box, Button, CompareSlider, Stack } from '@mantine/core';

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

  return (
    <Stack align="center">
      <CompareSlider
        aria-label="Compare colors"
        w={{ base: 300, sm: 500 }}
        h={160}
        transition={400}
        position={position}
        onPositionChange={setPosition}
        before={<Box h="100%" bg="indigo.7" />}
        after={<Box h="100%" bg="teal.5" />}
      />
      <Button onClick={() => setPosition(position < 50 ? 75 : 25)}>Move divider</Button>
    </Stack>
  );
}
```


## Disabled

Set `disabled` to lock the divider:

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

Set `aria-label` to describe what the handle compares:

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

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

## Keyboard interactions

In vertical orientation, `ArrowUp`
decreases the position and `ArrowDown` increases 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 | false | If set, the divider position follows the pointer on hover, without requiring a drag |
| defaultPosition | number | 50 | 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 | 5 | Arrow keys position increment/decrement in % |
| onPositionChange | (position: number) => void | - | Called when the divider position changes |
| orientation | "horizontal" \| "vertical" | 'horizontal' | Divider direction |
| position | number | - | Controlled divider position, number between 0 and 100 |
| transition | number \| boolean | false | 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 | true | 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 | - |
