ColorSwatch

Display a color swatch.

Import

import { ColorSwatch } from '@tidbcloud/uikit'

Usage

import { ColorSwatch, Group } from '@tidbcloud/uikit'
 
function Demo() {
  return (
    <Group>
      <ColorSwatch color="#009790" />
      <ColorSwatch color="rgba(234, 22, 174, 0.5)" />
      <ColorSwatch color="var(--mantine-color-orange-5)" />
    </Group>
  )
}

With shadow

By default, ColorSwatch has an inner box-shadow to make it more visible on light backgrounds. You can disable it with withShadow={false}:

import { ColorSwatch } from '@tidbcloud/uikit'
 
function Demo() {
  return <ColorSwatch color="rgba(255, 255, 255, 0.7)" withShadow={false} />
}

As button

ColorSwatch is a polymorphic component – its root element can be changed with component prop:

import { useState } from 'react'
import { ColorSwatch, CheckIcon } from '@tidbcloud/uikit'
 
function Demo() {
  const [checked, setChecked] = useState(true)
 
  return (
    <ColorSwatch
      component="button"
      color="var(--mantine-color-grape-6)"
      onClick={() => setChecked((c) => !c)}
      style={{ color: '#fff', cursor: 'pointer' }}
    >
      {checked && <CheckIcon size={12} />}
    </ColorSwatch>
  )
}

Props

PropTypeDefaultDescription
colorstringrequiredValid CSS color to display
sizeCSSProperties[‘width’]-Controls width and height of the swatch
radiusMantineRadius-Border radius
withShadowbooleantrueWhether to have inner box-shadow
childrenReactNode-Content displayed inside the swatch
componentElementType’div’Root element or component