Swatch
A swatch shows one colour in a square of its own, four fifths of a square wide and high (16px), so it stands on the sixteen beside words. A 1px line runs round the colour, so a pale colour still shows on the paper. It has square corners, and it draws nothing else.
Design examples
Section titled “Design examples”Named swatches, exact values and the native picker share a row. Sample colours come from the shared palette.
<section class="specimen">
<ds-row class="line" ><ds-swatch value="#99ff73" aria-hidden="true"></ds-swatch><ds-text literal class="grow">Apple green</ds-text><ds-color><input type="color" value="#99ff73" aria-label="Apple green"></ds-color></ds-row><ds-row class="line" ><ds-swatch value="#ccccff" aria-hidden="true"></ds-swatch><ds-text literal class="grow">Zenith blue</ds-text><ds-color><input type="color" value="#ccccff" aria-label="Zenith blue"></ds-color></ds-row><ds-row class="line" ><ds-swatch value="#f5aad1" aria-hidden="true"></ds-swatch><ds-text literal class="grow">Pink</ds-text><ds-color><input type="color" value="#f5aad1" aria-label="Pink"></ds-color></ds-row>
</section>
<style>
.specimen { width: 100%; min-width: 0; --cell: calc(20px * var(--sf, 1)); }
.specimen .line { width: 100%; }
.specimen .grow { flex: 1; min-width: 0; }
.specimen .muted { color: var(--ds-text-color-muted); }
.specimen .mono { --ds-typeface-regular: var(--ds-typeface-mono); font-variant-numeric: tabular-nums; }
.specimen .value { width: calc(4 * var(--cell)); text-align: right; }
.specimen .mark { display: inline-flex; align-items: flex-end; width: var(--cell); height: var(--ds-control-height); flex: 0 0 var(--cell); }
.specimen .columns { display: flex; flex-wrap: wrap; gap: var(--cell); }
.specimen .columns > * { min-width: 0; }
.specimen fieldset { border: 0; padding: 0; margin: 0; }
.specimen legend { padding: 0; }
.specimen label { display: flex; align-items: flex-end; gap: calc(var(--cell) / 2); min-height: var(--cell); }
.specimen ds-panel { height: calc(10 * var(--cell)); }
</style> Try it
Section titled “Try it”Each sample stands in an icon button with its own name. The selected one carries the outline.
<ds-button control="slotted" icon><button type="button" aria-label="Apple green" aria-pressed="true"><ds-swatch value="#99ff73" selected aria-hidden="true"></ds-swatch></button></ds-button>
<ds-button control="slotted" icon><button type="button" aria-label="Zenith blue" aria-pressed="false"><ds-swatch value="#ccccff" aria-hidden="true"></ds-swatch></button></ds-button>
<ds-button control="slotted" icon><button type="button" aria-label="Pink" aria-pressed="false"><ds-swatch value="#f5aad1" aria-hidden="true"></ds-swatch></button></ds-button> <ds-button control="slotted" icon> <button type="button" aria-label="Apple green" aria-pressed="true"> <ds-swatch value="#99ff73" selected aria-hidden="true"></ds-swatch> </button></ds-button>Behaviour
Section titled “Behaviour”value takes any CSS colour, a token included. It sets only the swatch’s background, so a value that is not a colour draws nothing and cannot add other styles. With no value it is transparent, and only the line shows.
selected draws a 2px outline in the text’s ink around the swatch. The swatch does not change it by itself: the page sets and clears it.
A swatch has no press of its own. For a choice, put it in a shared button. Select draws one in its control when given swatch. Colour data, saving and any contrast or permission rules belong to the product.
Accessibility
Section titled “Accessibility”A swatch has no role and no name, and a colour alone says nothing to someone who cannot see it. Mark it aria-hidden="true", then name the colour on the button around it or in the words beside it. Show a choice with aria-pressed on the button: the outline is only drawn.
| Attribute | Type | Default | What it does |
|---|---|---|---|
value | string | transparent | The CSS colour shown. |
selected | boolean | false | Draws the selection outline. |
It has no slots, events or tokens of its own. The line is --ds-border-color and the outline --ds-text-color. Add hidden to hide it.
Copy swatch through Sync 1. There is no React adapter: render ds-swatch itself after registering ds1/2-core/ds-swatch. On Apple platforms it is DS1Swatch.