feat: migrate public ColorPicker composition
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import {
|
||||
ColorPicker,
|
||||
ColorPickerPopover,
|
||||
ColorPickerTrigger,
|
||||
ColorSlider,
|
||||
ColorSliderOutput,
|
||||
ColorSliderThumb,
|
||||
ColorSliderTrack,
|
||||
ColorSwatch,
|
||||
Label,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
Select,
|
||||
SelectIndicator,
|
||||
SelectPopover,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
type ColorChannel,
|
||||
type ColorSliderProps,
|
||||
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", "alpha"],
|
||||
hsl: ["hue", "saturation", "lightness", "alpha"],
|
||||
rgb: ["red", "green", "blue", "alpha"],
|
||||
};
|
||||
|
||||
function handleColorSpace(value: SelectModelValue) {
|
||||
if (typeof value !== "string") return;
|
||||
colorSpace.value = value as ColorSpace;
|
||||
}
|
||||
|
||||
/** 与固定源示例相同的动态通道限制:判别联合无法在运行时联动两者。 */
|
||||
function sliderProps(channel: ColorChannel) {
|
||||
return {
|
||||
channel,
|
||||
colorSpace: colorSpace.value,
|
||||
} as unknown as ColorSliderProps;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ColorPicker default-value="hsl(219, 58%, 93%)">
|
||||
<ColorPickerTrigger>
|
||||
<ColorSwatch size="lg" />
|
||||
<Label>选择颜色</Label>
|
||||
</ColorPickerTrigger>
|
||||
<ColorPickerPopover class="max-w-62 gap-2 px-2 py-3">
|
||||
<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="flex flex-col gap-2">
|
||||
<ColorSlider
|
||||
v-for="channel in colorChannelsByColorSpace[colorSpace]"
|
||||
:key="channel"
|
||||
v-bind="sliderProps(channel)"
|
||||
:aria-label="CHANNEL_LABELS[channel]"
|
||||
class="gap-1 px-1"
|
||||
>
|
||||
<Label>{{ CHANNEL_LABELS[channel] }}</Label>
|
||||
<ColorSliderOutput class="text-muted" />
|
||||
<ColorSliderTrack>
|
||||
<ColorSliderThumb />
|
||||
</ColorSliderTrack>
|
||||
</ColorSlider>
|
||||
</div>
|
||||
</ColorPickerPopover>
|
||||
</ColorPicker>
|
||||
</template>
|
||||
Reference in New Issue
Block a user