ColorWheel

Circular hue selector

Usage

ColorWheel is a circular hue selector. Its value is a hue in degrees, from 0 at the top (red) increasing clockwise to 360:

Size
Thickness
Thumb size
Saturation
Lightness
import { ColorWheel } from '@mantine/core';

function Demo() {
  return <ColorWheel aria-label="Color wheel" thumbSize={20} />;
}

Controlled

Convert the value to a color with hsl(hue, 100%, 50%) or any color conversion function:

Hue: 210°

import { useState } from 'react';
import { ColorSwatch, ColorWheel, Group, Stack, Text } from '@mantine/core';

function Demo() {
  const [hue, setHue] = useState(210);

  return (
    <Stack align="center">
      <Group>
        <ColorSwatch color={`hsl(${hue}, 100%, 50%)`} />
        <Text size="sm">Hue: {hue}°</Text>
      </Group>
      <ColorWheel aria-label="Color wheel" value={hue} onChange={setHue} />
    </Stack>
  );
}

Ring colors

Use saturation and lightness props (0 to 100) to change the ring colors. They change only the appearance, the value is always the hue:

saturation=100 lightness=50

saturation=60 lightness=70

import { ColorWheel, Group, Stack, Text } from '@mantine/core';

function Demo() {
  return (
    <Group justify="center">
      <Stack align="center" gap="xs">
        <ColorWheel aria-label="Vivid" saturation={100} lightness={50} defaultValue={210} />
        <Text size="sm">saturation=100 lightness=50</Text>
      </Stack>

      <Stack align="center" gap="xs">
        <ColorWheel aria-label="Pastel" saturation={60} lightness={70} defaultValue={210} />
        <Text size="sm">saturation=60 lightness=70</Text>
      </Stack>
    </Group>
  );
}

onChangeEnd

onChangeEnd is called when the user stops dragging the wheel or changes the value with the keyboard. Use it as a debounced callback to avoid too frequent updates:

Current value: 0

End value: 0

import { useState } from 'react';
import { ColorWheel, Text } from '@mantine/core';

function Demo() {
  const [value, setValue] = useState(0);
  const [endValue, setEndValue] = useState(0);

  return (
    <>
      <ColorWheel
        aria-label="Color wheel"
        value={value}
        onChange={setValue}
        onChangeEnd={setEndValue}
      />
      <Text mt="md">Current value: {value}</Text>
      <Text>End value: {endValue}</Text>
    </>
  );
}

Disabled

Set disabled to prevent user interaction:

import { ColorWheel } from '@mantine/core';

function Demo() {
  return <ColorWheel aria-label="Color wheel" defaultValue={210} disabled />;
}

ColorWheel with uncontrolled forms

Set name prop to include the hue value in the FormData object on form submission and defaultValue to set the initial value:

import { ColorWheel } from '@mantine/core';

function Demo() {
  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        const formData = new FormData(event.currentTarget);
        console.log('Hue value:', formData.get('hue'));
      }}
    >
      <ColorWheel name="hue" defaultValue={120} />
      <button type="submit">Submit</button>
    </form>
  );
}

Accessibility

Set aria-label to make the wheel accessible for screen readers:

import { ColorWheel } from '@mantine/core';

function Demo() {
  return <ColorWheel aria-label="Hue" />;
}

Keyboard interactions

When the thumb is focused, arrow keys wrap around at 0 and 360:

KeyDescription
ArrowDownDecreases value by step
ArrowLeftDecreases value by step
ArrowUpIncreases value by step
ArrowRightIncreases value by step
HomeSets value to 0
EndSets value to the last step before 360 (359 with the default step)

Based on use-radial-move

ColorWheel is based on the use-radial-move hook. Use it to build a custom radial selector:

115°
import { useState } from 'react';
import { Box } from '@mantine/core';
import { useRadialMove } from '@mantine/hooks';
import classes from './Demo.module.css';

function Demo() {
  const [value, setValue] = useState(115);
  const { ref } = useRadialMove(setValue);

  return (
    <Box className={classes.root} ref={ref} style={{ '--angle': `${value}deg` }}>
      <div className={classes.value}>{value}°</div>
      <div className={classes.thumb} />
    </Box>
  );
}