Line data Source code
1 16 : <template>
2 1 : <div class="color-picker-wrapper flex items-center gap-2">
3 : <AppColorPicker
4 : :id="id"
5 : v-model="hexValue"
6 : :disabled="disabled"
7 : :aria-labelledby="ariaLabelledby"
8 : />
9 : <Input
10 : v-model="draftText"
11 : class="w-28"
12 : :placeholder="label"
13 : :disabled="disabled"
14 : @blur="commitDraft"
15 : @keydown.enter="commitDraft"
16 : />
17 : </div>
18 : </template>
19 :
20 : <script setup lang="ts">
21 : import { computed, ref, watch } from 'vue'
22 :
23 : import AppColorPicker from '@/components/common/AppColorPicker.vue'
24 : import Input from '@comfyorg/design-system/input/Input'
25 :
26 1 : const modelValue = defineModel<string>('modelValue')
27 : const {
28 : disabled = false,
29 : id,
30 : ariaLabelledby
31 : } = defineProps<{
32 : label?: string
33 : disabled?: boolean
34 : id?: string
35 : ariaLabelledby?: string
36 : }>()
37 :
38 : defineOptions({
39 : inheritAttrs: false
40 : })
41 :
42 1 : const hexValue = computed<string>({
43 : get: () =>
44 : modelValue.value?.startsWith('#')
45 : ? modelValue.value
46 : : `#${modelValue.value ?? '000000'}`,
47 : set: (next) => {
48 0 : modelValue.value = next.replace(/^#/, '')
49 : }
50 : })
51 :
52 : // Free-text draft so partial typing (e.g. "#f") doesn't roundtrip through
53 : // the picker and snap back to black. Only commit on blur or Enter when the
54 : // input fully parses as 6- or 8-digit hex.
55 1 : const draftText = ref(modelValue.value ?? '')
56 1 : watch(modelValue, (next) => {
57 0 : draftText.value = next ?? ''
58 : })
59 :
60 1 : const FULL_HEX = /^#?([0-9a-f]{6}|[0-9a-f]{8})$/i
61 :
62 0 : function commitDraft() {
63 0 : const raw = draftText.value.trim()
64 0 : if (raw === '') {
65 0 : draftText.value = modelValue.value ?? ''
66 0 : return
67 : }
68 0 : if (FULL_HEX.test(raw)) {
69 0 : modelValue.value = raw.replace(/^#/, '').toLowerCase()
70 : } else {
71 0 : draftText.value = modelValue.value ?? ''
72 : }
73 : }
74 : </script>
|