Skip to content

Color Grid Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Let the user pick a color from a grid of preset colors. value holds the hex color of the selected cell, and the valuechange event updates the readout.

The sliders set columns and rows, and the built-in palette spreads its hues over the columns and its shades over the rows.

Palette
12
10
State
<div class="color-grid-basic">
<sv-color-grid aria-label="Color" value="#3b82f6"></sv-color-grid>
<output></output>
</div>
<script type="application/x-typescript" module>
const colorGrid = document.querySelector<ColorGrid>(".color-grid-basic sv-color-grid")!;
const valueOutput = document.querySelector<HTMLOutputElement>(".color-grid-basic output")!;
updateValueOutput();
colorGrid.addEventListener("valuechange", updateValueOutput);
function updateValueOutput() {
valueOutput.value = `value="${colorGrid.value}"`;
}
</script>
<style>
.color-grid-basic {
display: flex;
flex-direction: column;
gap: 0.75em;
align-items: center;
justify-content: center;
output {
font-family: var(--sv-font-mono, monospace);
}
}
</style>

Translated Names

Read the built-in color names to assistive technology in another language. valueLabelHandler gets the English name of each built-in color, like "dark blue", and returns the Spanish name.

The readout shows the name that assistive technology reads for the selected color.

<div class="color-grid-translated-names" lang="es">
<sv-color-grid aria-label="Color" value="#2c8ae8"></sv-color-grid>
<output></output>
</div>
<script type="application/x-typescript" module>
const colorGrid = document.querySelector<ColorGrid>(
".color-grid-translated-names sv-color-grid"
)!;
const nameOutput = document.querySelector<HTMLOutputElement>(
".color-grid-translated-names output"
)!;
const spanishHueNames: Record<string, string> = {
red: "rojo",
orange: "naranja",
yellow: "amarillo",
lime: "lima",
green: "verde",
mint: "menta",
cyan: "cian",
"sky blue": "celeste",
blue: "azul",
violet: "violeta",
magenta: "magenta",
pink: "rosa",
white: "blanco",
gray: "gris",
black: "negro",
};
const spanishShadeNames: Record<string, string> = {
dark: "oscuro",
light: "claro",
};
colorGrid.valueLabelHandler = (_hexColor, label) => {
const [, shadeName = "", hueName] = label.match(/^(?:(dark|light) )?(.+)$/)!;
const spanishName = [spanishHueNames[hueName], spanishShadeNames[shadeName]]
.filter(Boolean)
.join(" ");
nameOutput.value = spanishName;
return spanishName;
};
</script>
<style>
.color-grid-translated-names {
display: flex;
flex-direction: column;
gap: 0.75em;
align-items: center;
justify-content: center;
}
</style>

Custom Palette

Offer your own set of named colors, like tag colors in a form. A slotted <datalist> gives one cell for each option, columns="4" fills them in rows of four, and the label of each option names its color.

The readout shows the label of the selected color.

<!-- eslint-disable html/use-baseline -->
<form class="color-grid-custom-palette">
<label for="color-grid-tag-color">Tag color</label>
<sv-color-grid id="color-grid-tag-color" columns="4" name="tag-color" value="#16a34a">
<datalist>
<option label="Bug" value="#dc2626"></option>
<option label="Urgent" value="#ea580c"></option>
<option label="Review" value="#ca8a04"></option>
<option label="Done" value="#16a34a"></option>
<option label="Docs" value="#0891b2"></option>
<option label="Feature" value="#2563eb"></option>
<option label="Design" value="#7c3aed"></option>
<option label="Idea" value="#db2777"></option>
</datalist>
</sv-color-grid>
<output></output>
</form>
<script type="application/x-typescript" module>
const colorGrid = document.querySelector<ColorGrid>(".color-grid-custom-palette sv-color-grid")!;
const labelOutput = document.querySelector<HTMLOutputElement>(
".color-grid-custom-palette output"
)!;
updateLabelOutput();
colorGrid.addEventListener("valuechange", updateLabelOutput);
function updateLabelOutput() {
const selectedOption = colorGrid.querySelector<HTMLOptionElement>(
`option[value="${colorGrid.value}"]`
);
labelOutput.value = selectedOption?.label ?? "";
}
</script>
<style>
.color-grid-custom-palette {
display: flex;
flex-direction: column;
gap: 0.5em;
align-items: center;
justify-content: center;
label {
font-weight: 600;
}
sv-color-grid {
--sv-area-slider-width: 10em;
}
}
</style>