import { afterEach, describe, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, shallowRef } from "vue"; import { ColorSlider, ColorSliderOutput, ColorSliderRoot, ColorSliderThumb, ColorSliderTrack, ColorSwatch, ConfigProvider, Description, Label, colorSliderVariants, parseColor, type Color, } from "../packages/vue/src"; import Picker from "../packages/vue/tests/fixtures/ColorPickerFoundation.vue"; const mounted: ReturnType[] = []; function render( component: Parameters[0], options: Parameters[1] = {}, ) { const w = mount(component, { attachTo: document.body, ...options }); mounted.push(w); return w; } function slide( props: Record = {}, template = '', setup: () => Record = () => ({}), ) { return render( defineComponent({ components: { ColorSlider, ColorSliderOutput, ColorSliderTrack, ColorSliderThumb, ColorSwatch, ConfigProvider, Description, Label, Picker, }, setup: () => ({ props, ...setup() }), template, }), ); } afterEach(() => { mounted.splice(0).forEach((w) => w.unmount()); document.body.innerHTML = ""; vi.restoreAllMocks(); }); const root = (w: ReturnType) => w.get("[data-slot=color-slider]"); const output = (w: ReturnType) => w.get("[data-slot=color-slider-output]"); const track = (w: ReturnType) => w.get("[data-slot=color-slider-track]"); const thumb = (w: ReturnType) => w.get("[data-slot=color-slider-thumb]"); const input = (w: ReturnType) => w.get("[data-slot=color-slider-thumb] input[type=range]"); const emitted = (w: ReturnType, name: string) => w.findComponent(ColorSlider).emitted(name) ?? []; const values = (w: ReturnType, name: string) => emitted(w, name).map(([value]) => value as Color); async function key( w: ReturnType, name: string, up = true, extra = {}, ) { const element = input(w); await element.trigger("keydown", { key: name, ...extra }); if (up) await element.trigger("keyup", { key: name, ...extra }); await flushPromises(); } function geometry( w: ReturnType, vertical = false, scale = 1, border = 0, ) { const node = track(w).element; const width = vertical ? 20 : 220, height = vertical ? 220 : 20; node.style.border = `${border}px solid transparent`; Object.defineProperties(node, { offsetWidth: { get: () => width, configurable: true }, offsetHeight: { get: () => height, configurable: true }, }); vi.spyOn(node, "getBoundingClientRect").mockReturnValue({ left: 10, top: 20, width: width * scale, height: height * scale, right: 10 + width * scale, bottom: 20 + height * scale, x: 10, y: 20, toJSON: () => ({}), }); let captured: number | undefined; node.setPointerCapture = vi.fn((id) => { captured = id; }); node.hasPointerCapture = vi.fn((id) => captured === id); node.releasePointerCapture = vi.fn(() => { captured = undefined; }); return node; } async function pointer(node: Element, type: string, extra = {}) { const event = new Event(type, { bubbles: true, cancelable: true }); Object.assign(event, { pointerId: 1, pointerType: "mouse", button: 0, isPrimary: true, clientX: 65, clientY: 30, ...extra, }); node.dispatchEvent(event); await flushPromises(); return event; } describe("ColorSlider public contract", () => { it("renders the baseline root, output, track, thumb and native range", () => { const w = slide({ defaultValue: "hsl(200, 100%, 50%)", }); expect(ColorSliderRoot).toBe(ColorSlider); const node = root(w); expect(node.classes()).toEqual(expect.arrayContaining(["hv-color-slider"])); expect(node.attributes("data-slot")).toBe("color-slider"); expect(node.attributes("data-orientation")).toBe("horizontal"); expect(node.attributes("dir")).toBe("ltr"); expect(colorSliderVariants().base()).toBe("hv-color-slider"); expect(colorSliderVariants().output()).toBe("hv-color-slider__output"); expect(colorSliderVariants().track()).toBe("hv-color-slider__track"); expect(colorSliderVariants().thumb()).toBe("hv-color-slider__thumb"); expect(output(w).attributes("data-slot")).toBe("color-slider-output"); expect(output(w).text()).toBe("200°"); expect(output(w).attributes("for")).toBe(input(w).element.id); expect(output(w).element.tagName).toBe("OUTPUT"); const label = w.get("[data-slot=label]"); expect(label.element.tagName).toBe("LABEL"); expect(label.attributes("for")).toBe(input(w).element.id); const bar = track(w); expect(bar.attributes("data-slot")).toBe("color-slider-track"); expect(bar.element.style.getPropertyValue("--track-start-color")).toBe( "rgb(255, 0, 0)", ); expect(bar.element.style.getPropertyValue("--track-end-color")).toBe( "rgb(255, 0, 0)", ); expect(bar.element.style.getPropertyValue("--track-background")).toContain( "linear-gradient", ); expect(bar.element.style.getPropertyValue("--track-background")).toContain( "repeating-conic-gradient", ); const handle = thumb(w); expect(handle.attributes("data-slot")).toBe("color-slider-thumb"); expect(handle.classes()).toContain("hv-color-slider__thumb"); expect(handle.element.style.left).toBe(`${(200 / 360) * 100}%`); expect(handle.element.style.top).toBe("50%"); expect(handle.element.style.transform).toBe("translate(-50%, -50%)"); expect(handle.element.style.backgroundColor).toBe( parseColor("hsl(200, 100%, 50%)").toString("css"), ); expect(handle.find("input[type=range]").exists()).toBe(true); const range = input(w); expect(range.attributes("type")).toBe("range"); expect(range.attributes("step")).toBe("any"); expect(range.attributes("min")).toBe("0"); expect(range.attributes("max")).toBe("360"); expect(range.attributes("aria-valuetext")).toBe("200°"); expect(range.attributes("aria-orientation")).toBe("horizontal"); expect(range.element.value).toBe("200"); }); it("supports every channel, alpha scale and library space inference", () => { const saturation = slide({ channel: "saturation", defaultValue: "hsb(240, 50%, 50%)", }); expect(input(saturation).attributes("max")).toBe("100"); expect(input(saturation).element.value).toBe("50"); expect(output(saturation).text()).toBe("50%"); expect((emitted(saturation, "update:modelValue") as unknown[]).length).toBe( 0, ); const lightness = slide({ channel: "lightness", defaultValue: "hsl(200, 100%, 50%)", }); expect(input(lightness).attributes("max")).toBe("100"); expect(input(lightness).element.value).toBe("50"); const brightness = slide({ channel: "brightness", defaultValue: "hsb(200, 100%, 40%)", }); expect(input(brightness).element.value).toBe("40"); const red = slide({ channel: "red", defaultValue: "rgb(10, 20, 30)", }); expect(input(red).attributes("max")).toBe("255"); expect(input(red).element.value).toBe("10"); expect(output(red).text()).toBe("10"); // The private channel bridge infers the required space when one is omitted. const inferred = slide({ channel: "red", defaultValue: "hsl(0, 100%, 50%)", }); expect(input(inferred).attributes("max")).toBe("255"); expect(input(inferred).element.value).toBe("255"); const alpha = slide({ channel: "alpha", defaultValue: "hsla(0, 100%, 50%, 0.5)", }); expect(input(alpha).attributes("min")).toBe("0"); expect(input(alpha).attributes("max")).toBe("1"); expect(input(alpha).element.value).toBe("0.5"); expect(output(alpha).text()).toBe("50%"); expect( track(alpha).element.style.getPropertyValue("--track-background"), ).toContain("repeating-conic-gradient"); }); it("auto-corrects invalid explicit spaces and warns in development", () => { const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); const red = slide({ channel: "red", colorSpace: "hsl", defaultValue: "hsl(0, 100%, 50%)", }); expect(input(red).attributes("max")).toBe("255"); expect(warn).toHaveBeenCalledWith( expect.stringContaining('channel="red" requires colorSpace="rgb"'), ); const hue = slide({ channel: "hue", colorSpace: "rgb", defaultValue: "rgb(10, 20, 30)", }); expect(input(hue).attributes("max")).toBe("360"); expect(warn).toHaveBeenCalledWith( expect.stringContaining("is not available in RGB color space"), ); const lightness = slide({ channel: "lightness", colorSpace: "hsb", defaultValue: "hsb(200, 100%, 40%)", }); expect(input(lightness).attributes("max")).toBe("100"); expect(warn).toHaveBeenCalledWith( expect.stringContaining('channel="lightness" requires colorSpace="hsl"'), ); }); it("requires a value, default value or inherited owner", () => { expect(() => render( defineComponent({ components: { ColorSlider, ColorSliderTrack, ColorSliderThumb }, template: ``, }), ), ).toThrow(/requires a value or defaultValue/); const cleared = slide( {}, ``, ); expect(input(cleared).element.value).toBe("90"); expect(() => render( defineComponent({ components: { ColorSlider, ColorSliderTrack, ColorSliderThumb }, template: ``, }), ), ).toThrow(/requires a value or defaultValue/); }); it("starts keyboard edits, uses page steps, bounds and one end event", async () => { const w = slide({ defaultValue: "hsl(10, 100%, 50%)" }); await key(w, "ArrowRight", false); expect(input(w).element.value).toBe("11"); expect(values(w, "change")).toHaveLength(1); expect(emitted(w, "changeEnd")).toHaveLength(0); await input(w).trigger("keyup", { key: "ArrowRight" }); expect(emitted(w, "changeEnd")).toHaveLength(1); await key(w, "ArrowRight", true, { shiftKey: true }); expect(input(w).element.value).toBe("26"); await key(w, "PageUp"); expect(input(w).element.value).toBe("41"); await key(w, "PageDown"); expect(input(w).element.value).toBe("26"); await key(w, "Home"); expect(input(w).element.value).toBe("0"); await key(w, "End"); expect(input(w).element.value).toBe("360"); expect(values(w, "changeEnd").at(-1)?.getChannelValue("hue")).toBe(360); const rtl = slide({ defaultValue: "hsl(10, 100%, 50%)", dir: "rtl", }); await key(rtl, "ArrowRight"); expect(input(rtl).element.value).toBe("9"); await key(rtl, "ArrowLeft"); expect(input(rtl).element.value).toBe("10"); const vertical = slide({ defaultValue: "hsl(10, 100%, 50%)", dir: "rtl", orientation: "vertical", }); await key(vertical, "ArrowLeft"); expect(input(vertical).element.value).toBe("9"); await key(vertical, "ArrowUp"); expect(input(vertical).element.value).toBe("10"); expect(input(vertical).attributes("aria-orientation")).toBe("vertical"); }); it("cancels with Escape, blur commits and disabled/readonly block edits", async () => { const w = slide({ defaultValue: "hsl(10, 100%, 50%)" }); await key(w, "ArrowRight", false); expect(values(w, "change")).toHaveLength(1); await key(w, "Escape", false); await input(w).trigger("keyup", { key: "ArrowRight" }); await flushPromises(); expect(emitted(w, "changeEnd")).toHaveLength(0); expect(input(w).element.value).toBe("11"); await key(w, "ArrowRight", false); await input(w).trigger("blur"); expect(emitted(w, "changeEnd")).toHaveLength(1); const disabled = slide({ defaultValue: "hsl(10, 100%, 50%)", isDisabled: true, }); expect(root(disabled).attributes("data-disabled")).toBe("true"); expect(thumb(disabled).attributes("data-disabled")).toBe("true"); expect(input(disabled).element.disabled).toBe(true); await key(disabled, "ArrowRight"); expect(emitted(disabled, "change")).toHaveLength(0); const readOnly = slide({ defaultValue: "hsl(10, 100%, 50%)", isReadOnly: true, }); expect(root(readOnly).attributes("data-readonly")).toBe("true"); expect(input(readOnly).attributes("aria-readonly")).toBe("true"); expect(input(readOnly).attributes()).not.toHaveProperty("disabled"); await key(readOnly, "ArrowRight"); expect(emitted(readOnly, "change")).toHaveLength(0); }); it("keeps accepted values on controlled rejection and allows retries", async () => { const value = shallowRef(parseColor("hsb(240,50%,50%)")); const w = slide( {}, ``, () => ({ value }), ); for (let i = 0; i < 2; i++) await key(w, "ArrowRight"); expect(emitted(w, "change")).toHaveLength(2); expect(values(w, "change")[1]?.getChannelValue("hue")).toBe(241); expect(input(w).element.value).toBe("240"); expect(output(w).text()).toBe("240°"); expect(thumb(w).element.style.left).toBe(`${(240 / 360) * 100}%`); expect(values(w, "changeEnd")).toHaveLength(2); expect(values(w, "changeEnd")[1]?.getChannelValue("hue")).toBe(241); value.value = parseColor("hsb(90,50%,50%)"); await flushPromises(); expect(input(w).element.value).toBe("90"); expect(output(w).text()).toBe("90°"); expect(emitted(w, "changeEnd")).toHaveLength(2); }); it("accepts controlled updates and preserves alpha and fractional precision", async () => { const value = shallowRef(parseColor("hsb(240,50.123%,50.567%,.321)")); const w = slide( {}, ``, () => ({ value }), ); expect(input(w).element.value).toBe("50.123"); await key(w, "ArrowRight"); expect(value.value?.getChannelValue("saturation")).toBe(51.123); expect(value.value?.getChannelValue("alpha")).toBe(0.321); expect(input(w).element.value).toBe("51.123"); expect(output(w).text()).toBe("51%"); }); it("preserves gray hue and normalizes RTL/vertical thumb geometry", async () => { const gray = slide({ channel: "hue", defaultValue: "hsb(270,0%,50%,.125)", }); expect(input(gray).element.value).toBe("270"); expect(output(gray).text()).toBe("270°"); await key(gray, "ArrowRight"); expect(values(gray, "change")[0]?.getChannelValue("alpha")).toBe(0.125); const rtl = slide({ defaultValue: "hsb(90,50%,50%)", dir: "rtl", }); expect(thumb(rtl).element.style.left).toBe("75%"); expect( track(rtl).element.style.getPropertyValue("--track-background"), ).toContain("to left"); const vertical = slide({ defaultValue: "hsb(270,50%,50%)", orientation: "vertical", }); expect(root(vertical).attributes("data-orientation")).toBe("vertical"); expect(thumb(vertical).element.style.left).toBe("50%"); expect(thumb(vertical).element.style.top).toBe("25%"); expect( track(vertical).element.style.getPropertyValue("--track-background"), ).toContain("to top"); }); it("maps pointer clicks, borders, ancestor scaling and grab offsets", async () => { const click = slide({ defaultValue: "hsl(0, 100%, 50%)" }); const bar = geometry(click); vi.spyOn(input(click).element, "matches").mockReturnValue(true); await pointer(bar, "pointerdown", { clientX: 65, clientY: 30 }); expect(thumb(click).attributes("data-focus-visible")).toBeUndefined(); expect(values(click, "change")[0]?.getChannelValue("hue")).toBe(90); await pointer(bar, "pointerup", { clientX: 65, clientY: 30 }); expect(emitted(click, "change")).toHaveLength(1); expect(values(click, "changeEnd")).toHaveLength(1); expect(bar.releasePointerCapture).toHaveBeenCalledWith(1); const scaled = slide({ defaultValue: "hsl(0, 100%, 50%)" }); const scaledBar = geometry(scaled, false, 2, 10); await pointer(scaledBar, "pointerdown", { clientX: 330, clientY: 40 }); await pointer(scaledBar, "pointerup", { clientX: 330, clientY: 40 }); expect(values(scaled, "changeEnd")[0]?.getChannelValue("hue")).toBe(270); const grab = slide({ defaultValue: "hsl(240, 100%, 50%)" }); const grabBar = geometry(grab); const start = 10 + 220 * (240 / 360) + 3; await pointer(thumb(grab).element, "pointerdown", { clientX: start, clientY: 30, }); expect(emitted(grab, "change")).toHaveLength(0); await pointer(grabBar, "pointermove", { clientX: start + 22, clientY: 30, }); expect(values(grab, "change").at(-1)?.getChannelValue("hue")).toBe(276); await pointer(grabBar, "pointerup", { clientX: 1000, clientY: 30 }); expect(values(grab, "changeEnd").at(-1)?.getChannelValue("hue")).toBe(360); expect(emitted(grab, "changeEnd")).toHaveLength(1); expect(thumb(grab).attributes("data-dragging")).toBeUndefined(); }); it("maps RTL and vertical pointers independently", async () => { const rtl = slide({ defaultValue: "hsl(0, 100%, 50%)", dir: "rtl" }); const rtlBar = geometry(rtl); await pointer(rtlBar, "pointerdown", { clientX: 65, clientY: 30 }); await pointer(rtlBar, "pointerup", { clientX: 65, clientY: 30 }); expect(values(rtl, "changeEnd")[0]?.getChannelValue("hue")).toBe(270); const vertical = slide({ defaultValue: "hsl(0, 100%, 50%)", orientation: "vertical", dir: "rtl", }); const verticalBar = geometry(vertical, true); await pointer(verticalBar, "pointerdown", { clientX: 20, clientY: 75 }); await pointer(verticalBar, "pointerup", { clientX: 20, clientY: 75 }); expect(values(vertical, "changeEnd")[0]?.getChannelValue("hue")).toBe(270); }); it("cancels pointer gestures without faking end and isolates pointers", async () => { const w = slide({ defaultValue: "hsl(10, 100%, 50%)" }); const bar = geometry(w); await pointer(bar, "pointerdown"); await pointer(bar, "pointermove", { clientX: 175, pointerId: 2 }); await pointer(bar, "pointercancel", { pointerId: 2 }); expect(bar.hasPointerCapture(1)).toBe(true); await pointer(bar, "pointercancel"); expect(bar.hasPointerCapture(1)).toBe(false); await pointer(bar, "pointerup"); expect(emitted(w, "changeEnd")).toHaveLength(0); const lost = slide({ defaultValue: "hsl(10, 100%, 50%)" }); const lostBar = geometry(lost); await pointer(lostBar, "pointerdown"); await pointer(lostBar, "lostpointercapture"); expect(emitted(lost, "changeEnd")).toHaveLength(0); const prevented = slide({ defaultValue: "hsl(10, 100%, 50%)" }); const preventedBar = geometry(prevented); preventedBar.addEventListener( "pointerdown", (event) => event.preventDefault(), { capture: true, once: true }, ); await pointer(preventedBar, "pointerdown"); expect(emitted(prevented, "change")).toHaveLength(0); expect(preventedBar.setPointerCapture).not.toHaveBeenCalled(); }); it("releases capture on disable, orientation changes, replacement and unmount", async () => { const off = shallowRef(false), orientation = shallowRef<"horizontal" | "vertical">("horizontal"); const w = slide( {}, ``, () => ({ off, orientation }), ); const bar = geometry(w); await pointer(bar, "pointerdown"); off.value = true; await flushPromises(); expect(bar.hasPointerCapture(1)).toBe(false); expect(emitted(w, "changeEnd")).toHaveLength(0); off.value = false; await flushPromises(); await pointer(bar, "pointerdown"); orientation.value = "vertical"; await flushPromises(); expect(bar.hasPointerCapture(1)).toBe(false); const trackKey = shallowRef("first"); const replaced = slide( {}, ``, () => ({ trackKey }), ); const oldBar = geometry(replaced); await pointer(oldBar, "pointerdown"); trackKey.value = "second"; await flushPromises(); expect(oldBar.hasPointerCapture(1)).toBe(false); expect(track(replaced).element).not.toBe(oldBar); await pointer(oldBar, "pointerup", { clientX: 175 }); expect(emitted(replaced, "changeEnd")).toHaveLength(0); const unmounted = slide({ defaultValue: "hsl(10,100%,50%)" }); const lastBar = geometry(unmounted); await pointer(lastBar, "pointerdown"); const component = unmounted.findComponent(ColorSlider); unmounted.unmount(); expect(lastBar.hasPointerCapture(1)).toBe(false); 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( {}, ``, () => ({ 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( {}, ``, () => ({ 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( {}, ``, () => ({ value }), ); const bar = geometry(w); await pointer(bar, "pointerdown"); value.value = parseColor("hsb(180,50%,50%)"); await flushPromises(); expect(bar.hasPointerCapture(1)).toBe(false); await pointer(bar, "pointerup"); expect(emitted(w, "changeEnd")).toHaveLength(0); const accepted = slide( {}, ``, () => ({ value: shallowRef(parseColor("hsb(240,50%,50%)")) }), ); const acceptedBar = geometry(accepted); await pointer(acceptedBar, "pointerdown"); await pointer(acceptedBar, "pointermove", { clientX: 120 }); await pointer(acceptedBar, "pointerup", { clientX: 175 }); expect(emitted(accepted, "changeEnd")).toHaveLength(1); expect(values(accepted, "changeEnd")[0]?.getChannelValue("hue")).toBe(270); }); it("submits the accepted channel value and restores input.value on reset", async () => { const w = slide( {}, `
`, ); await key(w, "ArrowRight"); const form = w.get("form").element as HTMLFormElement; expect(new FormData(form).getAll("hue")).toEqual(["201"]); expect(values(w, "change")).toHaveLength(1); form.reset(); await flushPromises(); expect(input(w).element.value).toBe("200"); expect(output(w).text()).toBe("200°"); expect(emitted(w, "change")).toHaveLength(1); expect(emitted(w, "changeEnd")).toHaveLength(1); const external = slide( {}, `
`, ); const outside = external.get("#external-slider").element as HTMLFormElement; expect(new FormData(outside).get("alpha")).toBe("0.5"); await key(external, "ArrowRight"); expect(new FormData(outside).get("alpha")).toBe("0.51"); outside.reset(); await flushPromises(); expect(input(external).element.value).toBe("0.5"); const disabled = slide( {}, `
`, ); expect(new FormData(disabled.get("form").element).has("hue")).toBe(false); }); it("inherits the nearest owner, supports overrides and restores inheritance", async () => { const override = shallowRef>({}); const w = slide( {}, `
`, () => ({ override }), ); const picker = w.findComponent(Picker); const swatch = () => w.get("[data-owner-swatch]").element.style.backgroundColor; expect(input(w).element.value).toBe("240"); expect(swatch()).toBe(parseColor("hsb(240,50%,50%)").toString("css")); await key(w, "ArrowRight"); expect(picker.emitted("change")).toHaveLength(1); expect(input(w).element.value).toBe("241"); expect(swatch()).toBe(parseColor("hsb(241,50%,50%)").toString("css")); const form = w.get("form").element as HTMLFormElement; const data = new FormData(form); expect(data.getAll("color")).toHaveLength(1); expect(data.getAll("hue")).toEqual(["241"]); expect(input(w).element.value).toBe("241"); override.value = { modelValue: "hsb(10,20%,30%)" }; await flushPromises(); expect(input(w).element.value).toBe("10"); await key(w, "ArrowRight"); expect(picker.emitted("change")).toHaveLength(1); expect(input(w).element.value).toBe("10"); override.value = {}; await flushPromises(); expect(input(w).element.value).toBe("241"); await key(w, "ArrowRight"); expect(picker.emitted("change")).toHaveLength(2); expect(new FormData(form).getAll("color")).toHaveLength(1); }); it("keeps sibling transactions independent when one slider is disabled", async () => { const off = shallowRef(false); const w = slide( {}, ` `, () => ({ off }), ); const picker = w.findComponent(Picker); const bars = w.findAll("[data-slot=color-slider-track]"); const first = bars[0]!.element; Object.defineProperties(first, { offsetWidth: { get: () => 220, configurable: true }, offsetHeight: { get: () => 20, configurable: true }, }); vi.spyOn(first, "getBoundingClientRect").mockReturnValue({ left: 0, top: 0, width: 220, height: 20, right: 220, bottom: 20, x: 0, y: 0, toJSON: () => ({}), }); first.setPointerCapture = vi.fn(); first.hasPointerCapture = vi.fn(() => true); first.releasePointerCapture = vi.fn(); await pointer(first, "pointerdown", { clientX: 165, clientY: 10 }); off.value = true; await flushPromises(); await pointer(first, "pointermove", { clientX: 170, clientY: 10 }); expect(picker.emitted("change")?.length).toBeGreaterThan(1); await pointer(first, "pointerup", { clientX: 170, clientY: 10 }); expect(picker.emitted("changeEnd")).toHaveLength(1); }); it("supports as, asChild, class/style/styleFn, slots and exposed state", async () => { const styleFn = vi.fn(({ color }: { color: Color }) => ({ outline: `1px solid ${color.toString("hex")}`, })); const w = slide( { defaultValue: "hsl(200,100%,50%)" }, `
{{ state.label }} / {{ state.value }}
`, () => ({ styleFn }), ); const node = root(w); expect(node.element.tagName).toBe("DIV"); expect(node.attributes("data-custom")).toBe("slider"); expect(node.classes()).toContain("hv-color-slider"); expect(node.element.style.opacity).toBe("0.9"); expect(node.element.style.outline).toBe("1px solid #00AAFF"); expect(styleFn).toHaveBeenCalledWith( expect.objectContaining({ channel: "hue", colorSpace: "hsl", orientation: "horizontal", isDisabled: false, isReadOnly: false, }), ); expect(output(w).text()).toBe("200° / 200"); expect(track(w).attributes("data-custom")).toBe("track"); expect(thumb(w).element.tagName).toBe("SPAN"); expect(thumb(w).attributes("data-custom")).toBe("thumb"); expect(w.text()).toContain("Hue"); const exposed = w.vm.$refs.sliderRef as { element: HTMLElement; color: Color; focus: () => void; }; expect(exposed.element).toBe(node.element); expect(exposed.color.toString("hsl")).toBe("hsl(200, 100%, 50%)"); exposed.focus(); expect(document.activeElement).toBe(input(w).element); await key(w, "ArrowRight"); expect(output(w).text()).toBe("201° / 201"); }); it("uses style and styleFn for output, track and thumb render props", () => { const outputFn = vi.fn(() => ({ color: "red" })); const trackFn = vi.fn(({ percent }: { percent: number }) => ({ opacity: percent, })); const thumbFn = vi.fn(({ isDragging }: { isDragging: boolean }) => ({ width: isDragging ? "20px" : "16px", })); const w = slide( { defaultValue: "hsl(90,100%,50%)" }, ` `, () => ({ outputFn, trackFn, thumbFn }), ); expect(output(w).element.tagName).toBe("SPAN"); expect(output(w).element.style.fontSize).toBe("14px"); expect(output(w).element.style.color).toBe("red"); expect(track(w).element.style.borderColor).toBe("black"); expect(track(w).element.style.opacity).toBe("0.25"); expect(thumb(w).element.style.borderWidth).toBe("3px"); expect(thumb(w).element.style.width).toBe("16px"); expect(outputFn).toHaveBeenCalledWith( expect.objectContaining({ value: 90, label: "90°" }), ); expect(trackFn).toHaveBeenCalledWith( expect.objectContaining({ percent: 0.25 }), ); expect(thumbFn).toHaveBeenCalledWith( expect.objectContaining({ percent: 0.25, isDragging: false, isDisabled: false, }), ); }); it("exposes root, output, track and thumb state through scoped slots", async () => { const w = slide( { defaultValue: "hsl(90,100%,50%)" }, ` {{ root.channel }} / {{ root.colorSpace }} / {{ root.orientation }} / {{ root.color.toString("hsl") }} {{ out.label }} / {{ out.value }} {{ track.percent.toFixed(3) }}{{ thumb.isDragging ? "dragging" : "idle" }} `, ); expect(w.get("[data-root-scope]").text()).toBe( "hue / hsl / horizontal / hsl(90, 100%, 50%)", ); expect(w.get("[data-output-scope]").text()).toBe("90° / 90"); expect(w.get("[data-track-scope]").text()).toBe("0.250"); expect(w.get("[data-thumb-scope]").text()).toBe("idle"); await key(w, "ArrowRight"); expect(w.get("[data-output-scope]").text()).toBe("91° / 91"); expect(w.get("[data-root-scope]").text()).toBe( "hue / hsl / horizontal / hsl(91, 100%, 50%)", ); expect(w.get("[data-track-scope]").text()).toBe("0.253"); }); it("associates Label, aria attributes, description and output with the input", async () => { const labelled = slide( {}, `
说明
`, ); const inputs = labelled.findAll( "[data-slot=color-slider-thumb] input", ); expect(inputs[0]!.attributes("aria-label")).toBe("主题色"); expect(inputs[1]!.attributes("aria-labelledby")).toBe("slider-caption"); const roots = labelled.findAll("[data-slot=color-slider]"); expect(roots[0]!.attributes("aria-label")).toBeUndefined(); expect(roots[1]!.attributes("aria-labelledby")).toBeUndefined(); const described = slide( {}, ` 拖动调整色相 `, ); const description = described.get("[data-slot=description]"); expect(input(described).attributes("aria-describedby")).toBe( description.attributes("id"), ); await input(described).trigger("keydown", { key: "ArrowRight" }); await flushPromises(); expect(described.findAll("[data-slot=description]")).toHaveLength(1); }); it("applies ConfigProvider locale and dir to labels, values and ramps", async () => { const w = slide( {}, ` `, ); expect(root(w).attributes("dir")).toBe("rtl"); expect(output(w).text()).toBe("25%"); expect(input(w).attributes("aria-valuetext")).toBe("25%"); expect(thumb(w).element.style.left).toBe("75%"); expect( track(w).element.style.getPropertyValue("--track-background"), ).toContain("to left"); expect(w.text()).toContain("饱和度"); }); it("mirrors focus-visible to the thumb and ignores canceled keyboard defaults", async () => { const w = slide({ defaultValue: "hsl(10,100%,50%)" }); const range = input(w); const matches = vi.spyOn(range.element, "matches"); matches.mockReturnValue(true); await range.trigger("focus"); expect(thumb(w).attributes("data-focus-visible")).toBe("true"); await range.trigger("blur"); expect(thumb(w).attributes("data-focus-visible")).toBeUndefined(); matches.mockReturnValue(false); await range.trigger("focus"); expect(thumb(w).attributes("data-focus-visible")).toBeUndefined(); matches.mockReturnValue(true); await range.trigger("keydown", { key: "ArrowRight" }); expect(thumb(w).attributes("data-focus-visible")).toBe("true"); await range.trigger("keyup", { key: "ArrowRight" }); const canceled = slide({ defaultValue: "hsl(10,100%,50%)" }); const element = input(canceled).element; element.addEventListener("keydown", (event) => event.preventDefault(), { capture: true, once: true, }); await input(canceled).trigger("keydown", { key: "ArrowRight" }); await input(canceled).trigger("keyup", { key: "ArrowRight" }); expect(emitted(canceled, "change")).toHaveLength(0); expect(emitted(canceled, "changeEnd")).toHaveLength(0); }); it("keeps disabled thumb paint default and reflects dynamic space-changing ramps", async () => { const disabled = slide({ defaultValue: "hsl(200,100%,50%)", isDisabled: true, }); expect(thumb(disabled).element.style.backgroundColor).toBe(""); const props = shallowRef>({ channel: "hue", colorSpace: "hsl", defaultValue: "hsl(200,100%,50%)", }); const w = slide( {}, ``, () => ({ props }), ); await key(w, "ArrowRight", false); const start = track(w).element.style.getPropertyValue( "--track-start-color", ); props.value = { channel: "hue", colorSpace: "hsl", defaultValue: "hsl(200,100%,50%)", orientation: "vertical", }; await flushPromises(); expect(emitted(w, "changeEnd")).toHaveLength(0); expect( track(w).element.style.getPropertyValue("--track-background"), ).toContain("to top"); expect(start).toContain("rgb(255, 0, 0)"); }); it("requires parts to be rendered inside the root", () => { for (const part of [ ColorSliderThumb, ColorSliderTrack, ColorSliderOutput, ]) { expect(() => render( defineComponent({ components: { ColorSliderThumb, ColorSliderTrack, ColorSliderOutput, }, template: `<${part === ColorSliderThumb ? "ColorSliderThumb" : part === ColorSliderTrack ? "ColorSliderTrack" : "ColorSliderOutput"} />`, }), ), ).toThrow(/must be rendered inside ColorSlider/); } }); it("renders label/output without a thumb but exposes no native input", () => { const w = slide( {}, ``, ); expect(output(w).text()).toBe("20°"); expect( w.findAll("[data-slot=color-slider-thumb] input[type=range]"), ).toHaveLength(0); expect(w.findAll("[data-slot=color-slider-track]")).toHaveLength(1); }); it("supports readonly while parent updates still render", async () => { const value = shallowRef(parseColor("hsb(240,0%,50%)")); const w = slide( {}, ``, () => ({ value }), ); const bar = geometry(w); await pointer(bar, "pointerdown"); await pointer(bar, "pointerup"); expect(emitted(w, "change")).toHaveLength(0); value.value = parseColor("hsb(90,0%,50%)"); await flushPromises(); expect(input(w).element.value).toBe("90"); expect(thumb(w).element.style.left).toBe("25%"); }); it("keeps hue ramp opaque for transparent gray and alpha ramp with checkerboard", () => { const gray = slide({ channel: "hue", defaultValue: "hsb(270,0%,50%,.1)", }); const hueRamp = track(gray).element.style.getPropertyValue("--track-background"); expect(hueRamp).toContain("rgb(255, 0, 0)"); expect(hueRamp).toContain("rgb(0, 255, 0)"); expect(hueRamp).not.toContain("rgba("); const alpha = slide({ channel: "alpha", defaultValue: "hsla(0,100%,50%,.25)", }); const alphaRamp = track(alpha).element.style.getPropertyValue("--track-background"); expect( track(alpha).element.style.getPropertyValue("--track-start-color"), ).toBe("rgba(255, 0, 0, 0)"); expect( track(alpha).element.style.getPropertyValue("--track-end-color"), ).toBe("rgb(255, 0, 0)"); expect(alphaRamp).toContain("repeating-conic-gradient"); }); });