Skip to content

Color picker ​

Preset swatches, a custom color from the system picker and a hex field, kept in sync with one form value.

Installation ​

bash
npx bootcn add color-picker
bash
pnpm dlx bootcn add color-picker
bash
yarn dlx bootcn add color-picker
bash
bunx --bun bootcn add color-picker

The CLI also installs the design tokens and these components: label. It prints the <link> and <script> tags to add to your layout.

Usage ​

Offer a few brand-safe presets and let people pick any other color with the system picker or by typing a hex value. If text will sit on the chosen color, check its contrast before saving it.

JavaScript API ​

  • data-bootcn-color-picker on the fieldset. Swatches are radio inputs (their value is the hex color), with the system picker (type="color"), the hex field and the hidden input marked data-bootcn-color-picker-value kept in sync.
  • An invalid hex gets aria-invalid="true" and changes nothing.
  • Event bootcn:color-picker:change (bubbles), detail.value.
  • Bootcn.colorPicker.setColor(root, '#rrggbb'); init() / destroy().

Accessibility ​

  • A fieldset with a legend; swatches are native radio buttons named after their color ("Blue"), so arrow keys move between them and screen readers announce the choice.
  • The selected swatch has a ring in the foreground color (not color alone); the hex field has a label, help text and an error state.

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.
  • The label component.

Source ​

The exact files the CLI installs (color-picker@1.0.0). View on GitHub · Report an issue

html
<fieldset class="bootcn-color-picker" data-bootcn-color-picker>
  <legend class="bootcn-label">Brand color</legend>
  <div class="bootcn-color-picker__swatches">
    <label class="bootcn-color-swatch" title="Blue">
      <input type="radio" name="bootcn-color-picker-swatch" value="#2563eb" checked />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #2563eb"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Blue</span>
    </label>
    <label class="bootcn-color-swatch" title="Teal">
      <input type="radio" name="bootcn-color-picker-swatch" value="#0d9488" />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #0d9488"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Teal</span>
    </label>
    <label class="bootcn-color-swatch" title="Green">
      <input type="radio" name="bootcn-color-picker-swatch" value="#16a34a" />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #16a34a"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Green</span>
    </label>
    <label class="bootcn-color-swatch" title="Amber">
      <input type="radio" name="bootcn-color-picker-swatch" value="#d97706" />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #d97706"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Amber</span>
    </label>
    <label class="bootcn-color-swatch" title="Red">
      <input type="radio" name="bootcn-color-picker-swatch" value="#dc2626" />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #dc2626"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Red</span>
    </label>
    <label class="bootcn-color-swatch" title="Pink">
      <input type="radio" name="bootcn-color-picker-swatch" value="#db2777" />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #db2777"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Pink</span>
    </label>
    <label class="bootcn-color-swatch" title="Violet">
      <input type="radio" name="bootcn-color-picker-swatch" value="#7c3aed" />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #7c3aed"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Violet</span>
    </label>
    <label class="bootcn-color-swatch" title="Black">
      <input type="radio" name="bootcn-color-picker-swatch" value="#18181b" />
      <span
        class="bootcn-color-swatch__chip"
        style="--bootcn-swatch: #18181b"
        aria-hidden="true"
      ></span>
      <span class="visually-hidden">Black</span>
    </label>
  </div>
  <div class="bootcn-color-picker__custom">
    <input
      class="bootcn-color-picker__native"
      type="color"
      value="#2563eb"
      aria-label="Choose a custom color"
    />
    <div class="bootcn-field">
      <label class="bootcn-label" for="bootcn-color-picker-hex">Hex</label>
      <input
        class="bootcn-color-picker__hex"
        type="text"
        id="bootcn-color-picker-hex"
        value="#2563eb"
        maxlength="7"
        spellcheck="false"
        autocomplete="off"
        aria-describedby="bootcn-color-picker-hex-help"
      />
      <p class="bootcn-field__help" id="bootcn-color-picker-hex-help">
        Six digits, for example #2563eb.
      </p>
    </div>
  </div>
  <input type="hidden" name="brand-color" value="#2563eb" data-bootcn-color-picker-value />
</fieldset>
css
.bootcn-color-picker {
  min-width: 0;
  max-width: 20rem;
  margin: 0;
  padding: 0;
  border: 0;
}

.bootcn-color-picker > legend {
  float: none;
  margin-bottom: 0.75rem;
  padding: 0;
}

.bootcn-color-picker__swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.bootcn-color-swatch {
  position: relative;
  cursor: pointer;
}

.bootcn-color-swatch input {
  position: absolute;
  opacity: 0;
}

.bootcn-color-swatch__chip {
  display: block;
  width: 1.75rem;
  height: 1.75rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: 9999px;
  background-color: var(--bootcn-swatch);
}

/* Selected: a ring in the foreground color, offset by the background. */
.bootcn-color-swatch input:checked + .bootcn-color-swatch__chip {
  box-shadow:
    0 0 0 2px var(--bootcn-background),
    0 0 0 4px var(--bootcn-foreground);
}

.bootcn-color-swatch input:focus-visible + .bootcn-color-swatch__chip {
  box-shadow:
    0 0 0 2px var(--bootcn-background),
    0 0 0 4px var(--bootcn-ring);
}

.bootcn-color-picker__custom {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-top: 1rem;
}

.bootcn-color-picker__native {
  flex: none;
  width: var(--bootcn-control-height);
  height: var(--bootcn-control-height);
  margin-top: 1.625rem;
  padding: 0.25rem;
  border: var(--bootcn-border-width) solid var(--bootcn-input);
  border-radius: var(--bootcn-radius);
  background: transparent;
  cursor: pointer;
}

.bootcn-color-picker__native:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-color-picker__hex {
  width: 8rem;
  min-height: var(--bootcn-control-height);
  padding: 0 0.75rem;
  border: var(--bootcn-border-width) solid var(--bootcn-input);
  border-radius: var(--bootcn-radius);
  background: transparent;
  color: var(--bootcn-foreground);
  font-family: var(--bs-font-monospace);
  font-size: 0.875rem;
}

.bootcn-color-picker__hex:focus {
  border-color: var(--bootcn-ring);
  outline: 0;
  box-shadow: 0 0 0 3px var(--bs-focus-ring-color);
}

.bootcn-color-picker__hex[aria-invalid='true'] {
  border-color: var(--bootcn-destructive);
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.colorPicker) return; // already loaded: idempotent

  const ROOT = '[data-bootcn-color-picker]';
  const HEX = /^#[0-9a-f]{6}$/i;
  let listening = false;

  // Writes one color to every part: swatches, system picker, hex field, value.
  function setColor(root, color, from) {
    const value = color.toLowerCase();
    root.querySelectorAll('.bootcn-color-swatch input').forEach((radio) => {
      radio.checked = radio.value.toLowerCase() === value;
    });
    const native = root.querySelector('.bootcn-color-picker__native');
    const hex = root.querySelector('.bootcn-color-picker__hex');
    const hidden = root.querySelector('[data-bootcn-color-picker-value]');
    if (native && from !== native) native.value = value;
    if (hex && from !== hex) {
      hex.value = value;
      hex.removeAttribute('aria-invalid');
    }
    if (hidden) hidden.value = value;
    root.dispatchEvent(
      new CustomEvent('bootcn:color-picker:change', { bubbles: true, detail: { value: value } }),
    );
  }

  function onInput(event) {
    const root = event.target.closest && event.target.closest(ROOT);
    if (!root) return;
    const target = event.target;
    if (
      target.matches('.bootcn-color-swatch input') ||
      target.matches('.bootcn-color-picker__native')
    ) {
      setColor(root, target.value, target);
    } else if (target.matches('.bootcn-color-picker__hex')) {
      const value = target.value.trim();
      const valid = HEX.test(value);
      target.setAttribute('aria-invalid', String(!valid));
      if (valid) setColor(root, value, target);
    }
  }

  function init() {
    if (listening) return;
    document.addEventListener('input', onInput);
    document.addEventListener('change', onInput);
    listening = true;
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('input', onInput);
    document.removeEventListener('change', onInput);
    listening = false;
  }

  ns.colorPicker = { init: init, destroy: destroy, setColor: setColor };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.