One color shown as a small square or dot, over a checkerboard so a see-through color shows, or crossed out when no color is set.
Options
| Choice | Values | Default | What it does |
|---|---|---|---|
sizesmbk-color-swatch--size-<value> | lgmdsmxs | lg | 32, 24, 20 or 16 px across. The square’s corners get smaller with it. |
shapesmbk-color-swatch--shape-<value> | squareround | square | Square for lists and fields; round for a row of dots. |
nonesmbk-color-swatch--none | On or off | Off | No color is set: the default surface crossed by a slash, named “No color”. Set by the component when it is given no color. |
Examples
Usage
Each swatch is an image a screen reader names by its color; these are named as their brand calls them.
Sizes
Four sizes, 32, 24, 20 and 16 px, square and round.
See through colors
A see-through color shows the checkerboard behind it.
No color
With no color, the swatch is crossed out and named “No color”.
Beside its name
Beside words that name the color, the swatch is hidden from screen readers, which would hear it twice.
Guidelines
Purpose
Shows one color as a small square or dot: a saved color, a color in a list of settings, the color a field holds. A see-through color shows a checkerboard behind it, so its opacity can be seen, and no color at all shows as a crossed-out square. The swatch only shows a color; to choose one, use the color swatch picker or the color picker.
When to use
- To show a color beside its name or its code, in a list of colors or a row of settings.
- Inside the color field, the color picker and the swatch picker, which hold one.
When not to use
- To let the reader pick a color. Use the color swatch picker for a few set colors, or the color picker for any color.
- For a status or a category. Use the status dot, the badge or the legend item, which carry their meaning in words too.
Usage rules
Do
- Name a brand's colors as the brand does.
- Leave the color out when none is set, so the swatch is crossed out.
Don't
- Show “no color” as white or a pale gray: it reads as a color that is set.
- Rely on the swatch alone where the exact color matters; show the code beside it.
Give a color its brand name when it has one, so a screen reader hears the name the team uses.
Tell “no color” by a pale color: it looks like a color that is set. Leave the color out, so the swatch is crossed out.
Anatomy
- The block, a square or a dot with a hairline edge drawn inside it, over everything else.
- The checkerboard, the color checker's block (
smbk-color-checker): an empty element first inside the swatch, drawn under the color. - The color part, which the page paints with the color as an inline background color.
- The slash part, drawn instead of the checkerboard and the color when no color is set: a line from corner to corner in the danger icon color.
Behavior
The swatch holds no state. Given no color, it shows the none look.
In plain markup, put an empty smbk-color-checker element first inside the swatch and the color part after it, with the color as its inline background color; for none, give the block smbk-color-swatch--none and the slash part alone.
Accessibility
What it does for you
- A swatch is an image that a screen reader names by its color in words, such as “vibrant blue”, made from its value. With no color, it is named “No color”.
- No color is shown by a slash, a shape, not by a color alone.
- In forced colors mode the swatch keeps its color, since the color is what it shows, and its edge and slash take the reader's text color.
What you must do
- Give the swatch the color's own name when it has one, such as “Signal blue”.
- Beside words that already name the color, such as a code in a field, hide the swatch from screen readers (
decorative, oraria-hidden="true"), so the color is not heard twice. - In plain markup, give the swatch
role="img"and anaria-label.
How to test
- With a screen reader, a swatch on its own is read as an image by its color name, and one with no color as “No color”. One beside the color's name or code is not read.
- With no color set, the swatch shows a slash, not a blank or pale square.
- In a forced-color mode, the swatch keeps its color and its edge and slash are visible.
- With text at 200% and the page 320 pixels wide, the swatch keeps its shape.
Source
The files the color swatch is made of. Its folder on GitHub (opens in a new tab) holds them with its wrapper and its document.
Stylesheet
/* smbk-color-swatch — Color swatch. Figma "Color swatch" 7406:48 (Size: lg, md, sm, xs × Shape: Square, Round ×
* Value: Color, None).
* One color in a square or a dot: the checkerboard (smbk-color-checker) under the color part, so a see-through
* color shows what it lets through, and a hairline edge over both, so a color near the surface's own still shows
* its outline. The color is data, not a design value: the wrapper paints it on the color part as an inline
* background color. None, when no color is set, is the default surface crossed by a danger slash: a shape, so
* "no color" is not told by color alone. */
.smbk-color-swatch {
position: relative;
display: inline-block;
flex-shrink: 0;
box-sizing: border-box;
width: var(--smbk-size-32);
height: var(--smbk-size-32);
overflow: hidden;
border-radius: var(--smbk-radius-control-md);
background: var(--smbk-color-surface-default);
vertical-align: middle;
forced-color-adjust: none;
}
/* The edge, drawn inside and over the color, as Figma's inside stroke is. */
.smbk-color-swatch::after {
content: '';
position: absolute;
inset: 0;
border: var(--smbk-border-width-hairline) solid var(--smbk-color-border-edge-shade-subtle);
border-radius: inherit;
pointer-events: none;
}
.smbk-color-swatch__color {
position: absolute;
inset: 0;
border-radius: inherit;
}
/* None: a slash from corner to corner in the danger icon color, two pixels wide. */
.smbk-color-swatch__slash {
position: absolute;
inset: 0;
background: linear-gradient(
to bottom right,
transparent calc(50% - var(--smbk-border-width-2) / 2),
var(--smbk-color-feedback-danger-icon) 0 calc(50% + var(--smbk-border-width-2) / 2),
transparent 0
);
}
/* ---- sizes: lg, md, sm, xs; the square's corner follows the size ---- */
.smbk-color-swatch--size-md {
width: var(--smbk-size-24);
height: var(--smbk-size-24);
border-radius: var(--smbk-radius-control-sm);
}
.smbk-color-swatch--size-sm {
width: var(--smbk-size-20);
height: var(--smbk-size-20);
border-radius: var(--smbk-radius-control-sm);
}
.smbk-color-swatch--size-xs {
width: var(--smbk-size-16);
height: var(--smbk-size-16);
border-radius: var(--smbk-radius-control-xs);
}
/* Round: a dot at any size. Two classes, so it wins over each size's corner. */
.smbk-color-swatch.smbk-color-swatch--shape-round {
border-radius: var(--smbk-radius-full);
}
/* ---- high contrast (forced colors) ---- */
/* The color is what the swatch shows, so it keeps it (forced-color-adjust above); the edge and the slash take
the reader's text color, so the swatch's shape and "no color" still show. */
@media (forced-colors: active) {
.smbk-color-swatch::after {
border-color: CanvasText;
}
.smbk-color-swatch__slash {
background: linear-gradient(
to bottom right,
transparent calc(50% - var(--smbk-border-width-2) / 2),
CanvasText 0 calc(50% + var(--smbk-border-width-2) / 2),
transparent 0
);
}
}Tokens it reads
| Token and CSS | Value | Preview | Foundation |
|---|---|---|---|
| size.32--smbk-size-32 | 32px | Size | |
| radius.control.md--smbk-radius-control-md | 6px | Radius | |
| color.surface.default--smbk-color-surface-default | light: #f3f5f8dark: #181a1cchanges with theme | Color | |
| border-width.hairline--smbk-border-width-hairline | 1px | Border width | |
| color.border.edge-shade-subtle--smbk-color-border-edge-shade-subtle | light: #0000001adark: #00000033changes with theme | Color | |
| border-width.2--smbk-border-width-2 | 2px | Border width | |
| color.feedback.danger.icon--smbk-color-feedback-danger-icon | light: #cc0d0ddark: #fe7d6dchanges with theme | Color | |
| size.24--smbk-size-24 | 24px | Size | |
| radius.control.sm--smbk-radius-control-sm | 4px | Radius | |
| size.20--smbk-size-20 | 20px | Size | |
| size.16--smbk-size-16 | 16px | Size | |
| radius.control.xs--smbk-radius-control-xs | 2px | Radius | |
| radius.full--smbk-radius-full | 9999px | Radius |
Manifest
{
"name": "color-swatch",
"block": "smbk-color-swatch",
"summary": "One color shown as a small square or dot, over a checkerboard so a see-through color shows, or crossed out when no color is set.",
"adapt": "css",
"axes": {
"size": {
"values": [
"lg",
"md",
"sm",
"xs"
],
"default": "lg",
"description": "32, 24, 20 or 16 px across. The square’s corners get smaller with it."
},
"shape": {
"values": [
"square",
"round"
],
"default": "square",
"description": "Square for lists and fields; round for a row of dots."
}
},
"flags": {
"none": {
"description": "No color is set: the default surface crossed by a slash, named “No color”. Set by the component when it is given no color."
}
},
"parts": [
"color",
"slash"
],
"figmaNodeId": "7406:48"
}