ColorSwatchpolymorphic

Display color swatch
Import

Usage

import { ColorSwatch, Group, useMantineTheme } from '@mantine/core';
function Demo() {
const theme = useMantineTheme();
const swatches = Object.keys(theme.colors).map((color) => (
<ColorSwatch key={color} color={theme.colors[color][6]} />
));
return (
<Group position="center" spacing="xs">
{swatches}
</Group>
);
}

Alpha channel

Color swatch has checkers background – you can use it to show transparency in colors:

import { ColorSwatch, Group, useMantineTheme } from '@mantine/core';
function Demo() {
const theme = useMantineTheme();
const swatches = Object.keys(theme.colors).map((color) => (
<ColorSwatch key={color} color={theme.fn.rgba(theme.colors[color][6], 0.5)} />
));
return (
<Group position="center" spacing="xs">
{swatches}
</Group>
);
}

Polymorphic component

ColorSwatch is a polymorphic component component, you can change root element:

import { useState } from 'react';
import { ColorSwatch, Group, useMantineTheme, CheckIcon } from '@mantine/core';
function Demo() {
const theme = useMantineTheme();
const [checked, setChecked] = useState(true);
return (
<Group position="center" spacing="xs">
<ColorSwatch
component="button"
color={theme.colors.grape[6]}
onClick={() => setChecked((c) => !c)}
sx={{ color: '#fff', cursor: 'pointer' }}
>
{checked && <CheckIcon width={10} />}
</ColorSwatch>
</Group>
);
}

ColorSwatch component props

NameTypeDescription
children
ReactNode
ColorSwatch children
color *
string
Swatch color value in any css valid format (hex, rgb, etc.)
radius
number | "xs" | "sm" | "md" | "lg" | "xl"
Swatch border-radius predefined from theme or number for px value
size
number
Width, height and border-radius in px
withShadow
boolean
Determines whether swatch should have inner shadow