feat: migrate public ColorSlider with docs and tests

This commit is contained in:
maoyingjie
2026-10-27 09:16:32 +08:00
parent e487bcc8ec
commit ce6c43eb47
31 changed files with 3386 additions and 122 deletions
@@ -1,34 +1,9 @@
import type { ColorAxisChannel } from "../color/value";
const english: Record<ColorAxisChannel, string> = {
red: "Red",
green: "Green",
blue: "Blue",
hue: "Hue",
saturation: "Saturation",
lightness: "Lightness",
brightness: "Brightness",
};
const chinese: Record<ColorAxisChannel, string> = {
red: "红色",
green: "绿色",
blue: "蓝色",
hue: "色相",
saturation: "饱和度",
lightness: "亮度",
brightness: "明度",
};
export { colorChannelLabel } from "../color/labels";
function isChinese(locale: string) {
return locale.toLowerCase().startsWith("zh");
}
/** Localized native axis name; other locales fall back to the English term. */
export function colorChannelLabel(channel: ColorAxisChannel, locale: string) {
return (isChinese(locale) ? chinese : english)[channel];
}
/** Default group name used when the consumer does not supply a label. */
export function colorAreaLabel(locale: string) {
return isChinese(locale) ? "颜色区域" : "Color area";
@@ -0,0 +1,340 @@
<script setup lang="ts">
import {
computed,
getCurrentInstance,
provide,
ref,
shallowRef,
toRef,
useAttrs,
useId,
watch,
type StyleValue,
} from "vue";
import { Primitive, useForwardExpose } from "reka-ui";
import { colorSliderVariants } from "@heroui-vue/styles";
import { fieldContextKey, mergeIds } from "../field/context";
import { useConfig } from "../../providers/context";
import { useColorState, useColorStateContext } from "../color/model";
import {
copyColor,
type Color,
type ColorChannel,
type ColorSpace,
type ColorValue,
} from "../color/value";
import { useColorSlider } from "../color/slider";
import { colorSliderKey, type ColorSliderElements } from "./context";
import type {
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,
},
);
defineSlots<ColorSliderSlots>();
const emit = defineEmits<{
"update:modelValue": [value: Color | null];
change: [value: Color | null];
changeEnd: [value: Color | null];
}>();
const attrs = useAttrs();
const instance = getCurrentInstance()!;
const parent = useColorStateContext();
const channelSpaces: Partial<Record<ColorChannel, ColorSpace>> = {
red: "rgb",
green: "rgb",
blue: "rgb",
lightness: "hsl",
brightness: "hsb",
};
const hslHsbOnly = new Set<string>(["hue", "saturation"]);
/** Mirrors the fixed source: explicit invalid spaces are corrected at runtime. */
function correctedSpace(
channel: ColorChannel,
space: ColorSpace | undefined,
): ColorSpace | undefined {
const required = channelSpaces[channel];
if (required && space && space !== required) return required;
if (hslHsbOnly.has(channel) && space === "rgb") return "hsl";
return space;
}
const space = computed(() => correctedSpace(props.channel, props.colorSpace));
function warnInvalid(channel: ColorChannel, colorSpace?: ColorSpace) {
if (!colorSpace) return;
const required = channelSpaces[channel];
if (required && colorSpace !== required) {
if (process.env.NODE_ENV === "production") return;
console.warn(
`[HeroUI Vue ColorSlider] Invalid combination: channel="${channel}" requires colorSpace="${required}", ` +
`but received colorSpace="${colorSpace}". Auto-correcting to "${required}".`,
);
return;
}
if (hslHsbOnly.has(channel) && colorSpace === "rgb") {
if (process.env.NODE_ENV === "production") return;
console.warn(
`[HeroUI Vue ColorSlider] Invalid combination: channel="${channel}" is not available in RGB color space. ` +
`Use colorSpace="hsl" or colorSpace="hsb" instead. Auto-correcting to "hsl".`,
);
}
}
watch(
[() => props.channel, () => props.colorSpace],
([channel, colorSpace]) => warnInvalid(channel, colorSpace),
{ immediate: true },
);
const has = (key: string) => Object.hasOwn(instance.vnode.props ?? {}, key);
const ownsValue = () =>
["modelValue", "model-value", "value", "defaultValue", "default-value"].some(
has,
);
/**
* The fixed source requires a value or defaultValue. Vue additionally inherits
* the nearest color owner when every own value prop is omitted.
*/
function fallback(): Color {
const own = copyColor(
has("modelValue") || has("model-value")
? props.modelValue
: has("value")
? props.value
: undefined,
);
if (own) return own;
if (props.defaultValue !== undefined) {
const preferred = copyColor(props.defaultValue);
if (preferred) return preferred;
}
if (!ownsValue() && parent) {
const inherited = copyColor(parent.value.value);
if (inherited) return inherited;
}
throw new Error(
"ColorSlider requires a value or defaultValue, or an inherited color.",
);
}
const state = useColorState(props, {
fallback,
inherit: true,
allowEmpty: false,
change: (value) => {
emit("update:modelValue", value);
emit("change", value);
},
changeEnd: (value) => emit("changeEnd", value),
});
const elements: ColorSliderElements = {
root: shallowRef<HTMLElement>(),
track: shallowRef<HTMLElement>(),
thumb: shallowRef<HTMLElement>(),
input: shallowRef<HTMLInputElement>(),
};
// Getters keep the private slider kernel reactive without creating a second
// options object or copying emitted state.
const options = {
get channel() {
return props.channel;
},
get colorSpace() {
return space.value;
},
get locale() {
return props.locale;
},
get orientation() {
return props.orientation;
},
get dir() {
return props.dir;
},
};
const control = useColorSlider(state, options, elements);
const { descriptor, dir, orientation } = control;
const disabled = state.disabled;
const readOnly = state.readOnly;
const focusVisible = shallowRef(false);
const pointerFocus = shallowRef(false);
const styles = colorSliderVariants();
const effectiveSpace = computed(
() => descriptor.value.color.getColorSpace() as ColorSpace,
);
const channel = toRef(props, "channel");
const config = useConfig();
const locale = computed(() => props.locale ?? config?.locale.value ?? "en-US");
const renderProps = computed<ColorSliderRenderProps>(() => ({
color: descriptor.value.color,
channel: props.channel,
colorSpace: effectiveSpace.value,
orientation: orientation.value,
isDisabled: disabled.value,
isReadOnly: readOnly.value,
}));
const ordinaryStyle = computed<StyleValue | undefined>(() => {
const value = props.style;
return typeof value === "function" ? value(renderProps.value) : value;
});
const computedStyle = computed<StyleValue | undefined>(() =>
props.styleFn?.(renderProps.value),
);
const id = useId();
const inputId = computed(() => props.id ?? id);
const descriptionIds = ref<string[]>([]);
const errorIds = ref<string[]>([]);
const label = () => attrs["aria-label"] as string | undefined;
const labelledBy = () => attrs["aria-labelledby"] as string | undefined;
const describedBy = () =>
mergeIds(
attrs["aria-describedby"] as string | undefined,
descriptionIds.value,
errorIds.value,
);
provide(fieldContextKey, {
kind: "text",
controlId: inputId,
disabled,
readOnly,
required: computed(() => false),
invalid: computed(() => errorIds.value.length > 0),
errors: computed(() => errorIds.value),
descriptionIds,
errorIds,
});
provide(colorSliderKey, {
elements,
state,
control,
styles,
focusVisible,
pointerFocus,
disabled,
readOnly,
channel,
space: effectiveSpace,
orientation,
dir,
locale,
inputId,
name: toRef(props, "name"),
form: toRef(props, "form"),
label,
labelledBy,
describedBy,
renderProps,
});
// Aria label props belong to the real range input, not the non-interactive root.
const rootAttrs = computed(() => {
const rest: Record<string, unknown> = { ...attrs };
delete rest["aria-label"];
delete rest["aria-labelledby"];
delete rest["aria-describedby"];
return rest;
});
defineExpose({
element: elements.root,
color: computed(() => state.value.value),
focus: () => elements.input.value?.focus(),
});
const { forwardRef, currentElement } = useForwardExpose();
watch(
currentElement,
(element) => {
elements.root.value = (element as HTMLElement | null) ?? undefined;
},
{ immediate: true, flush: "sync" },
);
</script>
<template>
<Primitive
:ref="forwardRef"
:as="as"
:as-child="asChild"
v-bind="rootAttrs"
:class="styles.base()"
:style="[ordinaryStyle, computedStyle]"
data-slot="color-slider"
:data-orientation="orientation"
:dir="dir"
:data-disabled="disabled || undefined"
:data-readonly="readOnly || undefined"
>
<slot v-bind="renderProps" />
</Primitive>
</template>
@@ -0,0 +1,81 @@
<script lang="ts">
import {
computed,
defineComponent,
h,
mergeProps,
renderSlot,
type PropType,
type StyleValue,
} from "vue";
import { useColorSliderContext } from "./context";
import type {
ColorSliderOutputRenderProps,
ColorSliderOutputStyle,
ColorSliderOutputStyleFn,
} from "./types";
export default defineComponent({
name: "ColorSliderOutput",
inheritAttrs: false,
// Only used if setup fails before returning its render function, so a missing
// ColorSlider context reports the context error instead of a template warning.
render: () => null,
props: {
/** Render a different output element. */
as: { type: String, default: "output" },
/** Vue style binding or upstream style function, merged after base styles. */
style: {
type: [
String,
Object,
Array,
Function,
] as PropType<ColorSliderOutputStyle>,
default: undefined,
},
/** Template-friendly alias for the style function; receives render props. */
styleFn: {
type: Function as PropType<ColorSliderOutputStyleFn>,
default: undefined,
},
},
setup(props, { attrs, slots }) {
const { styles, control, inputId, renderProps } = useColorSliderContext();
const renderState = computed<ColorSliderOutputRenderProps>(() => ({
...renderProps.value,
value: control.value.value,
label: control.label.value,
}));
const ordinaryStyle = computed<StyleValue | undefined>(() => {
const value = props.style;
return typeof value === "function" ? value(renderState.value) : value;
});
const computedStyle = computed<StyleValue | undefined>(() =>
props.styleFn?.(renderState.value),
);
return () => {
const { class: attrClass, ...rest } = attrs;
const custom = slots.default;
const children = custom
? renderSlot(
slots,
"default",
renderState.value as unknown as Record<string, unknown>,
)
: renderState.value.label;
return h(
props.as,
mergeProps(rest, {
class: [styles.output(), attrClass],
style: [ordinaryStyle.value, computedStyle.value],
"data-slot": "color-slider-output",
for: inputId.value,
}),
children,
);
};
},
});
</script>
@@ -0,0 +1,174 @@
<script setup lang="ts">
import {
computed,
shallowRef,
watch,
type CSSProperties,
type StyleValue,
} from "vue";
import { Primitive, useForwardExpose } from "reka-ui";
import { colorSliderDisplay } from "../color/slider-gradient";
import { useColorSliderContext } from "./context";
import type {
ColorSliderThumbRenderProps,
ColorSliderThumbSlots,
ColorSliderThumbStyle,
ColorSliderThumbStyleFn,
} from "./types";
defineOptions({ inheritAttrs: false, name: "ColorSliderThumb" });
const props = withDefaults(
defineProps<{
/** Render a different thumb element; corresponds to the upstream render prop. */
as?: string;
/** Vue style binding or upstream style function, merged after base styles. */
style?: ColorSliderThumbStyle;
/** Template-friendly alias for the style function; receives render props. */
styleFn?: ColorSliderThumbStyleFn;
}>(),
{
as: "div",
style: undefined,
styleFn: undefined,
},
);
defineSlots<ColorSliderThumbSlots>();
const {
elements,
styles,
control,
focusVisible,
pointerFocus,
disabled,
readOnly,
channel,
space,
orientation,
inputId,
name,
form,
label,
labelledBy,
describedBy,
renderProps,
} = useColorSliderContext();
const input = shallowRef<HTMLInputElement>();
const display = computed(() =>
colorSliderDisplay(
control.descriptor.value.color,
channel.value,
space.value,
).toString("css"),
);
const baseStyle = computed<CSSProperties>(() => {
const style: CSSProperties = {
...control.position.value,
transform: "translate(-50%, -50%)",
};
if (!disabled.value) style.backgroundColor = display.value;
return style;
});
const renderState = computed<ColorSliderThumbRenderProps>(() => ({
...renderProps.value,
percent: control.percent.value,
isDragging: control.dragging.value,
}));
const ordinaryStyle = computed<StyleValue | undefined>(() => {
const value = props.style;
return typeof value === "function" ? value(renderState.value) : value;
});
const computedStyle = computed<StyleValue | undefined>(() =>
props.styleFn?.(renderState.value),
);
const hiddenStyle: CSSProperties = {
position: "absolute",
width: "1px",
height: "1px",
overflow: "hidden",
clipPath: "inset(50%)",
pointerEvents: "none",
};
function onFocus() {
focusVisible.value =
!pointerFocus.value && (input.value?.matches(":focus-visible") ?? false);
}
function onKeydown(event: KeyboardEvent) {
// Modality can change while the input keeps focus (pointer focus, then keys).
if (input.value?.matches(":focus-visible")) focusVisible.value = true;
control.keydown(event);
}
function onBlur() {
focusVisible.value = false;
control.commit();
}
defineExpose({ element: elements.thumb });
const { forwardRef, currentElement } = useForwardExpose();
watch(
currentElement,
(element) => {
elements.thumb.value = (element as HTMLElement | null) ?? undefined;
},
{ immediate: true, flush: "sync" },
);
watch(
input,
(element) => {
elements.input.value = element;
},
{ immediate: true, flush: "sync" },
);
</script>
<template>
<Primitive
:ref="forwardRef"
:as="as"
v-bind="$attrs"
:class="styles.thumb()"
:style="[baseStyle, ordinaryStyle, computedStyle]"
data-slot="color-slider-thumb"
:data-dragging="control.dragging.value || undefined"
:data-focus-visible="focusVisible || undefined"
:data-disabled="disabled || undefined"
:data-readonly="readOnly || undefined"
>
<input
ref="input"
:id="inputId"
type="range"
step="any"
:style="hiddenStyle"
:min="control.range.value.minValue"
:max="control.range.value.maxValue"
:value="control.value.value"
:name="name"
:form="form"
:disabled="disabled"
:aria-readonly="readOnly || undefined"
:aria-orientation="orientation"
:aria-label="label()"
:aria-labelledby="labelledBy()"
:aria-describedby="describedBy()"
:aria-valuetext="control.label.value"
@input="control.input"
@change="control.commit"
@keydown="onKeydown"
@keyup="control.keyup"
@focus="onFocus"
@blur="onBlur"
/>
<slot v-bind="renderState" />
</Primitive>
</template>
@@ -0,0 +1,122 @@
<script setup lang="ts">
import { computed, watch, type CSSProperties, type StyleValue } from "vue";
import { Primitive, useForwardExpose } from "reka-ui";
import { colorSliderBackground } from "../color/slider-gradient";
import { useColorSliderContext } from "./context";
import type {
ColorSliderTrackRenderProps,
ColorSliderTrackSlots,
ColorSliderTrackStyle,
ColorSliderTrackStyleFn,
} from "./types";
defineOptions({ inheritAttrs: false, name: "ColorSliderTrack" });
const props = withDefaults(
defineProps<{
/** Render a different track element. */
as?: string;
/** Vue style binding or upstream style function, merged after base styles. */
style?: ColorSliderTrackStyle;
/** Template-friendly alias for the style function; receives render props. */
styleFn?: ColorSliderTrackStyleFn;
}>(),
{
as: "div",
style: undefined,
styleFn: undefined,
},
);
defineSlots<ColorSliderTrackSlots>();
const {
elements,
styles,
control,
channel,
space,
orientation,
dir,
disabled,
readOnly,
focusVisible,
pointerFocus,
renderProps,
} = useColorSliderContext();
function onPointerDown(event: PointerEvent) {
pointerFocus.value = true;
focusVisible.value = false;
control.pointerdown(event);
queueMicrotask(() => {
pointerFocus.value = false;
});
}
const background = computed(() =>
colorSliderBackground(
control.descriptor.value.color,
channel.value,
space.value,
orientation.value,
dir.value,
),
);
const baseStyle = computed<CSSProperties>(() => {
const paint = background.value;
return {
background: paint.background,
"--track-background": paint.background,
"--track-start-color": paint["--track-start-color"],
"--track-end-color": paint["--track-end-color"],
touchAction: disabled.value || readOnly.value ? "auto" : "none",
forcedColorAdjust: "none",
} as CSSProperties;
});
const renderState = computed<ColorSliderTrackRenderProps>(() => ({
...renderProps.value,
percent: control.percent.value,
}));
const ordinaryStyle = computed<StyleValue | undefined>(() => {
const value = props.style;
return typeof value === "function" ? value(renderState.value) : value;
});
const computedStyle = computed<StyleValue | undefined>(() =>
props.styleFn?.(renderState.value),
);
defineExpose({ element: elements.track });
const { forwardRef, currentElement } = useForwardExpose();
watch(
currentElement,
(element) => {
elements.track.value = (element as HTMLElement | null) ?? undefined;
},
{ immediate: true, flush: "sync" },
);
</script>
<template>
<Primitive
:ref="forwardRef"
:as="as"
v-bind="$attrs"
:class="styles.track()"
:style="[baseStyle, ordinaryStyle, computedStyle]"
data-slot="color-slider-track"
:data-disabled="disabled || undefined"
:data-readonly="readOnly || undefined"
@pointerdown="onPointerDown"
@pointermove="control.pointermove"
@pointerup="control.pointerup"
@pointercancel="control.cancel"
@lostpointercapture="control.cancel"
>
<slot v-bind="renderState" />
</Primitive>
</template>
@@ -0,0 +1,55 @@
import {
inject,
type ComputedRef,
type InjectionKey,
type Ref,
type ShallowRef,
} from "vue";
import type { colorSliderVariants } from "@heroui-vue/styles";
import type { ColorChannel, ColorSpace } from "../color/value";
import type { ColorState } from "../color/model";
import type { useColorSlider } from "../color/slider";
import type { ColorSliderRenderProps } from "./types";
export type ColorSliderControl = ReturnType<typeof useColorSlider>;
export type ColorSliderStyles = ReturnType<typeof colorSliderVariants>;
export interface ColorSliderElements {
root: ShallowRef<HTMLElement | undefined>;
track: ShallowRef<HTMLElement | undefined>;
thumb: ShallowRef<HTMLElement | undefined>;
input: ShallowRef<HTMLInputElement | undefined>;
}
export interface ColorSliderContext {
elements: ColorSliderElements;
state: ColorState;
control: ColorSliderControl;
styles: ColorSliderStyles;
focusVisible: ShallowRef<boolean>;
pointerFocus: ShallowRef<boolean>;
disabled: ComputedRef<boolean>;
readOnly: ComputedRef<boolean>;
channel: Ref<ColorChannel>;
space: ComputedRef<ColorSpace>;
orientation: ComputedRef<"horizontal" | "vertical">;
dir: ComputedRef<"ltr" | "rtl">;
locale: ComputedRef<string>;
inputId: ComputedRef<string>;
name: Ref<string | undefined>;
form: Ref<string | undefined>;
label: () => string | undefined;
labelledBy: () => string | undefined;
describedBy: () => string | undefined;
renderProps: ComputedRef<ColorSliderRenderProps>;
}
export const colorSliderKey: InjectionKey<ColorSliderContext> =
Symbol("hv-color-slider");
export function useColorSliderContext() {
const context = inject(colorSliderKey, undefined);
if (!context)
throw new Error("ColorSlider parts must be rendered inside ColorSlider.");
return context;
}
@@ -0,0 +1,176 @@
import type { StyleValue, VNodeChild } from "vue";
import type {
Color,
ColorChannel,
ColorSpace,
ColorValue,
} from "../color/value";
/** Values shared by the public style functions, slots and render state. */
export interface ColorSliderRenderProps {
/** Display color converted for the active channel; defaults to the accepted value. */
color: Color;
channel: ColorChannel;
/** Color space actually used to interpret the channel after auto-correction. */
colorSpace: ColorSpace;
orientation: "horizontal" | "vertical";
isDisabled: boolean;
isReadOnly: boolean;
}
export interface ColorSliderOutputRenderProps extends ColorSliderRenderProps {
/** Accepted channel value in the model scale (alpha is 0–1). */
value: number;
/** Locale-formatted channel value, also used as the native input value text. */
label: string;
}
export interface ColorSliderTrackRenderProps extends ColorSliderRenderProps {
/** Accepted channel position in the 0–1 content box, before RTL mapping. */
percent: number;
}
export interface ColorSliderThumbRenderProps extends ColorSliderTrackRenderProps {
isDragging: boolean;
}
export type ColorSliderStyleFn = (
renderProps: ColorSliderRenderProps,
) => StyleValue | undefined;
/** Vue style binding (object/array/string) or the upstream style function. */
export type ColorSliderStyle = StyleValue | ColorSliderStyleFn;
export type ColorSliderOutputStyleFn = (
renderProps: ColorSliderOutputRenderProps,
) => StyleValue | undefined;
export type ColorSliderOutputStyle = StyleValue | ColorSliderOutputStyleFn;
export type ColorSliderTrackStyleFn = (
renderProps: ColorSliderTrackRenderProps,
) => StyleValue | undefined;
export type ColorSliderTrackStyle = StyleValue | ColorSliderTrackStyleFn;
export type ColorSliderThumbStyleFn = (
renderProps: ColorSliderThumbRenderProps,
) => StyleValue | undefined;
export type ColorSliderThumbStyle = StyleValue | ColorSliderThumbStyleFn;
export interface ColorSliderSlots {
default?: (renderProps: ColorSliderRenderProps) => VNodeChild;
}
export interface ColorSliderOutputSlots {
default?: (renderProps: ColorSliderOutputRenderProps) => VNodeChild;
}
export interface ColorSliderTrackSlots {
default?: (renderProps: ColorSliderTrackRenderProps) => VNodeChild;
}
export interface ColorSliderThumbSlots {
default?: (renderProps: ColorSliderThumbRenderProps) => VNodeChild;
}
/** Channels available in the HSL color space. */
export type HSLChannel = "hue" | "saturation" | "lightness" | "alpha";
/** Channels available in the HSB color space. */
export type HSBChannel = "hue" | "saturation" | "brightness" | "alpha";
/** Channels available in the RGB color space. */
export type RGBChannel = "red" | "green" | "blue" | "alpha";
/** Channels shared by HSL and HSB but not RGB. */
export type HSLHSBSharedChannel = "hue" | "saturation";
/** Alpha works across every color space. */
export type AlphaChannel = "alpha";
/**
* Discriminated union for valid channel/colorSpace pairs, mirroring the fixed
* source. Invalid combinations such as `channel="red"` with `colorSpace="hsl"`
* or `channel="hue"` with `colorSpace="rgb"` fail type checking.
*/
export type ColorSliderChannelProps =
| { channel: HSLChannel; colorSpace?: "hsl" }
| { channel: HSBChannel; colorSpace?: "hsb" }
| { channel: RGBChannel; colorSpace?: "rgb" }
| { channel: HSLHSBSharedChannel; colorSpace?: "hsl" | "hsb" }
| { channel: AlphaChannel; colorSpace?: ColorSpace };
/** Props shared by every valid channel/colorSpace combination. */
export interface ColorSliderBaseProps {
/** 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;
/** Slider direction. */
orientation?: "horizontal" | "vertical";
/** Native form field name for the channel value. */
name?: string;
/** Id of an external form. */
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;
}
export type ColorSliderProps = ColorSliderBaseProps & ColorSliderChannelProps;
export type ColorSliderRootProps = ColorSliderProps;
export interface ColorSliderOutputProps {
/** Render a different output element. */
as?: string;
/** Vue style binding or upstream style function, merged after base styles. */
style?: ColorSliderOutputStyle;
/** Template-friendly alias for the style function; receives render props. */
styleFn?: ColorSliderOutputStyleFn;
}
export interface ColorSliderTrackProps {
/** Render a different track element. */
as?: string;
/** Vue style binding or upstream style function, merged after base styles. */
style?: ColorSliderTrackStyle;
/** Template-friendly alias for the style function; receives render props. */
styleFn?: ColorSliderTrackStyleFn;
}
export interface ColorSliderThumbProps {
/** Render a different thumb element; corresponds to the upstream render prop. */
as?: string;
/** Vue style binding or upstream style function, merged after base styles. */
style?: ColorSliderThumbStyle;
/** Template-friendly alias for the style function; receives render props. */
styleFn?: ColorSliderThumbStyleFn;
}
/**
* State exposed by the root instance. Refs are unwrapped on the public
* instance proxy, matching the ColorArea `element`/`color` convention.
*/
export interface ColorSliderExpose {
element: HTMLElement | null;
color: Color | null;
focus: () => void;
}
@@ -0,0 +1,32 @@
import type { ColorChannel } from "./value";
const english: Record<ColorChannel, string> = {
red: "Red",
green: "Green",
blue: "Blue",
hue: "Hue",
saturation: "Saturation",
lightness: "Lightness",
brightness: "Brightness",
alpha: "Alpha",
};
const chinese: Record<ColorChannel, string> = {
red: "红色",
green: "绿色",
blue: "蓝色",
hue: "色相",
saturation: "饱和度",
lightness: "亮度",
brightness: "明度",
alpha: "透明度",
};
function isChinese(locale: string) {
return locale.toLowerCase().startsWith("zh");
}
/** Localized native slider/channel name; other locales fall back to English. */
export function colorChannelLabel(channel: ColorChannel, locale: string) {
return (isChinese(locale) ? chinese : english)[channel];
}