Give it a name and it submits with the form, responds to form.reset(), and
participates in native validation.
Loading…
"use client";
import { ColorInput } from "chroma-panel";
import { useState } from "react";
export default function ColorInputFormDemo() {
const [submitted, setSubmitted] = useState<string | null>(null);
return (
<form
className="flex items-center gap-3"
onSubmit={(event) => {
event.preventDefault();
const data = new FormData(event.currentTarget);
setSubmitted(String(data.get("brand")));
}}
>
<ColorInput name="brand" defaultValue="#cc3366" injectStyles={false} />
<button
type="submit"
className="rounded-md border border-edge px-3 py-1.5 text-sm font-medium"
>
Submit
</button>
<button
type="reset"
className="rounded-md px-3 py-1.5 text-sm font-medium text-muted-foreground"
onClick={() => setSubmitted(null)}
>
Reset
</button>
{submitted && (
<span className="font-mono text-sm text-muted-foreground">
{submitted}
</span>
)}
</form>
);
}<form onSubmit={handleSubmit}>
<ColorInput name="brand" defaultValue="#cc3366" required />
<button type="submit">Save</button>
</form>The submitted value is the string in whichever format you set, so a form that
needs rgba() gets it without a conversion step on your side.
Validation
required marks the control invalid while it is empty. validationBehavior
chooses between the browser's own bubble and an ARIA-only message:
<ColorInput name="brand" required validationBehavior="aria" />Resetting
form.reset() returns the control to defaultValue, like any native input. A
controlled ColorInput is yours to reset — the form cannot change state you own.
API reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | — | Whether the popover is open, when you control it. |
| defaultOpen | boolean | false | Whether the popover starts open. |
| onOpenChange | (open: boolean) => void | — | Fires when the popover opens or closes, however it was triggered. |
| name | string | — | Submits with the surrounding form under this name. The submitted value is the string in whichever format you set. |
| form | string | — | Associates the control with a form by id, for when it cannot be nested inside one. |
| required | boolean | false | Marks the control invalid while it has no value. |
| readOnly | boolean | false | Shows the colour but does not allow changing it. Unlike disabled, the value still submits. |
| autoComplete | string | — | Passed through to the hidden input backing the control. |
| validationBehavior | "native" | "aria" | 'native' | 'native' uses the browser's own validation bubble. 'aria' reports the message through ARIA only, for when you render your own. |
| id | string | — | Id on the trigger, so your own <label htmlFor> can point at it. |
| aria-label | string | 'Choose a colour' | Accessible name for the trigger, used when there is no visible label. |
| triggerClassName | string | — | Class on the swatch button only. Equivalent to classNames.trigger. |
| value | string | Hsva | — | The colour, when you keep it in your own state.Inherited from ChromaPanel. |
| defaultValue | string | Hsva | '#3366cc' | The starting colour, when you want the panel to keep it.Inherited from ChromaPanel. |
| onChange | (color: ColorChangeResult) => void | — | Fires continuously while a drag is in progress.Inherited from ChromaPanel. |
| onChangeComplete | (color: ColorChangeResult) => void | — | Fires once when the drag ends. Use it for saving, undo entries and network calls.Inherited from ChromaPanel. |
| modes | (ModeId | string | PickerMode)[] | all five | Which tabs appear, in the order you list them.Inherited from ChromaPanel. |
| mode | string | — | The open tab, when you control it.Inherited from ChromaPanel. |
| defaultMode | string | first mode | The tab to open on.Inherited from ChromaPanel. |
| onModeChange | (mode: string) => void | — | Fires when the reader switches tabs.Inherited from ChromaPanel. |
| format | ColorFormat | 'hex' | Sets the css string on the change result, and the value a form submits.Inherited from ChromaPanel. |
| showAlpha | boolean | true | Turn off when opacity is not allowed.Inherited from ChromaPanel. |
| showEyedropper | boolean | true | Turn off to hide the eyedropper even where it is supported.Inherited from ChromaPanel. |
| showRecentColors | boolean | true | Turn off in a one-shot picker.Inherited from ChromaPanel. |
| recentColors | string[] | — | The recent-colour history, when you keep it. Persist it yourself to carry the list between sessions.Inherited from ChromaPanel. |
| defaultRecentColors | string[] | [] | The starting history, when you want the panel to keep it.Inherited from ChromaPanel. |
| onRecentColorsChange | (colors: string[]) => void | — | Fires with the whole list whenever a colour is added to it.Inherited from ChromaPanel. |
| palettes | ColorPalette[] | built-in set | Replaces the palette swatches with your own.Inherited from ChromaPanel. |
| pencils | string[] | built-in 120-colour grid | Replaces the pencil grid.Inherited from ChromaPanel. |
| modeProps | Record<string, Record<string, unknown>> | — | Props spread onto one mode's panel, keyed by mode id. Lets a custom mode take props without the panel knowing about it.Inherited from ChromaPanel. |
| imageOptions | ExtractOptions | — | Shorthand for modeProps.image.extractOptions.Inherited from ChromaPanel. |
| disabled | boolean | false | Makes the panel read-only.Inherited from ChromaPanel. |
| theme | "dark" | "light" | system | Forces one theme instead of following the OS.Inherited from ChromaPanel. |
| showTitleBar | boolean | true | Turn off for an inline panel with no chrome.Inherited from ChromaPanel. |
| title | string | 'Colours' | The title bar text.Inherited from ChromaPanel. |
| onClose | () => void | — | Fires when the red window control is used. An inline panel has nothing to close, so that control is dimmed until you pass this.Inherited from ChromaPanel. |
| collapsed | boolean | — | Whether the panel is collapsed to its title bar, when you control it.Inherited from ChromaPanel. |
| defaultCollapsed | boolean | false | Whether it starts collapsed. Collapsing hides the body with CSS rather than unmounting, so nothing is lost.Inherited from ChromaPanel. |
| onCollapsedChange | (collapsed: boolean) => void | — | Fires when the yellow window control is used.Inherited from ChromaPanel. |
| size | PanelSize | — | The panel size, when you control it.Inherited from ChromaPanel. |
| defaultSize | PanelSize | 'default' | The starting size. 'expanded' widens the panel, and the wheel with it.Inherited from ChromaPanel. |
| onSizeChange | (size: PanelSize) => void | — | Fires when the green window control is used.Inherited from ChromaPanel. |
| injectStyles | boolean | true | Turn off when you import chroma-panel/style.css yourself, as this site does.Inherited from ChromaPanel. |
| className | string | — | Class applied to the panel root.Inherited from ChromaPanel. |
| classNames | ChromaClassNames | — | Classes applied per part.Inherited from ChromaPanel. |
| style | React.CSSProperties | — | Inline styles on the panel root. Setting the --cp-* custom properties here themes a single panel.Inherited from ChromaPanel. |
| store | ColorStore | — | An external colour store, for driving several panels from one colour.Inherited from ChromaPanel. |