@m3ui-vue/m3ui-vue 0.9.1 → 0.9.2

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.
Files changed (86) hide show
  1. package/README.md +41 -0
  2. package/dist/API.md +199 -54
  3. package/dist/{MTextField-pJU1vFaR.js → MTextField-Ck0oWDMk.js} +8 -2
  4. package/dist/MTextField-Ck0oWDMk.js.map +1 -0
  5. package/dist/api.json +1 -1
  6. package/dist/code-editor.js +1 -1
  7. package/dist/components/MBox.vue.d.ts +3 -3
  8. package/dist/components/MColorPicker.vue.d.ts +13 -1
  9. package/dist/components/MColorPickerModal.vue.d.ts +14 -2
  10. package/dist/components/MDragHandle.vue.d.ts +12 -0
  11. package/dist/components/MEmojiSelector.vue.d.ts +1 -1
  12. package/dist/components/MFab.vue.d.ts +1 -1
  13. package/dist/components/MFabMenu.vue.d.ts +1 -1
  14. package/dist/components/MListItem.vue.d.ts +5 -5
  15. package/dist/components/MOverlay.vue.d.ts +1 -1
  16. package/dist/components/MScrollField.vue.d.ts +40 -0
  17. package/dist/components/MScrollable.vue.d.ts +9 -0
  18. package/dist/components/MSplitter.vue.d.ts +2 -2
  19. package/dist/components/MSwipeToDismiss.vue.d.ts +49 -0
  20. package/dist/components/MTransition.vue.d.ts +34 -0
  21. package/dist/components/MWindow.vue.d.ts +1 -1
  22. package/dist/components/_MColorPanel.vue.d.ts +18 -0
  23. package/dist/composables/_colorUtils.d.ts +34 -0
  24. package/dist/composables/_themeColor.d.ts +1 -0
  25. package/dist/composables/_useSpring.d.ts +15 -0
  26. package/dist/composables/dynamicColor.d.ts +18 -0
  27. package/dist/composables/useColorPalette.d.ts +27 -0
  28. package/dist/composables/useLocale.d.ts +10 -0
  29. package/dist/index.d.ts +9 -1
  30. package/dist/locales/index.js +70 -0
  31. package/dist/locales/index.js.map +1 -1
  32. package/dist/m3ui-vue.css +1 -1
  33. package/dist/m3ui.js +4808 -3622
  34. package/dist/m3ui.js.map +1 -1
  35. package/dist/material-color-utilities-Cktu8DBU.js +3160 -0
  36. package/dist/material-color-utilities-Cktu8DBU.js.map +1 -0
  37. package/dist/plugin.d.ts +18 -0
  38. package/dist/rich-text-editor.js +2 -2
  39. package/dist/styles/palettes.css +546 -0
  40. package/dist/styles/theme.css +77 -0
  41. package/dist/styles.css +1 -1
  42. package/dist/{useLocale-Drihe5G2.js → useLocale-CFW8cIzL.js} +11 -1
  43. package/dist/useLocale-CFW8cIzL.js.map +1 -0
  44. package/package.json +2 -1
  45. package/src/__tests__/MButtonGroup.test.ts +51 -0
  46. package/src/__tests__/MColorPicker.test.ts +141 -0
  47. package/src/__tests__/MDragHandle.test.ts +57 -0
  48. package/src/__tests__/MListItemShape.test.ts +76 -0
  49. package/src/__tests__/MScrollField.test.ts +41 -0
  50. package/src/__tests__/MScrollable.test.ts +42 -0
  51. package/src/__tests__/MSwipeToDismiss.test.ts +104 -0
  52. package/src/__tests__/MTransition.test.ts +27 -0
  53. package/src/__tests__/dynamicColor.test.ts +191 -0
  54. package/src/components/MBox.vue +4 -2
  55. package/src/components/MButtonGroup.vue +116 -35
  56. package/src/components/MColorPicker.vue +114 -210
  57. package/src/components/MColorPickerModal.vue +50 -251
  58. package/src/components/MDragHandle.vue +54 -0
  59. package/src/components/MList.vue +33 -3
  60. package/src/components/MListItem.vue +75 -5
  61. package/src/components/MScrollField.vue +302 -0
  62. package/src/components/MScrollable.vue +160 -5
  63. package/src/components/MSplitter.vue +33 -10
  64. package/src/components/MSwipeToDismiss.vue +247 -0
  65. package/src/components/MTransition.vue +149 -0
  66. package/src/components/_MColorPanel.vue +478 -0
  67. package/src/composables/_colorUtils.ts +107 -0
  68. package/src/composables/_themeColor.ts +33 -0
  69. package/src/composables/_useSpring.ts +89 -0
  70. package/src/composables/dynamicColor.ts +85 -0
  71. package/src/composables/useColorPalette.ts +171 -25
  72. package/src/composables/useFieldBg.ts +6 -0
  73. package/src/composables/useLocale.ts +22 -0
  74. package/src/index.ts +9 -1
  75. package/src/locales/de.ts +10 -0
  76. package/src/locales/es.ts +10 -0
  77. package/src/locales/fr.ts +10 -0
  78. package/src/locales/ja.ts +10 -0
  79. package/src/locales/ko.ts +10 -0
  80. package/src/locales/pt.ts +10 -0
  81. package/src/locales/zh.ts +10 -0
  82. package/src/plugin.ts +28 -7
  83. package/src/styles/palettes.css +546 -0
  84. package/src/styles/theme.css +77 -0
  85. package/dist/MTextField-pJU1vFaR.js.map +0 -1
  86. package/dist/useLocale-Drihe5G2.js.map +0 -1
@@ -5,8 +5,9 @@
5
5
  *
6
6
  * @event update:modelValue Emitted on selection change — powers v-model
7
7
  */
8
- import { computed } from 'vue'
8
+ import { computed, ref } from 'vue'
9
9
  import MIcon from './MIcon.vue'
10
+ import { FAST_SPATIAL, useSpring } from '../composables/_useSpring'
10
11
 
11
12
  export interface ButtonGroupOption {
12
13
  value: unknown
@@ -73,26 +74,20 @@ function toggle(opt: ButtonGroupOption) {
73
74
  }
74
75
  }
75
76
 
77
+ // pad: ButtonDefaults content padding per tier, also the most a neighbor can be compressed by (animateWidth's
78
+ // default compressionLimit). checked/pressed: ToggleButtonDefaults.shapesFor() radii, as in MToggleButton (the
79
+ // standard group's items are ToggleButtons). The button group spec only has the small tier; lg/xl are library
80
+ // sizes and reuse the medium radii.
76
81
  const sizeMap = {
77
- xs: { h: 'h-8', px: 'px-3', text: 'text-label-medium', icon: 16, check: 14 },
78
- sm: { h: 'h-10', px: 'px-4', text: 'text-label-large', icon: 18, check: 16 },
79
- md: { h: 'h-14', px: 'px-6', text: 'text-title-medium', icon: 20, check: 18 },
80
- lg: { h: 'h-16', px: 'px-7', text: 'text-title-large', icon: 22, check: 20 },
81
- xl: { h: 'h-20', px: 'px-8', text: 'text-headline-small', icon: 24, check: 22 },
82
+ xs: { h: 'h-8', px: 'px-3', text: 'text-label-medium', icon: 16, check: 14, height: 32, pad: 12, checked: 12, pressed: 8 },
83
+ sm: { h: 'h-10', px: 'px-4', text: 'text-label-large', icon: 18, check: 16, height: 40, pad: 16, checked: 12, pressed: 6 },
84
+ md: { h: 'h-14', px: 'px-6', text: 'text-title-medium', icon: 20, check: 18, height: 56, pad: 24, checked: 16, pressed: 12 },
85
+ lg: { h: 'h-16', px: 'px-7', text: 'text-title-large', icon: 22, check: 20, height: 64, pad: 28, checked: 16, pressed: 12 },
86
+ xl: { h: 'h-20', px: 'px-8', text: 'text-headline-small', icon: 24, check: 22, height: 80, pad: 32, checked: 16, pressed: 12 },
82
87
  }
83
88
 
84
89
  const s = computed(() => sizeMap[props.size] ?? sizeMap.sm)
85
90
 
86
- const fullRadius = computed(() => {
87
- const hMap = { xs: '16px', sm: '20px', md: '28px', lg: '32px', xl: '40px' }
88
- return hMap[props.size] ?? '20px'
89
- })
90
-
91
- const transitionMs = computed(() => {
92
- const msMap = { xs: 350, sm: 300, md: 250, lg: 200, xl: 180 }
93
- return msMap[props.size] ?? 250
94
- })
95
-
96
91
  const selectedColors = computed(() => {
97
92
  const map = {
98
93
  primary: 'bg-primary text-on-primary',
@@ -115,25 +110,108 @@ function createRipple(event: PointerEvent) {
115
110
  el.addEventListener('animationend', () => el.remove(), { once: true })
116
111
  }
117
112
 
118
- function btnStyle(i: number, selected: boolean) {
119
- const count = props.options.length
120
- const full = fullRadius.value
121
- const inner = '8px'
122
- let tl: string, tr: string, br: string, bl: string
123
- if (props.variant === 'standard' || selected) {
124
- tl = tr = br = bl = full
125
- } else {
126
- tl = bl = i === 0 ? full : inner
127
- tr = br = i === count - 1 ? full : inner
113
+ const buttons: (HTMLElement | null)[] = []
114
+ const pressedIndex = ref(-1)
115
+
116
+ function onPointerDown(e: PointerEvent, i: number) {
117
+ createRipple(e)
118
+ if (e.button !== 0 || props.options[i]?.disabled) return
119
+ measureWidths()
120
+ pressedIndex.value = i
121
+ }
122
+ function onPointerUp() {
123
+ pressedIndex.value = -1
124
+ }
125
+
126
+ // ── Shapes ──────────────────────────────────────────────────────────────────────────────────
127
+ // Both variants morph like ToggleButton's shapeByInteraction(): pressed > checked > resting, on
128
+ // MotionSchemeKeyTokens.FastSpatial.
129
+ // Standard: ToggleButtonDefaults shapes, a pill that squares off when checked and more when pressed.
130
+ // Connected (ButtonGroupDefaults.connected*ButtonShapes, ConnectedButtonGroupSmallTokens): outer corners
131
+ // full, inner corners InnerCornerCornerSize (8dp) → PressedInnerCornerCornerSize (4dp) while pressed, and a
132
+ // checked button becomes a full pill (connectedButtonCheckedShape = CornerFull).
133
+ const CONNECTED_INNER = 8
134
+ const CONNECTED_PRESSED_INNER = 4
135
+
136
+ function radiiFor(i: number): number[] {
137
+ const full = s.value.height / 2
138
+ const pressed = pressedIndex.value === i
139
+ const checked = isSelected(props.options[i]!.value)
140
+ if (props.variant === 'standard') {
141
+ const r = pressed ? s.value.pressed : checked ? s.value.checked : full
142
+ return [r, r, r, r]
128
143
  }
129
- return {
130
- borderTopLeftRadius: tl,
131
- borderTopRightRadius: tr,
132
- borderBottomRightRadius: br,
133
- borderBottomLeftRadius: bl,
134
- transition: `border-top-left-radius ${transitionMs.value}ms ease, border-top-right-radius ${transitionMs.value}ms ease, border-bottom-right-radius ${transitionMs.value}ms ease, border-bottom-left-radius ${transitionMs.value}ms ease, background-color ${transitionMs.value}ms ease, color ${transitionMs.value}ms ease`,
144
+ if (!pressed && checked) return [full, full, full, full]
145
+ const inner = pressed ? CONNECTED_PRESSED_INNER : CONNECTED_INNER
146
+ const start = i === 0 ? full : inner
147
+ const end = i === props.options.length - 1 ? full : inner
148
+ return [start, end, end, start]
149
+ }
150
+
151
+ useSpring(
152
+ () => props.options.flatMap((_, i) => radiiFor(i)),
153
+ (v) => {
154
+ buttons.forEach((el, i) => {
155
+ if (el) el.style.borderRadius = `${v[i * 4]}px ${v[i * 4 + 1]}px ${v[i * 4 + 2]}px ${v[i * 4 + 3]}px`
156
+ })
157
+ },
158
+ FAST_SPATIAL,
159
+ )
160
+
161
+ // ── Width: the pressed button grows, its neighbors give way (standard only) ──────────────────
162
+ // ButtonGroup's layout with ButtonGroupScope.animateWidth: the pressed item grows by expandedRatio
163
+ // (ButtonGroupDefaults.ExpandedRatio = 0.15) of its width, taken from its neighbors and capped by how much
164
+ // each can be compressed (its content padding). A middle item splits the growth between both sides.
165
+ // The connected samples don't use animateWidth, so the connected variant keeps its widths.
166
+ const EXPANDED_RATIO = 0.15
167
+ let natural: number[] = []
168
+ let widthsActive = false
169
+
170
+ function measureWidths() {
171
+ if (widthsActive) return
172
+ natural = props.options.map((_, i) => buttons[i]?.offsetWidth ?? 0)
173
+ }
174
+
175
+ function widthsFor(progress: readonly number[]): number[] {
176
+ const w = [...natural]
177
+ const pad = s.value.pad
178
+ const last = w.length - 1
179
+ for (let i = 0; i < w.length; i++) {
180
+ const p = progress[i] ?? 0
181
+ if (p === 0) continue
182
+ let growth: number
183
+ if (i > 0 && i < last) {
184
+ const g = Math.round(p * Math.min((EXPANDED_RATIO * w[i]!) / 2, pad))
185
+ const left = Math.min(g, w[i - 1]!)
186
+ const right = Math.min(g, w[i + 1]!)
187
+ w[i - 1]! -= left
188
+ w[i + 1]! -= right
189
+ growth = left + right
190
+ } else if (i === 0) {
191
+ const g = Math.round(p * Math.min(EXPANDED_RATIO * w[i]!, pad))
192
+ growth = Math.min(g, w[i + 1]!)
193
+ w[i + 1]! -= growth
194
+ } else {
195
+ const g = Math.round(p * Math.min(EXPANDED_RATIO * w[i]!, pad))
196
+ growth = Math.min(g, w[i - 1]!)
197
+ w[i - 1]! -= growth
198
+ }
199
+ w[i]! += growth
135
200
  }
201
+ return w
136
202
  }
203
+
204
+ useSpring(
205
+ () => props.options.map((_, i) => (props.variant === 'standard' && props.options.length > 1 && pressedIndex.value === i ? 1 : 0)),
206
+ (progress) => {
207
+ widthsActive = progress.some((p) => p !== 0)
208
+ const w = widthsActive && natural.length === props.options.length ? widthsFor(progress) : null
209
+ buttons.forEach((el, i) => {
210
+ if (el) el.style.width = w ? `${w[i]}px` : ''
211
+ })
212
+ },
213
+ FAST_SPATIAL,
214
+ )
137
215
  </script>
138
216
 
139
217
  <template>
@@ -146,16 +224,19 @@ function btnStyle(i: number, selected: boolean) {
146
224
  v-for="(opt, i) in options"
147
225
  :key="String(opt.value)"
148
226
  type="button"
149
- class="btn-group-item relative inline-flex items-center justify-center gap-2 overflow-hidden font-medium"
227
+ :ref="(el) => { buttons[i] = el as HTMLElement | null }"
228
+ class="btn-group-item relative inline-flex shrink-0 items-center justify-center gap-2 overflow-hidden font-medium whitespace-nowrap transition-[background-color,color] duration-200"
150
229
  :class="[
151
230
  s.h, s.px, s.text,
152
231
  opt.disabled ? 'cursor-not-allowed opacity-[0.38]' : 'cursor-pointer',
153
232
  isSelected(opt.value) ? selectedColors : unselectedColors,
154
233
  ]"
155
- :style="btnStyle(i, isSelected(opt.value))"
156
234
  :disabled="opt.disabled"
157
235
  :aria-pressed="isSelected(opt.value)"
158
- @pointerdown="createRipple"
236
+ @pointerdown="onPointerDown($event, i)"
237
+ @pointerup="onPointerUp"
238
+ @pointerleave="onPointerUp"
239
+ @pointercancel="onPointerUp"
159
240
  @click="toggle(opt)"
160
241
  >
161
242
  <MIcon
@@ -1,20 +1,33 @@
1
1
  <script setup lang="ts">
2
2
  /**
3
- * An inline, dropdown-anchored color picker — hue slider, saturation/brightness area, and preset swatches.
3
+ * An outlined field that opens a color picker in a dropdown: saturation/brightness area, hue (and optional
4
+ * opacity) slider, eyedropper, HEX/RGB/HSL fields, recent colors and preset swatches. Updates the value live
5
+ * while picking.
4
6
  *
5
- * @event update:modelValue Emitted with the picked color.
7
+ * @event update:modelValue Emitted with the picked color, live while dragging or typing.
6
8
  */
7
- import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
9
+ import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
8
10
  import MIcon from './MIcon.vue'
11
+ import MColorPanel from './_MColorPanel.vue'
9
12
  import { useFieldBg } from '../composables/useFieldBg'
13
+ import { useLocale } from '../composables/useLocale'
14
+ import { defaultColorPresets, normalizeHex, pushRecentColor } from '../composables/_colorUtils'
10
15
 
11
16
  const props = withDefaults(defineProps<{
12
- /** Selected color in hex format (v-model) */
17
+ /** Selected color in hex format (v-model). `#rrggbb`, or `#rrggbbaa` when `alpha` is on and the color isn't opaque. */
13
18
  modelValue: string
14
19
  /** Field label */
15
20
  label?: string
16
21
  /** Preset color swatches shown in the panel */
17
22
  presets?: string[]
23
+ /** Adds an opacity slider and an alpha channel to the value */
24
+ alpha?: boolean
25
+ /** Remember picked colors and show them above the presets (stored in localStorage, shared by every picker). */
26
+ recent?: boolean
27
+ /** How many recent colors to keep. */
28
+ recentLimit?: number
29
+ /** Show a button to pick a color from the screen, where the browser supports it (Chrome, Edge). */
30
+ eyedropper?: boolean
18
31
  /** Disables the picker */
19
32
  disabled?: boolean
20
33
  /** Error state */
@@ -26,150 +39,88 @@ const props = withDefaults(defineProps<{
26
39
  /** Overrides the auto-detected background color used to cut the floating label out of the border */
27
40
  fieldBg?: string
28
41
  }>(), {
29
- presets: () => [
30
- '#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
31
- '#03a9f4', '#00bcd4', '#009688', '#4caf50', '#8bc34a', '#cddc39',
32
- '#ffeb3b', '#ffc107', '#ff9800', '#ff5722', '#795548', '#607d8b',
33
- ],
42
+ presets: () => defaultColorPresets,
43
+ alpha: false,
44
+ recent: true,
45
+ recentLimit: 7,
46
+ eyedropper: true,
34
47
  })
35
48
 
36
49
  const emit = defineEmits<{ 'update:modelValue': [string] }>()
50
+ const t = useLocale()
37
51
 
38
52
  const open = ref(false)
39
53
  const triggerEl = ref<HTMLElement | null>(null)
54
+ const buttonEl = ref<HTMLButtonElement | null>(null)
40
55
  const panelEl = ref<HTMLElement | null>(null)
56
+ const colorPanel = ref<InstanceType<typeof MColorPanel> | null>(null)
41
57
  const dropPos = ref({ top: '0px', left: '0px' })
42
58
  const { resolvedFieldBg } = useFieldBg(triggerEl, () => props.fieldBg)
43
59
 
44
- const satBrightEl = ref<HTMLElement>()
45
- const draggingSB = ref(false)
60
+ // The value when the panel opened: shown as the "before" half of the preview, and compared on close to
61
+ // decide whether the color goes into the recents
62
+ const original = ref('')
46
63
 
47
- // HSV state
48
- const hue = ref(0)
49
- const sat = ref(100)
50
- const bright = ref(100)
51
-
52
- function hexToHsv(hex: string) {
53
- let c = hex.replace('#', '')
54
- if (c.length === 3) c = c[0]!+c[0]!+c[1]!+c[1]!+c[2]!+c[2]!
55
- const r = parseInt(c.substring(0, 2), 16) / 255
56
- const g = parseInt(c.substring(2, 4), 16) / 255
57
- const b = parseInt(c.substring(4, 6), 16) / 255
58
- const max = Math.max(r, g, b), min = Math.min(r, g, b)
59
- const d = max - min
60
- let h = 0
61
- if (d !== 0) {
62
- if (max === r) h = ((g - b) / d + 6) % 6
63
- else if (max === g) h = (b - r) / d + 2
64
- else h = (r - g) / d + 4
65
- h *= 60
66
- }
67
- const s = max === 0 ? 0 : (d / max) * 100
68
- const v = max * 100
69
- return { h, s, v }
70
- }
71
-
72
- function hsvToHex(h: number, s: number, v: number) {
73
- s /= 100; v /= 100
74
- const c = v * s
75
- const x = c * (1 - Math.abs((h / 60) % 2 - 1))
76
- const m = v - c
77
- let r = 0, g = 0, b = 0
78
- if (h < 60) { r = c; g = x }
79
- else if (h < 120) { r = x; g = c }
80
- else if (h < 180) { g = c; b = x }
81
- else if (h < 240) { g = x; b = c }
82
- else if (h < 300) { r = x; b = c }
83
- else { r = c; b = x }
84
- const toHex = (n: number) => Math.round((n + m) * 255).toString(16).padStart(2, '0')
85
- return `#${toHex(r)}${toHex(g)}${toHex(b)}`
86
- }
87
-
88
- function syncFromProp() {
89
- const hsv = hexToHsv(props.modelValue)
90
- hue.value = hsv.h
91
- sat.value = hsv.s
92
- bright.value = hsv.v
93
- }
94
- syncFromProp()
95
-
96
- watch(() => props.modelValue, syncFromProp)
97
-
98
- function emitColor() {
99
- emit('update:modelValue', hsvToHex(hue.value, sat.value, bright.value))
100
- }
101
-
102
- const currentHex = computed(() => hsvToHex(hue.value, sat.value, bright.value))
103
- const hueColor = computed(() => `hsl(${hue.value}, 100%, 50%)`)
104
-
105
- function onSBPointerDown(e: PointerEvent) {
106
- e.preventDefault()
107
- draggingSB.value = true
108
- updateSB(e)
109
- ;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
110
- }
111
- function onSBPointerMove(e: PointerEvent) {
112
- if (!draggingSB.value) return
113
- updateSB(e)
114
- }
115
- function onSBPointerUp() {
116
- draggingSB.value = false
117
- emitColor()
118
- }
119
- function updateSB(e: PointerEvent) {
120
- if (!satBrightEl.value) return
121
- const rect = satBrightEl.value.getBoundingClientRect()
122
- sat.value = Math.max(0, Math.min(100, ((e.clientX - rect.left) / rect.width) * 100))
123
- bright.value = Math.max(0, Math.min(100, (1 - (e.clientY - rect.top) / rect.height) * 100))
124
- }
125
-
126
- function onHueInput(e: Event) {
127
- hue.value = Number((e.target as HTMLInputElement).value)
128
- emitColor()
129
- }
130
-
131
- function selectPreset(color: string) {
132
- emit('update:modelValue', color)
133
- }
134
-
135
- function onHexInput(e: Event) {
136
- const v = (e.target as HTMLInputElement).value
137
- if (/^#[0-9a-fA-F]{6}$/.test(v)) {
138
- emit('update:modelValue', v)
139
- }
140
- }
64
+ const GAP = 4
65
+ const MARGIN = 8
141
66
 
142
67
  function computeDropPos() {
143
68
  if (!triggerEl.value) return
144
69
  const rect = triggerEl.value.getBoundingClientRect()
145
- const panelH = 380
146
- const spaceBelow = window.innerHeight - rect.bottom - 8
147
- const above = spaceBelow < panelH && rect.top > panelH
148
- dropPos.value = {
149
- top: above ? `${rect.top - 4 - panelH}px` : `${rect.bottom + 4}px`,
150
- left: `${rect.left}px`,
151
- }
70
+ const w = panelEl.value?.offsetWidth ?? 312
71
+ const ph = panelEl.value?.offsetHeight ?? 480
72
+ const below = window.innerHeight - rect.bottom - GAP - MARGIN
73
+ const top = below >= ph || rect.top - GAP - MARGIN < ph
74
+ ? Math.max(MARGIN, Math.min(rect.bottom + GAP, window.innerHeight - ph - MARGIN))
75
+ : rect.top - GAP - ph
76
+ const left = Math.max(MARGIN, Math.min(rect.left, window.innerWidth - w - MARGIN))
77
+ dropPos.value = { top: `${top}px`, left: `${left}px` }
152
78
  }
153
79
 
154
- function onOut(e: MouseEvent) {
155
- const t = e.target as Node
156
- if (triggerEl.value?.contains(t)) return
157
- if (panelEl.value?.contains(t)) return
80
+ function close(refocus = false) {
81
+ if (!open.value) return
158
82
  open.value = false
83
+ if (refocus) buttonEl.value?.focus()
84
+ }
85
+
86
+ function onOut(e: MouseEvent) {
87
+ const target = e.target as Node
88
+ if (triggerEl.value?.contains(target) || panelEl.value?.contains(target)) return
89
+ close()
159
90
  }
160
91
 
161
92
  function onScroll(e: Event) {
162
93
  if (!open.value) return
163
94
  if (panelEl.value?.contains(e.target as Node)) return
164
- open.value = false
95
+ close()
165
96
  }
166
97
 
167
- watch(open, (v) => {
98
+ function onPanelKeydown(e: KeyboardEvent) {
99
+ if (e.key === 'Escape') {
100
+ e.stopPropagation()
101
+ close(true)
102
+ }
103
+ }
104
+
105
+ // The panel is teleported to <body>, so tabbing out of it lands outside the field: close when focus leaves
106
+ function onPanelFocusOut(e: FocusEvent) {
107
+ const to = e.relatedTarget as Node | null
108
+ if (!to || panelEl.value?.contains(to) || triggerEl.value?.contains(to)) return
109
+ close()
110
+ }
111
+
112
+ watch(open, async (v) => {
168
113
  if (v) {
114
+ original.value = normalizeHex(props.modelValue)
169
115
  computeDropPos()
116
+ await nextTick()
117
+ computeDropPos()
118
+ colorPanel.value?.focus()
170
119
  setTimeout(() => document.addEventListener('mousedown', onOut), 0)
171
120
  } else {
172
121
  document.removeEventListener('mousedown', onOut)
122
+ const picked = normalizeHex(props.modelValue)
123
+ if (props.recent && picked && picked !== original.value) pushRecentColor(picked, props.recentLimit)
173
124
  }
174
125
  })
175
126
 
@@ -184,23 +135,34 @@ onUnmounted(() => {
184
135
  <div class="flex flex-col gap-1">
185
136
  <div ref="triggerEl" class="relative mt-2" :style="{ '--field-bg': resolvedFieldBg }">
186
137
  <button
138
+ ref="buttonEl"
187
139
  type="button"
188
- class="flex h-14 w-full items-center gap-3 rounded-xs border bg-transparent px-4 text-left text-body-large transition-[border-color,border-width] duration-150"
140
+ aria-haspopup="dialog"
141
+ :aria-expanded="open"
142
+ :disabled="disabled"
143
+ class="flex h-14 w-full items-center gap-3 rounded-xs border bg-transparent px-4 text-left text-body-large outline-none transition-[border-color,border-width] duration-150"
189
144
  :class="[
190
- disabled ? 'pointer-events-none cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
145
+ disabled ? 'cursor-not-allowed border-on-surface/12' : 'cursor-pointer',
191
146
  !disabled && (open
192
147
  ? error ? 'border-2 border-error' : 'border-2 border-primary'
193
- : error ? 'border-error' : 'border-outline hover:border-on-surface'),
148
+ : error
149
+ ? 'border-error focus-visible:border-2'
150
+ : 'border-outline hover:border-on-surface focus-visible:border-2 focus-visible:border-primary'),
194
151
  ]"
195
- @click="!disabled && (open = !open)"
152
+ @click="open = !open"
196
153
  >
197
154
  <span
198
- class="h-6 w-6 shrink-0 rounded-full border border-outline-variant"
155
+ class="m3-cp-swatch h-6 w-6 shrink-0 rounded-full"
199
156
  :class="disabled ? 'opacity-38' : ''"
200
- :style="{ backgroundColor: modelValue }"
157
+ :style="{ background: `linear-gradient(${modelValue}, ${modelValue}), repeating-conic-gradient(#d4d4d4 0 25%, #fff 0 50%) 0 0 / 8px 8px` }"
201
158
  />
202
159
  <span class="flex-1 font-mono" :class="disabled ? 'text-on-surface/38' : 'text-on-surface'">{{ modelValue }}</span>
203
- <MIcon name="palette" :size="24" class="shrink-0" :class="disabled ? 'text-on-surface/38' : 'text-on-surface-variant'" />
160
+ <MIcon
161
+ name="palette"
162
+ :size="24"
163
+ class="shrink-0 transition-colors"
164
+ :class="disabled ? 'text-on-surface/38' : open ? (error ? 'text-error' : 'text-primary') : 'text-on-surface-variant'"
165
+ />
204
166
  </button>
205
167
  <label
206
168
  v-if="label"
@@ -215,78 +177,27 @@ onUnmounted(() => {
215
177
  <p v-else-if="hint" class="px-4 text-body-small text-on-surface-variant">{{ hint }}</p>
216
178
 
217
179
  <Teleport to="body">
218
- <Transition
219
- enter-active-class="transition-[opacity,transform] duration-150"
220
- enter-from-class="opacity-0 -translate-y-1 scale-[0.98]"
221
- leave-active-class="transition-[opacity,transform] duration-100"
222
- leave-to-class="opacity-0 -translate-y-1 scale-[0.98]"
223
- >
180
+ <Transition name="m3-cp-drop">
224
181
  <div
225
182
  v-if="open"
226
183
  ref="panelEl"
227
- class="fixed z-500 w-[280px] rounded-lg bg-surface-container p-4 shadow-elevation-3"
184
+ role="dialog"
185
+ :aria-label="label || t.selectColor"
186
+ class="fixed z-500 w-[312px] max-w-[calc(100vw-16px)] rounded-lg bg-surface-container p-4 shadow-elevation-2"
228
187
  :style="dropPos"
188
+ @keydown="onPanelKeydown"
189
+ @focusout="onPanelFocusOut"
229
190
  >
230
- <!-- Saturation / Brightness area -->
231
- <div
232
- ref="satBrightEl"
233
- class="relative mb-3 h-40 w-full cursor-crosshair overflow-hidden rounded-lg"
234
- :style="{ background: `linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, ${hueColor})`, touchAction: 'none' }"
235
- @pointerdown="onSBPointerDown"
236
- @pointermove="onSBPointerMove"
237
- @pointerup="onSBPointerUp"
238
- >
239
- <div
240
- class="pointer-events-none absolute h-4 w-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white shadow-elevation-1"
241
- :style="{
242
- left: `${sat}%`,
243
- top: `${100 - bright}%`,
244
- backgroundColor: currentHex,
245
- }"
246
- />
247
- </div>
248
-
249
- <!-- Hue slider -->
250
- <div class="mb-3">
251
- <input
252
- type="range"
253
- min="0"
254
- max="360"
255
- :value="hue"
256
- class="hue-slider h-3 w-full cursor-pointer appearance-none rounded-full outline-none"
257
- @input="onHueInput"
258
- />
259
- </div>
260
-
261
- <!-- Preview + hex input -->
262
- <div class="mb-3 flex items-center gap-3">
263
- <span
264
- class="h-9 w-9 shrink-0 rounded-full border border-outline-variant"
265
- :style="{ backgroundColor: currentHex }"
266
- />
267
- <input
268
- type="text"
269
- :value="modelValue"
270
- maxlength="7"
271
- class="flex-1 rounded-xs border border-outline bg-transparent px-3 py-2 font-mono text-body-medium text-on-surface outline-none caret-primary transition-colors focus:border-primary"
272
- @input="onHexInput"
273
- />
274
- </div>
275
-
276
- <!-- Presets -->
277
- <div class="flex flex-wrap gap-1.5">
278
- <button
279
- v-for="color in presets"
280
- :key="color"
281
- type="button"
282
- class="flex h-7 w-7 cursor-pointer items-center justify-center rounded-full border transition-transform duration-100 hover:scale-110"
283
- :class="color === modelValue ? 'border-on-surface' : 'border-transparent'"
284
- :style="{ backgroundColor: color }"
285
- @click="selectPreset(color)"
286
- >
287
- <MIcon v-if="color === modelValue" name="check" :size="14" class="text-white drop-shadow-[0_1px_1px_rgba(0,0,0,0.5)]" />
288
- </button>
289
- </div>
191
+ <MColorPanel
192
+ ref="colorPanel"
193
+ :model-value="modelValue"
194
+ :presets="presets"
195
+ :alpha="alpha"
196
+ :recent="recent"
197
+ :eyedropper="eyedropper"
198
+ :original="original"
199
+ @update:model-value="emit('update:modelValue', $event)"
200
+ />
290
201
  </div>
291
202
  </Transition>
292
203
  </Teleport>
@@ -294,25 +205,18 @@ onUnmounted(() => {
294
205
  </template>
295
206
 
296
207
  <style scoped>
297
- .hue-slider {
298
- background: linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);
208
+ .m3-cp-swatch {
209
+ box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--color-on-surface) 12%, transparent);
210
+ }
211
+ .m3-cp-drop-enter-active {
212
+ transition: opacity 150ms cubic-bezier(0.2, 0, 0, 1), transform 250ms cubic-bezier(0.2, 0, 0, 1);
299
213
  }
300
- .hue-slider::-webkit-slider-thumb {
301
- -webkit-appearance: none;
302
- width: 16px;
303
- height: 16px;
304
- border-radius: 50%;
305
- background: white;
306
- box-shadow: 0 1px 3px rgba(0,0,0,0.4);
307
- cursor: pointer;
214
+ .m3-cp-drop-leave-active {
215
+ transition: opacity 100ms ease, transform 100ms ease;
308
216
  }
309
- .hue-slider::-moz-range-thumb {
310
- width: 16px;
311
- height: 16px;
312
- border-radius: 50%;
313
- background: white;
314
- box-shadow: 0 1px 3px rgba(0,0,0,0.4);
315
- border: none;
316
- cursor: pointer;
217
+ .m3-cp-drop-enter-from,
218
+ .m3-cp-drop-leave-to {
219
+ opacity: 0;
220
+ transform: translateY(-4px) scale(0.98);
317
221
  }
318
222
  </style>