feat: establish composed color picker protocol
This commit is contained in:
@@ -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