feat: establish shared color state and area interaction foundation

This commit is contained in:
maoyingjie
2026-10-25 11:10:47 +08:00
parent 58ca16e774
commit 57d7cbb2d6
26 changed files with 2429 additions and 88 deletions
+164
View File
@@ -0,0 +1,164 @@
<script setup lang="ts">
import { computed, shallowRef, ref, useAttrs, useId } from "vue";
import {
useColorState,
type ColorModelProps,
} from "../../src/components/color/model";
import {
useColorArea,
type ColorAreaOptions,
} from "../../src/components/color/area";
import { colorAreaBackground } from "../../src/components/color/gradient";
import { parseColor, type Color } from "../../src/components/color/value";
defineOptions({ inheritAttrs: false });
const props = defineProps<ColorModelProps & ColorAreaOptions>();
const emit = defineEmits<{
"update:modelValue": [Color | null];
change: [Color | null];
changeEnd: [Color | null];
}>();
const attrs = useAttrs();
const state = useColorState(props, {
fallback: () => parseColor("hsb(240, 50%, 50%)"),
inherit: true,
change: (value) => {
emit("update:modelValue", value);
emit("change", value);
},
changeEnd: (value) => emit("changeEnd", value),
});
const root = shallowRef<HTMLElement>(),
thumb = shallowRef<HTMLInputElement>(),
handle = shallowRef<HTMLElement>(),
focusY = shallowRef<HTMLInputElement>();
const control = useColorArea(state, props, {
root,
focusX: thumb,
focusY,
thumb: handle,
});
const { axes, x, y, percent, dragging, dir, focusedAxis, locale } = control;
const background = computed(() =>
colorAreaBackground(
axes.value.color,
axes.value.space,
axes.value.x,
axes.value.y,
dir.value,
),
);
const disabled = state.disabled,
readonly = state.readOnly;
const id = useId();
const hidden = {
position: "absolute" as const,
width: "1px",
height: "1px",
overflow: "hidden",
clipPath: "inset(50%)",
};
const focusVisible = ref(false);
function onFocusin(event: FocusEvent) {
focusVisible.value = (event.target as HTMLElement).matches(":focus-visible");
}
function onFocusout(event: FocusEvent) {
if (!root.value?.contains(event.relatedTarget as Node | null)) {
focusVisible.value = false;
control.blur();
}
}
function axis(event: Event) {
const value = (event.target as HTMLElement).getAttribute("data-axis");
return value === "x" || value === "y" ? value : undefined;
}
function onKeydown(event: KeyboardEvent) {
const channel = axis(event);
if (channel) control.keydown(event);
}
function onInput(event: Event) {
const channel = axis(event);
if (channel) control.input(channel, event);
}
defineExpose({ state, control });
</script>
<template>
<div
ref="root"
v-bind="attrs"
class="hv-color-area"
data-color-area
role="group"
:dir="dir"
:aria-label="(attrs['aria-label'] as string) ?? 'Color area'"
:data-disabled="disabled || undefined"
:style="{
...background,
touchAction: disabled || readonly ? 'auto' : 'none',
}"
@keydown="onKeydown"
@keyup="control.keyup"
@input="onInput"
@change="control.commit"
@focusin="onFocusin"
@focusout="onFocusout"
@pointerdown="control.pointerdown"
@pointermove="control.pointermove"
@pointerup="control.pointerup"
@pointercancel="control.cancel"
@lostpointercapture="control.cancel"
>
<div
ref="handle"
class="hv-color-area__thumb"
data-color-thumb
:data-dragging="dragging || undefined"
:data-focus-visible="focusVisible || undefined"
:data-disabled="disabled || undefined"
:style="{
position: 'absolute',
left: `${(dir === 'rtl' ? 1 - percent.x : percent.x) * 100}%`,
top: `${(1 - percent.y) * 100}%`,
transform: 'translate(-50%,-50%)',
'--color-area-thumb-color': axes.color.toString('css'),
}"
>
<input
:id="`color-${id}-x`"
ref="thumb"
type="range"
step="any"
data-axis="x"
:tabindex="focusedAxis === 'x' ? 0 : -1"
:name="xName"
:form="form"
:aria-label="axes.x"
:aria-valuetext="axes.color.formatChannelValue(axes.x, locale)"
:min="0"
:max="axes.color.getChannelRange(axes.x).maxValue"
:value="x"
:disabled="disabled"
:aria-readonly="readonly || undefined"
:style="hidden"
/>
<input
:id="`color-${id}-y`"
type="range"
step="any"
data-axis="y"
ref="focusY"
:tabindex="focusedAxis === 'y' ? 0 : -1"
:name="yName"
:form="form"
:aria-label="axes.y"
:aria-valuetext="axes.color.formatChannelValue(axes.y, locale)"
aria-orientation="vertical"
:min="0"
:max="axes.color.getChannelRange(axes.y).maxValue"
:value="y"
:disabled="disabled"
:aria-readonly="readonly || undefined"
:style="hidden"
/>
</div>
</div>
</template>
+125
View File
@@ -0,0 +1,125 @@
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import {
useColorState,
type ColorModelProps,
} from "../../src/components/color/model";
import { Color, parseColor, colorKey } from "../../src/components/color/value";
import { useChoiceForm } from "../../src/components/choice/shared";
import Area from "./ColorAreaFoundation.vue";
const props = defineProps<ColorModelProps & { name?: string; form?: string }>();
const emit = defineEmits<{
"update:modelValue": [Color | null];
change: [Color | null];
changeEnd: [Color | null];
}>();
const state = useColorState(props, {
fallback: () => parseColor("hsb(240,50%,50%)"),
provide: true,
change: (value) => {
emit("update:modelValue", value);
emit("change", value);
},
changeEnd: (value) => emit("changeEnd", value),
});
const value = state.value,
disabled = state.disabled,
readOnly = state.readOnly;
const text = ref(value.value?.toString("hexa") ?? ""),
native = ref<HTMLInputElement>();
const serialized = computed(() => value.value?.toString("hexa") ?? "");
useChoiceForm(native, state.reset, () => {});
watch(
() => colorKey(value.value),
() => {
text.value = value.value?.toString("hexa") ?? "";
},
);
const edit = Symbol("color-text");
function commit(event: Event) {
const input = event.currentTarget as HTMLInputElement;
try {
if (state.begin(edit)) {
state.request(input.value.trim() ? parseColor(input.value) : null, edit);
state.end(edit);
}
} catch {
state.cancel(edit);
}
text.value = value.value?.toString("hexa") ?? "";
input.value = text.value;
}
const hue = Symbol("color-hue");
function setHue(event: Event) {
const input = event.currentTarget as HTMLInputElement;
if (state.begin(hue))
state.request(
(value.value ?? parseColor("#ff0000"))
.toFormat("hsb")
.withChannelValue("hue", Number(input.value)),
hue,
);
input.value = String(
value.value?.toFormat("hsb").getChannelValue("hue") ?? 0,
);
}
defineExpose({ state });
</script>
<template>
<div data-color-picker-foundation>
<slot :value="value">
<Area
aria-label="Shared HSB area"
color-space="hsb"
x-channel="saturation"
y-channel="brightness"
/>
<input
data-color-hue
type="range"
step="any"
min="0"
max="360"
:value="value?.toFormat('hsb').getChannelValue('hue') ?? 0"
aria-label="Hue"
:disabled="disabled"
:aria-readonly="readOnly || undefined"
@input="setHue"
@change="state.end(hue)"
@pointercancel="state.cancel(hue)"
@blur="state.end(hue)"
/>
<input
data-color-text
aria-label="Hex color"
:value="text"
:disabled="disabled"
:readonly="readOnly"
@change="commit"
/>
<output data-color-output>
{{
value?.toString(
value.getChannelValue("alpha") === 1 ? "hsb" : "hsba",
) ?? "empty"
}}
</output>
<div
data-color-swatch
:style="{
width: '32px',
height: '32px',
background: value?.toString('css'),
}"
/>
</slot>
<input
ref="native"
type="hidden"
:name="name"
:form="form"
:disabled="disabled"
:value="serialized"
/>
</div>
</template>
+10
View File
@@ -0,0 +1,10 @@
// Installed-version probes use only public exports, resolved from the component package.
export {
ColorAreaRoot,
ColorAreaArea,
ColorAreaThumb,
parseColor,
getChannelRange,
getChannelValue,
getAreaBackgroundStyle,
} from "reka-ui";