DisplayReact
One color, shown and named.
Rendered by React 19.3.1
Not measured yetTests passing in React
Not measured yetSize
Not measured yetMemory
Not measured yetSpeed
[N] commits
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<ColorSwatch.Root />Examples
The demo at the top is the one example this component has so far.
API reference
4 props across 1 parts, in React syntax. className, style, render are listed once, under Shared props.
RootColorSwatch.Root
An image of one color, named by the color's description. Inside ColorPicker.Root it shows the picker's color unless color is given. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
colorThe color, any CSS color string, or null for no color. | string | null | — |
labelsThe color name words and the name of no color, in English by default. | Partial<ColorSwatchLabels> | — |
getColorNameNames the color in place of the generated name. | ((color: Color) => string) | — |
localeThe locale of the transparency percentage. | Intl.LocalesArgument | — |
| Data attribute | Description |
|---|---|
data-empty | Present when there is no color. |
data-transparent | Present when the color's alpha is below 1. |
| CSS variable | Description |
|---|---|
--color | The color. |
--color-opaque | The color at alpha 1. |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
Color Swatch takes no focus and has no keys of its own.
Accessibility
| Part | Sets | So that |
|---|---|---|
| Root | role="img" and aria-label with the color's name, unless it has aria-label or aria-labelledby; no role description | the color is read as words, once, and not skipped as decoration |
| Root | "No color" (labels.noColor) and data-empty for null | an empty value is said, not shown as a blank square |
Focus order
- Color Swatch is not focusable; focus passes over it.