@teammodlr/ui-kit 2.3.9 → 2.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs.js +2 -2
- package/lib/index.es.js +142 -141
- package/package.json +2 -2
- package/src/components/colorpicker/ColorPicker.vue +455 -450
- package/src/components/colorpicker/examples/basic.vue +23 -23
- package/src/components/colorpicker/utils.ts +205 -206
|
@@ -1,450 +1,455 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div ref="wrapper" class="m-color-picker">
|
|
3
|
-
<div ref="referenceRef" @click="isOpen = !isOpen">
|
|
4
|
-
<slot :r="finalColor.r" :g="finalColor.g" :b="finalColor.b" :a="finalColor.a" :hex="hexInput"
|
|
5
|
-
:transparent="style.transparent">
|
|
6
|
-
<div>
|
|
7
|
-
<template v-if="modelValue === 'inherit'">
|
|
8
|
-
<div :style="{ border: '1px dashed #999', padding: '8px', textAlign: 'center' }">Inherit</div>
|
|
9
|
-
</template>
|
|
10
|
-
<template v-else>
|
|
11
|
-
<!-- Regular color display -->
|
|
12
|
-
<div :class="style.referenceColor"
|
|
13
|
-
:style="{ backgroundColor: `rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, ${finalColor.a})` }">
|
|
14
|
-
</div>
|
|
15
|
-
</template>
|
|
16
|
-
</div>
|
|
17
|
-
</slot>
|
|
18
|
-
</div>
|
|
19
|
-
<Teleport to="body">
|
|
20
|
-
<transition :enter-active-class="scale.enterActive" :leave-active-class="scale.leaveActive"
|
|
21
|
-
:enter-from-class="scale.enterFrom" :leave-to-class="scale.leaveTo">
|
|
22
|
-
<div :class="style.container" v-if="isOpen" ref="popper" :style="{ zIndex: zIndex }">
|
|
23
|
-
<div ref="picker" @mousedown="handleMouseDown" :class="style.colours"
|
|
24
|
-
:style="`background: rgb(${mainColor.r}, ${mainColor.g}, ${mainColor.b});`">
|
|
25
|
-
<div :class="style.whiteGradient"></div>
|
|
26
|
-
<div :class="style.blackGradient"></div>
|
|
27
|
-
<div :class="style.sliderThumb" @mousedown="handleMouseDown" @dragover.prevent=""
|
|
28
|
-
:style="{ left: `${position.x * 100}%`, top: `${position.y * 100}%` }"></div>
|
|
29
|
-
</div>
|
|
30
|
-
|
|
31
|
-
<div :class="style.colorActions">
|
|
32
|
-
<template v-if="canPickColor">
|
|
33
|
-
<MButton icon :disabled="!canPickColor" :class="style.eyeDrop" @click="openEyeDropper">
|
|
34
|
-
<svg width="18" height="18" viewBox="0 0 24 24" fill="none"
|
|
35
|
-
xmlns="http://www.w3.org/2000/svg">
|
|
36
|
-
<path
|
|
37
|
-
d="M20.3847 2.87868C19.2132 1.70711 17.3137 1.70711 16.1421 2.87868L14.0202 5.00052L13.313 4.29332C12.9225 3.9028 12.2894 3.9028 11.8988 4.29332C11.5083 4.68385 11.5083 5.31701 11.8988 5.70754L17.5557 11.3644C17.9462 11.7549 18.5794 11.7549 18.9699 11.3644C19.3604 10.9739 19.3604 10.3407 18.9699 9.95018L18.2629 9.24316L20.3847 7.12132C21.5563 5.94975 21.5563 4.05025 20.3847 2.87868Z"
|
|
38
|
-
fill="currentColor" />
|
|
39
|
-
<path fill-rule="evenodd" clip-rule="evenodd"
|
|
40
|
-
d="M11.9297 7.09116L4.1515 14.8693C3.22786 15.793 3.03239 17.169 3.5651 18.2842L1.99994 19.8493L3.41415 21.2635L4.97931 19.6984C6.09444 20.2311 7.4705 20.0356 8.39414 19.112L16.1723 11.3338L11.9297 7.09116ZM13.3439 11.3338L11.9297 9.91959L5.56571 16.2835C5.17518 16.6741 5.17518 17.3072 5.56571 17.6978C5.95623 18.0883 6.5894 18.0883 6.97992 17.6978L13.3439 11.3338Z"
|
|
41
|
-
fill="currentColor" />
|
|
42
|
-
</svg>
|
|
43
|
-
</MButton>
|
|
44
|
-
</template>
|
|
45
|
-
<template v-else>
|
|
46
|
-
<div :class="style.preview">
|
|
47
|
-
<div
|
|
48
|
-
:style="`width: 100%; height: 100%; background-color: rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, ${finalColor.a});`">
|
|
49
|
-
</div>
|
|
50
|
-
</div>
|
|
51
|
-
</template>
|
|
52
|
-
<div style="width: 100%; display: flex; flex-direction: column; gap: 10px;">
|
|
53
|
-
<MSlider v-model="hueSlider" @change="onSliderChangeComplete"
|
|
54
|
-
background="linear-gradient(to right, #FF0000, #FFFF00, #00FF00, #00FFFF, #0000FF, #FF00FF, #FF0000)">
|
|
55
|
-
</MSlider>
|
|
56
|
-
|
|
57
|
-
<MSlider v-model="opacitySlider" @change="onSliderChangeComplete"
|
|
58
|
-
:background="`linear-gradient(to right, rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, 0), rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, 1))`">
|
|
59
|
-
</MSlider>
|
|
60
|
-
</div>
|
|
61
|
-
</div>
|
|
62
|
-
|
|
63
|
-
<div :class="style.colorActions">
|
|
64
|
-
<template v-if="rgbOrHex">
|
|
65
|
-
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="rInputElement"
|
|
66
|
-
v-model="rInput" name="r" placeholder="R" />
|
|
67
|
-
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="gInputElement"
|
|
68
|
-
v-model="gInput" name="g" placeholder="G" />
|
|
69
|
-
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="bInputElement"
|
|
70
|
-
v-model="bInput" name="b" placeholder="B" />
|
|
71
|
-
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="aInputElement"
|
|
72
|
-
v-model="aInput" name="a" placeholder="A" />
|
|
73
|
-
</template>
|
|
74
|
-
<template v-else>
|
|
75
|
-
<MInput @keyup.enter="commitHexInput" @blur="commitHexInput" v-model="hexInputUser" block
|
|
76
|
-
ref="hexInputElement" name="hex" placeholder="#00000000" />
|
|
77
|
-
</template>
|
|
78
|
-
|
|
79
|
-
<MButton icon @click.prevent="rgbOrHex = !rgbOrHex">
|
|
80
|
-
|
|
81
|
-
<svg style="width: 18px; height: 18px;" xmlns="http://www.w3.org/2000/svg"
|
|
82
|
-
viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5">
|
|
83
|
-
<path fill-rule="evenodd"
|
|
84
|
-
d="M10 3a.75.75 0 01.55.24l3.25 3.5a.75.75 0 11-1.1 1.02L10 4.852 7.3 7.76a.75.75 0 01-1.1-1.02l3.25-3.5A.75.75 0 0110 3zm-3.76 9.2a.75.75 0 011.06.04l2.7 2.908 2.7-2.908a.75.75 0 111.1 1.02l-3.25 3.5a.75.75 0 01-1.1 0l-3.25-3.5a.75.75 0 01.04-1.06z"
|
|
85
|
-
clip-rule="evenodd" />
|
|
86
|
-
</svg>
|
|
87
|
-
|
|
88
|
-
</MButton>
|
|
89
|
-
<MButton icon @click.prevent="toggleInherit">
|
|
90
|
-
<svg style="width: 18px; height: 18px;" xmlns="http://www.w3.org/2000/svg" width="24"
|
|
91
|
-
height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
|
|
92
|
-
stroke-linecap="round" stroke-linejoin="round">
|
|
93
|
-
<path d="M21 6H3" />
|
|
94
|
-
<path d="M7 12H3" />
|
|
95
|
-
<path d="M7 18H3" />
|
|
96
|
-
<path d="M12 18a5 5 0 0 0 9-3 4.5 4.5 0 0 0-4.5-4.5c-1.33 0-2.54.54-3.41 1.41L11 14" />
|
|
97
|
-
<path d="M11 10v4h4" />
|
|
98
|
-
</svg>
|
|
99
|
-
</MButton>
|
|
100
|
-
</div>
|
|
101
|
-
</div>
|
|
102
|
-
</transition>
|
|
103
|
-
</Teleport>
|
|
104
|
-
</div>
|
|
105
|
-
</template>
|
|
106
|
-
|
|
107
|
-
<script lang="ts">
|
|
108
|
-
import { onClickOutside, useEyeDropper, useMouseInElement, useMousePressed } from '@vueuse/core';
|
|
109
|
-
import { computed, defineComponent, onMounted, type PropType, reactive, ref, toRef, watch } from "vue";
|
|
110
|
-
import * as style from './style';
|
|
111
|
-
import { scale } from "../../styles/transitions.css";
|
|
112
|
-
import { hexToRGB, type HSV, HSVToRGBA, HueToRGBA, RGBAToHex, RGBToHSV } from './utils';
|
|
113
|
-
import { usePopper } from "../../composables/usePopper";
|
|
114
|
-
import type { Placement } from "@popperjs/core";
|
|
115
|
-
import { useZIndex } from "../../composables/useZIndex";
|
|
116
|
-
import MSlider from "../../components/slider/Slider.vue";
|
|
117
|
-
import MButton from "../../components/button/Button.vue";
|
|
118
|
-
import MInput from "../../components/input/Input.vue";
|
|
119
|
-
|
|
120
|
-
export default defineComponent({
|
|
121
|
-
name: "MColorPicker",
|
|
122
|
-
props: {
|
|
123
|
-
modelValue: {
|
|
124
|
-
type: String,
|
|
125
|
-
default: "#FFFFFF"
|
|
126
|
-
},
|
|
127
|
-
modelModifiers: {
|
|
128
|
-
type: Object,
|
|
129
|
-
default: () => ({})
|
|
130
|
-
},
|
|
131
|
-
transfer: {
|
|
132
|
-
type: Boolean,
|
|
133
|
-
default: false
|
|
134
|
-
},
|
|
135
|
-
placement: {
|
|
136
|
-
type: String as PropType<Placement>,
|
|
137
|
-
default: "bottom-start"
|
|
138
|
-
},
|
|
139
|
-
zIndex: {
|
|
140
|
-
type: Number
|
|
141
|
-
},
|
|
142
|
-
defaultColor: {
|
|
143
|
-
type: String,
|
|
144
|
-
default: "#FFFFFF"
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
|
-
emits: ['update:modelValue'],
|
|
148
|
-
components: {
|
|
149
|
-
MSlider,
|
|
150
|
-
MInput,
|
|
151
|
-
MButton
|
|
152
|
-
},
|
|
153
|
-
setup(props, { emit }) {
|
|
154
|
-
const modelModifiers = props.modelModifiers;
|
|
155
|
-
const { isSupported: canPickColor, open: openEyeDrop, sRGBHex: eyeDropColor } = useEyeDropper();
|
|
156
|
-
const { nextZIndex } = useZIndex();
|
|
157
|
-
const isOpen = ref(false);
|
|
158
|
-
const rgbOrHex = ref<boolean>(false);
|
|
159
|
-
const hexInput = ref("");
|
|
160
|
-
const rInput = ref("");
|
|
161
|
-
const gInput = ref("");
|
|
162
|
-
const bInput = ref("");
|
|
163
|
-
const aInput = ref("");
|
|
164
|
-
const internalValue = ref(props.modelValue);
|
|
165
|
-
|
|
166
|
-
const picker = ref<HTMLElement>();
|
|
167
|
-
const wrapper = ref<HTMLElement>();
|
|
168
|
-
const referenceRef = ref<HTMLElement>();
|
|
169
|
-
const transfer = toRef(props, 'transfer');
|
|
170
|
-
const placement = toRef(props, 'placement');
|
|
171
|
-
const hexInputElement = ref();
|
|
172
|
-
const hueSlider = ref(0);
|
|
173
|
-
const opacitySlider = ref(100);
|
|
174
|
-
const dragging = ref(false);
|
|
175
|
-
const position = reactive({
|
|
176
|
-
x: 0,
|
|
177
|
-
y: 0
|
|
178
|
-
});
|
|
179
|
-
const zIndex = ref(props.zIndex ?? nextZIndex());
|
|
180
|
-
|
|
181
|
-
const { elementX, elementY } = useMouseInElement(picker);
|
|
182
|
-
const { pressed } = useMousePressed();
|
|
183
|
-
const { popper, transferTo, updatePopper } = usePopper({
|
|
184
|
-
reference: referenceRef,
|
|
185
|
-
placement,
|
|
186
|
-
transfer,
|
|
187
|
-
wrapper,
|
|
188
|
-
isDrop: false,
|
|
189
|
-
offset: [-2, 2]
|
|
190
|
-
});
|
|
191
|
-
const brightness = computed(() => {
|
|
192
|
-
return 1 - position.y;
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
const saturation = computed(() => {
|
|
196
|
-
return position.x;
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
const finalColor = computed(() => {
|
|
200
|
-
if (hexInput.value === "inherit") {
|
|
201
|
-
return { r: 0, g: 0, b: 0, a: 0 };
|
|
202
|
-
}
|
|
203
|
-
return HSVToRGBA(hueSlider.value * 3.6, brightness.value, saturation.value, opacitySlider.value / 100);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
const mainColor = computed(() => {
|
|
207
|
-
return HueToRGBA(hueSlider.value * 3.6, opacitySlider.value / 100);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
const emitModelValue = () => {
|
|
211
|
-
emit('update:modelValue', hexInput.value);
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
const setInputs = () => {
|
|
215
|
-
if (hexInput.value !== 'inherit') {
|
|
216
|
-
hexInput.value = RGBAToHex(finalColor.value.r, finalColor.value.g, finalColor.value.b, finalColor.value.a);
|
|
217
|
-
}
|
|
218
|
-
rInput.value = finalColor.value.r.toString();
|
|
219
|
-
gInput.value = finalColor.value.g.toString();
|
|
220
|
-
bInput.value = finalColor.value.b.toString();
|
|
221
|
-
aInput.value = finalColor.value.a.toFixed(2).toString();
|
|
222
|
-
|
|
223
|
-
if (!modelModifiers.lazy) {
|
|
224
|
-
emitModelValue();
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
watch(() => finalColor.value, () => {
|
|
229
|
-
setInputs();
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
const updateFromHSV = (hsv: HSV, alpha: number) => {
|
|
233
|
-
position.x = hsv.s;
|
|
234
|
-
position.y = 1 - hsv.v;
|
|
235
|
-
hueSlider.value = ((1 / 360) * hsv.h) * 100;
|
|
236
|
-
opacitySlider.value = alpha * 100;
|
|
237
|
-
setInputs();
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const updateFromHex = () => {
|
|
241
|
-
if (hexInput.value === 'inherit') {
|
|
242
|
-
if (modelModifiers.lazy) {
|
|
243
|
-
emitModelValue();
|
|
244
|
-
}
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
let hex = hexInput.value.replace("#", "");
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
position.
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
1
|
+
<template>
|
|
2
|
+
<div ref="wrapper" class="m-color-picker">
|
|
3
|
+
<div ref="referenceRef" @click="isOpen = !isOpen">
|
|
4
|
+
<slot :r="finalColor.r" :g="finalColor.g" :b="finalColor.b" :a="finalColor.a" :hex="hexInput"
|
|
5
|
+
:transparent="style.transparent">
|
|
6
|
+
<div>
|
|
7
|
+
<template v-if="modelValue === 'inherit'">
|
|
8
|
+
<div :style="{ border: '1px dashed #999', padding: '8px', textAlign: 'center' }">Inherit</div>
|
|
9
|
+
</template>
|
|
10
|
+
<template v-else>
|
|
11
|
+
<!-- Regular color display -->
|
|
12
|
+
<div :class="style.referenceColor"
|
|
13
|
+
:style="{ backgroundColor: `rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, ${finalColor.a})` }">
|
|
14
|
+
</div>
|
|
15
|
+
</template>
|
|
16
|
+
</div>
|
|
17
|
+
</slot>
|
|
18
|
+
</div>
|
|
19
|
+
<Teleport to="body">
|
|
20
|
+
<transition :enter-active-class="scale.enterActive" :leave-active-class="scale.leaveActive"
|
|
21
|
+
:enter-from-class="scale.enterFrom" :leave-to-class="scale.leaveTo">
|
|
22
|
+
<div :class="style.container" v-if="isOpen" ref="popper" :style="{ zIndex: zIndex }">
|
|
23
|
+
<div ref="picker" @mousedown="handleMouseDown" :class="style.colours"
|
|
24
|
+
:style="`background: rgb(${mainColor.r}, ${mainColor.g}, ${mainColor.b});`">
|
|
25
|
+
<div :class="style.whiteGradient"></div>
|
|
26
|
+
<div :class="style.blackGradient"></div>
|
|
27
|
+
<div :class="style.sliderThumb" @mousedown="handleMouseDown" @dragover.prevent=""
|
|
28
|
+
:style="{ left: `${position.x * 100}%`, top: `${position.y * 100}%` }"></div>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
<div :class="style.colorActions">
|
|
32
|
+
<template v-if="canPickColor">
|
|
33
|
+
<MButton icon :disabled="!canPickColor" :class="style.eyeDrop" @click="openEyeDropper">
|
|
34
|
+
<svg width="18" height="18" viewBox="0 0 24 24" fill="none"
|
|
35
|
+
xmlns="http://www.w3.org/2000/svg">
|
|
36
|
+
<path
|
|
37
|
+
d="M20.3847 2.87868C19.2132 1.70711 17.3137 1.70711 16.1421 2.87868L14.0202 5.00052L13.313 4.29332C12.9225 3.9028 12.2894 3.9028 11.8988 4.29332C11.5083 4.68385 11.5083 5.31701 11.8988 5.70754L17.5557 11.3644C17.9462 11.7549 18.5794 11.7549 18.9699 11.3644C19.3604 10.9739 19.3604 10.3407 18.9699 9.95018L18.2629 9.24316L20.3847 7.12132C21.5563 5.94975 21.5563 4.05025 20.3847 2.87868Z"
|
|
38
|
+
fill="currentColor" />
|
|
39
|
+
<path fill-rule="evenodd" clip-rule="evenodd"
|
|
40
|
+
d="M11.9297 7.09116L4.1515 14.8693C3.22786 15.793 3.03239 17.169 3.5651 18.2842L1.99994 19.8493L3.41415 21.2635L4.97931 19.6984C6.09444 20.2311 7.4705 20.0356 8.39414 19.112L16.1723 11.3338L11.9297 7.09116ZM13.3439 11.3338L11.9297 9.91959L5.56571 16.2835C5.17518 16.6741 5.17518 17.3072 5.56571 17.6978C5.95623 18.0883 6.5894 18.0883 6.97992 17.6978L13.3439 11.3338Z"
|
|
41
|
+
fill="currentColor" />
|
|
42
|
+
</svg>
|
|
43
|
+
</MButton>
|
|
44
|
+
</template>
|
|
45
|
+
<template v-else>
|
|
46
|
+
<div :class="style.preview">
|
|
47
|
+
<div
|
|
48
|
+
:style="`width: 100%; height: 100%; background-color: rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, ${finalColor.a});`">
|
|
49
|
+
</div>
|
|
50
|
+
</div>
|
|
51
|
+
</template>
|
|
52
|
+
<div style="width: 100%; display: flex; flex-direction: column; gap: 10px;">
|
|
53
|
+
<MSlider v-model="hueSlider" @change="onSliderChangeComplete"
|
|
54
|
+
background="linear-gradient(to right, #FF0000, #FFFF00, #00FF00, #00FFFF, #0000FF, #FF00FF, #FF0000)">
|
|
55
|
+
</MSlider>
|
|
56
|
+
|
|
57
|
+
<MSlider v-model="opacitySlider" @change="onSliderChangeComplete"
|
|
58
|
+
:background="`linear-gradient(to right, rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, 0), rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, 1))`">
|
|
59
|
+
</MSlider>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
<div :class="style.colorActions">
|
|
64
|
+
<template v-if="rgbOrHex">
|
|
65
|
+
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="rInputElement"
|
|
66
|
+
v-model="rInput" name="r" placeholder="R" />
|
|
67
|
+
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="gInputElement"
|
|
68
|
+
v-model="gInput" name="g" placeholder="G" />
|
|
69
|
+
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="bInputElement"
|
|
70
|
+
v-model="bInput" name="b" placeholder="B" />
|
|
71
|
+
<MInput @keyup.enter="updateFromRGB" @blur="updateFromRGB" block ref="aInputElement"
|
|
72
|
+
v-model="aInput" name="a" placeholder="A" />
|
|
73
|
+
</template>
|
|
74
|
+
<template v-else>
|
|
75
|
+
<MInput @keyup.enter="commitHexInput" @blur="commitHexInput" v-model="hexInputUser" block
|
|
76
|
+
ref="hexInputElement" name="hex" placeholder="#00000000" />
|
|
77
|
+
</template>
|
|
78
|
+
|
|
79
|
+
<MButton icon @click.prevent="rgbOrHex = !rgbOrHex">
|
|
80
|
+
|
|
81
|
+
<svg style="width: 18px; height: 18px;" xmlns="http://www.w3.org/2000/svg"
|
|
82
|
+
viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5">
|
|
83
|
+
<path fill-rule="evenodd"
|
|
84
|
+
d="M10 3a.75.75 0 01.55.24l3.25 3.5a.75.75 0 11-1.1 1.02L10 4.852 7.3 7.76a.75.75 0 01-1.1-1.02l3.25-3.5A.75.75 0 0110 3zm-3.76 9.2a.75.75 0 011.06.04l2.7 2.908 2.7-2.908a.75.75 0 111.1 1.02l-3.25 3.5a.75.75 0 01-1.1 0l-3.25-3.5a.75.75 0 01.04-1.06z"
|
|
85
|
+
clip-rule="evenodd" />
|
|
86
|
+
</svg>
|
|
87
|
+
|
|
88
|
+
</MButton>
|
|
89
|
+
<MButton icon @click.prevent="toggleInherit">
|
|
90
|
+
<svg style="width: 18px; height: 18px;" xmlns="http://www.w3.org/2000/svg" width="24"
|
|
91
|
+
height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
|
|
92
|
+
stroke-linecap="round" stroke-linejoin="round">
|
|
93
|
+
<path d="M21 6H3" />
|
|
94
|
+
<path d="M7 12H3" />
|
|
95
|
+
<path d="M7 18H3" />
|
|
96
|
+
<path d="M12 18a5 5 0 0 0 9-3 4.5 4.5 0 0 0-4.5-4.5c-1.33 0-2.54.54-3.41 1.41L11 14" />
|
|
97
|
+
<path d="M11 10v4h4" />
|
|
98
|
+
</svg>
|
|
99
|
+
</MButton>
|
|
100
|
+
</div>
|
|
101
|
+
</div>
|
|
102
|
+
</transition>
|
|
103
|
+
</Teleport>
|
|
104
|
+
</div>
|
|
105
|
+
</template>
|
|
106
|
+
|
|
107
|
+
<script lang="ts">
|
|
108
|
+
import { onClickOutside, useEyeDropper, useMouseInElement, useMousePressed } from '@vueuse/core';
|
|
109
|
+
import { computed, defineComponent, onMounted, type PropType, reactive, ref, toRef, watch } from "vue";
|
|
110
|
+
import * as style from './style';
|
|
111
|
+
import { scale } from "../../styles/transitions.css";
|
|
112
|
+
import { hexToRGB, type HSV, HSVToRGBA, HueToRGBA, RGBAToHex, RGBToHSV } from './utils';
|
|
113
|
+
import { usePopper } from "../../composables/usePopper";
|
|
114
|
+
import type { Placement } from "@popperjs/core";
|
|
115
|
+
import { useZIndex } from "../../composables/useZIndex";
|
|
116
|
+
import MSlider from "../../components/slider/Slider.vue";
|
|
117
|
+
import MButton from "../../components/button/Button.vue";
|
|
118
|
+
import MInput from "../../components/input/Input.vue";
|
|
119
|
+
|
|
120
|
+
export default defineComponent({
|
|
121
|
+
name: "MColorPicker",
|
|
122
|
+
props: {
|
|
123
|
+
modelValue: {
|
|
124
|
+
type: String,
|
|
125
|
+
default: "#FFFFFF"
|
|
126
|
+
},
|
|
127
|
+
modelModifiers: {
|
|
128
|
+
type: Object,
|
|
129
|
+
default: () => ({})
|
|
130
|
+
},
|
|
131
|
+
transfer: {
|
|
132
|
+
type: Boolean,
|
|
133
|
+
default: false
|
|
134
|
+
},
|
|
135
|
+
placement: {
|
|
136
|
+
type: String as PropType<Placement>,
|
|
137
|
+
default: "bottom-start"
|
|
138
|
+
},
|
|
139
|
+
zIndex: {
|
|
140
|
+
type: Number
|
|
141
|
+
},
|
|
142
|
+
defaultColor: {
|
|
143
|
+
type: String,
|
|
144
|
+
default: "#FFFFFF"
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
emits: ['update:modelValue'],
|
|
148
|
+
components: {
|
|
149
|
+
MSlider,
|
|
150
|
+
MInput,
|
|
151
|
+
MButton
|
|
152
|
+
},
|
|
153
|
+
setup(props, { emit }) {
|
|
154
|
+
const modelModifiers = props.modelModifiers;
|
|
155
|
+
const { isSupported: canPickColor, open: openEyeDrop, sRGBHex: eyeDropColor } = useEyeDropper();
|
|
156
|
+
const { nextZIndex } = useZIndex();
|
|
157
|
+
const isOpen = ref(false);
|
|
158
|
+
const rgbOrHex = ref<boolean>(false);
|
|
159
|
+
const hexInput = ref("");
|
|
160
|
+
const rInput = ref("");
|
|
161
|
+
const gInput = ref("");
|
|
162
|
+
const bInput = ref("");
|
|
163
|
+
const aInput = ref("");
|
|
164
|
+
const internalValue = ref(props.modelValue);
|
|
165
|
+
|
|
166
|
+
const picker = ref<HTMLElement>();
|
|
167
|
+
const wrapper = ref<HTMLElement>();
|
|
168
|
+
const referenceRef = ref<HTMLElement>();
|
|
169
|
+
const transfer = toRef(props, 'transfer');
|
|
170
|
+
const placement = toRef(props, 'placement');
|
|
171
|
+
const hexInputElement = ref();
|
|
172
|
+
const hueSlider = ref(0);
|
|
173
|
+
const opacitySlider = ref(100);
|
|
174
|
+
const dragging = ref(false);
|
|
175
|
+
const position = reactive({
|
|
176
|
+
x: 0,
|
|
177
|
+
y: 0
|
|
178
|
+
});
|
|
179
|
+
const zIndex = ref(props.zIndex ?? nextZIndex());
|
|
180
|
+
|
|
181
|
+
const { elementX, elementY } = useMouseInElement(picker);
|
|
182
|
+
const { pressed } = useMousePressed();
|
|
183
|
+
const { popper, transferTo, updatePopper } = usePopper({
|
|
184
|
+
reference: referenceRef,
|
|
185
|
+
placement,
|
|
186
|
+
transfer,
|
|
187
|
+
wrapper,
|
|
188
|
+
isDrop: false,
|
|
189
|
+
offset: [-2, 2]
|
|
190
|
+
});
|
|
191
|
+
const brightness = computed(() => {
|
|
192
|
+
return 1 - position.y;
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
const saturation = computed(() => {
|
|
196
|
+
return position.x;
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
const finalColor = computed(() => {
|
|
200
|
+
if (hexInput.value === "inherit") {
|
|
201
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
202
|
+
}
|
|
203
|
+
return HSVToRGBA(hueSlider.value * 3.6, brightness.value, saturation.value, opacitySlider.value / 100);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const mainColor = computed(() => {
|
|
207
|
+
return HueToRGBA(hueSlider.value * 3.6, opacitySlider.value / 100);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const emitModelValue = () => {
|
|
211
|
+
emit('update:modelValue', hexInput.value);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const setInputs = () => {
|
|
215
|
+
if (hexInput.value !== 'inherit') {
|
|
216
|
+
hexInput.value = RGBAToHex(finalColor.value.r, finalColor.value.g, finalColor.value.b, finalColor.value.a);
|
|
217
|
+
}
|
|
218
|
+
rInput.value = finalColor.value.r.toString();
|
|
219
|
+
gInput.value = finalColor.value.g.toString();
|
|
220
|
+
bInput.value = finalColor.value.b.toString();
|
|
221
|
+
aInput.value = finalColor.value.a.toFixed(2).toString();
|
|
222
|
+
|
|
223
|
+
if (!modelModifiers.lazy) {
|
|
224
|
+
emitModelValue();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
watch(() => finalColor.value, () => {
|
|
229
|
+
setInputs();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const updateFromHSV = (hsv: HSV, alpha: number) => {
|
|
233
|
+
position.x = hsv.s;
|
|
234
|
+
position.y = 1 - hsv.v;
|
|
235
|
+
hueSlider.value = ((1 / 360) * hsv.h) * 100;
|
|
236
|
+
opacitySlider.value = alpha * 100;
|
|
237
|
+
setInputs();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const updateFromHex = () => {
|
|
241
|
+
if (hexInput.value === 'inherit') {
|
|
242
|
+
if (modelModifiers.lazy) {
|
|
243
|
+
emitModelValue();
|
|
244
|
+
}
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let hex = hexInput.value.replace("#", "");
|
|
249
|
+
debugger;
|
|
250
|
+
if (hex.length === 3) {
|
|
251
|
+
hex = hex.split("").map(char => char + char).join("");
|
|
252
|
+
}
|
|
253
|
+
const color = hexToRGB(`#${hex}`);
|
|
254
|
+
if (color.a === 0) {
|
|
255
|
+
color.a = 0;
|
|
256
|
+
opacitySlider.value = 0;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
updateFromHSV(RGBToHSV(color.r, color.g, color.b), color.a);
|
|
260
|
+
if (modelModifiers.lazy) {
|
|
261
|
+
emitModelValue();
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
const toggleInherit = () => {
|
|
267
|
+
if (props.modelValue === "inherit") {
|
|
268
|
+
hexInput.value = "#FFFFFF";
|
|
269
|
+
} else {
|
|
270
|
+
hexInput.value = "inherit";
|
|
271
|
+
}
|
|
272
|
+
emitModelValue();
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const updateFromRGB = () => {
|
|
276
|
+
let red = parseInt(rInput.value);
|
|
277
|
+
if (isNaN(red) || red > 255 || red < 0) {
|
|
278
|
+
red = finalColor.value.r;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
let green = parseInt(gInput.value);
|
|
282
|
+
if (isNaN(green) || green > 255 || green < 0) {
|
|
283
|
+
green = finalColor.value.g;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
let blue = parseInt(bInput.value);
|
|
287
|
+
if (isNaN(blue) || blue > 255 || blue < 0) {
|
|
288
|
+
blue = finalColor.value.b;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let alpha = parseFloat(aInput.value);
|
|
292
|
+
if (isNaN(alpha) || alpha > 1 || alpha < 0) {
|
|
293
|
+
alpha = finalColor.value.a;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
updateFromHSV(RGBToHSV(red, green, blue), alpha);
|
|
297
|
+
|
|
298
|
+
if (hexInput.value === 'inherit') {
|
|
299
|
+
hexInput.value = RGBAToHex(red, green, blue, alpha);
|
|
300
|
+
emitModelValue();
|
|
301
|
+
} else if (modelModifiers.lazy) {
|
|
302
|
+
emitModelValue();
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
watch(() => pressed.value, () => {
|
|
307
|
+
if (!pressed.value) {
|
|
308
|
+
dragging.value = false;
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
watch(() => dragging.value, (newVal, oldVal) => {
|
|
313
|
+
if (oldVal && !newVal && modelModifiers.lazy) {
|
|
314
|
+
emitModelValue();
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
watch(() => [elementX.value, elementY.value], () => {
|
|
319
|
+
if (!dragging.value) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
setThumbPosition();
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
watch(() => props.modelValue, () => {
|
|
327
|
+
let val = props.modelValue;
|
|
328
|
+
if (!val || val.trim() === "") {
|
|
329
|
+
val = props.defaultColor ?? "#FFFFFF";
|
|
330
|
+
}
|
|
331
|
+
hexInput.value = val;
|
|
332
|
+
updateFromHex();
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
const setThumbPosition = () => {
|
|
336
|
+
position.x = ((Math.min(Math.max(elementX.value, 0), picker.value?.clientWidth || 0) / (picker.value?.clientWidth || 0)));
|
|
337
|
+
position.y = ((Math.min(Math.max(elementY.value, 0), picker.value?.clientHeight || 0) / (picker.value?.clientHeight || 0)));
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const handleMouseDown = () => {
|
|
341
|
+
if (hexInput.value === 'inherit') {
|
|
342
|
+
hexInput.value = RGBAToHex(finalColor.value.r, finalColor.value.g, finalColor.value.b, finalColor.value.a);
|
|
343
|
+
emitModelValue();
|
|
344
|
+
}
|
|
345
|
+
dragging.value = true;
|
|
346
|
+
setThumbPosition();
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
onMounted(() => {
|
|
350
|
+
const match = props.modelValue.match(/rgba?\s*\(\s*(\d{1,3})\s*[,\s]\s*(\d{1,3})\s*[,\s]\s*(\d{1,3})\s*(?:[,\s]\s*([\d.]+%?))?\s*\)/i);
|
|
351
|
+
|
|
352
|
+
if (match) {
|
|
353
|
+
const [_, r, g, b, a] = match;
|
|
354
|
+
const alpha = a !== undefined
|
|
355
|
+
? a.endsWith('%')
|
|
356
|
+
? parseFloat(a) / 100
|
|
357
|
+
: parseFloat(a)
|
|
358
|
+
: 1.0;
|
|
359
|
+
|
|
360
|
+
hexInput.value = RGBAToHex(parseInt(r), parseInt(g), parseInt(b), alpha);
|
|
361
|
+
|
|
362
|
+
} else {
|
|
363
|
+
hexInput.value = props.modelValue;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
updateFromHex();
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
onClickOutside(popper, (e) => {
|
|
371
|
+
isOpen.value = false;
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
watch(() => eyeDropColor.value, () => {
|
|
376
|
+
if (eyeDropColor.value) {
|
|
377
|
+
hexInput.value = eyeDropColor.value;
|
|
378
|
+
emitModelValue();
|
|
379
|
+
updateFromHex();
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
const openEyeDropper = () => {
|
|
384
|
+
try {
|
|
385
|
+
openEyeDrop();
|
|
386
|
+
} catch (e) {
|
|
387
|
+
console.log(e);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const onSliderChangeComplete = () => {
|
|
392
|
+
if (modelModifiers.lazy) {
|
|
393
|
+
emitModelValue();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const hexInputUser = ref(hexInput.value);
|
|
398
|
+
|
|
399
|
+
watch(hexInput, (newVal) => {
|
|
400
|
+
if (document.activeElement !== hexInputElement.value?.$el?.querySelector('input')) {
|
|
401
|
+
hexInputUser.value = newVal;
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const commitHexInput = () => {
|
|
406
|
+
if (hexInputUser.value.trim() === '') {
|
|
407
|
+
hexInputUser.value = hexInput.value;
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
hexInput.value = hexInputUser.value;
|
|
412
|
+
updateFromHex();
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
return {
|
|
417
|
+
isOpen,
|
|
418
|
+
popper,
|
|
419
|
+
transferTo,
|
|
420
|
+
wrapper,
|
|
421
|
+
referenceRef,
|
|
422
|
+
updateFromRGB,
|
|
423
|
+
updateFromHex,
|
|
424
|
+
setInputs,
|
|
425
|
+
rgbOrHex,
|
|
426
|
+
hexInputElement,
|
|
427
|
+
hexInput,
|
|
428
|
+
rInput,
|
|
429
|
+
gInput,
|
|
430
|
+
bInput,
|
|
431
|
+
aInput,
|
|
432
|
+
finalColor,
|
|
433
|
+
hueSlider,
|
|
434
|
+
opacitySlider,
|
|
435
|
+
dragging,
|
|
436
|
+
setThumbPosition,
|
|
437
|
+
position,
|
|
438
|
+
elementX,
|
|
439
|
+
pressed,
|
|
440
|
+
handleMouseDown,
|
|
441
|
+
picker,
|
|
442
|
+
style,
|
|
443
|
+
mainColor,
|
|
444
|
+
zIndex,
|
|
445
|
+
canPickColor,
|
|
446
|
+
openEyeDropper,
|
|
447
|
+
scale,
|
|
448
|
+
onSliderChangeComplete,
|
|
449
|
+
hexInputUser,
|
|
450
|
+
toggleInherit,
|
|
451
|
+
commitHexInput
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
})
|
|
455
|
+
</script>
|