<x-widget.color-picker>
Colour picker Blade component for Laravel and Livewire
A colour in a field: a hex box that submits #rrggbb, the browser's own picker beside it for any colour, and optional swatches as a radio group, named for screen readers. A preview shows the colour with black or white text, whichever reads better. Typing #abc or #ABCDEF tidies to #aabbcc, and anything that isn't a colour goes back to the last one. Works with Livewire wire:model.
php artisan bladewell:add color-picker
- Works with
- Laravel 12 and 13, Livewire 3 and 4, Tailwind CSS 4.1+, PHP 8.3+
Usage
Livewire
In a Livewire component, bind with wire:model (deferred) or wire:model.live to a string property: public string $accent = '#7c3aed'. No name is needed. A swatch, the browser's picker or a typed colour updates it, and setting it in PHP selects the matching swatch and repaints the preview after the render.
<x-widget.color-picker label="Accent colour" wire:model.live="accent" :swatches="['#dc2626', '#16a34a', '#2563eb']" />
On the server
The colour picker can't stop a request that skips it. Here's what to check on your server.
Security
-
A colour usually ends up in CSS or a
styleattribute, shown to other people, and anything typed there can change the page. Validate withhex_colorbefore you save it, and only ever output a value that passed.
Examples
Basic
A hex box that submits #rrggbb, with the browser's own picker beside it. Typing #ABC tidies to #aabbcc when you leave the box; text that isn't a colour goes back to the last one. Validate with 'hex_color'.
Show code Hide code
<x-widget.color-picker name="accent" label="Accent colour" value="#7c3aed" class="max-w-xs" />
Swatches
swatches offers colours to pick in one click, as a radio group: the arrow keys move between them, and a screen reader hears each one's name. Give them names (name => hex) or a plain list of hex values. custom="false" leaves out the browser's own picker, so only the box and the swatches choose.
Show code Hide code
<x-widget.color-picker name="label_color" label="Label colour" value="#16a34a" :custom="false" :swatches="[
'Red' => '#dc2626',
'Amber' => '#d97706',
'Green' => '#16a34a',
'Teal' => '#0d9488',
'Blue' => '#2563eb',
'Violet' => '#7c3aed',
'Slate' => '#475569',
]" class="max-w-xs" />
Props
Other attributes, such as autocomplete or data-*, are passed through to the element. class styles the component's outer wrapper.
<x-widget.color-picker>
| Prop | Default | Description |
|---|---|---|
| name |
null
|
What the colour submits as (#rrggbb); its error and old input are found under it. Optional with wire:model, which then names it. |
| id |
null
|
Defaults to one made from the name (or the wire:model property). |
| label |
null
|
Shown above the field, and its name for screen readers. |
| value |
null
|
The colour, as #rrggbb or #rgb. Old input wins after a failed submit; with wire:model and no value, the bound property. |
| swatches |
[]
|
Colours to pick from in one click: a list of hex values, or name => hex ("Brand" => "#7c3aed"), whose names screen readers hear. |
| custom |
true
|
Offers the browser's own picker beside the box, for any colour. false for the swatches and the box only. |
| error |
null
|
An error message of your own; otherwise the validation error for the name, from the session or Livewire. |
| info |
null
|
A hint under the field. |
| bag |
'default'
|
Which error bag to read the error from. |
| disabled |
false
|
Greyed out: it can't be changed. |
| readonly |
false
|
Shown, and submitted, but it can't be changed: no picker or swatches, and the box can't be typed in. With disabled, disabled wins. |
Accessibility
All 2 examples above are checked with axe-core against the WCAG 2.2 A and AA rules, in the light theme and the dark one, both as the page draws and with each popover, dialog, toast and tooltip opened, on every change. A change that fails can't be merged. Where axe can't decide, such as contrast on SVG text, the test measures the colours itself instead of letting it pass.
Automated checks can't judge everything: how it sounds in a screen reader, and how it feels to use from the keyboard, still need a person. Check those on your own pages too.
Source
What bladewell:add color-picker writes to your app with the default namespaces. Prefer to copy by hand? Take these files, plus the ones from
Field, and the theme and base CSS.
resources/views/components/widget/color-picker/index.blade.php Show
resources/js/widget/color-picker/index.js Show
app/View/Widget/ElementIds.php Show
app/View/Widget/FormField.php Show