feat: migrate public ColorPicker composition
This commit is contained in:
@@ -237,6 +237,56 @@ for (const id of process.argv.slice(2)) {
|
||||
"ColorSwatchPickerItem declarations leak Reka types",
|
||||
);
|
||||
}
|
||||
if (id === "color-picker") {
|
||||
assert(lib.ColorPicker === lib.ColorPickerRoot);
|
||||
assert(lib.ColorPickerTrigger);
|
||||
assert(lib.ColorPickerPopover);
|
||||
assert(
|
||||
lib.colorPickerVariants().base() === "hv-color-picker",
|
||||
"ColorPicker base class must stay in the baseline namespace",
|
||||
);
|
||||
assert(
|
||||
lib.colorPickerVariants().trigger() === "hv-color-picker__trigger",
|
||||
"ColorPicker trigger class must stay in the baseline namespace",
|
||||
);
|
||||
assert(
|
||||
lib.colorPickerVariants().popover() === "hv-color-picker__popover",
|
||||
"ColorPicker popover class must stay in the baseline namespace",
|
||||
);
|
||||
const pickerDts = readFileSync(
|
||||
new URL(
|
||||
"./node_modules/@heroui-vue/components/dist/components/color-picker/ColorPicker.vue.d.ts",
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
);
|
||||
const triggerDts = readFileSync(
|
||||
new URL(
|
||||
"./node_modules/@heroui-vue/components/dist/components/color-picker/ColorPickerTrigger.vue.d.ts",
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
);
|
||||
const popoverDts = readFileSync(
|
||||
new URL(
|
||||
"./node_modules/@heroui-vue/components/dist/components/color-picker/ColorPickerPopover.vue.d.ts",
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
);
|
||||
assert(
|
||||
!pickerDts.includes("reka-ui"),
|
||||
"ColorPicker declarations leak Reka types",
|
||||
);
|
||||
assert(
|
||||
!triggerDts.includes("reka-ui"),
|
||||
"ColorPickerTrigger declarations leak Reka types",
|
||||
);
|
||||
assert(
|
||||
!popoverDts.includes("reka-ui"),
|
||||
"ColorPickerPopover declarations leak Reka types",
|
||||
);
|
||||
}
|
||||
if (id === "color-slider") {
|
||||
assert(lib.ColorSlider === lib.ColorSliderRoot);
|
||||
assert(lib.ColorSliderOutput);
|
||||
|
||||
@@ -128,7 +128,7 @@
|
||||
]
|
||||
},
|
||||
"color-field-protocol": {
|
||||
"exports": [],
|
||||
"exports": ["ColorPicker"],
|
||||
"css": [
|
||||
".hv-color-field",
|
||||
".hv-color-input-group",
|
||||
@@ -136,25 +136,28 @@
|
||||
".hv-color-input-group--secondary",
|
||||
".hv-color-input-group__prefix",
|
||||
".hv-color-input-group__suffix"
|
||||
],
|
||||
"absent": ["ColorPicker"]
|
||||
]
|
||||
},
|
||||
"color-foundation": {
|
||||
"exports": [],
|
||||
"css": [".hv-color-area", ".hv-color-area__thumb"],
|
||||
"absent": ["ColorPicker"]
|
||||
"exports": ["ColorPicker"],
|
||||
"css": [".hv-color-area", ".hv-color-area__thumb"]
|
||||
},
|
||||
"color-picker-protocol": {
|
||||
"exports": [],
|
||||
"exports": [
|
||||
"ColorPicker",
|
||||
"ColorPickerRoot",
|
||||
"ColorPickerTrigger",
|
||||
"ColorPickerPopover",
|
||||
"colorPickerVariants"
|
||||
],
|
||||
"css": [
|
||||
".hv-color-picker",
|
||||
".hv-color-picker__trigger",
|
||||
".hv-color-picker__popover"
|
||||
],
|
||||
"absent": ["ColorPicker"]
|
||||
]
|
||||
},
|
||||
"color-slider-foundation": {
|
||||
"exports": [],
|
||||
"exports": ["ColorPicker"],
|
||||
"css": [
|
||||
".hv-color-slider",
|
||||
".hv-color-slider__track",
|
||||
@@ -164,8 +167,7 @@
|
||||
"--reka-popover-content-available-height",
|
||||
"max-height:inherit",
|
||||
"overscroll-behavior:contain"
|
||||
],
|
||||
"absent": ["ColorPicker"]
|
||||
]
|
||||
},
|
||||
"date-field": {
|
||||
"exports": [
|
||||
@@ -498,7 +500,8 @@
|
||||
"ColorInputGroupPrefix",
|
||||
"ColorInputGroupSuffix",
|
||||
"colorFieldVariants",
|
||||
"colorInputGroupVariants"
|
||||
"colorInputGroupVariants",
|
||||
"ColorPicker"
|
||||
],
|
||||
"css": [
|
||||
".hv-color-field",
|
||||
@@ -508,7 +511,26 @@
|
||||
".hv-color-input-group__input",
|
||||
".hv-color-input-group__prefix",
|
||||
".hv-color-input-group__suffix"
|
||||
]
|
||||
},
|
||||
"color-picker": {
|
||||
"exports": [
|
||||
"ColorPicker",
|
||||
"ColorPickerRoot",
|
||||
"ColorPickerTrigger",
|
||||
"ColorPickerPopover",
|
||||
"colorPickerVariants"
|
||||
],
|
||||
"absent": ["ColorPicker"]
|
||||
"css": [
|
||||
".hv-color-picker",
|
||||
".hv-color-picker__trigger",
|
||||
".hv-color-picker__popover",
|
||||
"min-width:calc(var(--spacing) * 62)",
|
||||
"--reka-popper-transform-origin",
|
||||
"--reka-popover-content-available-height",
|
||||
"overscroll-behavior:contain",
|
||||
"background-color:var(--overlay)",
|
||||
"data-exiting"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# ColorPicker 独立产物验收
|
||||
|
||||
先运行 `node scripts/check-package.mjs color-picker color-picker-protocol color-foundation color-area color-slider color-field color-field-protocol color-slider-foundation color-swatch color-swatch-picker` 统一构建、打包、安装和类型验收;它会把本夹具放入独立消费者并打印路径和 Vite 命令。按输出显式选空闲端口启动,不另外复制安装流程;入口不会自动启动服务。main.js 只从包根/发布 CSS 导入,不用源码 alias、私有路径或 Vue 模板编译器。
|
||||
|
||||
在真实浏览器核对:
|
||||
|
||||
1. basic:点 Trigger(色块 + 选择颜色)在锚点下方打开弹层,二维取色区域与色相滑块可见可点;键盘/拖动改色后 Trigger 色块与可访问名称同步,Esc 关闭并把焦点还给 Trigger。
|
||||
2. controlled:已选输出与 Trigger 色块跟随接受颜色;弹层内色板预设改色、Hex 字段 Enter 提交改色、随机按钮换色;点弹层外的随机按钮属于外部点击,会先关闭弹层(组件既有弹层行为)。
|
||||
3. 表单:`name="color"` 存在时 FormData 是完整精度颜色(如 `hsba(210.148…, 98.38…%, 96.86…%, 1)`),`form.reset()` 后 FormData、滑块 DOM 值与 Trigger 名称恢复初值;Trigger 是 `type=button`,不会提交表单。受控拒绝/重试与弹层关闭取消事务由 `tests/color-picker-protocol.test.ts`、`tests/color-picker-composition.test.ts` 覆盖。
|
||||
|
||||
记录真实坐标、点击、焦点与截图;不能因 DOM 已挂载或 CSS 文本存在就通过。构建产物还应执行 `vite build`,类型正反例使用上级 `color-picker-contract.ts` 的两种解析模式。
|
||||
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>ColorPicker 独立产物验收</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,158 @@
|
||||
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");
|
||||
@@ -0,0 +1,16 @@
|
||||
main {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 32px;
|
||||
padding: 32px;
|
||||
}
|
||||
|
||||
form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
output {
|
||||
font-family: monospace;
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
import { h } from "vue";
|
||||
import {
|
||||
ColorPicker,
|
||||
ColorPickerRoot,
|
||||
ColorPickerTrigger,
|
||||
ColorPickerPopover,
|
||||
colorPickerVariants,
|
||||
parseColor,
|
||||
type Color,
|
||||
type ColorPickerExpose,
|
||||
type ColorPickerPopoverProps,
|
||||
type ColorPickerPopoverRenderProps,
|
||||
type ColorPickerPopoverSlots,
|
||||
type ColorPickerProps,
|
||||
type ColorPickerRenderProps,
|
||||
type ColorPickerRootProps,
|
||||
type ColorPickerSlots,
|
||||
type ColorPickerTriggerProps,
|
||||
type ColorPickerTriggerRenderProps,
|
||||
type ColorPickerTriggerSlots,
|
||||
type ColorPickerVariants,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
const props: ColorPickerProps = {
|
||||
modelValue: parseColor("#123456"),
|
||||
value: "#abcdef",
|
||||
defaultValue: "#0485F7",
|
||||
open: false,
|
||||
isOpen: true,
|
||||
defaultOpen: true,
|
||||
isDisabled: false,
|
||||
disabled: false,
|
||||
isReadOnly: true,
|
||||
name: "color",
|
||||
form: "theme",
|
||||
as: "section",
|
||||
};
|
||||
const root: ColorPickerRootProps = props;
|
||||
const trigger: ColorPickerTriggerProps = {
|
||||
as: "button",
|
||||
asChild: false,
|
||||
isDisabled: true,
|
||||
};
|
||||
const popover: ColorPickerPopoverProps = {
|
||||
placement: "bottom left",
|
||||
offset: 8,
|
||||
crossOffset: 2,
|
||||
shouldFlip: true,
|
||||
containerPadding: 12,
|
||||
isNonModal: true,
|
||||
isDismissable: false,
|
||||
isKeyboardDismissDisabled: true,
|
||||
};
|
||||
const slots: ColorPickerSlots = {
|
||||
default: (state: ColorPickerRenderProps) => [
|
||||
h("span", {}, [
|
||||
state.color.toString("hex"),
|
||||
String(state.isOpen),
|
||||
String(state.isDisabled),
|
||||
String(state.isReadOnly),
|
||||
state.close(),
|
||||
]),
|
||||
],
|
||||
};
|
||||
const triggerSlots: ColorPickerTriggerSlots = {
|
||||
default: (state: ColorPickerTriggerRenderProps) => [
|
||||
h("span", {}, [state.color?.toString("hex"), String(state.isDisabled)]),
|
||||
],
|
||||
};
|
||||
const popoverSlots: ColorPickerPopoverSlots = {
|
||||
default: (state: ColorPickerPopoverRenderProps) => [
|
||||
h("span", {}, [String(state.isOpen), state.close()]),
|
||||
],
|
||||
};
|
||||
const state: ColorPickerRenderProps = {
|
||||
color: parseColor("#8B5CF6"),
|
||||
isOpen: true,
|
||||
isDisabled: false,
|
||||
isReadOnly: false,
|
||||
close: () => {},
|
||||
};
|
||||
const triggerState: ColorPickerTriggerRenderProps = {
|
||||
color: parseColor("#8B5CF6"),
|
||||
isDisabled: false,
|
||||
};
|
||||
const popoverState: ColorPickerPopoverRenderProps = {
|
||||
isOpen: false,
|
||||
close: () => {},
|
||||
};
|
||||
const variants: ColorPickerVariants = {};
|
||||
colorPickerVariants(variants).base();
|
||||
colorPickerVariants(variants).trigger();
|
||||
colorPickerVariants(variants).popover();
|
||||
|
||||
h(
|
||||
ColorPicker,
|
||||
{
|
||||
...props,
|
||||
"onUpdate:modelValue": (value: Color) => value.toString("hex"),
|
||||
onChange: (value: Color) => value.toString("hexa"),
|
||||
onChangeEnd: (value: Color) => value.getChannelValue("alpha"),
|
||||
"onUpdate:open": (value: boolean) => !value,
|
||||
"onUpdate:isOpen": (value: boolean) => !value,
|
||||
onOpenChange: (value: boolean) => !value,
|
||||
},
|
||||
{
|
||||
default: (state: ColorPickerRenderProps) => [
|
||||
h("span", {}, state.color.toString("hex")),
|
||||
h("span", {}, String(state.isOpen)),
|
||||
],
|
||||
},
|
||||
);
|
||||
h(ColorPickerRoot, root, () => [h("span", "内容")]);
|
||||
h(ColorPickerTrigger, trigger, {
|
||||
default: (state: ColorPickerTriggerRenderProps) => [
|
||||
h("span", {}, state.color?.toString("hex")),
|
||||
],
|
||||
});
|
||||
h(ColorPickerPopover, popover, {
|
||||
default: (state: ColorPickerPopoverRenderProps) => [
|
||||
h("span", {}, String(state.isOpen)),
|
||||
],
|
||||
});
|
||||
declare const picker: InstanceType<typeof ColorPicker>;
|
||||
declare const rootInstance: InstanceType<typeof ColorPickerRoot>;
|
||||
picker.element?.focus();
|
||||
picker.color?.toString("hex");
|
||||
rootInstance.element?.focus();
|
||||
rootInstance.color?.toString("hex");
|
||||
const exposed: ColorPickerExpose = picker;
|
||||
type ColorPickerInstanceProps = InstanceType<typeof ColorPicker>["$props"];
|
||||
const instanceProps: ColorPickerInstanceProps = { as: "div" };
|
||||
|
||||
// @ts-expect-error invalid model
|
||||
const bad: ColorPickerProps = { modelValue: 123 };
|
||||
// @ts-expect-error picker is non-null even when a child field is cleared
|
||||
const empty: ColorPickerProps = { modelValue: null };
|
||||
// @ts-expect-error open accepts booleans only
|
||||
const badOpen: ColorPickerProps = { open: "yes" };
|
||||
// @ts-expect-error actual public component rejects invalid value
|
||||
h(ColorPicker, { value: 123 });
|
||||
// @ts-expect-error the component's own props reject numeric values
|
||||
const badInstance: ColorPickerInstanceProps = { value: 123 };
|
||||
// @ts-expect-error no public transaction or owner state
|
||||
picker.state.begin();
|
||||
// @ts-expect-error popover placement is an overlay placement
|
||||
const badPlacement: ColorPickerPopoverProps = { placement: "center" };
|
||||
// @ts-expect-error trigger renders a real button type only through `as`
|
||||
const badTrigger: ColorPickerTriggerProps = { asChild: "yes" };
|
||||
|
||||
void [
|
||||
slots,
|
||||
triggerSlots,
|
||||
popoverSlots,
|
||||
state,
|
||||
triggerState,
|
||||
popoverState,
|
||||
exposed,
|
||||
variants,
|
||||
instanceProps,
|
||||
bad,
|
||||
empty,
|
||||
badOpen,
|
||||
badInstance,
|
||||
badPlacement,
|
||||
badTrigger,
|
||||
];
|
||||
Reference in New Issue
Block a user