@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.
Files changed (139) hide show
  1. package/README.md +43 -0
  2. package/dist/API.md +266 -60
  3. package/dist/{MIcon-CaEooCmZ.js → MIcon-DLOkQduI.js} +10 -3
  4. package/dist/MIcon-DLOkQduI.js.map +1 -0
  5. package/dist/{MTextField-pJU1vFaR.js → MTextField-DKtgPymd.js} +422 -206
  6. package/dist/MTextField-DKtgPymd.js.map +1 -0
  7. package/dist/THIRD_PARTY_LICENSES.md +547 -0
  8. package/dist/api.json +1 -1
  9. package/dist/code-editor.js +2 -2
  10. package/dist/components/MBox.vue.d.ts +3 -3
  11. package/dist/components/MButton.vue.d.ts +4 -2
  12. package/dist/components/MButtonGroup.vue.d.ts +2 -0
  13. package/dist/components/MColorPicker.vue.d.ts +13 -1
  14. package/dist/components/MColorPickerModal.vue.d.ts +14 -2
  15. package/dist/components/MDragHandle.vue.d.ts +12 -0
  16. package/dist/components/MEmojiSelector.vue.d.ts +1 -1
  17. package/dist/components/MFab.vue.d.ts +5 -3
  18. package/dist/components/MFabMenu.vue.d.ts +1 -1
  19. package/dist/components/MIcon.vue.d.ts +3 -0
  20. package/dist/components/MIconButton.vue.d.ts +15 -1
  21. package/dist/components/MListItem.vue.d.ts +5 -5
  22. package/dist/components/MMenuItem.vue.d.ts +17 -4
  23. package/dist/components/MOverlay.vue.d.ts +1 -1
  24. package/dist/components/MScrollField.vue.d.ts +40 -0
  25. package/dist/components/MScrollable.vue.d.ts +9 -0
  26. package/dist/components/MSplitButton.vue.d.ts +2 -0
  27. package/dist/components/MSplitter.vue.d.ts +2 -2
  28. package/dist/components/MSwipeToDismiss.vue.d.ts +49 -0
  29. package/dist/components/MTabs.vue.d.ts +6 -0
  30. package/dist/components/MTimePickerModal.vue.d.ts +3 -0
  31. package/dist/components/MTimeScroll.vue.d.ts +22 -0
  32. package/dist/components/MToggleButton.vue.d.ts +2 -0
  33. package/dist/components/MTransition.vue.d.ts +34 -0
  34. package/dist/components/MTree.vue.d.ts +2 -2
  35. package/dist/components/MWindow.vue.d.ts +1 -1
  36. package/dist/components/_MColorPanel.vue.d.ts +18 -0
  37. package/dist/components/_MDrawerItemList.vue.d.ts +3 -0
  38. package/dist/composables/_colorUtils.d.ts +34 -0
  39. package/dist/composables/_themeColor.d.ts +1 -0
  40. package/dist/composables/_useSpring.d.ts +17 -0
  41. package/dist/composables/dynamicColor.d.ts +18 -0
  42. package/dist/composables/useColorPalette.d.ts +27 -0
  43. package/dist/composables/useLocale.d.ts +16 -0
  44. package/dist/directives/vRipple.d.ts +16 -0
  45. package/dist/emoji-names-de-BphO5XpV.js.map +1 -1
  46. package/dist/emoji-names-en-9ZvMRvzi.js.map +1 -1
  47. package/dist/emoji-names-es-DvfWU3Pj.js.map +1 -1
  48. package/dist/emoji-names-fr-B-jhUlsP.js.map +1 -1
  49. package/dist/emoji-names-ja-l8cMFJo1.js.map +1 -1
  50. package/dist/emoji-names-ko-YP-o9G0c.js.map +1 -1
  51. package/dist/emoji-names-pt-DGj_Js4M.js.map +1 -1
  52. package/dist/emoji-names-zh-Dzecwtw_.js.map +1 -1
  53. package/dist/index.d.ts +12 -1
  54. package/dist/locales/index.js +112 -0
  55. package/dist/locales/index.js.map +1 -1
  56. package/dist/m3ui-vue.css +1 -1
  57. package/dist/m3ui.js +7443 -6150
  58. package/dist/m3ui.js.map +1 -1
  59. package/dist/material-color-utilities-Cktu8DBU.js +3160 -0
  60. package/dist/material-color-utilities-Cktu8DBU.js.map +1 -0
  61. package/dist/plugin.d.ts +18 -0
  62. package/dist/rich-text-editor.js +22 -22
  63. package/dist/styles/palettes.css +546 -0
  64. package/dist/styles/theme.css +90 -0
  65. package/dist/styles.css +1 -1
  66. package/dist/terminal.js +1 -1
  67. package/dist/{useLocale-Drihe5G2.js → useLocale-Cn9MVIdw.js} +17 -1
  68. package/dist/useLocale-Cn9MVIdw.js.map +1 -0
  69. package/package.json +3 -2
  70. package/src/__tests__/MButtonGroup.test.ts +51 -0
  71. package/src/__tests__/MColorPicker.test.ts +141 -0
  72. package/src/__tests__/MDragHandle.test.ts +57 -0
  73. package/src/__tests__/MIconButton.test.ts +60 -0
  74. package/src/__tests__/MListItemShape.test.ts +76 -0
  75. package/src/__tests__/MMenuItem.test.ts +84 -0
  76. package/src/__tests__/MScrollField.test.ts +69 -0
  77. package/src/__tests__/MScrollable.test.ts +42 -0
  78. package/src/__tests__/MSwipeToDismiss.test.ts +104 -0
  79. package/src/__tests__/MTabs.test.ts +39 -0
  80. package/src/__tests__/MTimePickerModal.test.ts +52 -0
  81. package/src/__tests__/MTimeScroll.test.ts +44 -0
  82. package/src/__tests__/MTransition.test.ts +27 -0
  83. package/src/__tests__/dynamicColor.test.ts +191 -0
  84. package/src/__tests__/vRipple.test.ts +92 -0
  85. package/src/components/MBox.vue +4 -2
  86. package/src/components/MButton.vue +4 -13
  87. package/src/components/MButtonGroup.vue +118 -44
  88. package/src/components/MColorPicker.vue +114 -210
  89. package/src/components/MColorPickerModal.vue +50 -251
  90. package/src/components/MDragHandle.vue +54 -0
  91. package/src/components/MFab.vue +8 -15
  92. package/src/components/MIcon.vue +4 -2
  93. package/src/components/MIconButton.vue +96 -24
  94. package/src/components/MList.vue +33 -3
  95. package/src/components/MListItem.vue +75 -5
  96. package/src/components/MMenuItem.vue +115 -13
  97. package/src/components/MScrollField.vue +310 -0
  98. package/src/components/MScrollable.vue +160 -5
  99. package/src/components/MSplitButton.vue +5 -12
  100. package/src/components/MSplitter.vue +33 -10
  101. package/src/components/MSwipeToDismiss.vue +247 -0
  102. package/src/components/MTabs.vue +16 -5
  103. package/src/components/MTimePickerModal.vue +48 -8
  104. package/src/components/MTimeScroll.vue +161 -0
  105. package/src/components/MToggleButton.vue +5 -12
  106. package/src/components/MTransition.vue +149 -0
  107. package/src/components/_MColorPanel.vue +478 -0
  108. package/src/composables/_carouselStrategy.ts +3 -0
  109. package/src/composables/_colorUtils.ts +107 -0
  110. package/src/composables/_themeColor.ts +33 -0
  111. package/src/composables/_useSpring.ts +91 -0
  112. package/src/composables/dynamicColor.ts +85 -0
  113. package/src/composables/useColorPalette.ts +171 -25
  114. package/src/composables/useFieldBg.ts +6 -0
  115. package/src/composables/useLocale.ts +34 -0
  116. package/src/data/emoji-names/emoji-names-de.ts +1 -0
  117. package/src/data/emoji-names/emoji-names-en.ts +1 -0
  118. package/src/data/emoji-names/emoji-names-es.ts +1 -0
  119. package/src/data/emoji-names/emoji-names-fr.ts +1 -0
  120. package/src/data/emoji-names/emoji-names-ja.ts +1 -0
  121. package/src/data/emoji-names/emoji-names-ko.ts +1 -0
  122. package/src/data/emoji-names/emoji-names-pt.ts +1 -0
  123. package/src/data/emoji-names/emoji-names-zh.ts +1 -0
  124. package/src/data/emojis.ts +1 -0
  125. package/src/directives/vRipple.ts +73 -0
  126. package/src/index.ts +14 -1
  127. package/src/locales/de.ts +16 -0
  128. package/src/locales/es.ts +16 -0
  129. package/src/locales/fr.ts +16 -0
  130. package/src/locales/ja.ts +16 -0
  131. package/src/locales/ko.ts +16 -0
  132. package/src/locales/pt.ts +16 -0
  133. package/src/locales/zh.ts +16 -0
  134. package/src/plugin.ts +30 -7
  135. package/src/styles/palettes.css +546 -0
  136. package/src/styles/theme.css +90 -0
  137. package/dist/MIcon-CaEooCmZ.js.map +0 -1
  138. package/dist/MTextField-pJU1vFaR.js.map +0 -1
  139. package/dist/useLocale-Drihe5G2.js.map +0 -1
@@ -0,0 +1,191 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
+ import { createApp, defineComponent, h, nextTick } from 'vue'
3
+ import { mount } from '@vue/test-utils'
4
+ import { generateScheme, schemeRoles, schemeToCss } from '../composables/dynamicColor'
5
+
6
+ describe('generateScheme', () => {
7
+ it('builds every role for light and dark, with the official tonal spot values', async () => {
8
+ const scheme = await generateScheme('#6750A4')
9
+ expect(Object.keys(scheme.light)).toEqual([...schemeRoles])
10
+ expect(Object.keys(scheme.dark)).toEqual([...schemeRoles])
11
+ // SchemeTonalSpot of the M3 baseline seed (material-color-utilities)
12
+ expect(scheme.light.primary).toBe('#65558f')
13
+ expect(scheme.light['primary-container']).toBe('#e9ddff')
14
+ })
15
+
16
+ it('keeps fixed roles the same in light and dark', async () => {
17
+ const { light, dark } = await generateScheme('#0b57d0')
18
+ for (const role of schemeRoles.filter((r) => r.includes('fixed'))) expect(dark[role]).toBe(light[role])
19
+ })
20
+
21
+ it('honors variant and contrast', async () => {
22
+ const base = await generateScheme('#0b57d0')
23
+ const mono = await generateScheme('#0b57d0', { variant: 'monochrome' })
24
+ const high = await generateScheme('#0b57d0', { contrast: 1 })
25
+ expect(mono.light.primary).not.toBe(base.light.primary)
26
+ expect(high.light['on-surface-variant']).not.toBe(base.light['on-surface-variant'])
27
+ })
28
+
29
+ it('writes CSS blocks like palettes.css', async () => {
30
+ const css = schemeToCss({ light: { primary: '#111111' }, dark: { primary: '#eeeeee' } }, 'brand')
31
+ expect(css).toContain("[data-palette='brand'] {\n --color-primary: #111111;\n}")
32
+ expect(css).toContain("[data-palette='brand'].dark {\n color-scheme: dark;\n --color-primary: #eeeeee;\n}")
33
+ })
34
+ })
35
+
36
+ // useColorPalette keeps module-level state: load a fresh copy of it (and the plugin) per test
37
+ async function fresh() {
38
+ vi.resetModules()
39
+ const palette = await import('../composables/useColorPalette')
40
+ const plugin = await import('../plugin')
41
+ return { ...palette, ...plugin }
42
+ }
43
+ function setup(useColorPalette: () => ReturnType<typeof import('../composables/useColorPalette')['useColorPalette']>) {
44
+ let result!: ReturnType<typeof useColorPalette>
45
+ mount(defineComponent({ setup() { result = useColorPalette(); return () => h('div') } }))
46
+ return result
47
+ }
48
+ const generatedCss = () => document.getElementById('m3-dynamic-palette')?.textContent ?? ''
49
+ const settle = () => new Promise((r) => setTimeout(r, 50))
50
+
51
+ describe('seed palettes', () => {
52
+ beforeEach(() => {
53
+ localStorage.clear()
54
+ document.documentElement.removeAttribute('data-palette')
55
+ document.head.innerHTML = ''
56
+ })
57
+ afterEach(() => vi.resetModules())
58
+
59
+ it('setSeed generates the scheme, switches to it and remembers it', async () => {
60
+ const { useColorPalette } = await fresh()
61
+ const p = setup(useColorPalette)
62
+ await p.setSeed('#0b57d0', { variant: 'vibrant' })
63
+ await nextTick()
64
+ expect(p.palette.value).toBe('seed')
65
+ expect(document.documentElement.getAttribute('data-palette')).toBe('seed')
66
+ expect(generatedCss()).toContain("[data-palette='seed']")
67
+ expect(p.seed.value).toEqual({ seed: '#0b57d0', variant: 'vibrant' })
68
+ expect(JSON.parse(localStorage.getItem('m3-seed')!)).toEqual({ seed: '#0b57d0', variant: 'vibrant' })
69
+ expect(JSON.parse(localStorage.getItem('m3-palette-css')!).id).toBe('seed')
70
+ })
71
+
72
+ it('the last choice wins: a palette after a seed, and the seed is kept', async () => {
73
+ const { useColorPalette } = await fresh()
74
+ const p = setup(useColorPalette)
75
+ await p.setSeed('#0b57d0')
76
+ p.set('teal')
77
+ await nextTick()
78
+ expect(p.palette.value).toBe('teal')
79
+ expect(document.documentElement.getAttribute('data-palette')).toBe('teal')
80
+ expect(p.seed.value?.seed).toBe('#0b57d0')
81
+ })
82
+
83
+ it('restores a generated palette on reload before the generator loads', async () => {
84
+ localStorage.setItem('m3-palette', 'seed')
85
+ localStorage.setItem('m3-palette-css', JSON.stringify({ id: 'seed', css: "[data-palette='seed'] { --color-primary: #123456; }" }))
86
+ await fresh()
87
+ expect(generatedCss()).toContain('#123456')
88
+ expect(document.documentElement.getAttribute('data-palette')).toBe('seed')
89
+ })
90
+
91
+ it('generates a custom palette from its seed when it has no CSS of its own', async () => {
92
+ const { useColorPalette, createM3UI } = await fresh()
93
+ createApp({}).use(createM3UI({ customPalettes: [{ id: 'brand', label: 'Brand', seed: '#0b6e4f' }] }))
94
+ const p = setup(useColorPalette)
95
+ p.set('brand')
96
+ await settle()
97
+ expect(generatedCss()).toContain("[data-palette='brand']")
98
+ expect(p.palette.value).toBe('brand')
99
+ })
100
+
101
+ it('keeps a custom palette\'s own CSS instead of generating it', async () => {
102
+ const style = document.createElement('style')
103
+ style.textContent = "[data-palette='brand'] { --color-primary: #ff0000; }"
104
+ document.head.appendChild(style)
105
+ const { useColorPalette, createM3UI } = await fresh()
106
+ createApp({}).use(createM3UI({ customPalettes: [{ id: 'brand', label: 'Brand', seed: '#0b6e4f' }] }))
107
+ const p = setup(useColorPalette)
108
+ p.set('brand')
109
+ await settle()
110
+ expect(generatedCss()).toBe('')
111
+ expect(p.palette.value).toBe('brand')
112
+ })
113
+ })
114
+
115
+ describe('createM3UI seed', () => {
116
+ beforeEach(() => {
117
+ localStorage.clear()
118
+ document.documentElement.removeAttribute('data-palette')
119
+ document.head.innerHTML = ''
120
+ })
121
+
122
+ it('uses the seed on the first visit', async () => {
123
+ const { createM3UI } = await fresh()
124
+ createApp({}).use(createM3UI({ seed: '#0b57d0' }))
125
+ await settle()
126
+ expect(document.documentElement.getAttribute('data-palette')).toBe('seed')
127
+ expect(localStorage.getItem('m3-palette')).toBe('seed')
128
+ })
129
+
130
+ it('prefers palette over seed', async () => {
131
+ const { createM3UI } = await fresh()
132
+ createApp({}).use(createM3UI({ palette: 'teal', seed: '#0b57d0' }))
133
+ await settle()
134
+ expect(document.documentElement.getAttribute('data-palette')).toBe('teal')
135
+ expect(generatedCss()).toBe('')
136
+ })
137
+
138
+ it('leaves a stored choice alone', async () => {
139
+ localStorage.setItem('m3-palette', 'iron')
140
+ const { createM3UI } = await fresh()
141
+ createApp({}).use(createM3UI({ seed: '#0b57d0' }))
142
+ await settle()
143
+ expect(localStorage.getItem('m3-palette')).toBe('iron')
144
+ })
145
+
146
+ it('a first-visit palette survives the first useColorPalette() call', async () => {
147
+ const { createM3UI, useColorPalette } = await fresh()
148
+ createApp({}).use(createM3UI({ palette: 'teal' }))
149
+ const p = setup(useColorPalette)
150
+ await nextTick()
151
+ expect(p.palette.value).toBe('teal')
152
+ expect(document.documentElement.getAttribute('data-palette')).toBe('teal')
153
+ expect(localStorage.getItem('m3-palette')).toBe('teal')
154
+ })
155
+ })
156
+
157
+ describe('createM3UI themeColor', () => {
158
+ beforeEach(() => {
159
+ localStorage.clear()
160
+ document.head.innerHTML = ''
161
+ document.documentElement.className = ''
162
+ document.documentElement.style.setProperty('--color-surface', '#fef7ff')
163
+ })
164
+
165
+ it('creates the meta and follows the palette', async () => {
166
+ const { createM3UI } = await fresh()
167
+ createApp({}).use(createM3UI({ themeColor: 'surface' }))
168
+ const meta = () => document.querySelector<HTMLMetaElement>('meta[name="theme-color"]')!
169
+ expect(meta().content).toBe('#fef7ff')
170
+ // Dark mode (or a palette switch) changes the variable and touches <html>
171
+ document.documentElement.style.setProperty('--color-surface', '#141218')
172
+ document.documentElement.classList.add('dark')
173
+ await settle()
174
+ expect(meta().content).toBe('#141218')
175
+ })
176
+
177
+ it('takes over an existing light/dark pair', async () => {
178
+ document.head.innerHTML = '<meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff"><meta name="theme-color" media="(prefers-color-scheme: dark)" content="#000000">'
179
+ const { createM3UI } = await fresh()
180
+ createApp({}).use(createM3UI({ themeColor: 'surface' }))
181
+ const all = Array.from(document.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]'))
182
+ expect(all.map((m) => [m.content, m.hasAttribute('media')])).toEqual([['#fef7ff', false], ['#fef7ff', false]])
183
+ })
184
+
185
+ it('leaves the meta alone by default', async () => {
186
+ document.head.innerHTML = '<meta name="theme-color" content="#6750a4">'
187
+ const { createM3UI } = await fresh()
188
+ createApp({}).use(createM3UI({}))
189
+ expect(document.querySelector<HTMLMetaElement>('meta[name="theme-color"]')!.content).toBe('#6750a4')
190
+ })
191
+ })
@@ -0,0 +1,92 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, ref, withDirectives } from 'vue'
4
+ import { vRipple, type RippleValue } from '../directives/vRipple'
5
+ import MButton from '../components/MButton.vue'
6
+
7
+ function host(value: RippleValue | (() => RippleValue)) {
8
+ return mount(defineComponent({
9
+ setup() {
10
+ return () => withDirectives(h('div', { class: 'host' }), [[vRipple, typeof value === 'function' ? value() : value]])
11
+ },
12
+ }))
13
+ }
14
+
15
+ function press(el: Element, button = 0) {
16
+ el.dispatchEvent(new PointerEvent('pointerdown', { button, clientX: 10, clientY: 10, bubbles: true }))
17
+ }
18
+
19
+ describe('vRipple', () => {
20
+ it('spawns a ripple inside a clipping layer on press', () => {
21
+ const w = host(undefined)
22
+ press(w.element)
23
+ const layer = w.element.querySelector('.m3-ripple-layer')
24
+ expect(layer).not.toBeNull()
25
+ expect(layer!.querySelector('.m3-ripple')).not.toBeNull()
26
+ })
27
+
28
+ it('removes the layer when the animation ends', () => {
29
+ const w = host(true)
30
+ press(w.element)
31
+ w.element.querySelector('.m3-ripple')!.dispatchEvent(new Event('animationend'))
32
+ expect(w.element.querySelector('.m3-ripple-layer')).toBeNull()
33
+ })
34
+
35
+ it('makes a statically positioned host relative', () => {
36
+ const w = mount(defineComponent({
37
+ setup: () => () => withDirectives(h('div'), [[vRipple, true]]),
38
+ }), { attachTo: document.body })
39
+ press(w.element)
40
+ expect((w.element as HTMLElement).style.position).toBe('relative')
41
+ w.unmount()
42
+ })
43
+
44
+ it('does nothing when false or disabled', () => {
45
+ for (const value of [false, { disabled: true }] as RippleValue[]) {
46
+ const w = host(value)
47
+ press(w.element)
48
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
49
+ }
50
+ })
51
+
52
+ it('ignores secondary buttons', () => {
53
+ const w = host(true)
54
+ press(w.element, 2)
55
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
56
+ })
57
+
58
+ it('takes a color as a string or an option', () => {
59
+ for (const value of ['rgb(255, 0, 0)', { color: 'rgb(255, 0, 0)' }] as RippleValue[]) {
60
+ const w = host(value)
61
+ press(w.element)
62
+ expect((w.element.querySelector('.m3-ripple') as HTMLElement).style.background).toBe('rgb(255, 0, 0)')
63
+ }
64
+ })
65
+
66
+ it('follows binding updates', async () => {
67
+ const disabled = ref(true)
68
+ const w = mount(defineComponent({
69
+ setup() {
70
+ return () => withDirectives(h('div'), [[vRipple, { disabled: disabled.value }]])
71
+ },
72
+ }))
73
+ press(w.element)
74
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
75
+ disabled.value = false
76
+ await w.vm.$nextTick()
77
+ press(w.element)
78
+ expect(w.element.querySelector('.m3-ripple')).not.toBeNull()
79
+ })
80
+ })
81
+
82
+ describe('MButton ripple', () => {
83
+ it('uses rippleColor and skips the ripple while loading', async () => {
84
+ const w = mount(MButton, { props: { rippleColor: 'rgb(0, 0, 255)' }, slots: { default: 'Go' } })
85
+ press(w.element)
86
+ expect((w.element.querySelector('.m3-ripple') as HTMLElement).style.background).toBe('rgb(0, 0, 255)')
87
+ await w.setProps({ loading: true })
88
+ w.element.querySelectorAll('.m3-ripple-layer').forEach((l) => l.remove())
89
+ press(w.element)
90
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
91
+ })
92
+ })
@@ -15,8 +15,8 @@ const props = withDefaults(
15
15
  padding?: 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl'
16
16
  /** Border radius */
17
17
  rounded?: 'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full'
18
- /** Shadow elevation level */
19
- elevation?: 0 | 1 | 2 | 3
18
+ /** Shadow elevation level (M3 levels 0–5) */
19
+ elevation?: 0 | 1 | 2 | 3 | 4 | 5
20
20
  /** Show border */
21
21
  border?: boolean
22
22
  /** HTML tag to render */
@@ -60,6 +60,8 @@ const elevationClasses: Record<number, string> = {
60
60
  1: 'shadow-elevation-1',
61
61
  2: 'shadow-elevation-2',
62
62
  3: 'shadow-elevation-3',
63
+ 4: 'shadow-elevation-4',
64
+ 5: 'shadow-elevation-5',
63
65
  }
64
66
 
65
67
  const classes = computed(() => [
@@ -7,6 +7,7 @@
7
7
  import { computed } from 'vue'
8
8
  import MSpinner from './MSpinner.vue'
9
9
  import MIcon from './MIcon.vue'
10
+ import { vRipple } from '../directives/vRipple'
10
11
 
11
12
  const NAMED_COLORS = ['primary', 'error'] as const
12
13
  type NamedColor = (typeof NAMED_COLORS)[number]
@@ -46,6 +47,8 @@ const props = withDefaults(
46
47
  shape?: 'rounded' | 'squared'
47
48
  /** Vue Router destination — renders as RouterLink instead of button */
48
49
  to?: string | Record<string, any>
50
+ /** Any CSS color for the press ripple; defaults to the button's content color */
51
+ rippleColor?: string
49
52
  }>(),
50
53
  {
51
54
  variant: 'filled',
@@ -165,18 +168,6 @@ const variantClasses = computed(() => {
165
168
  return ''
166
169
  }
167
170
  })
168
-
169
- function createRipple(event: PointerEvent) {
170
- if (props.disabled || props.loading) return
171
- const button = event.currentTarget as HTMLElement
172
- const rect = button.getBoundingClientRect()
173
- const d = Math.max(rect.width, rect.height) * 2
174
- const el = document.createElement('span')
175
- el.className = 'm3-ripple'
176
- el.style.cssText = `width:${d}px;height:${d}px;top:${event.clientY - rect.top - d / 2}px;left:${event.clientX - rect.left - d / 2}px`
177
- button.appendChild(el)
178
- el.addEventListener('animationend', () => el.remove(), { once: true })
179
- }
180
171
  </script>
181
172
 
182
173
  <template>
@@ -187,7 +178,7 @@ function createRipple(event: PointerEvent) {
187
178
  :disabled="disabled || loading"
188
179
  :class="[base, shapeClass, sizeClasses, px, variantClasses, disabledContainerClasses]"
189
180
  :style="customStyle"
190
- @pointerdown="createRipple"
181
+ v-ripple="{ disabled: disabled || loading, color: rippleColor }"
191
182
  >
192
183
  <template v-if="iconPos === 'leading'">
193
184
  <MSpinner v-if="loading" :size="spinnerSize" />
@@ -5,8 +5,10 @@
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'
11
+ import { vRipple } from '../directives/vRipple'
10
12
 
11
13
  export interface ButtonGroupOption {
12
14
  value: unknown
@@ -30,6 +32,8 @@ const props = withDefaults(defineProps<{
30
32
  selectionRequired?: boolean
31
33
  /** Selected button color */
32
34
  color?: 'primary' | 'secondary' | 'tertiary'
35
+ /** Any CSS color for the press ripple; defaults to the button's content color */
36
+ rippleColor?: string
33
37
  }>(), {
34
38
  variant: 'standard',
35
39
  size: 'sm',
@@ -73,26 +77,20 @@ function toggle(opt: ButtonGroupOption) {
73
77
  }
74
78
  }
75
79
 
80
+ // pad: ButtonDefaults content padding per tier, also the most a neighbor can be compressed by (animateWidth's
81
+ // default compressionLimit). checked/pressed: ToggleButtonDefaults.shapesFor() radii, as in MToggleButton (the
82
+ // standard group's items are ToggleButtons). The button group spec only has the small tier; lg/xl are library
83
+ // sizes and reuse the medium radii.
76
84
  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 },
85
+ xs: { h: 'h-8', px: 'px-3', text: 'text-label-medium', icon: 16, check: 14, height: 32, pad: 12, checked: 12, pressed: 8 },
86
+ sm: { h: 'h-10', px: 'px-4', text: 'text-label-large', icon: 18, check: 16, height: 40, pad: 16, checked: 12, pressed: 6 },
87
+ md: { h: 'h-14', px: 'px-6', text: 'text-title-medium', icon: 20, check: 18, height: 56, pad: 24, checked: 16, pressed: 12 },
88
+ lg: { h: 'h-16', px: 'px-7', text: 'text-title-large', icon: 22, check: 20, height: 64, pad: 28, checked: 16, pressed: 12 },
89
+ xl: { h: 'h-20', px: 'px-8', text: 'text-headline-small', icon: 24, check: 22, height: 80, pad: 32, checked: 16, pressed: 12 },
82
90
  }
83
91
 
84
92
  const s = computed(() => sizeMap[props.size] ?? sizeMap.sm)
85
93
 
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
94
  const selectedColors = computed(() => {
97
95
  const map = {
98
96
  primary: 'bg-primary text-on-primary',
@@ -104,36 +102,108 @@ const selectedColors = computed(() => {
104
102
 
105
103
  const unselectedColors = 'bg-surface-container-highest text-on-surface'
106
104
 
107
- function createRipple(event: PointerEvent) {
108
- const button = event.currentTarget as HTMLElement
109
- const rect = button.getBoundingClientRect()
110
- const d = Math.max(rect.width, rect.height) * 2
111
- const el = document.createElement('span')
112
- el.className = 'm3-ripple'
113
- el.style.cssText = `width:${d}px;height:${d}px;top:${event.clientY - rect.top - d / 2}px;left:${event.clientX - rect.left - d / 2}px`
114
- button.appendChild(el)
115
- el.addEventListener('animationend', () => el.remove(), { once: true })
105
+
106
+ const buttons: (HTMLElement | null)[] = []
107
+ const pressedIndex = ref(-1)
108
+
109
+ function onPointerDown(e: PointerEvent, i: number) {
110
+ if (e.button !== 0 || props.options[i]?.disabled) return
111
+ measureWidths()
112
+ pressedIndex.value = i
113
+ }
114
+ function onPointerUp() {
115
+ pressedIndex.value = -1
116
116
  }
117
117
 
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
118
+ // ── Shapes ──────────────────────────────────────────────────────────────────────────────────
119
+ // Both variants morph like ToggleButton's shapeByInteraction(): pressed > checked > resting, on
120
+ // MotionSchemeKeyTokens.FastSpatial.
121
+ // Standard: ToggleButtonDefaults shapes, a pill that squares off when checked and more when pressed.
122
+ // Connected (ButtonGroupDefaults.connected*ButtonShapes, ConnectedButtonGroupSmallTokens): outer corners
123
+ // full, inner corners InnerCornerCornerSize (8dp) → PressedInnerCornerCornerSize (4dp) while pressed, and a
124
+ // checked button becomes a full pill (connectedButtonCheckedShape = CornerFull).
125
+ const CONNECTED_INNER = 8
126
+ const CONNECTED_PRESSED_INNER = 4
127
+
128
+ function radiiFor(i: number): number[] {
129
+ const full = s.value.height / 2
130
+ const pressed = pressedIndex.value === i
131
+ const checked = isSelected(props.options[i]!.value)
132
+ if (props.variant === 'standard') {
133
+ const r = pressed ? s.value.pressed : checked ? s.value.checked : full
134
+ return [r, r, r, r]
128
135
  }
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`,
136
+ if (!pressed && checked) return [full, full, full, full]
137
+ const inner = pressed ? CONNECTED_PRESSED_INNER : CONNECTED_INNER
138
+ const start = i === 0 ? full : inner
139
+ const end = i === props.options.length - 1 ? full : inner
140
+ return [start, end, end, start]
141
+ }
142
+
143
+ useSpring(
144
+ () => props.options.flatMap((_, i) => radiiFor(i)),
145
+ (v) => {
146
+ buttons.forEach((el, i) => {
147
+ if (el) el.style.borderRadius = `${v[i * 4]}px ${v[i * 4 + 1]}px ${v[i * 4 + 2]}px ${v[i * 4 + 3]}px`
148
+ })
149
+ },
150
+ FAST_SPATIAL,
151
+ )
152
+
153
+ // ── Width: the pressed button grows, its neighbors give way (standard only) ──────────────────
154
+ // ButtonGroup's layout with ButtonGroupScope.animateWidth: the pressed item grows by expandedRatio
155
+ // (ButtonGroupDefaults.ExpandedRatio = 0.15) of its width, taken from its neighbors and capped by how much
156
+ // each can be compressed (its content padding). A middle item splits the growth between both sides.
157
+ // The connected samples don't use animateWidth, so the connected variant keeps its widths.
158
+ const EXPANDED_RATIO = 0.15
159
+ let natural: number[] = []
160
+ let widthsActive = false
161
+
162
+ function measureWidths() {
163
+ if (widthsActive) return
164
+ natural = props.options.map((_, i) => buttons[i]?.offsetWidth ?? 0)
165
+ }
166
+
167
+ function widthsFor(progress: readonly number[]): number[] {
168
+ const w = [...natural]
169
+ const pad = s.value.pad
170
+ const last = w.length - 1
171
+ for (let i = 0; i < w.length; i++) {
172
+ const p = progress[i] ?? 0
173
+ if (p === 0) continue
174
+ let growth: number
175
+ if (i > 0 && i < last) {
176
+ const g = Math.round(p * Math.min((EXPANDED_RATIO * w[i]!) / 2, pad))
177
+ const left = Math.min(g, w[i - 1]!)
178
+ const right = Math.min(g, w[i + 1]!)
179
+ w[i - 1]! -= left
180
+ w[i + 1]! -= right
181
+ growth = left + right
182
+ } else if (i === 0) {
183
+ const g = Math.round(p * Math.min(EXPANDED_RATIO * w[i]!, pad))
184
+ growth = Math.min(g, w[i + 1]!)
185
+ w[i + 1]! -= growth
186
+ } else {
187
+ const g = Math.round(p * Math.min(EXPANDED_RATIO * w[i]!, pad))
188
+ growth = Math.min(g, w[i - 1]!)
189
+ w[i - 1]! -= growth
190
+ }
191
+ w[i]! += growth
135
192
  }
193
+ return w
136
194
  }
195
+
196
+ useSpring(
197
+ () => props.options.map((_, i) => (props.variant === 'standard' && props.options.length > 1 && pressedIndex.value === i ? 1 : 0)),
198
+ (progress) => {
199
+ widthsActive = progress.some((p) => p !== 0)
200
+ const w = widthsActive && natural.length === props.options.length ? widthsFor(progress) : null
201
+ buttons.forEach((el, i) => {
202
+ if (el) el.style.width = w ? `${w[i]}px` : ''
203
+ })
204
+ },
205
+ FAST_SPATIAL,
206
+ )
137
207
  </script>
138
208
 
139
209
  <template>
@@ -146,16 +216,20 @@ function btnStyle(i: number, selected: boolean) {
146
216
  v-for="(opt, i) in options"
147
217
  :key="String(opt.value)"
148
218
  type="button"
149
- class="btn-group-item relative inline-flex items-center justify-center gap-2 overflow-hidden font-medium"
219
+ :ref="(el) => { buttons[i] = el as HTMLElement | null }"
220
+ 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
221
  :class="[
151
222
  s.h, s.px, s.text,
152
223
  opt.disabled ? 'cursor-not-allowed opacity-[0.38]' : 'cursor-pointer',
153
224
  isSelected(opt.value) ? selectedColors : unselectedColors,
154
225
  ]"
155
- :style="btnStyle(i, isSelected(opt.value))"
156
226
  :disabled="opt.disabled"
157
227
  :aria-pressed="isSelected(opt.value)"
158
- @pointerdown="createRipple"
228
+ v-ripple="{ disabled: opt.disabled, color: rippleColor }"
229
+ @pointerdown="onPointerDown($event, i)"
230
+ @pointerup="onPointerUp"
231
+ @pointerleave="onPointerUp"
232
+ @pointercancel="onPointerUp"
159
233
  @click="toggle(opt)"
160
234
  >
161
235
  <MIcon