import { createApp, defineComponent, h, shallowRef } from "vue"; import { Button, ColorArea, ColorAreaThumb, ColorField, ColorFieldGroup, ColorFieldInput, ColorFieldPrefix, ColorPicker, ColorPickerPopover, ColorPickerTrigger, ColorSlider, ColorSliderOutput, ColorSliderThumb, ColorSliderTrack, ColorSwatch, ColorSwatchPicker, ColorSwatchPickerItem, ColorSwatchPickerSwatch, Label, parseColor, } from "@heroui-vue/components"; import "@heroui-vue/styles/css"; import "./style.css"; const area = () => h( ColorArea, { "aria-label": "取色区域", class: "max-w-full", colorSpace: "hsb", xChannel: "saturation", yChannel: "brightness", }, { default: () => h(ColorAreaThumb) }, ); const hueSlider = (label) => h( ColorSlider, { "aria-label": label || "色相滑块", channel: "hue", colorSpace: "hsb" }, { default: () => [ label ? h(Label, null, () => label) : null, h(ColorSliderOutput, { class: "text-muted" }), h(ColorSliderTrack, null, { default: () => h(ColorSliderThumb) }), ], }, ); const trigger = (text) => (state) => [ h(ColorSwatch, { size: "lg" }), h(Label, null, () => `${text} ${state.color ? state.color.toString("hex") : ""}`), ]; const Basic = defineComponent({ setup() { return () => h("form", { id: "basic-form" }, [ h( ColorPicker, { defaultValue: "#0485F7", name: "color", style: { width: "max-content" } }, { default: () => [ h(ColorPickerTrigger, null, { default: trigger("选择颜色") }), h(ColorPickerPopover, { "aria-label": "选择颜色" }, { default: () => [area(), hueSlider("色相")], }), ], }, ), ]); }, }); const presets = [ "#ef4444", "#f97316", "#eab308", "#22c55e", "#06b6d4", "#3b82f6", "#8b5cf6", "#ec4899", "#f43f5e", ]; const Controlled = defineComponent({ setup() { const color = shallowRef(parseColor("#325578")); return () => h("div", { class: "stack" }, [ h( ColorPicker, { modelValue: color.value, "onUpdate:modelValue": (value) => (color.value = value), style: { width: "max-content" }, }, { default: () => [ h(ColorPickerTrigger, null, { default: trigger("已选颜色") }), h(ColorPickerPopover, { "aria-label": "选择颜色", class: "gap-2" }, { default: () => [ h( ColorSwatchPicker, { class: "justify-center pt-2", size: "xs" }, { default: () => presets.map((preset) => h( ColorSwatchPickerItem, { key: preset, color: preset }, { default: () => h(ColorSwatchPickerSwatch) }, ), ), }, ), area(), hueSlider(""), h( ColorField, { "aria-label": "颜色" }, { default: () => [ h(ColorFieldGroup, { variant: "secondary" }, { default: () => [ h(ColorFieldPrefix, null, { default: () => h(ColorSwatch, { size: "xs" }), }), h(ColorFieldInput), ], }), ], }, ), ], }), ], }, ), h("output", { id: "controlled-value" }, `已选:${color.value.toString("hex")}`), h(Button, { id: "shuffle", onClick: () => { const hue = Math.floor(Math.random() * 360); color.value = parseColor(`hsl(${hue}, 80%, 50%)`); }, }, () => "随机颜色"), ]); }, }); createApp({ render: () => h("main", [h(Basic), h(Controlled)]), }).mount("#app");