color-swatches
Pick a colour from colors. A form control (a radio group): arrow keys move the choice. names are what each colour is called, in the same order ("Barn red"), for screen readers and the tooltip; a colour without one is announced by its value.
Try it: events appear here (valuechange).
Source: color-swatches.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/color-swatches.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/color-swatches.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/color-swatches.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/color-swatches.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/color-swatches.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/color-swatches.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/color-swatches.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/color-swatches.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/color-swatches.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/color-swatches.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/color-swatches.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/color-swatches.json
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/color-swatches.js"></script>The compiled install writes src/components/zubra/color-swatches.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.
Usage
<color-swatches label="Label colour" name="colour"></color-swatches>
<script type="module">
import './components/zubra/color-swatches.js';
const element = document.querySelector('color-swatches');
element.value = '#2563eb';
element.addEventListener('valuechange', (event) => console.log('valuechange', event.detail));
</script>import './components/zubra/color-swatches.js';
const value = '#2563eb';
export function ColorSwatchesExample() {
return (
<color-swatches
label="Label colour"
name="colour"
value={value}
onvaluechange={(event) => console.log('valuechange', event.detail)}
></color-swatches>
);
}<script setup>
import './components/zubra/color-swatches.js';
const value = '#2563eb';
const onValuechange = (event) => console.log('valuechange', event.detail);
</script>
<template>
<color-swatches
label="Label colour"
name="colour"
:value.prop="value"
@valuechange="onValuechange"
></color-swatches>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/color-swatches.js';
const value = '#2563eb';
</script>
<color-swatches
label="Label colour"
name="colour"
{value}
onvaluechange={(event) => console.log('valuechange', event.detail)}
></color-swatches>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/color-swatches.js';
@Component({
selector: 'app-color-swatches-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<color-swatches
label="Label colour"
name="colour"
[value]="value"
(valuechange)="onValuechange($event)"
></color-swatches>
`,
})
export class ColorSwatchesExample {
value = '#2563eb';
onValuechange(event: Event) {
console.log('valuechange', (event as CustomEvent).detail);
}
}API
| Property | Type | Default |
|---|---|---|
value | string | "#16a34a" |
colors | string[] | ["#16a34a","#2563eb","#db2777","#ea580c","#7c3aed","#0891b2"] |
names | string[] | ["Green","Blue","Pink","Orange","Violet","Cyan"] |
disabled | boolean | false |
label | string | "Colour" |
| Event | When |
|---|---|
valuechange | The colour changed; detail is the colour. |
Size
1.2 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 17.7 KB.