Color picker example

Color formats with canonical form values.

Edit HEX, RGB or HSL, adjust opacity and submit one canonical HEX value. External form ownership and native reset share the same draft contract.

Complete React client module · MIT · Next.js and Vite

Interactive example

Color formats with canonical form values

Edit HEX, RGB or HSL, adjust opacity and submit one canonical HEX value. External form ownership and native reset share the same draft contract.

Keyboard

  • Tab reaches text, format, opacity and swatches. Enter commits; Escape restores the committed color.
  • The opacity range supports native arrow keys. Invalid text prevents native submission.

States covered

  • HEX RGB HSL
  • alpha
  • external form
  • prevented reset
  • controlled null
  • read only and disabled

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-color-format-form.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useId,useState} from 'react';
import {Button,Checkbox,ColorPicker,UIProvider} from '@bld8/ui';
export default function ColorForm(){const id=useId(),[prevent,setPrevent]=useState(false),[owned,setOwned]=useState<string|null>('#273E35'),[reject,setReject]=useState(false),[result,setResult]=useState('No colors submitted.');return <UIProvider><div style={{display:'grid',gap:24,maxWidth:580}}>
<ColorPicker label="Brand color" name="brand" form={id} required defaultValue="#FFD900" swatches={['#FFD900','#273E35','#F2F3EF','#A8BDB0']}/>
<ColorPicker label="Host color" name="owned" form={id} value={owned} onValueChange={next=>{if(!reject)setOwned(next);}} alpha={false}/>
<ColorPicker label="Locked color" name="locked" form={id} defaultValue="#FFFFFF" readOnly alpha={false}/>
<ColorPicker label="Disabled color" name="disabled" form={id} defaultValue="#1B2420" disabled/>
<Checkbox label="Reject host color edits" checked={reject} onCheckedChange={setReject}/><Checkbox label="Prevent color reset" checked={prevent} onCheckedChange={setPrevent}/>
<form id={id} aria-label="Color settings" onReset={event=>{if(prevent)event.preventDefault();}} onSubmit={event=>{event.preventDefault();const data=new FormData(event.currentTarget);setResult('Brand: '+data.get('brand')+' | Owned: '+data.get('owned')+' | Locked: '+data.get('locked')+' | Disabled: '+data.has('disabled'));}}><div style={{display:'flex',gap:12,flexWrap:'wrap'}}><Button type="submit">Read colors</Button><Button type="reset" variant="secondary">Reset colors</Button><Button variant="ghost" onClick={()=>setOwned(null)}>Clear host color</Button></div></form><p role="status">{result}</p>
</div></UIProvider>;}