Demos
How to import
<script setup lang="ts">
import { ColorPicker } from '@fibrous/semi-ui-vue';
</script>
Basic Use
In portal
Code
<script setup lang="ts">
import { Button, ColorPicker, type ColorValue } from '@fibrous/semi-ui-vue';
function onChange(value: ColorValue) {
console.log(value);
}
</script>
<template>
<div>
<ColorPicker alpha usePopover @change="onChange" />
<br />
<div>自定义 trigger</div>
<ColorPicker alpha usePopover @change="onChange">
<Button> Trigger </Button>
</ColorPicker>
</div>
</template>Normal display
Code
<script setup lang="ts">
import { ColorPicker, type ColorValue } from '@fibrous/semi-ui-vue';
function onChange(value: ColorValue) {
console.log(value);
}
</script>
<template>
<ColorPicker alpha @change="onChange" />
</template>Eyedropper Color Picker
The eyedropper is enabled by default and supports picking colors from the browser or external software screen. Pass :eyeDropper="false" to turn it off.
Code
<script setup lang="ts">
import { ColorPicker, type ColorValue } from '@fibrous/semi-ui-vue';
function onChange(value: ColorValue) {
console.log(value);
}
</script>
<template>
<ColorPicker alpha eyeDropper @change="onChange" />
</template>Default Value
When converting between various color representation formats, there are theoretical errors between some formats, so the value delivered by the change and update:modelValue events is an object containing color values in three formats: hsva, hex, and rgba.
The defaultValue (uncontrolled) and modelValue (controlled) you pass in should also be objects containing the same three formats.
We provide a static tool function colorStringToValue on the component to convert common color strings to this object, supporting direct passing of strings such as rgb(57,197,187) #39c5bb (the short form #3cb is also accepted) and hsv(176,71,77). A string that cannot be parsed throws an error.
Code
<script setup lang="ts">
import { ColorPicker, type ColorValue } from '@fibrous/semi-ui-vue';
const defaultValue = ColorPicker.colorStringToValue('rgb(57,197,187)');
function onChange(value: ColorValue) {
console.log(value);
}
</script>
<template>
<div>
<ColorPicker alpha :defaultValue="defaultValue" @change="onChange" />
</div>
</template>Controlled
Bind modelValue with v-model for controlled use. Treat the color value as immutable and replace it as a whole when updating; mutating a field such as hsva.h in place does not sync the text inputs.
Code
<script setup lang="ts">
import { shallowRef } from 'vue';
import { ColorPicker } from '@fibrous/semi-ui-vue';
const value = shallowRef(ColorPicker.colorStringToValue('#39c5bb'));
</script>
<template>
<div>
<ColorPicker v-model="value" alpha />
</div>
</template>Rendering additional elements at the top and bottom
Use the #topSlot and #bottomSlot slots to render additional elements at the top and bottom
Code
<script setup lang="ts">
import { ColorPicker, type ColorValue } from '@fibrous/semi-ui-vue';
function onChange(value: ColorValue) {
console.log(value);
}
</script>
<template>
<ColorPicker alpha @change="onChange">
<template #topSlot>
<div>TopSlot</div>
</template>
<template #bottomSlot>
<div>Bottom Slot</div>
</template>
</ColorPicker>
</template>Keyboard and Focus
- With
usePopover, the default trigger is a named button, andEnterorSpaceopens the popup. The popup is adialogthat opens on click by default and closes withEsc. Its name defaults to the built-in text; anaria-labeloraria-labelledbypassed inpopoverProps(ariaLabel,ariaLabelledbyin Vue) takes precedence. - The handles of the color area, the hue slider and the alpha slider can all be operated with the keyboard. The color area has two sliders, saturation and brightness; the hue slider and the alpha slider have one each.
- Arrow keys step by 1,
PageUpandPageDownstep by 10, andHomeandEndjump to the ends. In the color area, whichever slider has focus, Left and Right change saturation, while Up, Down,PageUpandPageDownchange brightness;HomeandEndact on the focused slider. Keys pressed withAlt,CtrlorMetaare not handled, so the browser and assistive technology keep them. - A focused handle shows the theme focus ring; the color input, the alpha input and the format selector all have accessible names.
API Table
ColorPicker
Props
| Property | Description | Type | Default |
|---|---|---|---|
| modelValue | Controlled value, bound with v-model; same shape as defaultValue. undefined means uncontrolled, and going back to undefined from a controlled value shows defaultValue again. Treat ColorValue as immutable and replace it as a whole. | ColorValue | undefined |
| defaultValue | Initial value when uncontrolled, an object holding hsva, hex and rgba; defaults to the color object of #39c5bb. | ColorValue | semiDefault("ColorPicker", "defaultValue", DEFAULT_COLOR) |
| alpha | Whether to enable transparency selection. | boolean | semiDefault("ColorPicker", "alpha", true) |
| eyeDropper | Whether to enable the eyedropper color picker; requires a secure context and Chromium 95 or later. | boolean | semiDefault("ColorPicker", "eyeDropper", true) |
| defaultFormat | Color format used by default for manual input: rgba, hex or hsva. | ColorFormat | semiDefault("ColorPicker", "defaultFormat", "hex") |
| width | Width in pixels. | number | semiDefault("ColorPicker", "width", 280) |
| height | Height in pixels. | number | semiDefault("ColorPicker", "height", 280) |
| usePopover | Whether to render inside a Popover; the trigger comes from the default slot. | boolean | semiDefault("ColorPicker", "usePopover", false) |
| popoverProps | Props and listeners (such as onVisibleChange) passed to the Popover when usePopover is on; class and style replace className and style. The popup opens on click with role dialog by default and takes its default name from the locale; ariaLabel and ariaLabelledby take precedence. | ColorPickerPopoverProps | semiGlobalDefault("ColorPicker", "popoverProps") |
Events
| Event | Description | Callback parameters |
|---|---|---|
| update:modelValue | Emitted when the user picks a color, with a ColorValue payload, for v-model to write back. | [value: ColorValue] |
| change | Emitted when the user picks a color, with a ColorValue payload; native change events bubbling from the inner inputs do not reach it. | [value: ColorValue] |
Slots
| Slot | Description | Scope parameters |
|---|---|---|
| default | The trigger when usePopover is on, a color swatch button by default; not rendered otherwise. | any |
| topSlot | Renders extra elements at the top. | any |
| bottomSlot | Renders extra elements at the bottom. | any |
Exposes
| Name | Description | Type |
|---|---|---|
| root | The picker root element; inside the popup when usePopover is on, so null while the popup is closed. | HTMLElement | null |
Attribute placement
class, style and other undeclared attributes land on the picker root element; with usePopover they land on the picker element inside the popup, not on the trigger or the dialog. When the caller passes aria-label or aria-labelledby without a role, the picker root gets role="group".
ColorValue
modelValue, defaultValue and the event payloads are all the same kind of object:
| Field | Description | Type |
|---|---|---|
| hsva | HSV color and opacity; h is 0-360, s and v are 0-100, a is 0-1 | { h: number; s: number; v: number; a: number } |
| rgba | RGB color and opacity; r, g and b are 0-255, a is 0-1 | { r: number; g: number; b: number; a: number } |
| hex | Hexadecimal color, #rrggbb, or #rrggbbaa with opacity | string |