feat: establish color slider geometry and picker composition
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { defineComponent, watch } from "vue";
|
||||
import { usePopover } from "../../src/components/popover/context";
|
||||
import { useColorStateContext } from "../../src/components/color/model";
|
||||
/** Observe accepted overlay state, never its potentially rejected close proposal. */
|
||||
export default defineComponent({
|
||||
name: "ColorPickerBoundary",
|
||||
setup(_, { slots }) {
|
||||
const overlay = usePopover(),
|
||||
color = useColorStateContext()!;
|
||||
watch(
|
||||
overlay.state.isOpen,
|
||||
(open) => {
|
||||
if (!open) color.cancel();
|
||||
},
|
||||
{ flush: "sync" },
|
||||
);
|
||||
return () => slots.default?.();
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, shallowRef } from "vue";
|
||||
import {
|
||||
useColorState,
|
||||
type ColorModelProps,
|
||||
} from "../../src/components/color/model";
|
||||
import { serializeColorValue } from "../../src/components/color/submission";
|
||||
import { useChoiceForm } from "../../src/components/choice/shared";
|
||||
import { parseColor, type Color } from "../../src/components/color/value";
|
||||
import {
|
||||
Button,
|
||||
Popover,
|
||||
PopoverTrigger,
|
||||
PopoverContent,
|
||||
PopoverDialog,
|
||||
} from "../../src/index";
|
||||
import Boundary from "./ColorPickerBoundary";
|
||||
import Area from "./ColorAreaFoundation.vue";
|
||||
import Slider from "./ColorSliderFoundation.vue";
|
||||
import Input from "./ColorInputFoundation.vue";
|
||||
const props = withDefaults(
|
||||
defineProps<
|
||||
ColorModelProps & {
|
||||
name?: string;
|
||||
form?: string;
|
||||
open?: boolean;
|
||||
defaultOpen?: boolean;
|
||||
}
|
||||
>(),
|
||||
{ open: undefined },
|
||||
);
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [Color | null];
|
||||
change: [Color | null];
|
||||
changeEnd: [Color | null];
|
||||
"update:open": [boolean];
|
||||
}>();
|
||||
const state = useColorState(props, {
|
||||
provide: true,
|
||||
fallback: () => parseColor("hsb(270,0%,50%,.123456789)"),
|
||||
change: (value) => {
|
||||
emit("update:modelValue", value);
|
||||
emit("change", value);
|
||||
},
|
||||
changeEnd: (value) => emit("changeEnd", value),
|
||||
});
|
||||
const native = shallowRef<HTMLInputElement>();
|
||||
const value = state.value,
|
||||
disabled = state.disabled;
|
||||
const serialized = computed(() => serializeColorValue(value.value));
|
||||
useChoiceForm(native, state.reset, () => {
|
||||
if (native.value) native.value.value = serialized.value;
|
||||
});
|
||||
defineExpose({ state });
|
||||
</script>
|
||||
<template>
|
||||
<div data-color-picker-composition>
|
||||
<Popover
|
||||
:open="open"
|
||||
:default-open="defaultOpen"
|
||||
@update:open="emit('update:open', $event)"
|
||||
>
|
||||
<Boundary>
|
||||
<PopoverTrigger as-child>
|
||||
<Button
|
||||
:is-disabled="disabled"
|
||||
variant="secondary"
|
||||
data-color-picker-trigger
|
||||
>
|
||||
<span
|
||||
:style="{
|
||||
width: '20px',
|
||||
height: '20px',
|
||||
borderRadius: '6px',
|
||||
background: value?.toString('css'),
|
||||
}"
|
||||
/>
|
||||
选择颜色
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
placement="bottom start"
|
||||
:style="{ width: '256px' }"
|
||||
data-color-picker-panel
|
||||
>
|
||||
<PopoverDialog
|
||||
aria-label="选择颜色"
|
||||
class="color-picker-composition-panel"
|
||||
>
|
||||
<Area aria-label="Color area" />
|
||||
<Slider channel="hue" aria-label="Color hue" />
|
||||
<Slider channel="alpha" aria-label="Color alpha" />
|
||||
<Input channel="alpha" aria-label="Color alpha text" />
|
||||
<Input aria-label="Color hex text" />
|
||||
</PopoverDialog>
|
||||
</PopoverContent>
|
||||
</Boundary>
|
||||
</Popover>
|
||||
<input
|
||||
ref="native"
|
||||
type="hidden"
|
||||
:name="name"
|
||||
:form="form"
|
||||
:disabled="disabled"
|
||||
:value="serialized"
|
||||
/>
|
||||
<output data-color-picker-accepted>{{ serialized }}</output>
|
||||
</div>
|
||||
</template>
|
||||
<style>
|
||||
.color-picker-composition-panel {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
.color-picker-composition-panel [data-color-input] {
|
||||
width: 100%;
|
||||
padding: 8px 12px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 12px;
|
||||
background: var(--field-background);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,134 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, shallowRef, useAttrs } from "vue";
|
||||
import {
|
||||
useColorState,
|
||||
type ColorModelProps,
|
||||
} from "../../src/components/color/model";
|
||||
import {
|
||||
useColorSlider,
|
||||
type ColorSliderOptions,
|
||||
} from "../../src/components/color/slider";
|
||||
import {
|
||||
colorSliderBackground,
|
||||
colorSliderDisplay,
|
||||
} from "../../src/components/color/slider-gradient";
|
||||
import { parseColor, type Color } from "../../src/components/color/value";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = defineProps<
|
||||
ColorModelProps &
|
||||
ColorSliderOptions & { name?: string; form?: string; trackKey?: string }
|
||||
>();
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [Color | null];
|
||||
change: [Color | null];
|
||||
changeEnd: [Color | null];
|
||||
}>();
|
||||
const attrs = useAttrs();
|
||||
const state = useColorState(props, {
|
||||
inherit: true,
|
||||
fallback: () => parseColor("hsb(240,50%,50%)"),
|
||||
change: (value) => {
|
||||
emit("update:modelValue", value);
|
||||
emit("change", value);
|
||||
},
|
||||
changeEnd: (value) => emit("changeEnd", value),
|
||||
});
|
||||
const track = shallowRef<HTMLElement>(),
|
||||
thumb = shallowRef<HTMLElement>(),
|
||||
input = shallowRef<HTMLInputElement>();
|
||||
const control = useColorSlider(state, props, { track, thumb, input });
|
||||
const { dir, orientation, value, range, label, position, dragging } = control;
|
||||
const disabled = state.disabled,
|
||||
readonly = state.readOnly;
|
||||
const focusVisible = ref(false);
|
||||
const background = computed(() =>
|
||||
colorSliderBackground(
|
||||
control.descriptor.value.color,
|
||||
props.channel,
|
||||
props.colorSpace,
|
||||
orientation.value,
|
||||
dir.value,
|
||||
),
|
||||
);
|
||||
const hidden = {
|
||||
position: "absolute" as const,
|
||||
width: "1px",
|
||||
height: "1px",
|
||||
clipPath: "inset(50%)",
|
||||
overflow: "hidden",
|
||||
};
|
||||
function focused(event: FocusEvent) {
|
||||
focusVisible.value = (event.target as HTMLElement).matches(":focus-visible");
|
||||
}
|
||||
defineExpose({ state });
|
||||
</script>
|
||||
<template>
|
||||
<div
|
||||
class="hv-color-slider"
|
||||
data-color-slider-foundation
|
||||
:data-orientation="orientation"
|
||||
:dir="dir"
|
||||
:data-disabled="disabled || undefined"
|
||||
>
|
||||
<output class="hv-color-slider__output">{{ label }}</output>
|
||||
<div
|
||||
:key="trackKey"
|
||||
ref="track"
|
||||
data-color-slider-track
|
||||
class="hv-color-slider__track"
|
||||
:style="{ ...background, touchAction: 'none', forcedColorAdjust: 'none' }"
|
||||
@pointerdown="control.pointerdown"
|
||||
@pointermove="control.pointermove"
|
||||
@pointerup="control.pointerup"
|
||||
@pointercancel="control.cancel"
|
||||
@lostpointercapture="control.cancel"
|
||||
>
|
||||
<div
|
||||
ref="thumb"
|
||||
data-color-slider-thumb
|
||||
class="hv-color-slider__thumb"
|
||||
:data-disabled="disabled || undefined"
|
||||
:data-dragging="dragging || undefined"
|
||||
:data-focus-visible="focusVisible || undefined"
|
||||
:style="{
|
||||
...position,
|
||||
transform: 'translate(-50%, -50%)',
|
||||
backgroundColor: disabled
|
||||
? undefined
|
||||
: colorSliderDisplay(
|
||||
control.descriptor.value.color,
|
||||
channel,
|
||||
colorSpace,
|
||||
).toString('css'),
|
||||
}"
|
||||
>
|
||||
<input
|
||||
ref="input"
|
||||
data-color-slider-input
|
||||
v-bind="attrs"
|
||||
type="range"
|
||||
:style="hidden"
|
||||
:min="range.minValue"
|
||||
:max="range.maxValue"
|
||||
step="any"
|
||||
:value="value"
|
||||
:name="name"
|
||||
:form="form"
|
||||
:disabled="disabled"
|
||||
:aria-readonly="readonly || undefined"
|
||||
:aria-orientation="orientation"
|
||||
:aria-valuetext="label"
|
||||
@input="control.input"
|
||||
@change="control.commit"
|
||||
@keydown="control.keydown"
|
||||
@keyup="control.keyup"
|
||||
@focus="focused"
|
||||
@blur="
|
||||
focusVisible = false;
|
||||
control.commit();
|
||||
"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user