Files
heroui-vue/apps/docs/demos/color-picker-controlled.vue
T
2026-10-27 17:47:52 +08:00

114 lines
2.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<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>