Zubra UI
Code for
GitHub

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

Compiled (any project)
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
One file, no dependencies (3.5 KB gzipped)
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
.zb source (to edit it with Zubra)
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
No build: a script tag, straight from this site
HTML
<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

index.html
<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>
Example.jsx
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>
  );
}
Example.vue
<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('-') } } }) -->
Example.svelte
<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>
example.component.ts
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

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
valuestring"#16a34a"
colorsstring[]["#16a34a","#2563eb","#db2777","#ea580c","#7c3aed","#0891b2"]
namesstring[]["Green","Blue","Pink","Orange","Violet","Cyan"]
disabledbooleanfalse
labelstring"Colour"
Events
EventWhen
valuechangeThe 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.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour