@m3ui-vue/m3ui-vue 0.9.1 → 0.9.3
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/README.md +43 -0
- package/dist/API.md +266 -60
- package/dist/{MIcon-CaEooCmZ.js → MIcon-DLOkQduI.js} +10 -3
- package/dist/MIcon-DLOkQduI.js.map +1 -0
- package/dist/{MTextField-pJU1vFaR.js → MTextField-DKtgPymd.js} +422 -206
- package/dist/MTextField-DKtgPymd.js.map +1 -0
- package/dist/THIRD_PARTY_LICENSES.md +547 -0
- package/dist/api.json +1 -1
- package/dist/code-editor.js +2 -2
- package/dist/components/MBox.vue.d.ts +3 -3
- package/dist/components/MButton.vue.d.ts +4 -2
- package/dist/components/MButtonGroup.vue.d.ts +2 -0
- package/dist/components/MColorPicker.vue.d.ts +13 -1
- package/dist/components/MColorPickerModal.vue.d.ts +14 -2
- package/dist/components/MDragHandle.vue.d.ts +12 -0
- package/dist/components/MEmojiSelector.vue.d.ts +1 -1
- package/dist/components/MFab.vue.d.ts +5 -3
- package/dist/components/MFabMenu.vue.d.ts +1 -1
- package/dist/components/MIcon.vue.d.ts +3 -0
- package/dist/components/MIconButton.vue.d.ts +15 -1
- package/dist/components/MListItem.vue.d.ts +5 -5
- package/dist/components/MMenuItem.vue.d.ts +17 -4
- package/dist/components/MOverlay.vue.d.ts +1 -1
- package/dist/components/MScrollField.vue.d.ts +40 -0
- package/dist/components/MScrollable.vue.d.ts +9 -0
- package/dist/components/MSplitButton.vue.d.ts +2 -0
- package/dist/components/MSplitter.vue.d.ts +2 -2
- package/dist/components/MSwipeToDismiss.vue.d.ts +49 -0
- package/dist/components/MTabs.vue.d.ts +6 -0
- package/dist/components/MTimePickerModal.vue.d.ts +3 -0
- package/dist/components/MTimeScroll.vue.d.ts +22 -0
- package/dist/components/MToggleButton.vue.d.ts +2 -0
- package/dist/components/MTransition.vue.d.ts +34 -0
- package/dist/components/MTree.vue.d.ts +2 -2
- package/dist/components/MWindow.vue.d.ts +1 -1
- package/dist/components/_MColorPanel.vue.d.ts +18 -0
- package/dist/components/_MDrawerItemList.vue.d.ts +3 -0
- package/dist/composables/_colorUtils.d.ts +34 -0
- package/dist/composables/_themeColor.d.ts +1 -0
- package/dist/composables/_useSpring.d.ts +17 -0
- package/dist/composables/dynamicColor.d.ts +18 -0
- package/dist/composables/useColorPalette.d.ts +27 -0
- package/dist/composables/useLocale.d.ts +16 -0
- package/dist/directives/vRipple.d.ts +16 -0
- package/dist/emoji-names-de-BphO5XpV.js.map +1 -1
- package/dist/emoji-names-en-9ZvMRvzi.js.map +1 -1
- package/dist/emoji-names-es-DvfWU3Pj.js.map +1 -1
- package/dist/emoji-names-fr-B-jhUlsP.js.map +1 -1
- package/dist/emoji-names-ja-l8cMFJo1.js.map +1 -1
- package/dist/emoji-names-ko-YP-o9G0c.js.map +1 -1
- package/dist/emoji-names-pt-DGj_Js4M.js.map +1 -1
- package/dist/emoji-names-zh-Dzecwtw_.js.map +1 -1
- package/dist/index.d.ts +12 -1
- package/dist/locales/index.js +112 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +7443 -6150
- package/dist/m3ui.js.map +1 -1
- package/dist/material-color-utilities-Cktu8DBU.js +3160 -0
- package/dist/material-color-utilities-Cktu8DBU.js.map +1 -0
- package/dist/plugin.d.ts +18 -0
- package/dist/rich-text-editor.js +22 -22
- package/dist/styles/palettes.css +546 -0
- package/dist/styles/theme.css +90 -0
- package/dist/styles.css +1 -1
- package/dist/terminal.js +1 -1
- package/dist/{useLocale-Drihe5G2.js → useLocale-Cn9MVIdw.js} +17 -1
- package/dist/useLocale-Cn9MVIdw.js.map +1 -0
- package/package.json +3 -2
- package/src/__tests__/MButtonGroup.test.ts +51 -0
- package/src/__tests__/MColorPicker.test.ts +141 -0
- package/src/__tests__/MDragHandle.test.ts +57 -0
- package/src/__tests__/MIconButton.test.ts +60 -0
- package/src/__tests__/MListItemShape.test.ts +76 -0
- package/src/__tests__/MMenuItem.test.ts +84 -0
- package/src/__tests__/MScrollField.test.ts +69 -0
- package/src/__tests__/MScrollable.test.ts +42 -0
- package/src/__tests__/MSwipeToDismiss.test.ts +104 -0
- package/src/__tests__/MTabs.test.ts +39 -0
- package/src/__tests__/MTimePickerModal.test.ts +52 -0
- package/src/__tests__/MTimeScroll.test.ts +44 -0
- package/src/__tests__/MTransition.test.ts +27 -0
- package/src/__tests__/dynamicColor.test.ts +191 -0
- package/src/__tests__/vRipple.test.ts +92 -0
- package/src/components/MBox.vue +4 -2
- package/src/components/MButton.vue +4 -13
- package/src/components/MButtonGroup.vue +118 -44
- package/src/components/MColorPicker.vue +114 -210
- package/src/components/MColorPickerModal.vue +50 -251
- package/src/components/MDragHandle.vue +54 -0
- package/src/components/MFab.vue +8 -15
- package/src/components/MIcon.vue +4 -2
- package/src/components/MIconButton.vue +96 -24
- package/src/components/MList.vue +33 -3
- package/src/components/MListItem.vue +75 -5
- package/src/components/MMenuItem.vue +115 -13
- package/src/components/MScrollField.vue +310 -0
- package/src/components/MScrollable.vue +160 -5
- package/src/components/MSplitButton.vue +5 -12
- package/src/components/MSplitter.vue +33 -10
- package/src/components/MSwipeToDismiss.vue +247 -0
- package/src/components/MTabs.vue +16 -5
- package/src/components/MTimePickerModal.vue +48 -8
- package/src/components/MTimeScroll.vue +161 -0
- package/src/components/MToggleButton.vue +5 -12
- package/src/components/MTransition.vue +149 -0
- package/src/components/_MColorPanel.vue +478 -0
- package/src/composables/_carouselStrategy.ts +3 -0
- package/src/composables/_colorUtils.ts +107 -0
- package/src/composables/_themeColor.ts +33 -0
- package/src/composables/_useSpring.ts +91 -0
- package/src/composables/dynamicColor.ts +85 -0
- package/src/composables/useColorPalette.ts +171 -25
- package/src/composables/useFieldBg.ts +6 -0
- package/src/composables/useLocale.ts +34 -0
- package/src/data/emoji-names/emoji-names-de.ts +1 -0
- package/src/data/emoji-names/emoji-names-en.ts +1 -0
- package/src/data/emoji-names/emoji-names-es.ts +1 -0
- package/src/data/emoji-names/emoji-names-fr.ts +1 -0
- package/src/data/emoji-names/emoji-names-ja.ts +1 -0
- package/src/data/emoji-names/emoji-names-ko.ts +1 -0
- package/src/data/emoji-names/emoji-names-pt.ts +1 -0
- package/src/data/emoji-names/emoji-names-zh.ts +1 -0
- package/src/data/emojis.ts +1 -0
- package/src/directives/vRipple.ts +73 -0
- package/src/index.ts +14 -1
- package/src/locales/de.ts +16 -0
- package/src/locales/es.ts +16 -0
- package/src/locales/fr.ts +16 -0
- package/src/locales/ja.ts +16 -0
- package/src/locales/ko.ts +16 -0
- package/src/locales/pt.ts +16 -0
- package/src/locales/zh.ts +16 -0
- package/src/plugin.ts +30 -7
- package/src/styles/palettes.css +546 -0
- package/src/styles/theme.css +90 -0
- package/dist/MIcon-CaEooCmZ.js.map +0 -1
- package/dist/MTextField-pJU1vFaR.js.map +0 -1
- package/dist/useLocale-Drihe5G2.js.map +0 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* M3's navigation transitions between two views (a port of Compose's `MaterialTransitions`): `shared-axis`
|
|
4
|
+
* slides the new view in from the side as the old one slides out (use `reverse` when going back), and
|
|
5
|
+
* `fade-through` fades the old view out and the new one in with a slight scale, for destinations without a
|
|
6
|
+
* spatial relationship such as navigation bar tabs. Wrap content that changes its `key` (or a `v-if`/`v-else`
|
|
7
|
+
* pair, or a `<RouterView>` slot's component); both views overlap while they animate.
|
|
8
|
+
*
|
|
9
|
+
* @slot default The current view; give it a `key` that changes with it
|
|
10
|
+
*/
|
|
11
|
+
const props = withDefaults(defineProps<{
|
|
12
|
+
/** `shared-axis` (horizontal, for forward/back navigation) or `fade-through` (unrelated destinations) */
|
|
13
|
+
type?: 'shared-axis' | 'fade-through'
|
|
14
|
+
/** Shared axis: play the backward transition (the new view comes from the start side) */
|
|
15
|
+
reverse?: boolean
|
|
16
|
+
/** Animate the first view too */
|
|
17
|
+
appear?: boolean
|
|
18
|
+
}>(), { type: 'shared-axis', reverse: false, appear: false })
|
|
19
|
+
|
|
20
|
+
// slideIn/OutHorizontally and scaleIn/Out default to spring(stiffness = StiffnessMediumLow), no bounce
|
|
21
|
+
const STIFFNESS = 400
|
|
22
|
+
const DAMPING = 2 * Math.sqrt(STIFFNESS)
|
|
23
|
+
// HorizontalSharedAxis
|
|
24
|
+
const SLIDE_FRACTION = 0.2
|
|
25
|
+
const AXIS_FADE = { duration: 90, delay: 50 }
|
|
26
|
+
// FadeThrough
|
|
27
|
+
const FADE_OUT = 90
|
|
28
|
+
const FADE_IN = 210
|
|
29
|
+
const INITIAL_SCALE = 0.8
|
|
30
|
+
const TARGET_SCALE = 1.05
|
|
31
|
+
|
|
32
|
+
interface Motion {
|
|
33
|
+
/** Spring from `from` to `to` (an x offset in px, or a scale) */
|
|
34
|
+
from: number
|
|
35
|
+
to: number
|
|
36
|
+
apply: (value: number, el: HTMLElement) => void
|
|
37
|
+
/** Linear opacity tween */
|
|
38
|
+
fade: { from: number; to: number; duration: number; delay: number }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const running = new WeakMap<Element, () => void>()
|
|
42
|
+
|
|
43
|
+
function run(el: HTMLElement, m: Motion, done: () => void) {
|
|
44
|
+
running.get(el)?.()
|
|
45
|
+
let raf = 0
|
|
46
|
+
let value = m.from
|
|
47
|
+
let velocity = 0
|
|
48
|
+
const start = performance.now()
|
|
49
|
+
let last = start
|
|
50
|
+
const threshold = Math.abs(m.to - m.from) > 2 ? 0.5 : 0.001
|
|
51
|
+
const finish = () => {
|
|
52
|
+
cancelAnimationFrame(raf)
|
|
53
|
+
running.delete(el)
|
|
54
|
+
done()
|
|
55
|
+
}
|
|
56
|
+
running.set(el, finish)
|
|
57
|
+
const frame = (now: number) => {
|
|
58
|
+
let dt = Math.min((now - last) / 1000, 1 / 30)
|
|
59
|
+
last = now
|
|
60
|
+
while (dt > 0) {
|
|
61
|
+
const h = Math.min(dt, 0.004)
|
|
62
|
+
dt -= h
|
|
63
|
+
velocity += (-STIFFNESS * (value - m.to) - DAMPING * velocity) * h
|
|
64
|
+
value += velocity * h
|
|
65
|
+
}
|
|
66
|
+
const springDone = Math.abs(value - m.to) < threshold && Math.abs(velocity) < threshold * 10
|
|
67
|
+
if (springDone) value = m.to
|
|
68
|
+
m.apply(value, el)
|
|
69
|
+
const t = Math.min(1, Math.max(0, (now - start - m.fade.delay) / m.fade.duration))
|
|
70
|
+
el.style.opacity = String(m.fade.from + (m.fade.to - m.fade.from) * t)
|
|
71
|
+
if (springDone && t >= 1) finish()
|
|
72
|
+
else raf = requestAnimationFrame(frame)
|
|
73
|
+
}
|
|
74
|
+
m.apply(m.from, el)
|
|
75
|
+
el.style.opacity = String(m.fade.from)
|
|
76
|
+
raf = requestAnimationFrame(frame)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const translate = (x: number, el: HTMLElement) => { el.style.transform = x ? `translateX(${x}px)` : '' }
|
|
80
|
+
const scale = (s: number, el: HTMLElement) => { el.style.transform = s !== 1 ? `scale(${s})` : '' }
|
|
81
|
+
|
|
82
|
+
function clear(el: HTMLElement) {
|
|
83
|
+
el.style.transform = ''
|
|
84
|
+
el.style.opacity = ''
|
|
85
|
+
el.style.zIndex = ''
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function onEnter(element: Element, done: () => void) {
|
|
89
|
+
const el = element as HTMLElement
|
|
90
|
+
const finish = () => { clear(el); done() }
|
|
91
|
+
if (props.type === 'fade-through') {
|
|
92
|
+
run(el, { from: INITIAL_SCALE, to: 1, apply: scale, fade: { from: 0, to: 1, duration: FADE_IN, delay: FADE_OUT } }, finish)
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
const w = el.offsetWidth
|
|
96
|
+
// Backward: the entering view sits under the leaving one (targetContentZIndex = -10)
|
|
97
|
+
if (props.reverse) el.style.zIndex = '-1'
|
|
98
|
+
run(el, {
|
|
99
|
+
from: (props.reverse ? -1 : 1) * w * SLIDE_FRACTION,
|
|
100
|
+
to: 0,
|
|
101
|
+
apply: translate,
|
|
102
|
+
fade: { from: 0, to: 1, ...AXIS_FADE },
|
|
103
|
+
}, finish)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function onLeave(element: Element, done: () => void) {
|
|
107
|
+
const el = element as HTMLElement
|
|
108
|
+
const finish = () => { clear(el); done() }
|
|
109
|
+
if (props.type === 'fade-through') {
|
|
110
|
+
run(el, { from: 1, to: TARGET_SCALE, apply: scale, fade: { from: 1, to: 0, duration: FADE_OUT, delay: 0 } }, finish)
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
const w = el.offsetWidth
|
|
114
|
+
run(el, {
|
|
115
|
+
from: 0,
|
|
116
|
+
to: (props.reverse ? 1 : -1) * w * SLIDE_FRACTION,
|
|
117
|
+
apply: translate,
|
|
118
|
+
fade: { from: 1, to: 0, ...AXIS_FADE },
|
|
119
|
+
}, finish)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function onCancel(el: Element) {
|
|
123
|
+
running.get(el)?.()
|
|
124
|
+
}
|
|
125
|
+
</script>
|
|
126
|
+
|
|
127
|
+
<template>
|
|
128
|
+
<!-- Clipped, as AnimatedContent clips by default (SizeTransform(clip = true)) -->
|
|
129
|
+
<div class="m3-transition isolate grid overflow-hidden">
|
|
130
|
+
<Transition
|
|
131
|
+
:css="false"
|
|
132
|
+
:appear="appear"
|
|
133
|
+
@enter="onEnter"
|
|
134
|
+
@leave="onLeave"
|
|
135
|
+
@enter-cancelled="onCancel"
|
|
136
|
+
@leave-cancelled="onCancel"
|
|
137
|
+
>
|
|
138
|
+
<slot />
|
|
139
|
+
</Transition>
|
|
140
|
+
</div>
|
|
141
|
+
</template>
|
|
142
|
+
|
|
143
|
+
<style>
|
|
144
|
+
/* Both views share one grid cell while they animate */
|
|
145
|
+
.m3-transition > * {
|
|
146
|
+
grid-area: 1 / 1;
|
|
147
|
+
min-width: 0;
|
|
148
|
+
}
|
|
149
|
+
</style>
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Internal: the picking UI shared by MColorPicker (in its dropdown) and MColorPickerModal (in its dialog).
|
|
4
|
+
* Saturation/brightness area, hue and opacity sliders, eyedropper, HEX/RGB/HSL fields, recent and preset
|
|
5
|
+
* swatches. Emits on every change; the parent decides when that becomes the real value.
|
|
6
|
+
*/
|
|
7
|
+
import { computed, onMounted, ref, watch } from 'vue'
|
|
8
|
+
import MIcon from './MIcon.vue'
|
|
9
|
+
import MIconButton from './MIconButton.vue'
|
|
10
|
+
import { useLocale } from '../composables/useLocale'
|
|
11
|
+
import {
|
|
12
|
+
hslToHsv, hsvToHsl, hsvToRgb, inkFor, normalizeHex, parseHex, recentColors, rgbToHsv, toHex, type Rgba,
|
|
13
|
+
} from '../composables/_colorUtils'
|
|
14
|
+
|
|
15
|
+
const props = defineProps<{
|
|
16
|
+
modelValue: string
|
|
17
|
+
presets: string[]
|
|
18
|
+
alpha: boolean
|
|
19
|
+
recent: boolean
|
|
20
|
+
eyedropper: boolean
|
|
21
|
+
/** Shown as the left half of the preview, click to restore */
|
|
22
|
+
original?: string
|
|
23
|
+
}>()
|
|
24
|
+
|
|
25
|
+
const emit = defineEmits<{ 'update:modelValue': [string] }>()
|
|
26
|
+
const t = useLocale()
|
|
27
|
+
|
|
28
|
+
// ── State: HSV + alpha, kept here so hue survives grays and black ──────────────────────────
|
|
29
|
+
const h = ref(0)
|
|
30
|
+
const s = ref(100)
|
|
31
|
+
const v = ref(100)
|
|
32
|
+
const a = ref(1)
|
|
33
|
+
|
|
34
|
+
const hex = computed(() => toHex(hsvToRgb({ h: h.value, s: s.value, v: v.value }, props.alpha ? a.value : 1)))
|
|
35
|
+
const opaqueHex = computed(() => toHex(hsvToRgb({ h: h.value, s: s.value, v: v.value })))
|
|
36
|
+
const hueColor = computed(() => `hsl(${h.value}, 100%, 50%)`)
|
|
37
|
+
|
|
38
|
+
function setFromRgb(c: Rgba) {
|
|
39
|
+
const hsv = rgbToHsv(c)
|
|
40
|
+
// A gray has no hue and black no saturation: keep the ones we had, so the thumbs don't jump
|
|
41
|
+
if (hsv.v > 0 && hsv.s > 0) h.value = hsv.h
|
|
42
|
+
if (hsv.v > 0) s.value = hsv.s
|
|
43
|
+
v.value = hsv.v
|
|
44
|
+
a.value = props.alpha ? c.a : 1
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let lastEmitted = ''
|
|
48
|
+
function commit() {
|
|
49
|
+
lastEmitted = hex.value
|
|
50
|
+
if (hex.value !== normalizeHex(props.modelValue)) emit('update:modelValue', hex.value)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
watch(() => props.modelValue, (val) => {
|
|
54
|
+
const n = normalizeHex(val)
|
|
55
|
+
if (!n || n === lastEmitted) return
|
|
56
|
+
setFromRgb(parseHex(n)!)
|
|
57
|
+
lastEmitted = n
|
|
58
|
+
}, { immediate: true })
|
|
59
|
+
|
|
60
|
+
function pick(color: string) {
|
|
61
|
+
const c = parseHex(color)
|
|
62
|
+
if (!c) return
|
|
63
|
+
setFromRgb(c)
|
|
64
|
+
commit()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ── Pointer: area and sliders ──────────────────────────────────────────────────────────────
|
|
68
|
+
type Target = 'area' | 'hue' | 'alpha'
|
|
69
|
+
const areaEl = ref<HTMLElement>()
|
|
70
|
+
const hueEl = ref<HTMLElement>()
|
|
71
|
+
const alphaEl = ref<HTMLElement>()
|
|
72
|
+
const dragging = ref<Target | null>(null)
|
|
73
|
+
// The slider handles travel between the centers of the track's round ends (24px track)
|
|
74
|
+
const THUMB_INSET = 12
|
|
75
|
+
|
|
76
|
+
const clamp = (n: number, min: number, max: number) => Math.min(max, Math.max(min, n))
|
|
77
|
+
|
|
78
|
+
function update(target: Target, e: PointerEvent) {
|
|
79
|
+
const el = target === 'area' ? areaEl.value : target === 'hue' ? hueEl.value : alphaEl.value
|
|
80
|
+
if (!el) return
|
|
81
|
+
const r = el.getBoundingClientRect()
|
|
82
|
+
if (target === 'area') {
|
|
83
|
+
s.value = clamp(((e.clientX - r.left) / r.width) * 100, 0, 100)
|
|
84
|
+
v.value = clamp((1 - (e.clientY - r.top) / r.height) * 100, 0, 100)
|
|
85
|
+
} else {
|
|
86
|
+
const p = clamp((e.clientX - r.left - THUMB_INSET) / (r.width - THUMB_INSET * 2), 0, 1)
|
|
87
|
+
if (target === 'hue') h.value = p * 360
|
|
88
|
+
else a.value = Math.round(p * 100) / 100
|
|
89
|
+
}
|
|
90
|
+
commit()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function onPointerDown(target: Target, e: PointerEvent) {
|
|
94
|
+
if (e.button !== 0) return
|
|
95
|
+
e.preventDefault()
|
|
96
|
+
;(e.currentTarget as HTMLElement).focus({ preventScroll: true })
|
|
97
|
+
;(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId)
|
|
98
|
+
dragging.value = target
|
|
99
|
+
update(target, e)
|
|
100
|
+
}
|
|
101
|
+
function onPointerMove(target: Target, e: PointerEvent) {
|
|
102
|
+
if (dragging.value === target) update(target, e)
|
|
103
|
+
}
|
|
104
|
+
function onPointerUp() {
|
|
105
|
+
dragging.value = null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ── Keyboard ───────────────────────────────────────────────────────────────────────────────
|
|
109
|
+
function onKeydown(target: Target, e: KeyboardEvent) {
|
|
110
|
+
const step = e.shiftKey ? 10 : 1
|
|
111
|
+
const dx = e.key === 'ArrowRight' ? step : e.key === 'ArrowLeft' ? -step : 0
|
|
112
|
+
const dy = e.key === 'ArrowUp' ? step : e.key === 'ArrowDown' ? -step : 0
|
|
113
|
+
if (target === 'area') {
|
|
114
|
+
if (!dx && !dy) return
|
|
115
|
+
s.value = clamp(s.value + dx, 0, 100)
|
|
116
|
+
v.value = clamp(v.value + dy, 0, 100)
|
|
117
|
+
} else {
|
|
118
|
+
const max = target === 'hue' ? 360 : 100
|
|
119
|
+
const cur = target === 'hue' ? h.value : a.value * 100
|
|
120
|
+
let next: number
|
|
121
|
+
if (e.key === 'Home') next = 0
|
|
122
|
+
else if (e.key === 'End') next = max
|
|
123
|
+
else if (dx || dy) next = clamp(cur + (dx || dy), 0, max)
|
|
124
|
+
else return
|
|
125
|
+
if (target === 'hue') h.value = next
|
|
126
|
+
else a.value = Math.round(next) / 100
|
|
127
|
+
}
|
|
128
|
+
e.preventDefault()
|
|
129
|
+
commit()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ── Eyedropper (Chromium only; the button isn't shown elsewhere) ─────────────────────────────
|
|
133
|
+
const canEyedrop = ref(false)
|
|
134
|
+
onMounted(() => { canEyedrop.value = 'EyeDropper' in window })
|
|
135
|
+
|
|
136
|
+
async function pickFromScreen() {
|
|
137
|
+
try {
|
|
138
|
+
const Ctor = (window as unknown as { EyeDropper: new () => { open(): Promise<{ sRGBHex: string }> } }).EyeDropper
|
|
139
|
+
const { sRGBHex } = await new Ctor().open()
|
|
140
|
+
const c = parseHex(sRGBHex)
|
|
141
|
+
if (c) { setFromRgb({ ...c, a: a.value }); commit() }
|
|
142
|
+
} catch { /* cancelled */ }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── Text fields ─────────────────────────────────────────────────────────────────────────────
|
|
146
|
+
type Format = 'hex' | 'rgb' | 'hsl'
|
|
147
|
+
const formats: { id: Format; label: string }[] = [
|
|
148
|
+
{ id: 'hex', label: 'HEX' }, { id: 'rgb', label: 'RGB' }, { id: 'hsl', label: 'HSL' },
|
|
149
|
+
]
|
|
150
|
+
const format = ref<Format>('hex')
|
|
151
|
+
const formatLabel = computed(() => formats.find((f) => f.id === format.value)!.label)
|
|
152
|
+
function cycleFormat() {
|
|
153
|
+
format.value = formats[(formats.findIndex((f) => f.id === format.value) + 1) % formats.length]!.id
|
|
154
|
+
editing.value = null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface Field { key: string; label: string; display: string; max: number; apply: (n: number) => void }
|
|
158
|
+
|
|
159
|
+
function setHsv(next: { h: number; s: number; v: number }) {
|
|
160
|
+
h.value = next.h; s.value = next.s; v.value = next.v
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const alphaField = computed<Field>(() => ({
|
|
164
|
+
key: 'a', label: 'A', display: String(Math.round(a.value * 100)), max: 100,
|
|
165
|
+
apply: (n) => { a.value = n / 100 },
|
|
166
|
+
}))
|
|
167
|
+
|
|
168
|
+
const fields = computed<Field[]>(() => {
|
|
169
|
+
const list: Field[] = []
|
|
170
|
+
if (format.value === 'rgb') {
|
|
171
|
+
const rgb = hsvToRgb({ h: h.value, s: s.value, v: v.value })
|
|
172
|
+
const round = { r: Math.round(rgb.r), g: Math.round(rgb.g), b: Math.round(rgb.b), a: a.value }
|
|
173
|
+
for (const ch of ['r', 'g', 'b'] as const) {
|
|
174
|
+
list.push({
|
|
175
|
+
key: ch, label: ch.toUpperCase(), display: String(round[ch]), max: 255,
|
|
176
|
+
apply: (n) => setFromRgb({ ...round, [ch]: n }),
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
} else if (format.value === 'hsl') {
|
|
180
|
+
const hsl = hsvToHsl({ h: h.value, s: s.value, v: v.value })
|
|
181
|
+
const round = { h: Math.round(hsl.h), s: Math.round(hsl.s), l: Math.round(hsl.l) }
|
|
182
|
+
for (const ch of ['h', 's', 'l'] as const) {
|
|
183
|
+
list.push({
|
|
184
|
+
key: ch, label: ch.toUpperCase(), display: String(round[ch]), max: ch === 'h' ? 360 : 100,
|
|
185
|
+
// Straight from HSL to HSV, so a hue typed on a gray isn't lost
|
|
186
|
+
apply: (n) => setHsv(hslToHsv({ ...round, [ch]: n })),
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (props.alpha && format.value !== 'hex') list.push(alphaField.value)
|
|
191
|
+
return list
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
// While a field is focused it shows what the user typed, not the reformatted value
|
|
195
|
+
const editing = ref<{ key: string; text: string } | null>(null)
|
|
196
|
+
const shown = (key: string, display: string) => (editing.value?.key === key ? editing.value.text : display)
|
|
197
|
+
|
|
198
|
+
function onHexInput(e: Event) {
|
|
199
|
+
const text = (e.target as HTMLInputElement).value
|
|
200
|
+
editing.value = { key: 'hex', text }
|
|
201
|
+
const c = parseHex(text)
|
|
202
|
+
if (c) { setFromRgb(c); commit() }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function onFieldInput(f: Field, e: Event) {
|
|
206
|
+
const text = (e.target as HTMLInputElement).value
|
|
207
|
+
editing.value = { key: f.key, text }
|
|
208
|
+
if (!/^\d+$/.test(text.trim())) return
|
|
209
|
+
const n = Number(text)
|
|
210
|
+
if (n > f.max) return
|
|
211
|
+
f.apply(n)
|
|
212
|
+
commit()
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function onFieldKeydown(f: Field, e: KeyboardEvent) {
|
|
216
|
+
if (e.key === 'Enter') { editing.value = null; return }
|
|
217
|
+
if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return
|
|
218
|
+
e.preventDefault()
|
|
219
|
+
const step = (e.shiftKey ? 10 : 1) * (e.key === 'ArrowUp' ? 1 : -1)
|
|
220
|
+
f.apply(clamp(Number(f.display) + step, 0, f.max))
|
|
221
|
+
editing.value = null
|
|
222
|
+
commit()
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ── Swatches ────────────────────────────────────────────────────────────────────────────────
|
|
226
|
+
const recents = computed(() => (props.recent ? recentColors.value : []))
|
|
227
|
+
const isSelected = (color: string) => normalizeHex(color) === hex.value
|
|
228
|
+
// Swatches and preview segments paint the color over a checkerboard in one background, so a translucent color
|
|
229
|
+
// shows it and the edge is a single antialiased shape (nested clipped layers looked jagged in Firefox)
|
|
230
|
+
const CHECKER = 'repeating-conic-gradient(#d4d4d4 0 25%, #fff 0 50%) 0 0 / 8px 8px'
|
|
231
|
+
const fill = (color: string) => `linear-gradient(${color}, ${color}), ${CHECKER}`
|
|
232
|
+
// Over a mostly transparent color the checkerboard (light) is what's behind the text
|
|
233
|
+
const previewInk = computed(() => (a.value < 0.5 ? '#000000' : inkFor(opaqueHex.value)))
|
|
234
|
+
const showOriginal = computed(() => !!props.original && !!normalizeHex(props.original) && normalizeHex(props.original) !== hex.value)
|
|
235
|
+
|
|
236
|
+
defineExpose({ focus: () => areaEl.value?.focus({ preventScroll: true }) })
|
|
237
|
+
</script>
|
|
238
|
+
|
|
239
|
+
<template>
|
|
240
|
+
<div class="flex flex-col gap-4">
|
|
241
|
+
<!-- Saturation (x) / brightness (y) -->
|
|
242
|
+
<div
|
|
243
|
+
ref="areaEl"
|
|
244
|
+
role="slider"
|
|
245
|
+
tabindex="0"
|
|
246
|
+
:aria-label="`${t.colorSaturation}, ${t.colorBrightness}`"
|
|
247
|
+
:aria-valuenow="Math.round(s)"
|
|
248
|
+
aria-valuemin="0"
|
|
249
|
+
aria-valuemax="100"
|
|
250
|
+
:aria-valuetext="`${t.colorSaturation} ${Math.round(s)}%, ${t.colorBrightness} ${Math.round(v)}%`"
|
|
251
|
+
class="relative h-40 w-full shrink-0 cursor-crosshair touch-none rounded-md outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
|
252
|
+
:style="{ background: `linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, ${hueColor})` }"
|
|
253
|
+
@pointerdown="onPointerDown('area', $event)"
|
|
254
|
+
@pointermove="onPointerMove('area', $event)"
|
|
255
|
+
@pointerup="onPointerUp"
|
|
256
|
+
@pointercancel="onPointerUp"
|
|
257
|
+
@keydown="onKeydown('area', $event)"
|
|
258
|
+
>
|
|
259
|
+
<span
|
|
260
|
+
class="m3-cp-area-thumb pointer-events-none absolute size-5 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white transition-[scale] duration-150"
|
|
261
|
+
:class="dragging === 'area' ? 'scale-125' : ''"
|
|
262
|
+
:style="{ left: `${s}%`, top: `${100 - v}%`, backgroundColor: opaqueHex }"
|
|
263
|
+
/>
|
|
264
|
+
</div>
|
|
265
|
+
|
|
266
|
+
<!-- Hue / opacity: thick tracks with M3 Expressive's bar handle -->
|
|
267
|
+
<div class="flex flex-col gap-1">
|
|
268
|
+
<div
|
|
269
|
+
ref="hueEl"
|
|
270
|
+
role="slider"
|
|
271
|
+
tabindex="0"
|
|
272
|
+
:aria-label="t.colorHue"
|
|
273
|
+
:aria-valuenow="Math.round(h)"
|
|
274
|
+
aria-valuemin="0"
|
|
275
|
+
aria-valuemax="360"
|
|
276
|
+
class="m3-cp-slider group"
|
|
277
|
+
@pointerdown="onPointerDown('hue', $event)"
|
|
278
|
+
@pointermove="onPointerMove('hue', $event)"
|
|
279
|
+
@pointerup="onPointerUp"
|
|
280
|
+
@pointercancel="onPointerUp"
|
|
281
|
+
@keydown="onKeydown('hue', $event)"
|
|
282
|
+
>
|
|
283
|
+
<span class="m3-cp-track m3-cp-hue" />
|
|
284
|
+
<span
|
|
285
|
+
class="m3-cp-handle"
|
|
286
|
+
:class="{ 'm3-cp-handle-pressed': dragging === 'hue' }"
|
|
287
|
+
:style="{ left: `calc(${THUMB_INSET}px + ${h / 360} * (100% - ${THUMB_INSET * 2}px))` }"
|
|
288
|
+
/>
|
|
289
|
+
</div>
|
|
290
|
+
<div
|
|
291
|
+
v-if="alpha"
|
|
292
|
+
ref="alphaEl"
|
|
293
|
+
role="slider"
|
|
294
|
+
tabindex="0"
|
|
295
|
+
:aria-label="t.colorAlpha"
|
|
296
|
+
:aria-valuenow="Math.round(a * 100)"
|
|
297
|
+
aria-valuemin="0"
|
|
298
|
+
aria-valuemax="100"
|
|
299
|
+
:aria-valuetext="`${Math.round(a * 100)}%`"
|
|
300
|
+
class="m3-cp-slider group"
|
|
301
|
+
@pointerdown="onPointerDown('alpha', $event)"
|
|
302
|
+
@pointermove="onPointerMove('alpha', $event)"
|
|
303
|
+
@pointerup="onPointerUp"
|
|
304
|
+
@pointercancel="onPointerUp"
|
|
305
|
+
@keydown="onKeydown('alpha', $event)"
|
|
306
|
+
>
|
|
307
|
+
<span class="m3-cp-track" :style="{ background: `linear-gradient(to right, transparent, ${opaqueHex}), ${CHECKER}` }" />
|
|
308
|
+
<span
|
|
309
|
+
class="m3-cp-handle"
|
|
310
|
+
:class="{ 'm3-cp-handle-pressed': dragging === 'alpha' }"
|
|
311
|
+
:style="{ left: `calc(${THUMB_INSET}px + ${a} * (100% - ${THUMB_INSET * 2}px))` }"
|
|
312
|
+
/>
|
|
313
|
+
</div>
|
|
314
|
+
</div>
|
|
315
|
+
|
|
316
|
+
<!-- Preview (original | new) and eyedropper -->
|
|
317
|
+
<div class="flex items-center gap-2">
|
|
318
|
+
<div class="flex h-10 min-w-0 flex-1">
|
|
319
|
+
<button
|
|
320
|
+
v-if="showOriginal"
|
|
321
|
+
type="button"
|
|
322
|
+
class="m3-cp-swatch flex w-12 shrink-0 cursor-pointer items-center justify-center rounded-l-full outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
|
323
|
+
:style="{ background: fill(original!) }"
|
|
324
|
+
:aria-label="t.colorOriginal"
|
|
325
|
+
:title="t.colorOriginal"
|
|
326
|
+
@click="pick(original!)"
|
|
327
|
+
>
|
|
328
|
+
<MIcon name="undo" :size="18" :style="{ color: inkFor(original!) }" />
|
|
329
|
+
</button>
|
|
330
|
+
<span
|
|
331
|
+
class="m3-cp-swatch flex min-w-0 flex-1 items-center px-4 font-mono text-label-large transition-[border-radius] duration-200"
|
|
332
|
+
:class="showOriginal ? 'rounded-r-full' : 'rounded-full'"
|
|
333
|
+
:style="{ background: fill(hex), color: previewInk }"
|
|
334
|
+
>{{ hex }}</span>
|
|
335
|
+
</div>
|
|
336
|
+
<MIconButton v-if="eyedropper && canEyedrop" icon="colorize" variant="tonal" :label="t.colorEyedropper" @click="pickFromScreen" />
|
|
337
|
+
</div>
|
|
338
|
+
|
|
339
|
+
<!-- Format + fields -->
|
|
340
|
+
<div class="flex items-start gap-2">
|
|
341
|
+
<button
|
|
342
|
+
type="button"
|
|
343
|
+
class="flex h-10 shrink-0 cursor-pointer items-center gap-0.5 rounded-full bg-secondary-container pr-2 pl-3 text-label-large text-on-secondary-container outline-none transition-[background-color] duration-150 hover:bg-[color-mix(in_srgb,var(--color-secondary-container),var(--color-on-secondary-container)_8%)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
|
344
|
+
:aria-label="`${t.colorFormat}: ${formatLabel}`"
|
|
345
|
+
@click="cycleFormat"
|
|
346
|
+
>
|
|
347
|
+
{{ formatLabel }}
|
|
348
|
+
<MIcon name="unfold_more" :size="18" />
|
|
349
|
+
</button>
|
|
350
|
+
<input
|
|
351
|
+
v-if="format === 'hex'"
|
|
352
|
+
type="text"
|
|
353
|
+
spellcheck="false"
|
|
354
|
+
autocomplete="off"
|
|
355
|
+
:maxlength="alpha ? 9 : 7"
|
|
356
|
+
aria-label="HEX"
|
|
357
|
+
:value="shown('hex', hex)"
|
|
358
|
+
class="m3-cp-field min-w-0 flex-1 font-mono text-body-medium"
|
|
359
|
+
@input="onHexInput"
|
|
360
|
+
@blur="editing = null"
|
|
361
|
+
@keydown.enter="editing = null"
|
|
362
|
+
/>
|
|
363
|
+
<label v-for="f in fields" :key="f.key" class="flex min-w-0 flex-1 flex-col items-center gap-1">
|
|
364
|
+
<input
|
|
365
|
+
type="text"
|
|
366
|
+
inputmode="numeric"
|
|
367
|
+
autocomplete="off"
|
|
368
|
+
:maxlength="3"
|
|
369
|
+
:aria-label="f.label"
|
|
370
|
+
:value="shown(f.key, f.display)"
|
|
371
|
+
class="m3-cp-field w-full font-mono text-body-medium"
|
|
372
|
+
@input="onFieldInput(f, $event)"
|
|
373
|
+
@keydown="onFieldKeydown(f, $event)"
|
|
374
|
+
@blur="editing = null"
|
|
375
|
+
/>
|
|
376
|
+
<span class="text-label-small text-on-surface-variant">{{ f.label }}</span>
|
|
377
|
+
</label>
|
|
378
|
+
</div>
|
|
379
|
+
|
|
380
|
+
<!-- Swatches: rounded squares that morph into a circle when selected -->
|
|
381
|
+
<template v-for="group in [{ id: 'recent', label: t.colorRecent, colors: recents }, { id: 'presets', label: t.colorPresets, colors: presets }]" :key="group.id">
|
|
382
|
+
<div v-if="group.colors.length" role="group" :aria-label="group.label" class="flex flex-col gap-2">
|
|
383
|
+
<span v-if="recents.length" class="text-label-medium text-on-surface-variant">{{ group.label }}</span>
|
|
384
|
+
<div class="grid grid-cols-7 gap-2">
|
|
385
|
+
<button
|
|
386
|
+
v-for="color in group.colors"
|
|
387
|
+
:key="color"
|
|
388
|
+
type="button"
|
|
389
|
+
class="m3-cp-swatch m3-cp-morph flex aspect-square cursor-pointer items-center justify-center outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
|
390
|
+
:class="{ 'm3-cp-morph-selected': isSelected(color) }"
|
|
391
|
+
:style="{ background: fill(color) }"
|
|
392
|
+
:aria-label="color"
|
|
393
|
+
:aria-pressed="isSelected(color)"
|
|
394
|
+
@click="pick(color)"
|
|
395
|
+
>
|
|
396
|
+
<MIcon v-if="isSelected(color)" name="check" :size="18" :style="{ color: inkFor(color) }" />
|
|
397
|
+
</button>
|
|
398
|
+
</div>
|
|
399
|
+
</div>
|
|
400
|
+
</template>
|
|
401
|
+
</div>
|
|
402
|
+
</template>
|
|
403
|
+
|
|
404
|
+
<style scoped>
|
|
405
|
+
.m3-cp-hue {
|
|
406
|
+
background: linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);
|
|
407
|
+
}
|
|
408
|
+
/* Hairline so white swatches show on light surfaces and black ones on dark */
|
|
409
|
+
.m3-cp-swatch {
|
|
410
|
+
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--color-on-surface) 12%, transparent);
|
|
411
|
+
}
|
|
412
|
+
.m3-cp-morph {
|
|
413
|
+
border-radius: 30%;
|
|
414
|
+
transition: border-radius 250ms cubic-bezier(0.2, 0, 0, 1);
|
|
415
|
+
}
|
|
416
|
+
.m3-cp-morph:hover {
|
|
417
|
+
border-radius: 40%;
|
|
418
|
+
}
|
|
419
|
+
.m3-cp-morph-selected,
|
|
420
|
+
.m3-cp-morph-selected:hover {
|
|
421
|
+
border-radius: 50%;
|
|
422
|
+
}
|
|
423
|
+
.m3-cp-area-thumb {
|
|
424
|
+
box-shadow: 0 0 0 1px rgb(0 0 0 / 0.2), 0 1px 3px 1px rgb(0 0 0 / 0.25);
|
|
425
|
+
}
|
|
426
|
+
.m3-cp-slider {
|
|
427
|
+
position: relative;
|
|
428
|
+
height: 32px;
|
|
429
|
+
cursor: pointer;
|
|
430
|
+
touch-action: none;
|
|
431
|
+
outline: none;
|
|
432
|
+
}
|
|
433
|
+
.m3-cp-track {
|
|
434
|
+
position: absolute;
|
|
435
|
+
inset: 4px 0;
|
|
436
|
+
border-radius: 9999px;
|
|
437
|
+
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--color-on-surface) 12%, transparent);
|
|
438
|
+
}
|
|
439
|
+
/* After M3 Expressive's slider handle: a bar taller than the track that narrows while pressed */
|
|
440
|
+
.m3-cp-handle {
|
|
441
|
+
position: absolute;
|
|
442
|
+
top: 0;
|
|
443
|
+
width: 6px;
|
|
444
|
+
height: 32px;
|
|
445
|
+
translate: -50% 0;
|
|
446
|
+
border-radius: 9999px;
|
|
447
|
+
background: #fff;
|
|
448
|
+
box-shadow: 0 0 0 1px rgb(0 0 0 / 0.2), 0 1px 3px rgb(0 0 0 / 0.3);
|
|
449
|
+
pointer-events: none;
|
|
450
|
+
transition: width 150ms cubic-bezier(0.2, 0, 0, 1);
|
|
451
|
+
}
|
|
452
|
+
.m3-cp-handle-pressed {
|
|
453
|
+
width: 4px;
|
|
454
|
+
}
|
|
455
|
+
.group:focus-visible .m3-cp-handle {
|
|
456
|
+
outline: 2px solid var(--color-primary);
|
|
457
|
+
outline-offset: 2px;
|
|
458
|
+
}
|
|
459
|
+
.m3-cp-field {
|
|
460
|
+
height: 40px;
|
|
461
|
+
border-radius: var(--radius-sm);
|
|
462
|
+
border: none;
|
|
463
|
+
background: var(--color-surface-container-highest);
|
|
464
|
+
padding: 0 8px;
|
|
465
|
+
text-align: center;
|
|
466
|
+
color: var(--color-on-surface);
|
|
467
|
+
caret-color: var(--color-primary);
|
|
468
|
+
outline: 2px solid transparent;
|
|
469
|
+
outline-offset: -2px;
|
|
470
|
+
transition: outline-color 150ms;
|
|
471
|
+
}
|
|
472
|
+
.m3-cp-field:hover {
|
|
473
|
+
outline-color: color-mix(in srgb, var(--color-on-surface) 12%, transparent);
|
|
474
|
+
}
|
|
475
|
+
.m3-cp-field:focus {
|
|
476
|
+
outline-color: var(--color-primary);
|
|
477
|
+
}
|
|
478
|
+
</style>
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
|
+
* Portions Copyright The Android Open Source Project, licensed under the Apache License, Version 2.0
|
|
3
|
+
* (dist/THIRD_PARTY_LICENSES.md). Translated from Kotlin to TypeScript and modified for this library.
|
|
4
|
+
*
|
|
2
5
|
* Port of the Compose Material3 carousel engine (androidx compose/material3/carousel:
|
|
3
6
|
* Arrangement.kt, KeylineList.kt, Keylines.kt, Strategy.kt, KeylineSnapPosition.kt), kept close to
|
|
4
7
|
* the original so it can be diffed against upstream. Pure functions, no DOM: MCarousel feeds it the
|