Skip to content

Color field

Native colour picker as the swatch, a hex input kept in sync, and preset swatches from the kit's categorical palette. Value is always #rrggbb.

View as Markdown

Preview

Brand colour Presets default to the chart palette, so a chosen colour matches the charts.

<div class="nx-field">
  <label class="nx-field__label" for="brand-color">Colour</label>
  <div class="nx-color">
    <input type="color" class="nx-color__picker" aria-label="Colour"
        value="#2a78d6">
    <input id="brand-color" class="nx-field__input nx-color__hex"
        value="#2a78d6" maxlength="7">
    <div class="nx-color__presets" role="group" aria-label="Colour">
      <button type="button" class="nx-color__preset"
          style="background:#2a78d6" aria-label="Blue #2a78d6"
          aria-pressed="true"></button>
      <button type="button" class="nx-color__preset"
          style="background:#1baf7a" aria-label="Aqua #1baf7a"
          aria-pressed="false"></button>
      <button type="button" class="nx-color__preset"
          style="background:#eda100" aria-label="Yellow #eda100"
          aria-pressed="false"></button>
    </div>
  </div>
</div>

API

React props (NxKit)

React (NxKit) props
PropTypeDefaultRequiredDescription
labelstringRequiredVisible label; also names the picker unless pickerLabel is given.
valuestringRequired#rrggbb or "" (cleared).
onChange(value: string) => voidRequiredCalled with a complete #rrggbb (lower case), or "" when cleared. Never with a half-typed value.
presets{ value: string; name: string }[]NX_COLOR_PRESETSSwatches under the field; [] hides them.
clearablebooleanfalseShows a clear button and lets an empty value through.
namestringundefinedForm field name, on the hex input.

Classes

Framework-agnostic classes
ClassDescription
.nx-colorRow: picker, hex input, presets
.nx-color__pickerNative input[type=color] styled as a field-height swatch
.nx-color__hexField input, monospace, 9.5ch
.nx-color__preset24px round swatch button; aria-pressed ring when selected

States

invalid hex

Accessibility

  • The picker has its own accessible name (pickerLabel ?? label); the visible label points at the hex input.
  • Preset buttons are named by colour name + hex and carry aria-pressed, never colour alone.

Do / Don’t

  • Do: Store the hex the field gives you (#rrggbb) and validate it again on the server before it lands in a style attribute.Don't use a free-text input for colours. A typo silently produces no colour.

View in spec