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
@@ -1,5 +1,11 @@
<script setup lang="ts">
import { computed, provide, shallowRef, type VNode } from "vue";
import {
computed,
provide,
shallowRef,
type VNodeChild,
} from "vue";
import { colorPickerVariants } from "@heroui-vue/styles";
import { useColorState } from "../color/model";
import { parseColor, type Color } from "../color/value";
import { serializeColorValue } from "../color/submission";
@@ -12,7 +18,7 @@ import {
type ColorPickerRenderState,
} from "./context";
defineOptions({ inheritAttrs: false });
defineSlots<{ default?: (state: ColorPickerRenderState) => VNode[] }>();
defineSlots<{ default?: (state: ColorPickerRenderState) => VNodeChild }>();
const props = withDefaults(defineProps<ColorPickerProps>(), {
as: "div",
open: undefined,
@@ -41,11 +47,14 @@ const state = useColorState(props, {
},
});
provide(colorPickerKey, state);
const root = shallowRef<HTMLElement>();
const native = shallowRef<HTMLInputElement>();
const color = computed(() => state.value.value ?? parseColor("#000000"));
const accepted = computed(() => state.value.value);
const disabled = state.disabled,
readOnly = state.readOnly;
const serialized = computed(() => serializeColorValue(color.value));
const classes = colorPickerVariants();
useChoiceForm(native, state.reset, () => {
if (native.value) native.value.value = serialized.value;
});
@@ -54,7 +63,7 @@ function openChange(value: boolean) {
emit("update:isOpen", value);
emit("openChange", value);
}
defineExpose({ state });
defineExpose({ element: root, color: accepted });
</script>
<template>
<Popover
@@ -65,8 +74,9 @@ defineExpose({ state });
<Boundary v-slot="overlay">
<component
:is="as"
ref="root"
v-bind="$attrs"
class="hv-color-picker"
:class="classes.base()"
data-slot="color-picker"
:data-disabled="disabled || undefined"
:data-readonly="readOnly || undefined"
@@ -1,8 +1,11 @@
<script setup lang="ts">
import type { VNodeChild } from "vue";
import { useForwardExpose } from "reka-ui";
import { colorPickerVariants } from "@heroui-vue/styles";
import PopoverContent from "../popover/PopoverContent.vue";
import type { OverlayPlacement } from "../overlay/placement";
import { useColorPicker } from "./context";
import type { ColorPickerPopoverRenderProps } from "./types";
defineOptions({ inheritAttrs: false });
withDefaults(
defineProps<{
@@ -32,12 +35,15 @@ withDefaults(
);
useColorPicker();
const { forwardRef } = useForwardExpose();
defineSlots<{
default?: (state: ColorPickerPopoverRenderProps) => VNodeChild;
}>();
</script>
<template>
<PopoverContent
:ref="forwardRef"
v-bind="{ ...$props, ...$attrs }"
class="hv-color-picker__popover"
:class="colorPickerVariants().popover()"
data-slot="color-picker-popover"
>
<template #default="state"><slot v-bind="state" /></template>
@@ -1,12 +1,18 @@
<script setup lang="ts">
import type { VNodeChild } from "vue";
import { useForwardExpose } from "reka-ui";
import { colorPickerVariants } from "@heroui-vue/styles";
import PopoverTrigger from "../popover/PopoverTrigger.vue";
import { useColorPicker } from "./context";
import type { ColorPickerTriggerRenderProps } from "./types";
defineOptions({ inheritAttrs: false });
withDefaults(
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
{ as: "button" },
);
defineSlots<{
default?: (state: ColorPickerTriggerRenderProps) => VNodeChild;
}>();
const color = useColorPicker(),
disabled = color.disabled;
const { forwardRef } = useForwardExpose();
@@ -18,7 +24,7 @@ const { forwardRef } = useForwardExpose();
:as="as"
:as-child="asChild"
:is-disabled="isDisabled || disabled"
class="hv-color-picker__trigger"
:class="colorPickerVariants().trigger()"
data-slot="color-picker-trigger"
>
<slot :color="color.value.value" :is-disabled="isDisabled || disabled" />
@@ -0,0 +1,50 @@
import type { VNodeChild } from "vue";
import type { Color } from "../color/value";
import type ColorPicker from "./ColorPicker.vue";
import type ColorPickerTrigger from "./ColorPickerTrigger.vue";
import type ColorPickerPopover from "./ColorPickerPopover.vue";
export type {
ColorPickerProps,
ColorPickerProps as ColorPickerRootProps,
ColorPickerRenderState,
ColorPickerRenderState as ColorPickerRenderProps,
} from "./context";
export type ColorPickerTriggerProps = InstanceType<
typeof ColorPickerTrigger
>["$props"];
export type ColorPickerPopoverProps = InstanceType<
typeof ColorPickerPopover
>["$props"];
/** Trigger content state; the fixed source reuses Button render props here. */
export interface ColorPickerTriggerRenderProps {
/** Accepted color of the shared owner. */
color: Color | null;
isDisabled: boolean;
}
/** Popover content state shared with the overlay layer. */
export interface ColorPickerPopoverRenderProps {
isOpen: boolean;
close: () => void;
}
export interface ColorPickerSlots {
default?: (state: import("./context").ColorPickerRenderState) => VNodeChild;
}
export interface ColorPickerTriggerSlots {
default?: (state: ColorPickerTriggerRenderProps) => VNodeChild;
}
export interface ColorPickerPopoverSlots {
default?: (state: ColorPickerPopoverRenderProps) => VNodeChild;
}
/** Root instance surface; no transaction or owner state is exposed. */
export type ColorPickerExpose = Pick<
InstanceType<typeof ColorPicker>,
"element" | "color"
>;