ColorPanel
An inline color panel with saturation and brightness controls, opacity, preset colors, RGB/HSL inputs, and screen color picking. Use ColorPicker when you need a popup.
Import
import { HuiColorPanel } from '@nscode/handy-ui'
import type { ColorPanelInputMode, ColorPanelProps } from '@nscode/handy-ui'
Examples
Basic Usage
Bind a color with v-model. The panel stays visible in the page.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const color = ref('#409eff')
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiColorPanel v-model="color" />
<code>Current color: {{ color }}</code>
</HuiSpace>
</template>
Opacity
show-alpha displays an opacity slider and an A input. Hex output includes two opacity digits when the color is translucent.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const opaque = ref('#409eff')
const translucent = ref('#409eff80')
</script>
<template>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical" align="start">
<span>showAlpha = false</span>
<HuiColorPanel v-model="opaque" />
<code>{{ opaque }}</code>
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span>showAlpha = true</span>
<HuiColorPanel v-model="translucent" show-alpha />
<code>{{ translucent }}</code>
</HuiSpace>
</HuiSpace>
</template>
Output Formats
color-format controls the emitted string format. Compare opaque and translucent hex, rgb, and hsl values below. Switching the panel's RGB/HSL inputs does not change its output format.
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const cases = reactive([
{ label: 'hex', format: 'hex' as const, alpha: false, value: '#409eff' },
{ label: 'hex + alpha', format: 'hex' as const, alpha: true, value: '#409eff80' },
{ label: 'rgb', format: 'rgb' as const, alpha: false, value: 'rgb(64, 158, 255)' },
{ label: 'rgb + alpha', format: 'rgb' as const, alpha: true, value: 'rgba(64, 158, 255, 0.5)' },
{ label: 'hsl', format: 'hsl' as const, alpha: false, value: 'hsl(210, 100%, 63%)' },
{
label: 'hsl + alpha',
format: 'hsl' as const,
alpha: true,
value: 'hsla(210, 100%, 63%, 0.5)'
}
])
</script>
<template>
<HuiSpace wrap align="start" :size="24">
<HuiSpace v-for="item in cases" :key="item.label" direction="vertical" align="start">
<span>{{ item.label }}</span>
<HuiColorPanel v-model="item.value" :color-format="item.format" :show-alpha="item.alpha" />
<code style="max-width: 280px; overflow-wrap: anywhere">{{ item.value }}</code>
</HuiSpace>
</HuiSpace>
</template>
Input Modes
input-mode selects RGB or HSL numeric inputs. The hex input remains visible. Input mode and color-format can be configured independently.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const rgbInputs = ref('hsl(120, 100%, 50%)')
const hslInputs = ref('rgb(0, 255, 0)')
</script>
<template>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical" align="start">
<span>RGB inputs, HSL output</span>
<HuiColorPanel v-model="rgbInputs" input-mode="rgb" color-format="hsl" />
<code>{{ rgbInputs }}</code>
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span>HSL inputs, RGB output</span>
<HuiColorPanel v-model="hslInputs" input-mode="hsl" color-format="rgb" />
<code>{{ hslInputs }}</code>
</HuiSpace>
</HuiSpace>
</template>
Persisting Input Mode
Store the mode in the parent with v-model:input-mode to restore it when the panel mounts again. Without inputMode, only the initial colorFormat sets the starting mode; later output format changes keep the selected mode. A supplied mode is controlled, including explicit undefined, which falls back to the current output format.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
import type { ColorPanelInputMode } from '@nscode/handy-ui'
const color = ref('#67c23a')
const mode = ref<ColorPanelInputMode>('rgb')
const visible = ref(true)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center">
<HuiSwitch v-model="visible" aria-label="Show color panel" />
<span>Show panel; current mode: {{ mode }}</span>
</HuiSpace>
<HuiColorPanel v-if="visible" v-model="color" v-model:input-mode="mode" />
<code>{{ color }}</code>
</HuiSpace>
</template>
Preset Colors
Supply your own palette, or pass [] to hide the preset area. Presets are native buttons: focus one with Tab and select it with Enter or Space.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const brand = ref('#126b52')
const withoutPredefine = ref('#67c23a')
const palette = ['#126b52', '#409eff', '#e6a23c', '#f56c6c', '#909399', '#ffffff']
</script>
<template>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical" align="start">
<span>Brand palette</span>
<HuiColorPanel v-model="brand" :predefine="palette" />
<code>{{ brand }}</code>
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span>predefine = []</span>
<HuiColorPanel v-model="withoutPredefine" :predefine="[]" />
<code>{{ withoutPredefine }}</code>
</HuiSpace>
</HuiSpace>
</template>
Screen Color Picking
The eyedropper appears only when the browser supports screen color picking. A picked color keeps the panel's existing opacity. Canceling does not change the color. Set :eye-dropper="false" to disable the feature.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const enabled = ref('#409eff80')
const disabled = ref('#409eff80')
</script>
<template>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical" align="start">
<span>eyeDropper = true</span>
<HuiColorPanel v-model="enabled" show-alpha />
<code>{{ enabled }}</code>
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span>eyeDropper = false</span>
<HuiColorPanel v-model="disabled" show-alpha :eye-dropper="false" />
<code>{{ disabled }}</code>
</HuiSpace>
</HuiSpace>
</template>
Border
bordered is enabled by default. Disable it to place the panel inside an existing container while retaining its internal layout.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const bordered = ref('#409eff')
const unbordered = ref('#67c23a')
</script>
<template>
<HuiSpace wrap align="start">
<HuiSpace direction="vertical" align="start">
<span>bordered = true</span>
<HuiColorPanel v-model="bordered" />
<code>{{ bordered }}</code>
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span>bordered = false</span>
<HuiColorPanel v-model="unbordered" :bordered="false" />
<code>{{ unbordered }}</code>
</HuiSpace>
</HuiSpace>
</template>
Disabled
Disabling the panel prevents interaction with the color area, sliders, presets, and inputs. Color events are not emitted.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const color = ref('#409eff80')
const disabled = ref(true)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiCheckbox v-model="disabled">Disable panel</HuiCheckbox>
<HuiColorPanel v-model="color" show-alpha :disabled="disabled" />
<code>{{ color }}</code>
</HuiSpace>
</template>
Custom Width
The default width is 280px. Set style to resize the panel; its color area, sliders, and numeric inputs resize with it.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const narrow = ref('#409eff80')
const wide = ref('#67c23a80')
</script>
<template>
<HuiSpace wrap align="start" :size="24">
<HuiSpace direction="vertical" align="start">
<span>240px</span>
<HuiColorPanel v-model="narrow" show-alpha style="width: 240px" />
<code>{{ narrow }}</code>
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span>360px</span>
<HuiColorPanel v-model="wide" show-alpha style="width: 360px" />
<code>{{ wide }}</code>
</HuiSpace>
</HuiSpace>
</template>
In a Form
The panel follows the form's disabled state. By default, it triggers validation when the bound value changes or focus leaves the entire panel. Moving focus inside the panel does not trigger blur validation. :validate-event="false" disables automatic validation; the form's validate() method still checks the field.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiButton, HuiColorPanel, HuiForm, HuiFormItem, HuiSpace } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const form = ref<InstanceType<typeof HuiForm>>()
const model = reactive({ automatic: '', manual: '' })
const rules: Record<string, FormRule[]> = {
automatic: [{ required: true, message: 'Choose a primary color', trigger: ['blur', 'change'] }],
manual: [{ required: true, message: 'Choose a secondary color', trigger: ['blur', 'change'] }]
}
const result = ref('Not validated')
const validate = async () => {
const checked = await form.value?.validate()
result.value = checked?.valid ? 'Validation passed' : 'Choose both required colors'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiForm ref="form" :model="model" :rules="rules" label-position="top">
<HuiSpace wrap align="start" :size="24">
<HuiFormItem label="Primary: automatic validation" prop="automatic">
<HuiColorPanel v-model="model.automatic" />
</HuiFormItem>
<HuiFormItem label="Secondary: manual validation only" prop="manual">
<HuiColorPanel v-model="model.manual" :validate-event="false" />
</HuiFormItem>
</HuiSpace>
</HuiForm>
<HuiSpace align="center">
<HuiButton type="primary" icon="check" @click="validate">Validate form</HuiButton>
<span>{{ result }}</span>
</HuiSpace>
</HuiSpace>
</template>
Events
Dragging emits active-change continuously. update:modelValue and change are emitted on release. Selecting a preset, using the arrow keys, or completing an input also commits a color. This example displays the active value on the page and shows a message only when a color is committed.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiMessage, HuiSpace } from '@nscode/handy-ui'
import type { ColorPanelInputMode } from '@nscode/handy-ui'
const color = ref('#409eff')
const mode = ref<ColorPanelInputMode>('rgb')
const active = ref('No active change')
const committed = ref('No committed color')
const updates = ref(0)
const handleUpdate = () => {
updates.value++
}
const handleChange = (value: string) => {
committed.value = value
HuiMessage.success(`Selected color: ${value}`)
}
const handleActive = (value: string) => {
active.value = value
}
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiColorPanel
v-model="color"
v-model:input-mode="mode"
@update:model-value="handleUpdate"
@change="handleChange"
@active-change="handleActive"
/>
<span>update:modelValue: {{ updates }}; bound value: {{ color }}</span>
<span>activeChange: {{ active }}</span>
<span>change: {{ committed }}</span>
<span>update:inputMode: {{ mode }}</span>
</HuiSpace>
</template>
Empty and Invalid Values
undefined, null, an empty string, or an unrecognized color starts the panel internally at #409EFF without writing it back. The bound values below remain unchanged until a color is selected.
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
const colors = reactive<Record<string, string | null | undefined>>({
undefined: undefined,
null: null,
'Empty string': '',
Invalid: 'not-a-color'
})
const display = (value: string | null | undefined) =>
value === undefined ? 'undefined' : JSON.stringify(value)
</script>
<template>
<HuiSpace wrap align="start" :size="24">
<HuiSpace v-for="(value, name) in colors" :key="name" direction="vertical" align="start">
<span>{{ name }}</span>
<HuiColorPanel v-model="colors[name]" />
<code>Bound value: {{ display(value) }}</code>
</HuiSpace>
</HuiSpace>
</template>
With ColorPicker
The inline panel, popup picker, and preview share one bound value, as they would on a theme settings page.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiColorPicker, HuiSpace } from '@nscode/handy-ui'
const color = ref('#126b52')
</script>
<template>
<HuiSpace wrap align="start" :size="24">
<HuiColorPanel v-model="color" />
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center">
<span>Theme color</span>
<HuiColorPicker v-model="color" />
<code>{{ color }}</code>
</HuiSpace>
<article class="color-preview" :style="{ borderTopColor: color }">
<h3>Quarterly Project Report</h3>
<p>Development progress and delivery schedule</p>
<div class="color-preview__sample" :style="{ backgroundColor: color }"></div>
<span>Theme color preview</span>
</article>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.color-preview {
width: 280px;
max-width: 100%;
padding: var(--hui-spacing-4);
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-top-width: 4px;
border-radius: var(--hui-border-radius);
color: var(--hui-text-color);
background: var(--hui-bg-color);
box-sizing: border-box;
}
.color-preview h3 {
margin: 0 0 var(--hui-spacing-2);
font-size: var(--hui-font-size-lg);
}
.color-preview p {
margin: 0 0 var(--hui-spacing-4);
color: var(--hui-text-color-secondary);
}
.color-preview__sample {
height: 12px;
margin-bottom: var(--hui-spacing-2);
border-radius: var(--hui-border-radius-sm);
}
</style>
Internal color state and controlled updates
Omitting modelValue saves the color from null, including listener-only usage. Supplied props, even undefined or null, stay controlled. An empty or invalid value displays the default starting color without writing it back. Dragging remains a preview until release; commits then follow the actual parent value. Rejected requests restore accepted channels and HSV precision, including the hue of gray colors. Removing the prop restores internal state and mode changes warn in development. Changing control mode also cancels the old drag or screen-picker session. The inputMode behavior is described in the next example.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request')
const q1Model = ref<string | null>(null)
const q1Colors = ['#ff0000', '#00ff00', '#0000ff']
</script>
<template>
<HuiSpace class="color-panel-q1-demo" direction="vertical" fill>
<HuiButton class="q1-redraw" @click="q1Redraw++">Parent render {{ q1Redraw }}</HuiButton>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 16px;
"
>
<div class="q1-local">
<p>Omitted prop</p>
<HuiColorPanel :predefine="q1Colors" :eye-dropper="false" />
</div>
<div class="q1-listener">
<p>Update listener only</p>
<HuiColorPanel
:predefine="q1Colors"
:eye-dropper="false"
@update:model-value="q1Last = $event"
/>
</div>
<div class="q1-empty">
<p>Explicit undefined keeps the default starting point</p>
<HuiColorPanel :predefine="q1Colors" :eye-dropper="false" :model-value="undefined" />
</div>
<div class="q1-fixed">
<p>Parent rejects updates</p>
<HuiColorPanel :predefine="q1Colors" :eye-dropper="false" model-value="#00ff00" />
</div>
<div class="q1-bound">
<p>Two-way binding</p>
<HuiColorPanel :predefine="q1Colors" :eye-dropper="false" v-model="q1Model" />
</div>
</div>
<p class="q1-notification">Bound value: {{ q1Model }};Latest request: {{ q1Last }}</p>
</HuiSpace>
</template>
Internal input mode and controlled updates
Omitted inputMode, including listener-only usage, saves the selected mode internally. Only the initial colorFormat determines its starting mode: hsl starts with HSL, other formats with RGB. Later output format changes keep that internal mode. A supplied inputMode is controlled; explicit undefined falls back to the current colorFormat. Rejected requests preserve accepted channels and the next-mode button label. Removing the prop restores the saved internal mode; control mode changes warn in development. Changing input mode emits only update:inputMode and does not commit a color.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiColorPanel, HuiSpace } from '@nscode/handy-ui'
import type { ColorPanelInputMode } from '@nscode/handy-ui'
const modeRedraw = ref(0)
const modeLatest = ref('No request')
const modeBound = ref<ColorPanelInputMode>('rgb')
const modeFormat = ref<'hex' | 'rgb'>('hex')
</script>
<template>
<HuiSpace class="color-panel-input-mode-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="mode-redraw" @click="modeRedraw++"
>Parent render {{ modeRedraw }}</HuiButton
>
<HuiButton class="mode-format" @click="modeFormat = modeFormat === 'hex' ? 'rgb' : 'hex'">
Output format {{ modeFormat }}
</HuiButton>
</HuiSpace>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 16px;
"
>
<div class="mode-local">
<p>Omitted input mode</p>
<HuiColorPanel :color-format="modeFormat" :predefine="[]" :eye-dropper="false" />
</div>
<div class="mode-listener">
<p>Input mode listener only</p>
<HuiColorPanel
:color-format="modeFormat"
:predefine="[]"
:eye-dropper="false"
@update:input-mode="modeLatest = $event"
/>
</div>
<div class="mode-empty">
<p>Explicit undefined stays controlled with RGB fallback</p>
<HuiColorPanel
:input-mode="undefined"
:color-format="modeFormat"
:predefine="[]"
:eye-dropper="false"
/>
</div>
<div class="mode-fixed">
<p>Fixed HSL rejects mode updates</p>
<HuiColorPanel
input-mode="hsl"
:color-format="modeFormat"
:predefine="[]"
:eye-dropper="false"
/>
</div>
<div class="mode-bound">
<p>Two-way input mode binding</p>
<HuiColorPanel
v-model:input-mode="modeBound"
:color-format="modeFormat"
:predefine="[]"
:eye-dropper="false"
/>
</div>
</div>
<p class="mode-status">Bound mode: {{ modeBound }};Latest request: {{ modeLatest }}</p>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | null | undefined | undefined | Supplied props are controlled, including undefined/null; omitted props save the color. Empty or invalid values start the panel internally at #409EFF without writing it back |
| disabled | boolean | false | Disables the panel; also follows its parent form |
| showAlpha | boolean | false | Enables the opacity slider, A input, and output with opacity |
| colorFormat | 'hex' | 'rgb' | 'hsl' | 'hex' | Format of emitted color strings |
| predefine | string[] | 20 built-in colors | Preset colors. Pass [] to hide this area |
| eyeDropper | boolean | true | Enables screen color picking. The button is hidden when unsupported |
| inputMode | 'rgb' | 'hsl' | Initial colorFormat | Supplied props are controlled, including undefined with the current format fallback; omitted props save the mode from the initial colorFormat |
| bordered | boolean | true | Displays the outer border, rounded corners, and background |
| validateEvent | boolean | true | Triggers form validation when the bound value changes or focus leaves the panel |
Events
| Event | Callback | Description |
|---|---|---|
| update:modelValue | (value: string) | Emitted when a color is selected; dragging commits on release |
| change | (value: string) | Emitted together with update:modelValue |
| activeChange | (value: string) | Emitted continuously while dragging, and when a color is committed |
| update:inputMode | (mode: ColorPanelInputMode) | Emitted when the RGB/HSL mode button is clicked |
A color commit emits update:modelValue, change, and activeChange in that order. Use @update:model-value, @change, @active-change, and @update:input-mode in templates.
Slots
None.
Methods
Use a ref<ColorPanelExpose>(). See the focus example below.
| Method | Signature | Description |
|---|---|---|
| focus | () => void | Focus the color area without changing the color. Disabled, form-readonly, or unmounted controls do nothing. |
| blur | () => void | Blur only the active element inside this panel, using its owner document. External focus is preserved. |
Types
import type {
ColorPanelEmits,
ColorPanelInputMode,
ColorPanelProps,
ColorPickerFormat
} from '@nscode/handy-ui'
ColorPanelInputMode is 'rgb' | 'hsl'. ColorPanelProps.colorFormat uses ColorPickerFormat. ColorPanelProps and ColorPanelEmits are the public prop and event types.
Focus methods and exposed type
Use ColorPanelExpose for the component ref. focus() targets the color area without changing the color. Disabled, form-readonly, or unmounted controls ignore focus requests. blur() only affects the active element inside this panel, using its owner document, and preserves external focus. Group focus/blur events fire once on entering/leaving the whole component; internal movement does not repeat them. Existing validation timing is preserved.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiColorPanel, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { ColorPanelExpose } from '@nscode/handy-ui'
const control = ref<ColorPanelExpose>()
const value = ref('#409EFF')
const event = ref('Waiting')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
</HuiSpace>
<HuiColorPanel ref="control" v-model="value" @focus="event = 'focus'" @blur="event = 'blur'" />
<span>{{ event }}</span>
</HuiSpace>
</template>
| Method | Signature |
|---|---|
| focus | () => void |
| blur | () => void |
Whole-component focus events
| Event | Argument | Behavior |
|---|---|---|
| focus | event: FocusEvent | Once when focus enters the component |
| blur | event: FocusEvent | Once when focus leaves; internal moves do not repeat it |