ColorPicker
Choose a color with a popup panel. For an embedded panel, use ColorPanel.
Import
import { HuiColorPicker } from '@nscode/handy-ui'
Basic Usage
Bind a color string with v-model. Empty or invalid values show an unselected swatch.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
const color = ref<string | null>('#409EFF')
</script>
<template>
<HuiSpace>
<HuiColorPicker v-model="color" />
<span>{{ color ?? 'No color' }}</span>
</HuiSpace>
</template>
Self-managed And Controlled Values
Without modelValue, the picker starts unselected and stores choices internally, including when only an update listener is supplied. Passing modelValue makes it controlled, even when undefined, null, or invalid. After selection or clearing, the swatch, panel, and form read the value accepted by the parent; ignored requests leave the accepted value unchanged.
Dragging keeps the active-change preview. Committing returns to the accepted value. Removing the prop restores the saved internal value; changing control mode cancels pending drags and eyedropper results and emits a development diagnostic. Choose a color and redraw the parent to compare all five modes.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
const statePresets = ['#ff0000', '#00ff00', '#0000ff']
const stateBound = ref<string | null>('#ff0000')
const stateRedraw = ref(0)
const stateLast = ref('None')
const recordState = (value: string | null) => {
stateLast.value = value ?? 'null'
}
</script>
<template>
<HuiSpace class="color-picker-q1-demo" direction="vertical" fill :data-parent="stateRedraw">
<HuiSpace wrap>
<HuiButton class="color-redraw" @click="stateRedraw++"
>Redraw parent {{ stateRedraw }}</HuiButton
>
<span>Choose a preset or enter a color. Clearing requests null.</span>
</HuiSpace>
<HuiSpace class="color-local" wrap>
<span>Unbound</span>
<HuiColorPicker :predefine="statePresets" :eye-dropper="false" clearable />
</HuiSpace>
<HuiSpace class="color-listener" wrap>
<span>Listener only</span>
<HuiColorPicker
:predefine="statePresets"
:eye-dropper="false"
clearable
@update:model-value="recordState"
/>
</HuiSpace>
<HuiSpace class="color-empty" wrap>
<span>Explicit undefined</span>
<HuiColorPicker
:model-value="undefined"
:predefine="statePresets"
:eye-dropper="false"
clearable
/>
</HuiSpace>
<HuiSpace class="color-fixed" wrap>
<span>Fixed red; the parent ignores updates</span>
<HuiColorPicker
model-value="#ff0000"
:predefine="statePresets"
:eye-dropper="false"
clearable
/>
</HuiSpace>
<HuiSpace class="color-bound" wrap>
<span>Two-way binding</span>
<HuiColorPicker
v-model="stateBound"
:predefine="statePresets"
:eye-dropper="false"
clearable
/>
</HuiSpace>
<span class="color-status"
>Update request: {{ stateLast }}; bound color: {{ stateBound ?? 'null' }}</span
>
</HuiSpace>
</template>
Output Formats And Opacity
Choose hex, rgb, or hsl for emitted values. show-alpha enables opacity and includes it in the output when needed.
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
import type { ColorPickerFormat } from '@nscode/handy-ui'
const formats = ['hex', 'rgb', 'hsl'] as const
const solid = reactive<Record<ColorPickerFormat, string | null>>({
hex: '#409eff',
rgb: 'rgb(64, 158, 255)',
hsl: 'hsl(210, 100%, 63%)'
})
const alpha = reactive<Record<ColorPickerFormat, string | null>>({
hex: '#409eff80',
rgb: 'rgba(64, 158, 255, 0.5)',
hsl: 'hsla(210, 100%, 63%, 0.5)'
})
</script>
<template>
<HuiSpace direction="vertical">
<HuiSpace v-for="format in formats" :key="format" wrap>
<span>{{ format }}</span>
<HuiColorPicker v-model="solid[format]" :color-format="format" />
<span>{{ solid[format] }}</span>
<HuiColorPicker v-model="alpha[format]" :color-format="format" show-alpha />
<span>{{ alpha[format] }}</span>
</HuiSpace>
</HuiSpace>
</template>
Sizes
All three sizes align with buttons. Only large displays an opacity percentage on its swatch; smaller swatches use a checkerboard.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
const color = ref<string | null>('#409eff80')
const sizes = ['large', 'default', 'small'] as const
</script>
<template>
<HuiSpace direction="vertical">
<HuiSpace v-for="size in sizes" :key="size">
<HuiColorPicker v-model="color" :size="size" show-alpha />
<HuiButton :size="size">{{ size }}</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
Preset Colors
Provide a palette with predefine. An empty array hides the preset area.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
const custom = ref<string | null>('#126b52')
const withoutPresets = ref<string | null>('#409eff')
const palette = ['#126b52', '#409eff', '#e6a23c', '#f56c6c']
</script>
<template>
<HuiSpace>
<HuiColorPicker v-model="custom" :predefine="palette" />
<span>Custom palette</span>
<HuiColorPicker v-model="withoutPresets" :predefine="[]" />
<span>No presets</span>
</HuiSpace>
</template>
Disabled State
Disabled pickers cannot open or change a color. An open panel closes when the picker becomes disabled.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPicker, HuiSwitch, HuiSpace } from '@nscode/handy-ui'
const disabled = ref(false)
const color = ref<string | null>('#409eff')
</script>
<template>
<HuiSpace>
<HuiSwitch v-model="disabled" active-text="Disabled" inactive-text="Enabled" />
<HuiColorPicker v-model="color" :disabled="disabled" />
<HuiColorPicker model-value="#f56c6c80" show-alpha disabled />
</HuiSpace>
</template>
Empty Values And Clearing
undefined, null, an empty string, and invalid colors show a crossed swatch. Opening starts internally at #409EFF without emitting or writing back. Confirming that starting color through an input or the eyedropper selects it.
With clearable, the clear button is at the bottom of the panel. It requests null in update→clear→change order and closes the original panel session. A rejected request keeps the accepted color. Programmatic clearing preserves outside focus; callbacks that move focus or reopen the panel take precedence.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
const color = ref<string | null>(null)
const cleared = ref(0)
</script>
<template>
<HuiSpace wrap>
<HuiColorPicker v-model="color" clearable @clear="cleared++" />
<span>Color: {{ color ?? 'No color' }}; clear count: {{ cleared }}</span>
<HuiColorPicker :model-value="null" />
<HuiColorPicker model-value="invalid" />
</HuiSpace>
</template>
Numeric Inputs And Eyedropper
The RGB/HSL button switches numeric inputs while colorFormat still controls the emitted format. The initial input mode is read from colorFormat once, when the component is created (HSL for hsl, RGB otherwise); after that the user switches it in the panel. Changing colorFormat at runtime changes only the output format, not the numeric inputs. The hex input stays visible. When supported by the browser, the eyedropper picks a screen color and preserves opacity. Canceling leaves the color unchanged.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
const enabled = ref<string | null>('#409eff80')
const hidden = ref<string | null>('#f56c6c80')
</script>
<template>
<HuiSpace wrap>
<HuiColorPicker v-model="enabled" show-alpha />
<span>Eyedropper enabled when supported</span>
<HuiColorPicker v-model="hidden" show-alpha :eye-dropper="false" color-format="hsl" />
<span>Eyedropper hidden</span>
</HuiSpace>
</template>
Live And Committed Values
active-change updates during a drag. change and update:modelValue commit a selection. Clearing also emits clear.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPicker, HuiSpace, HuiMessage } from '@nscode/handy-ui'
const color = ref<string | null>('#409eff')
const active = ref('')
const changes = ref(0)
const cleared = ref(0)
const changed = (value: string | null) => {
changes.value++
HuiMessage.info(`Committed: ${value ?? 'No color'}`)
}
</script>
<template>
<HuiSpace direction="vertical">
<HuiColorPicker
v-model="color"
clearable
@active-change="active = $event"
@change="changed"
@clear="cleared++"
/>
<span>Active: {{ active || '-' }}</span>
<span>Committed: {{ color ?? 'No color' }}; changes: {{ changes }}; clears: {{ cleared }}</span>
</HuiSpace>
</template>
Form Validation
Changes validate change rules, and closing validates blur rules. validate-event="false" turns off automatic validation. Size and disabled state can be inherited from the form.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiColorPicker,
HuiForm,
HuiFormItem,
HuiSpace,
HuiSwitch,
HuiMessage
} from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive({ color: '' as string | null })
const autoValidate = ref(true)
const form = ref<InstanceType<typeof HuiForm>>()
const rules: Record<string, FormRule[]> = {
color: [{ required: true, message: 'Choose a color', trigger: ['change', 'blur'] }]
}
const validate = async () => {
try {
const valid = await form.value?.validate()
if (valid) HuiMessage.success('Valid')
else HuiMessage.warning('Choose a color')
} catch {
HuiMessage.warning('Choose a color')
}
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSwitch
v-model="autoValidate"
active-text="Automatic validation"
inactive-text="Manual validation"
/>
<HuiForm ref="form" :model="model" :rules="rules" label-width="100px">
<HuiFormItem label="Color" prop="color">
<HuiColorPicker v-model="model.color" :validate-event="autoValidate" clearable />
</HuiFormItem>
</HuiForm>
<HuiButton @click="validate">Validate</HuiButton>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | null | undefined | - | Passing the prop makes it controlled, including empty values; omission stores choices internally. |
| showAlpha | boolean | false | Enable opacity. |
| colorFormat | 'hex' | 'rgb' | 'hsl' | 'hex' | Emitted color format. |
| predefine | string[] | 20 built-in colors | Preset palette. Use [] to hide it. |
| eyeDropper | boolean | true | Enable the screen eyedropper; hidden when unsupported. |
| size | 'large' | 'default' | 'small' | Inherited, then 'default' | Trigger size; inherits form and global settings. |
| disabled | boolean | false | Disable interactions. |
| validateEvent | boolean | true | Validate form rules on change and closing. |
| clearable | boolean | false | Show a bottom clear button when a color is selected. |
Events
| Event | Arguments | Description |
|---|---|---|
| update:modelValue | (value: string | null) | Select a color or request null when clearing. |
| change | (value: string | null) | Selection or clear notification; clearing follows update→clear→change. |
| focus | (event: FocusEvent) | Focus enters the trigger or popup from outside, once per entry. |
| blur | (event: FocusEvent) | Focus leaves the combined trigger and popup scope, once per exit. |
| activeChange | (value: string) | Live color updates, including continuous drag updates. |
| clear | () | Click the clear button. |
Use @active-change in templates.
Slots
No slots.
Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
| focus | — | void | Focus the trigger without opening; disabled or unmounted instances do nothing. |
| blur | — | void | Blur the active element only when it belongs to this trigger or popup. |
| clear | — | void | Request null with update→clear→change notifications and close the original panel session; empty, disabled, or unmounted instances do nothing. |
Class and style stay on the outer element. Other attributes and native listeners belong to the actual trigger. An explicit aria-labelledby, including an empty string, takes precedence. When only aria-label is supplied, including an empty string, the form's default name is omitted. User description IDs are combined with form tip and error IDs in order, without duplicates. Internal focus moves do not emit extra focus/blur events, and the methods do not add blur validation.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPicker, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { ColorPickerExpose } from '@nscode/handy-ui'
const picker = ref<ColorPickerExpose>()
const value = ref<string | null>('#ff0000')
const events = ref<string[]>([])
const record = (kind: string, next?: string | null) => {
events.value = [...events.value, next === undefined ? kind : `${kind}:${String(next)}`].slice(
-12
)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="picker?.focus()">Focus trigger</HuiButton>
<HuiButton @click="picker?.blur()">Blur control</HuiButton>
<HuiButton @click="picker?.clear()">Clear to null</HuiButton>
</HuiSpace>
<span id="color-api-help"
>Trigger and popup share one focus scope; clearing emits update, clear, change.</span
>
<HuiColorPicker
ref="picker"
v-model="value"
clearable
id="color-api-trigger"
aria-label="Brand color"
aria-describedby="color-api-help"
@focus="record('focus')"
@blur="record('blur')"
@update:model-value="record('update', $event)"
@clear="record('clear')"
@change="record('change', $event)"
/>
<span>{{ value === null ? 'null' : value }}; {{ events.join(' → ') }}</span>
</HuiSpace>
</template>
Types
import type {
ColorPickerProps,
ColorPickerEmits,
ColorPickerExpose,
ColorPickerFormat,
ColorPickerValue
} from '@nscode/handy-ui'
type ColorPickerFormat = 'hex' | 'rgb' | 'hsl'
The legacy ColorFormat and ColorValue aliases are deprecated in favor of ColorPickerFormat and ColorPickerValue, and will be removed in 1.0.
Keyboard
- Enter or Space toggles the focused trigger.
- Opening focuses the color area. Tab and Shift+Tab move through its controls. Leaving the last or first control closes this nonmodal panel and returns to the trigger. Within a dialog, the highest modal layer manages the combined scope.
- Arrow keys adjust the color area, hue, and opacity. Hold Shift for larger steps.
- Escape closes the panel and returns focus to the trigger.
- Preset colors and other panel buttons are keyboard accessible.