feat: establish composed color picker protocol
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
/* Adapted from HeroUI ce90ea3, Apache-2.0; see third-party/HEROUI-LICENSE. */
|
||||
/* ==========================================================================
|
||||
ColorPicker - Color selection component with popover
|
||||
========================================================================== */
|
||||
|
||||
/* Base color-picker styles */
|
||||
.hv-color-picker {
|
||||
@apply inline-flex;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Trigger
|
||||
========================================================================== */
|
||||
|
||||
.hv-color-picker__trigger {
|
||||
@apply inline-flex items-center gap-3 rounded-sm text-sm no-highlight;
|
||||
|
||||
/* Cursor */
|
||||
cursor: var(--cursor-interactive);
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition:
|
||||
background-color 150ms var(--ease-smooth),
|
||||
box-shadow 150ms var(--ease-out);
|
||||
@apply motion-reduce:transition-none;
|
||||
|
||||
[data-slot="label"] {
|
||||
cursor: var(--cursor-interactive);
|
||||
}
|
||||
|
||||
/* Focus visible state */
|
||||
&:focus-visible:not([data-focused]),
|
||||
&[data-focus-visible="true"] {
|
||||
@apply status-focused;
|
||||
}
|
||||
|
||||
/* Disabled state */
|
||||
&:disabled,
|
||||
&[data-disabled="true"],
|
||||
&[aria-disabled="true"] {
|
||||
@apply status-disabled;
|
||||
}
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Popover
|
||||
========================================================================== */
|
||||
|
||||
.hv-color-picker__popover {
|
||||
@apply min-w-62 origin-(--reka-popper-transform-origin) scrollbar-none overflow-x-hidden overflow-y-auto overscroll-contain bg-overlay px-2 pt-2 pb-3;
|
||||
box-shadow: var(--overlay-shadow);
|
||||
border-radius: min(32px, calc(var(--radius) * 2.5));
|
||||
/* Content layout */
|
||||
@apply flex flex-col gap-3;
|
||||
|
||||
/* Focus state */
|
||||
&:focus-visible:not([data-focused]),
|
||||
&[data-focus-visible="true"] {
|
||||
@apply outline-none;
|
||||
}
|
||||
|
||||
/* Entering animations */
|
||||
&[data-entering="true"] {
|
||||
@apply animate-in duration-150 ease-smooth fade-in-0 zoom-in-95;
|
||||
|
||||
/* Placement-specific translations */
|
||||
&[data-side="top"] {
|
||||
@apply slide-in-from-bottom-1;
|
||||
}
|
||||
|
||||
&[data-side="bottom"] {
|
||||
@apply slide-in-from-top-1;
|
||||
}
|
||||
|
||||
&[data-side="left"] {
|
||||
@apply slide-in-from-right-1;
|
||||
}
|
||||
|
||||
&[data-side="right"] {
|
||||
@apply slide-in-from-left-1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Exiting animations */
|
||||
&[data-exiting="true"] {
|
||||
@apply animate-out duration-100 ease-smooth zoom-out-95 fade-out;
|
||||
/* Stays non-interactive while closing, even if the exit animation is interrupted. */
|
||||
@apply pointer-events-none;
|
||||
}
|
||||
|
||||
&[data-exiting="true"],
|
||||
&[data-entering="true"] {
|
||||
@apply transition-[opacity,transform] will-change-[opacity,transform];
|
||||
}
|
||||
}
|
||||
|
||||
/* Reka supplies available height on the positioned surface. Picker scrolls its own content. */
|
||||
.hv-popover.hv-color-picker__popover {
|
||||
max-height: var(--reka-popover-content-available-height);
|
||||
&[data-entering="true"] {
|
||||
--tw-enter-scale: 0.95;
|
||||
}
|
||||
}
|
||||
|
||||
.hv-popover.hv-color-picker__popover:is(
|
||||
[data-entering="true"],
|
||||
[data-exiting="true"]
|
||||
) {
|
||||
@apply motion-reduce:animate-none motion-reduce:transition-none;
|
||||
}
|
||||
@@ -168,3 +168,5 @@
|
||||
|
||||
@import "./components/color-field.css" layer(components);
|
||||
@import "./components/color-input-group.css" layer(components);
|
||||
|
||||
@import "./components/color-picker.css" layer(components);
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, provide, shallowRef, type VNode } from "vue";
|
||||
import { useColorState } from "../color/model";
|
||||
import { parseColor, type Color } from "../color/value";
|
||||
import { serializeColorValue } from "../color/submission";
|
||||
import { useChoiceForm } from "../choice/shared";
|
||||
import Popover from "../popover/Popover";
|
||||
import Boundary from "./ColorPickerBoundary";
|
||||
import {
|
||||
colorPickerKey,
|
||||
type ColorPickerProps,
|
||||
type ColorPickerRenderState,
|
||||
} from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
defineSlots<{ default?: (state: ColorPickerRenderState) => VNode[] }>();
|
||||
const props = withDefaults(defineProps<ColorPickerProps>(), {
|
||||
as: "div",
|
||||
open: undefined,
|
||||
isOpen: undefined,
|
||||
});
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [Color];
|
||||
change: [Color];
|
||||
changeEnd: [Color];
|
||||
"update:open": [boolean];
|
||||
"update:isOpen": [boolean];
|
||||
openChange: [boolean];
|
||||
}>();
|
||||
const state = useColorState(props, {
|
||||
provide: true,
|
||||
allowEmpty: false,
|
||||
fallback: () => parseColor("#000000"),
|
||||
change: (value) => {
|
||||
if (value) {
|
||||
emit("update:modelValue", value);
|
||||
emit("change", value);
|
||||
}
|
||||
},
|
||||
changeEnd: (value) => {
|
||||
if (value) emit("changeEnd", value);
|
||||
},
|
||||
});
|
||||
provide(colorPickerKey, state);
|
||||
const native = shallowRef<HTMLInputElement>();
|
||||
const color = computed(() => state.value.value ?? parseColor("#000000"));
|
||||
const disabled = state.disabled,
|
||||
readOnly = state.readOnly;
|
||||
const serialized = computed(() => serializeColorValue(color.value));
|
||||
useChoiceForm(native, state.reset, () => {
|
||||
if (native.value) native.value.value = serialized.value;
|
||||
});
|
||||
function openChange(value: boolean) {
|
||||
emit("update:open", value);
|
||||
emit("update:isOpen", value);
|
||||
emit("openChange", value);
|
||||
}
|
||||
defineExpose({ state });
|
||||
</script>
|
||||
<template>
|
||||
<Popover
|
||||
:open="open ?? isOpen"
|
||||
:default-open="defaultOpen"
|
||||
@update:open="openChange"
|
||||
>
|
||||
<Boundary v-slot="overlay">
|
||||
<component
|
||||
:is="as"
|
||||
v-bind="$attrs"
|
||||
class="hv-color-picker"
|
||||
data-slot="color-picker"
|
||||
:data-disabled="disabled || undefined"
|
||||
:data-readonly="readOnly || undefined"
|
||||
>
|
||||
<slot
|
||||
:color="color"
|
||||
:is-open="overlay.isOpen"
|
||||
:close="overlay.close"
|
||||
:is-disabled="disabled"
|
||||
:is-read-only="readOnly"
|
||||
/>
|
||||
<input
|
||||
ref="native"
|
||||
type="hidden"
|
||||
:name="name"
|
||||
:form="form"
|
||||
:disabled="disabled"
|
||||
:value="serialized"
|
||||
/>
|
||||
</component>
|
||||
</Boundary>
|
||||
</Popover>
|
||||
</template>
|
||||
@@ -0,0 +1,23 @@
|
||||
import { defineComponent, watch } from "vue";
|
||||
import { usePopover } from "../popover/context";
|
||||
import { useColorPicker } from "./context";
|
||||
/** Only an accepted close cancels the active color transaction. */
|
||||
export default defineComponent({
|
||||
name: "ColorPickerBoundary",
|
||||
setup(_, { slots }) {
|
||||
const overlay = usePopover(),
|
||||
color = useColorPicker();
|
||||
watch(
|
||||
overlay.state.isOpen,
|
||||
(open) => {
|
||||
if (!open) color.cancel();
|
||||
},
|
||||
{ flush: "sync" },
|
||||
);
|
||||
return () =>
|
||||
slots.default?.({
|
||||
isOpen: overlay.state.isOpen.value,
|
||||
close: overlay.state.close,
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
import { useForwardExpose } from "reka-ui";
|
||||
import PopoverContent from "../popover/PopoverContent.vue";
|
||||
import type { OverlayPlacement } from "../overlay/placement";
|
||||
import { useColorPicker } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
withDefaults(
|
||||
defineProps<{
|
||||
as?: string;
|
||||
id?: string;
|
||||
placement?: OverlayPlacement;
|
||||
offset?: number;
|
||||
crossOffset?: number;
|
||||
shouldFlip?: boolean;
|
||||
containerPadding?: number;
|
||||
to?: string | HTMLElement;
|
||||
isNonModal?: boolean;
|
||||
isDismissable?: boolean;
|
||||
isKeyboardDismissDisabled?: boolean;
|
||||
}>(),
|
||||
{
|
||||
as: "div",
|
||||
placement: "bottom left",
|
||||
offset: 8,
|
||||
crossOffset: 0,
|
||||
shouldFlip: true,
|
||||
containerPadding: 12,
|
||||
isNonModal: undefined,
|
||||
isDismissable: true,
|
||||
isKeyboardDismissDisabled: false,
|
||||
},
|
||||
);
|
||||
useColorPicker();
|
||||
const { forwardRef } = useForwardExpose();
|
||||
</script>
|
||||
<template>
|
||||
<PopoverContent
|
||||
:ref="forwardRef"
|
||||
v-bind="{ ...$props, ...$attrs }"
|
||||
class="hv-color-picker__popover"
|
||||
data-slot="color-picker-popover"
|
||||
>
|
||||
<template #default="state"><slot v-bind="state" /></template>
|
||||
</PopoverContent>
|
||||
</template>
|
||||
@@ -0,0 +1,26 @@
|
||||
<script setup lang="ts">
|
||||
import { useForwardExpose } from "reka-ui";
|
||||
import PopoverTrigger from "../popover/PopoverTrigger.vue";
|
||||
import { useColorPicker } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
withDefaults(
|
||||
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
|
||||
{ as: "button" },
|
||||
);
|
||||
const color = useColorPicker(),
|
||||
disabled = color.disabled;
|
||||
const { forwardRef } = useForwardExpose();
|
||||
</script>
|
||||
<template>
|
||||
<PopoverTrigger
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:as="as"
|
||||
:as-child="asChild"
|
||||
:is-disabled="isDisabled || disabled"
|
||||
class="hv-color-picker__trigger"
|
||||
data-slot="color-picker-trigger"
|
||||
>
|
||||
<slot :color="color.value.value" :is-disabled="isDisabled || disabled" />
|
||||
</PopoverTrigger>
|
||||
</template>
|
||||
@@ -0,0 +1,32 @@
|
||||
import { inject, type InjectionKey } from "vue";
|
||||
import type { ColorState } from "../color/model";
|
||||
import type { Color, ColorValue } from "../color/value";
|
||||
export interface ColorPickerProps {
|
||||
modelValue?: ColorValue;
|
||||
value?: ColorValue;
|
||||
defaultValue?: ColorValue;
|
||||
open?: boolean;
|
||||
isOpen?: boolean;
|
||||
defaultOpen?: boolean;
|
||||
isDisabled?: boolean;
|
||||
disabled?: boolean;
|
||||
isReadOnly?: boolean;
|
||||
/** Vue extension: opt in to a single full-precision native form value. */
|
||||
name?: string;
|
||||
form?: string;
|
||||
as?: string;
|
||||
}
|
||||
export interface ColorPickerRenderState {
|
||||
color: Color;
|
||||
isOpen: boolean;
|
||||
isDisabled: boolean;
|
||||
isReadOnly: boolean;
|
||||
close: () => void;
|
||||
}
|
||||
export const colorPickerKey: InjectionKey<ColorState> =
|
||||
Symbol("hv-color-picker");
|
||||
export function useColorPicker() {
|
||||
const context = inject(colorPickerKey);
|
||||
if (!context) throw new Error("ColorPicker parts require ColorPicker.");
|
||||
return context;
|
||||
}
|
||||
@@ -43,6 +43,8 @@ export function useColorState(
|
||||
options: {
|
||||
fallback: () => Color | null;
|
||||
inherit?: boolean;
|
||||
/** Composite pickers retain their accepted color when an editable child clears. */
|
||||
allowEmpty?: boolean;
|
||||
provide?: boolean;
|
||||
change: (value: Color | null) => void;
|
||||
changeEnd?: (value: Color | null) => void;
|
||||
@@ -93,8 +95,10 @@ export function useColorState(
|
||||
const inherited = computed(
|
||||
() => !!(options.inherit && parent && !ownValue.value),
|
||||
);
|
||||
const value = computed(() =>
|
||||
copyColor(inherited.value ? parent!.value.value : model.value.value),
|
||||
const value = computed(
|
||||
() =>
|
||||
copyColor(inherited.value ? parent!.value.value : model.value.value) ??
|
||||
(options.allowEmpty === false ? options.fallback() : null),
|
||||
);
|
||||
const active = computed(() =>
|
||||
inherited.value ? parent!.active.value : source.value,
|
||||
@@ -123,7 +127,12 @@ export function useColorState(
|
||||
return true;
|
||||
},
|
||||
request(next, token) {
|
||||
if (disabled.value || readOnly.value || (token && active.value !== token))
|
||||
if (
|
||||
(next === null && options.allowEmpty === false) ||
|
||||
disabled.value ||
|
||||
readOnly.value ||
|
||||
(token && active.value !== token)
|
||||
)
|
||||
return false;
|
||||
const copied = copyColor(next);
|
||||
if (inherited.value) return parent!.request(copied, token);
|
||||
|
||||
@@ -35,7 +35,7 @@ root.triggerId = ctx.triggerId;
|
||||
:is-disabled="isDisabled"
|
||||
:activate="ctx.state.toggle"
|
||||
class="hv-popover__trigger"
|
||||
data-slot="popover-trigger"
|
||||
:data-slot="$attrs['data-slot'] ?? 'popover-trigger'"
|
||||
aria-haspopup="dialog"
|
||||
:aria-expanded="ctx.state.isOpen.value"
|
||||
:aria-controls="ctx.state.isOpen.value ? ctx.contentId.value : undefined"
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
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?.();
|
||||
},
|
||||
});
|
||||
+45
-103
@@ -1,122 +1,64 @@
|
||||
<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 type { Color } from "../../src/components/color/value";
|
||||
import Picker from "../../src/components/color-picker/ColorPicker.vue";
|
||||
import Trigger from "../../src/components/color-picker/ColorPickerTrigger.vue";
|
||||
import Panel from "../../src/components/color-picker/ColorPickerPopover.vue";
|
||||
import Area from "./ColorAreaFoundation.vue";
|
||||
import Slider from "./ColorSliderFoundation.vue";
|
||||
import ColorField from "../../src/components/color-field/ColorField.vue";
|
||||
import ColorInputGroup from "../../src/components/color-field/ColorInputGroup.vue";
|
||||
import ColorInputGroupInput from "../../src/components/color-field/ColorInputGroupInput.vue";
|
||||
const props = withDefaults(
|
||||
defineProps<
|
||||
ColorModelProps & {
|
||||
name?: string;
|
||||
form?: string;
|
||||
open?: boolean;
|
||||
defaultOpen?: boolean;
|
||||
}
|
||||
>(),
|
||||
{ open: undefined },
|
||||
);
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [Color | null];
|
||||
change: [Color | null];
|
||||
changeEnd: [Color | null];
|
||||
"update:modelValue": [Color];
|
||||
change: [Color];
|
||||
changeEnd: [Color];
|
||||
"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;
|
||||
});
|
||||
const picker = shallowRef<InstanceType<typeof Picker>>();
|
||||
const state = computed(() => picker.value?.state);
|
||||
const accepted = computed(() =>
|
||||
serializeColorValue(picker.value?.state.value.value ?? null),
|
||||
);
|
||||
defineExpose({ state });
|
||||
</script>
|
||||
<template>
|
||||
<div data-color-picker-composition>
|
||||
<Popover
|
||||
:open="open"
|
||||
:default-open="defaultOpen"
|
||||
<Picker
|
||||
ref="picker"
|
||||
default-value="hsb(270,0%,50%,.123456789)"
|
||||
v-bind="$attrs"
|
||||
v-slot="{ color }"
|
||||
@update:model-value="emit('update:modelValue', $event)"
|
||||
@change="emit('change', $event)"
|
||||
@change-end="emit('changeEnd', $event)"
|
||||
@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" />
|
||||
<ColorField channel="alpha" aria-label="Color alpha text">
|
||||
<ColorInputGroup><ColorInputGroupInput /></ColorInputGroup>
|
||||
</ColorField>
|
||||
<ColorField aria-label="Color hex text">
|
||||
<ColorInputGroup><ColorInputGroupInput /></ColorInputGroup>
|
||||
</ColorField>
|
||||
</PopoverDialog>
|
||||
</PopoverContent>
|
||||
</Boundary>
|
||||
</Popover>
|
||||
<input
|
||||
ref="native"
|
||||
type="hidden"
|
||||
:name="name"
|
||||
:form="form"
|
||||
:disabled="disabled"
|
||||
:value="serialized"
|
||||
/>
|
||||
<output data-color-picker-accepted>{{ serialized }}</output>
|
||||
<Trigger data-color-picker-trigger>
|
||||
<span
|
||||
:style="{
|
||||
width: '20px',
|
||||
height: '20px',
|
||||
borderRadius: '6px',
|
||||
background: color.toString('css'),
|
||||
}"
|
||||
/>
|
||||
选择颜色
|
||||
</Trigger>
|
||||
<Panel aria-label="选择颜色" data-color-picker-panel>
|
||||
<Area aria-label="Color area" />
|
||||
<Slider channel="hue" aria-label="Color hue" />
|
||||
<Slider channel="alpha" aria-label="Color alpha" />
|
||||
<ColorField channel="alpha" aria-label="Color alpha text">
|
||||
<ColorInputGroup><ColorInputGroupInput /></ColorInputGroup>
|
||||
</ColorField>
|
||||
<ColorField aria-label="Color hex text">
|
||||
<ColorInputGroup><ColorInputGroupInput /></ColorInputGroup>
|
||||
</ColorField>
|
||||
</Panel>
|
||||
</Picker>
|
||||
<output data-color-picker-accepted>{{ accepted }}</output>
|
||||
</div>
|
||||
</template>
|
||||
<style>
|
||||
.color-picker-composition-panel {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user