feat: migrate public ColorPicker composition
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import {
|
||||
ColorArea,
|
||||
ColorAreaThumb,
|
||||
ColorField,
|
||||
ColorFieldGroup,
|
||||
ColorFieldInput,
|
||||
ColorPicker,
|
||||
ColorPickerPopover,
|
||||
ColorPickerTrigger,
|
||||
ColorSlider,
|
||||
ColorSliderOutput,
|
||||
ColorSliderThumb,
|
||||
ColorSliderTrack,
|
||||
ColorSwatch,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
Select,
|
||||
SelectIndicator,
|
||||
SelectPopover,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
type ColorChannel,
|
||||
type ColorSpace,
|
||||
type SelectModelValue,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
const CHANNEL_LABELS: Record<ColorChannel, string> = {
|
||||
alpha: "透明度",
|
||||
blue: "蓝",
|
||||
brightness: "亮度",
|
||||
green: "绿",
|
||||
hue: "色相",
|
||||
lightness: "明度",
|
||||
red: "红",
|
||||
saturation: "饱和度",
|
||||
};
|
||||
|
||||
const colorSpace = ref<ColorSpace>("hsl");
|
||||
|
||||
const colorChannelsByColorSpace: Record<ColorSpace, ColorChannel[]> = {
|
||||
hsb: ["hue", "saturation", "brightness"],
|
||||
hsl: ["hue", "saturation", "lightness"],
|
||||
rgb: ["red", "green", "blue"],
|
||||
};
|
||||
|
||||
function handleColorSpace(value: SelectModelValue) {
|
||||
if (typeof value !== "string") return;
|
||||
colorSpace.value = value as ColorSpace;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ColorPicker default-value="hsla(220, 90%, 50%, 0.8)">
|
||||
<ColorPickerTrigger>
|
||||
<ColorSwatch size="lg" />
|
||||
<Label>选择颜色</Label>
|
||||
</ColorPickerTrigger>
|
||||
<ColorPickerPopover class="max-w-62 gap-2">
|
||||
<ColorArea
|
||||
class="max-w-full"
|
||||
color-space="hsb"
|
||||
x-channel="saturation"
|
||||
y-channel="brightness"
|
||||
>
|
||||
<ColorAreaThumb />
|
||||
</ColorArea>
|
||||
<ColorSlider channel="hue" class="gap-1 px-1" color-space="hsb">
|
||||
<Label>色相</Label>
|
||||
<ColorSliderOutput class="text-muted" />
|
||||
<ColorSliderTrack>
|
||||
<ColorSliderThumb />
|
||||
</ColorSliderTrack>
|
||||
</ColorSlider>
|
||||
<Select
|
||||
aria-label="色彩空间"
|
||||
:model-value="colorSpace"
|
||||
variant="secondary"
|
||||
@update:model-value="handleColorSpace"
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue class="uppercase" />
|
||||
<SelectIndicator />
|
||||
</SelectTrigger>
|
||||
<SelectPopover>
|
||||
<ListBox>
|
||||
<ListBoxItem
|
||||
v-for="space in Object.keys(colorChannelsByColorSpace)"
|
||||
:key="space"
|
||||
:id="space"
|
||||
:text-value="space"
|
||||
class="uppercase"
|
||||
>
|
||||
{{ space }}
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
</ListBox>
|
||||
</SelectPopover>
|
||||
</Select>
|
||||
<div class="grid w-full grid-cols-3 items-center gap-2">
|
||||
<ColorField
|
||||
v-for="channel in colorChannelsByColorSpace[colorSpace]"
|
||||
:key="channel"
|
||||
:aria-label="CHANNEL_LABELS[channel]"
|
||||
:channel="channel"
|
||||
:color-space="colorSpace"
|
||||
>
|
||||
<ColorFieldGroup variant="secondary">
|
||||
<ColorFieldInput />
|
||||
</ColorFieldGroup>
|
||||
</ColorField>
|
||||
</div>
|
||||
</ColorPickerPopover>
|
||||
</ColorPicker>
|
||||
</template>
|
||||
Reference in New Issue
Block a user