feat: migrate public ColorPicker composition

This commit is contained in:
maoyingjie
2026-10-27 17:47:52 +08:00
parent 312d223f7c
commit ee5fce9adb
38 changed files with 1783 additions and 48 deletions
+113
View File
@@ -0,0 +1,113 @@
<script setup lang="ts">
import { shallowRef } from "vue";
import { IconArrowsShuffle } from "@tabler/icons-vue";
import {
Button,
ColorArea,
ColorAreaThumb,
ColorField,
ColorFieldGroup,
ColorFieldInput,
ColorFieldPrefix,
ColorPicker,
ColorPickerPopover,
ColorPickerTrigger,
ColorSlider,
ColorSliderThumb,
ColorSliderTrack,
ColorSwatch,
ColorSwatchPicker,
ColorSwatchPickerItem,
ColorSwatchPickerSwatch,
Label,
parseColor,
type Color,
} from "@heroui-vue/components";
const color = shallowRef<Color>(parseColor("#325578"));
const colorPresets = [
"#ef4444",
"#f97316",
"#eab308",
"#22c55e",
"#06b6d4",
"#3b82f6",
"#8b5cf6",
"#ec4899",
"#f43f5e",
];
const shuffleColor = () => {
const randomHue = Math.floor(Math.random() * 360);
const randomSaturation = 50 + Math.floor(Math.random() * 50); // 50-100%
const randomLightness = 40 + Math.floor(Math.random() * 30); // 40-70%
color.value = parseColor(
`hsl(${randomHue}, ${randomSaturation}%, ${randomLightness}%)`,
);
};
</script>
<template>
<div class="flex flex-col gap-4">
<ColorPicker v-model="color">
<ColorPickerTrigger>
<ColorSwatch size="lg" />
<Label>选择颜色</Label>
</ColorPickerTrigger>
<ColorPickerPopover class="gap-2">
<ColorSwatchPicker class="justify-center pt-2" size="xs">
<ColorSwatchPickerItem
v-for="preset in colorPresets"
:key="preset"
:color="preset"
>
<ColorSwatchPickerSwatch />
</ColorSwatchPickerItem>
</ColorSwatchPicker>
<ColorArea
aria-label="取色区域"
class="max-w-full"
color-space="hsb"
x-channel="saturation"
y-channel="brightness"
>
<ColorAreaThumb />
</ColorArea>
<div class="flex items-center gap-2 px-1">
<ColorSlider
aria-label="色相滑块"
channel="hue"
class="flex-1"
color-space="hsb"
>
<ColorSliderTrack>
<ColorSliderThumb />
</ColorSliderTrack>
</ColorSlider>
<Button
is-icon-only
aria-label="随机颜色"
size="sm"
variant="tertiary"
@click="shuffleColor"
>
<IconArrowsShuffle class="size-4" />
</Button>
</div>
<ColorField aria-label="颜色">
<ColorFieldGroup variant="secondary">
<ColorFieldPrefix>
<ColorSwatch size="xs" />
</ColorFieldPrefix>
<ColorFieldInput />
</ColorFieldGroup>
</ColorField>
</ColorPickerPopover>
</ColorPicker>
<p class="w-60 text-sm text-muted">
已选:<span class="font-medium">{{ color.toString("hex") }}</span>
</p>
</div>
</template>