fix: enforce ColorSlider union props and clean part refs

This commit is contained in:
maoyingjie
2026-10-27 10:23:10 +08:00
parent 92eb59e389
commit ea881cbcf8
9 changed files with 221 additions and 62 deletions
@@ -21,70 +21,36 @@ import {
type Color,
type ColorChannel,
type ColorSpace,
type ColorValue,
} from "../color/value";
import { useColorSlider } from "../color/slider";
import { colorSliderKey, type ColorSliderElements } from "./context";
import type {
ColorSliderProps,
ColorSliderRenderProps,
ColorSliderSlots,
ColorSliderStyle,
ColorSliderStyleFn,
} from "./types";
defineOptions({ inheritAttrs: false, name: "ColorSlider" });
const props = withDefaults(
defineProps<{
channel: ColorChannel;
colorSpace?: ColorSpace;
/** Accepted color; the Vue `v-model` value. Omitted inherits the nearest owner. */
modelValue?: ColorValue | null;
/** Lower-priority alias for `modelValue`, matching the shared color model. */
value?: ColorValue | null;
/** Initial color for uncontrolled use. */
defaultValue?: ColorValue | null;
orientation?: "horizontal" | "vertical";
name?: string;
form?: string;
isDisabled?: boolean;
/** Alias for `isDisabled`. */
disabled?: boolean;
/** Library extension: blocks edits without removing the slider from the tab order. */
isReadOnly?: boolean;
locale?: string;
dir?: "ltr" | "rtl";
/** Render a different root element; corresponds to the upstream render prop. */
as?: string;
/** Merge the slider attributes and handlers onto the single default-slot root. */
asChild?: boolean;
/** Vue style binding or upstream style function, merged after base styles. */
style?: ColorSliderStyle;
/** Template-friendly alias for the style function; receives render props. */
styleFn?: ColorSliderStyleFn;
/** Id assigned to the real native range input. */
id?: string;
}>(),
{
colorSpace: undefined,
modelValue: undefined,
value: undefined,
defaultValue: undefined,
orientation: "horizontal",
name: undefined,
form: undefined,
isDisabled: false,
disabled: false,
isReadOnly: false,
locale: undefined,
dir: undefined,
as: "div",
asChild: false,
style: undefined,
styleFn: undefined,
id: undefined,
},
);
const props = withDefaults(defineProps<ColorSliderProps>(), {
colorSpace: undefined,
modelValue: undefined,
value: undefined,
defaultValue: undefined,
orientation: "horizontal",
name: undefined,
form: undefined,
isDisabled: false,
disabled: false,
isReadOnly: false,
locale: undefined,
dir: undefined,
as: "div",
asChild: false,
style: undefined,
styleFn: undefined,
id: undefined,
});
defineSlots<ColorSliderSlots>();
@@ -1,6 +1,7 @@
<script setup lang="ts">
import {
computed,
onBeforeUnmount,
shallowRef,
watch,
type CSSProperties,
@@ -115,20 +116,34 @@ function onBlur() {
defineExpose({ element: elements.thumb });
const { forwardRef, currentElement } = useForwardExpose();
const registeredThumb = shallowRef<HTMLElement>();
const registeredInput = shallowRef<HTMLInputElement>();
watch(
currentElement,
(element) => {
elements.thumb.value = (element as HTMLElement | null) ?? undefined;
const node = (element as HTMLElement | null) ?? undefined;
registeredThumb.value = node;
elements.thumb.value = node;
},
{ immediate: true, flush: "sync" },
);
watch(
input,
(element) => {
registeredInput.value = element;
elements.input.value = element;
},
{ immediate: true, flush: "sync" },
);
onBeforeUnmount(() => {
// Clear only this instance's shared refs; a replacement may already be mounted.
if (elements.thumb.value === registeredThumb.value)
elements.thumb.value = undefined;
if (elements.input.value === registeredInput.value)
elements.input.value = undefined;
registeredThumb.value = undefined;
registeredInput.value = undefined;
});
</script>
<template>
@@ -1,5 +1,12 @@
<script setup lang="ts">
import { computed, watch, type CSSProperties, type StyleValue } from "vue";
import {
computed,
onBeforeUnmount,
shallowRef,
watch,
type CSSProperties,
type StyleValue,
} from "vue";
import { Primitive, useForwardExpose } from "reka-ui";
import { colorSliderBackground } from "../color/slider-gradient";
import { useColorSliderContext } from "./context";
@@ -92,13 +99,21 @@ const computedStyle = computed<StyleValue | undefined>(() =>
defineExpose({ element: elements.track });
const { forwardRef, currentElement } = useForwardExpose();
const registeredTrack = shallowRef<HTMLElement>();
watch(
currentElement,
(element) => {
elements.track.value = (element as HTMLElement | null) ?? undefined;
const node = (element as HTMLElement | null) ?? undefined;
registeredTrack.value = node;
elements.track.value = node;
},
{ immediate: true, flush: "sync" },
);
onBeforeUnmount(() => {
if (elements.track.value === registeredTrack.value)
elements.track.value = undefined;
registeredTrack.value = undefined;
});
</script>
<template>
@@ -0,0 +1,22 @@
import type { DefineSetupFnComponent, SlotsType } from "vue";
import type { Color } from "../color/value";
import type { ColorSliderProps, ColorSliderSlots } from "./types";
type ColorSliderEmits = {
"update:modelValue": [value: Color | null];
change: [value: Color | null];
changeEnd: [value: Color | null];
};
/**
* Type-only public shape for the SFC root. Vue's SFC declaration emit drops a
* discriminated-union props type, so the package root casts the runtime SFC to
* this component type. This keeps the channel/colorSpace constraint on the
* actual public component (templates, h() and `$props`) as well as the exported
* `ColorSliderProps` alias.
*/
export type ColorSliderRootComponent = DefineSetupFnComponent<
ColorSliderProps,
ColorSliderEmits,
SlotsType<ColorSliderSlots>
>;
@@ -0,0 +1,12 @@
import ColorSliderImpl from "./ColorSlider.vue";
import ColorSliderOutputImpl from "./ColorSliderOutput.vue";
import ColorSliderThumbImpl from "./ColorSliderThumb.vue";
import ColorSliderTrackImpl from "./ColorSliderTrack.vue";
import type { ColorSliderRootComponent } from "./component-type";
export const ColorSlider =
ColorSliderImpl as unknown as ColorSliderRootComponent;
export const ColorSliderRoot = ColorSlider;
export const ColorSliderOutput = ColorSliderOutputImpl;
export const ColorSliderTrack = ColorSliderTrackImpl;
export const ColorSliderThumb = ColorSliderThumbImpl;
+7 -5
View File
@@ -1156,11 +1156,13 @@ export type {
ColorAreaThumbProps,
} from "./types";
export { default as ColorSlider } from "./components/color-slider/ColorSlider.vue";
export { default as ColorSliderRoot } from "./components/color-slider/ColorSlider.vue";
export { default as ColorSliderOutput } from "./components/color-slider/ColorSliderOutput.vue";
export { default as ColorSliderTrack } from "./components/color-slider/ColorSliderTrack.vue";
export { default as ColorSliderThumb } from "./components/color-slider/ColorSliderThumb.vue";
export {
ColorSlider,
ColorSliderRoot,
ColorSliderOutput,
ColorSliderTrack,
ColorSliderThumb,
} from "./components/color-slider/public";
export {
colorSliderVariants,
type ColorSliderVariants,
+38
View File
@@ -0,0 +1,38 @@
<script setup lang="ts">
import {
ColorSlider,
ColorSliderThumb,
ColorSliderTrack,
} from "@heroui-vue/components";
</script>
<template>
<ColorSlider channel="red" color-space="rgb" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
<ColorSlider channel="hue" color-space="hsb" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
<ColorSlider channel="lightness" color-space="hsl" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
<ColorSlider channel="alpha" color-space="hsb" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
<!-- @vue-expect-error red is not available in the hsl color space -->
<ColorSlider channel="red" color-space="hsl" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
<!-- @vue-expect-error hue is not available in the rgb color space -->
<ColorSlider channel="hue" color-space="rgb" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
<!-- @vue-expect-error brightness requires the hsb color space -->
<ColorSlider channel="brightness" color-space="hsl" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
<!-- @vue-expect-error lightness requires the hsl color space -->
<ColorSlider channel="lightness" color-space="hsb" default-value="#ff0000">
<ColorSliderTrack><ColorSliderThumb /></ColorSliderTrack>
</ColorSlider>
</template>
+60
View File
@@ -579,6 +579,66 @@ describe("ColorSlider public contract", () => {
expect(component.emitted("changeEnd")).toBeUndefined();
});
it("clears thumb-owned refs when only the thumb unmounts and allows remount", async () => {
const show = shallowRef(true);
const w = slide(
{},
`<ColorSlider ref="slider" channel="hue" default-value="hsl(10,100%,50%)"><ColorSliderTrack><ColorSliderThumb v-if="show" /></ColorSliderTrack></ColorSlider>`,
() => ({ show }),
);
const first = input(w).element;
const focus = vi.spyOn(first, "focus");
const bar = geometry(w);
await pointer(bar, "pointerdown", { clientX: 65 });
expect(bar.hasPointerCapture(1)).toBe(true);
focus.mockClear();
show.value = false;
await flushPromises();
expect(bar.hasPointerCapture(1)).toBe(false);
expect(emitted(w, "changeEnd")).toHaveLength(0);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(focus).not.toHaveBeenCalled();
show.value = true;
await flushPromises();
expect(input(w).element).not.toBe(first);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(document.activeElement).toBe(input(w).element);
const remounted = geometry(w);
await pointer(remounted, "pointerdown", { clientX: 175 });
await pointer(remounted, "pointerup", { clientX: 175 });
expect(values(w, "changeEnd")[0]?.getChannelValue("hue")).toBe(270);
});
it("clears track-owned refs when the track unmounts and remounts", async () => {
const show = shallowRef(true);
const w = slide(
{},
`<ColorSlider ref="slider" channel="hue" default-value="hsl(10,100%,50%)"><ColorSliderTrack v-if="show"><ColorSliderThumb /></ColorSliderTrack></ColorSlider>`,
() => ({ show }),
);
const oldTrack = geometry(w);
const oldInput = input(w).element;
const focus = vi.spyOn(oldInput, "focus");
await pointer(oldTrack, "pointerdown", { clientX: 65 });
expect(oldTrack.hasPointerCapture(1)).toBe(true);
focus.mockClear();
show.value = false;
await flushPromises();
expect(oldTrack.hasPointerCapture(1)).toBe(false);
expect(emitted(w, "changeEnd")).toHaveLength(0);
(w.vm.$refs.slider as { focus: () => void }).focus();
expect(focus).not.toHaveBeenCalled();
show.value = true;
await flushPromises();
const remounted = geometry(w);
expect(remounted).not.toBe(oldTrack);
await pointer(remounted, "pointerdown", { clientX: 175 });
await pointer(remounted, "pointerup", { clientX: 175 });
expect(values(w, "changeEnd")[0]?.getChannelValue("hue")).toBe(270);
});
it("aborts stale drags on external replacement but accepts synchronous echoes", async () => {
const value = shallowRef(parseColor("hsb(240,50%,50%)"));
const w = slide(
+29
View File
@@ -1,3 +1,4 @@
import { h } from "vue";
import {
ColorSlider,
ColorSliderOutput,
@@ -140,6 +141,30 @@ const exposed: ColorSliderExpose = {
focus: () => {},
};
const typedColor: Color = color;
type ColorSliderInstanceProps = InstanceType<typeof ColorSlider>["$props"];
const instanceProps: ColorSliderInstanceProps = {
channel: "hue",
colorSpace: "hsb",
};
// @ts-expect-error the component's own props reject red + hsl
const badInstanceRed: ColorSliderInstanceProps = {
channel: "red",
colorSpace: "hsl",
};
// @ts-expect-error the component's own props reject hue + rgb
const badInstanceHue: ColorSliderInstanceProps = {
channel: "hue",
colorSpace: "rgb",
};
const sliderNode = h(ColorSlider, {
channel: "hue",
colorSpace: "hsb",
defaultValue: "hsb(240, 50%, 50%)",
});
// @ts-expect-error h() rejects red + hsl on the real component
h(ColorSlider, { channel: "red", colorSpace: "hsl" });
// @ts-expect-error h() rejects hue + rgb on the real component
h(ColorSlider, { channel: "hue", colorSpace: "rgb" });
// @ts-expect-error red requires the rgb color space
const badRed: ColorSliderProps = { channel: "red", colorSpace: "hsl" };
@@ -219,6 +244,10 @@ void [
variants,
exposed,
typedColor,
instanceProps,
badInstanceRed,
badInstanceHue,
sliderNode,
badRed,
badHue,
badBrightness,