fix: enforce ColorSlider union props and clean part refs
This commit is contained in:
@@ -21,51 +21,18 @@ import {
|
|||||||
type Color,
|
type Color,
|
||||||
type ColorChannel,
|
type ColorChannel,
|
||||||
type ColorSpace,
|
type ColorSpace,
|
||||||
type ColorValue,
|
|
||||||
} from "../color/value";
|
} from "../color/value";
|
||||||
import { useColorSlider } from "../color/slider";
|
import { useColorSlider } from "../color/slider";
|
||||||
import { colorSliderKey, type ColorSliderElements } from "./context";
|
import { colorSliderKey, type ColorSliderElements } from "./context";
|
||||||
import type {
|
import type {
|
||||||
|
ColorSliderProps,
|
||||||
ColorSliderRenderProps,
|
ColorSliderRenderProps,
|
||||||
ColorSliderSlots,
|
ColorSliderSlots,
|
||||||
ColorSliderStyle,
|
|
||||||
ColorSliderStyleFn,
|
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
defineOptions({ inheritAttrs: false, name: "ColorSlider" });
|
defineOptions({ inheritAttrs: false, name: "ColorSlider" });
|
||||||
|
|
||||||
const props = withDefaults(
|
const props = withDefaults(defineProps<ColorSliderProps>(), {
|
||||||
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,
|
colorSpace: undefined,
|
||||||
modelValue: undefined,
|
modelValue: undefined,
|
||||||
value: undefined,
|
value: undefined,
|
||||||
@@ -83,8 +50,7 @@ const props = withDefaults(
|
|||||||
style: undefined,
|
style: undefined,
|
||||||
styleFn: undefined,
|
styleFn: undefined,
|
||||||
id: undefined,
|
id: undefined,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
defineSlots<ColorSliderSlots>();
|
defineSlots<ColorSliderSlots>();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {
|
import {
|
||||||
computed,
|
computed,
|
||||||
|
onBeforeUnmount,
|
||||||
shallowRef,
|
shallowRef,
|
||||||
watch,
|
watch,
|
||||||
type CSSProperties,
|
type CSSProperties,
|
||||||
@@ -115,20 +116,34 @@ function onBlur() {
|
|||||||
defineExpose({ element: elements.thumb });
|
defineExpose({ element: elements.thumb });
|
||||||
|
|
||||||
const { forwardRef, currentElement } = useForwardExpose();
|
const { forwardRef, currentElement } = useForwardExpose();
|
||||||
|
const registeredThumb = shallowRef<HTMLElement>();
|
||||||
|
const registeredInput = shallowRef<HTMLInputElement>();
|
||||||
watch(
|
watch(
|
||||||
currentElement,
|
currentElement,
|
||||||
(element) => {
|
(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" },
|
{ immediate: true, flush: "sync" },
|
||||||
);
|
);
|
||||||
watch(
|
watch(
|
||||||
input,
|
input,
|
||||||
(element) => {
|
(element) => {
|
||||||
|
registeredInput.value = element;
|
||||||
elements.input.value = element;
|
elements.input.value = element;
|
||||||
},
|
},
|
||||||
{ immediate: true, flush: "sync" },
|
{ 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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
<script setup lang="ts">
|
<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 { Primitive, useForwardExpose } from "reka-ui";
|
||||||
import { colorSliderBackground } from "../color/slider-gradient";
|
import { colorSliderBackground } from "../color/slider-gradient";
|
||||||
import { useColorSliderContext } from "./context";
|
import { useColorSliderContext } from "./context";
|
||||||
@@ -92,13 +99,21 @@ const computedStyle = computed<StyleValue | undefined>(() =>
|
|||||||
defineExpose({ element: elements.track });
|
defineExpose({ element: elements.track });
|
||||||
|
|
||||||
const { forwardRef, currentElement } = useForwardExpose();
|
const { forwardRef, currentElement } = useForwardExpose();
|
||||||
|
const registeredTrack = shallowRef<HTMLElement>();
|
||||||
watch(
|
watch(
|
||||||
currentElement,
|
currentElement,
|
||||||
(element) => {
|
(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" },
|
{ immediate: true, flush: "sync" },
|
||||||
);
|
);
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
if (elements.track.value === registeredTrack.value)
|
||||||
|
elements.track.value = undefined;
|
||||||
|
registeredTrack.value = undefined;
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<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;
|
||||||
@@ -1156,11 +1156,13 @@ export type {
|
|||||||
ColorAreaThumbProps,
|
ColorAreaThumbProps,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
export { default as ColorSlider } from "./components/color-slider/ColorSlider.vue";
|
export {
|
||||||
export { default as ColorSliderRoot } from "./components/color-slider/ColorSlider.vue";
|
ColorSlider,
|
||||||
export { default as ColorSliderOutput } from "./components/color-slider/ColorSliderOutput.vue";
|
ColorSliderRoot,
|
||||||
export { default as ColorSliderTrack } from "./components/color-slider/ColorSliderTrack.vue";
|
ColorSliderOutput,
|
||||||
export { default as ColorSliderThumb } from "./components/color-slider/ColorSliderThumb.vue";
|
ColorSliderTrack,
|
||||||
|
ColorSliderThumb,
|
||||||
|
} from "./components/color-slider/public";
|
||||||
export {
|
export {
|
||||||
colorSliderVariants,
|
colorSliderVariants,
|
||||||
type ColorSliderVariants,
|
type ColorSliderVariants,
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -579,6 +579,66 @@ describe("ColorSlider public contract", () => {
|
|||||||
expect(component.emitted("changeEnd")).toBeUndefined();
|
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 () => {
|
it("aborts stale drags on external replacement but accepts synchronous echoes", async () => {
|
||||||
const value = shallowRef(parseColor("hsb(240,50%,50%)"));
|
const value = shallowRef(parseColor("hsb(240,50%,50%)"));
|
||||||
const w = slide(
|
const w = slide(
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { h } from "vue";
|
||||||
import {
|
import {
|
||||||
ColorSlider,
|
ColorSlider,
|
||||||
ColorSliderOutput,
|
ColorSliderOutput,
|
||||||
@@ -140,6 +141,30 @@ const exposed: ColorSliderExpose = {
|
|||||||
focus: () => {},
|
focus: () => {},
|
||||||
};
|
};
|
||||||
const typedColor: Color = color;
|
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
|
// @ts-expect-error red requires the rgb color space
|
||||||
const badRed: ColorSliderProps = { channel: "red", colorSpace: "hsl" };
|
const badRed: ColorSliderProps = { channel: "red", colorSpace: "hsl" };
|
||||||
@@ -219,6 +244,10 @@ void [
|
|||||||
variants,
|
variants,
|
||||||
exposed,
|
exposed,
|
||||||
typedColor,
|
typedColor,
|
||||||
|
instanceProps,
|
||||||
|
badInstanceRed,
|
||||||
|
badInstanceHue,
|
||||||
|
sliderNode,
|
||||||
badRed,
|
badRed,
|
||||||
badHue,
|
badHue,
|
||||||
badBrightness,
|
badBrightness,
|
||||||
|
|||||||
Reference in New Issue
Block a user