Demos
How to import
<script lang="ts">
import { Button, ColorPicker, colorStringToValue } from '@fibrae/semi-ui-svelte';
</script>
Basic Use
In portal
当前颜色:
Code
<script lang="ts">
import { Button, ColorPicker, type ColorValue } from '@fibrae/semi-ui-svelte';
let hex = $state('');
const onChange = (value: ColorValue) => {
hex = value.hex;
};
</script>
<div>
<ColorPicker alpha {onChange} usePopover />
<br />
<div>自定义 trigger</div>
<ColorPicker alpha {onChange} usePopover>
<Button>Trigger</Button>
</ColorPicker>
<p>当前颜色:{hex}</p>
</div>Normal display
当前颜色:
Code
<script lang="ts">
import { ColorPicker, type ColorValue } from '@fibrae/semi-ui-svelte';
let hex = $state('');
const onChange = (value: ColorValue) => {
hex = value.hex;
};
</script>
<ColorPicker alpha {onChange} />
<p>当前颜色:{hex}</p>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 lang="ts">
import { ColorPicker, type ColorValue } from '@fibrae/semi-ui-svelte';
let hex = $state('');
const onChange = (value: ColorValue) => {
hex = value.hex;
};
</script>
<ColorPicker alpha eyeDropper {onChange} />
<p>当前颜色:{hex}</p>Default Value
When converting between various color representation formats, there are theoretical errors between some formats, so the value returned to you by onChange is an object containing color values in three formats: hsva, hex, and rgba.
The value you pass in should also be an object containing the same three formats. Without value the picker manages its own color, and a one-way value={x} only seeds it.
We provide the tool function colorStringToValue, a named export of @fibrae/semi-ui-svelte rather than a member of 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.
当前颜色:#39c5bb
Code
<script lang="ts">
import { ColorPicker, colorStringToValue, type ColorValue } from '@fibrae/semi-ui-svelte';
// a one-way value only seeds the picker, which then manages its own color
const value = colorStringToValue('rgb(57,197,187)');
let hex = $state(value.hex);
const onChange = (next: ColorValue) => {
hex = next.hex;
};
</script>
<div>
<ColorPicker alpha {onChange} {value} />
<p>当前颜色:{hex}</p>
</div>Controlled
Bind value with bind:value for controlled use; for full control use the function binding bind:value={get, set}. Treat the color value as immutable and replace it as a whole when updating (declare it with $state.raw); mutating a field such as hsva.h in place does not sync the text inputs.
Code
<script lang="ts">
import { ColorPicker, colorStringToValue } from '@fibrae/semi-ui-svelte';
let value = $state.raw(colorStringToValue('#39c5bb'));
</script>
<div>
<ColorPicker alpha bind:value />
</div>Rendering additional elements at the top and bottom
Use the topSlot and bottomSlot snippets to render additional elements at the top and bottom
当前颜色:
Code
<script lang="ts">
import { ColorPicker, type ColorValue } from '@fibrae/semi-ui-svelte';
let hex = $state('');
const onChange = (value: ColorValue) => {
hex = value.hex;
};
</script>
<ColorPicker alpha {onChange}>
{#snippet topSlot()}
<div>TopSlot</div>
{/snippet}
{#snippet bottomSlot()}
<div>Bottom Slot</div>
{/snippet}
</ColorPicker>
<p>当前颜色:{hex}</p>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 |
|---|---|---|---|
| alpha | Whether to enable transparency selection. | boolean | true |
| bottomSlot | Snippet that renders extra elements at the bottom. | Snippet | - |
| children | Snippet used as the trigger when usePopover is on, a swatch of the current color by default; not rendered without usePopover. | Snippet | - |
| defaultFormat | Color format used by default for manual input: rgba, hex or hsva. | ColorFormat | "hex" |
| eyeDropper | Whether to enable the eyedropper color picker. | boolean | true |
| height | Height in pixels; 280 when not set. | number | 280 |
| onChange | Called with a ColorValue, holding hsva, hex and rgba, when the user picks a color. | Type(value: ColorValue) => void | - |
| popoverProps | Props passed to the Popover when usePopover is on, such as trigger, aria-label and aria-labelledby. The popup opens on click with role dialog by default and takes its name from the locale; aria-label or aria-labelledby takes precedence. | PopoverProps | - |
| topSlot | Snippet that renders extra elements at the top. | Snippet | - |
| usePopover | Whether to render inside a Popover; the trigger is children. Defaults to false. | boolean | false |
| value | The color, supports bind:value; an object holding hsva, hex and rgba. Left out, the component manages it and starts at #39c5bb. A one-way value={x} only seeds it; for a fully controlled picker use the function binding bind:value={get, set}. null is a controlled empty value: it shows the default color and only reports changes. Treat ColorValue as immutable and replace it as a whole. | ColorValue | null | - |
| width | Width in pixels; 280 when not set. | number | 280 |
Methods
| Method | Description | Type |
|---|---|---|
| getRoot | Returns the picker root element; inside the popup when usePopover is on, so null while the popup is closed. | Type() => HTMLDivElement | null |
Attribute placement
class (a string, array or object), style (a string) and other native attributes and events 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
The value and the onChange payload are 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 |