CompareSlider

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.

After
Before
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:

After

New design

Before

Old design

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:

After
Before
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:

After
Before

Divider position: 30%

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:

After
Before
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:

After
Before
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:

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:

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:

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:

After
Before
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:

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:

KeyDescription
ArrowLeft/ArrowDownDecreases position by keyboardStep
ArrowRight/ArrowUpIncreases position by keyboardStep
HomeSets position to 0
EndSets position to 100

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