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, 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:

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. 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 }: {
  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:

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 move the divider with the pointer instead of dragging:

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

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:

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:

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

Set aria-label to describe what the handle compares:

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:

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