@m3ui-vue/m3ui-vue 0.8.5 → 0.8.7

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 (80) hide show
  1. package/README.md +5 -1
  2. package/dist/{MMenuItem-BXIxwIdM.js → MMenuItem-DmrsQrK7.js} +2 -2
  3. package/dist/{MMenuItem-BXIxwIdM.js.map → MMenuItem-DmrsQrK7.js.map} +1 -1
  4. package/dist/chart.js +5 -1
  5. package/dist/chart.js.map +1 -1
  6. package/dist/code-editor.js +1 -1
  7. package/dist/components/MBottomSheet.vue.d.ts +3 -3
  8. package/dist/components/MChatBubble.vue.d.ts +4 -2
  9. package/dist/components/MCheckbox.vue.d.ts +2 -0
  10. package/dist/components/MDragDropList.vue.d.ts +19 -6
  11. package/dist/components/MFileUpload.vue.d.ts +16 -4
  12. package/dist/components/MKanban.vue.d.ts +15 -6
  13. package/dist/components/MListDetail.vue.d.ts +31 -0
  14. package/dist/components/MNavigationRail.vue.d.ts +6 -3
  15. package/dist/components/MNavigationSuite.vue.d.ts +46 -0
  16. package/dist/components/MShortNavigationBar.vue.d.ts +6 -1
  17. package/dist/components/MSideSheet.vue.d.ts +4 -4
  18. package/dist/components/MSupportingPane.vue.d.ts +34 -0
  19. package/dist/components/MTransferList.vue.d.ts +57 -30
  20. package/dist/components/MWindow.vue.d.ts +39 -1
  21. package/dist/components/_MTransferPane.vue.d.ts +46 -0
  22. package/dist/composables/_useItemLink.d.ts +30 -0
  23. package/dist/composables/_useSheetMotion.d.ts +7 -0
  24. package/dist/composables/useLocale.d.ts +27 -0
  25. package/dist/composables/useWindowSizeClass.d.ts +19 -0
  26. package/dist/index.d.ts +6 -0
  27. package/dist/locales/index.js +196 -7
  28. package/dist/locales/index.js.map +1 -1
  29. package/dist/m3ui-vue.css +1 -1
  30. package/dist/m3ui.js +4331 -2945
  31. package/dist/m3ui.js.map +1 -1
  32. package/dist/rich-text-editor.js +2 -2
  33. package/dist/styles.css +1 -1
  34. package/dist/{useLocale-DbREg9Zn.js → useLocale-Bircp823.js} +29 -2
  35. package/dist/useLocale-Bircp823.js.map +1 -0
  36. package/package.json +1 -1
  37. package/src/__tests__/MChart.test.ts +28 -0
  38. package/src/__tests__/MChatBubble.test.ts +33 -0
  39. package/src/__tests__/MDragDropList.test.ts +194 -0
  40. package/src/__tests__/MFileUpload.test.ts +78 -0
  41. package/src/__tests__/MKanban.test.ts +150 -0
  42. package/src/__tests__/MPullToRefresh.test.ts +67 -0
  43. package/src/__tests__/MRating.test.ts +72 -0
  44. package/src/__tests__/MShortNavigationBar.test.ts +25 -0
  45. package/src/__tests__/MTransferList.test.ts +149 -0
  46. package/src/__tests__/MWindow.test.ts +258 -0
  47. package/src/__tests__/adaptiveLayouts.test.ts +191 -0
  48. package/src/__tests__/sheets.test.ts +76 -0
  49. package/src/components/MBottomSheet.vue +11 -57
  50. package/src/components/MChart.vue +2 -1
  51. package/src/components/MChatBubble.vue +107 -20
  52. package/src/components/MCheckbox.vue +3 -0
  53. package/src/components/MDragDropList.vue +485 -56
  54. package/src/components/MFileUpload.vue +56 -20
  55. package/src/components/MKanban.vue +720 -78
  56. package/src/components/MListDetail.vue +103 -0
  57. package/src/components/MNavigationRail.vue +48 -31
  58. package/src/components/MNavigationSuite.vue +89 -0
  59. package/src/components/MPullToRefresh.vue +101 -35
  60. package/src/components/MRating.vue +163 -67
  61. package/src/components/MResult.vue +19 -9
  62. package/src/components/MShortNavigationBar.vue +37 -17
  63. package/src/components/MSideSheet.vue +11 -56
  64. package/src/components/MSupportingPane.vue +70 -0
  65. package/src/components/MTransferList.vue +174 -137
  66. package/src/components/MWindow.vue +507 -108
  67. package/src/components/_MTransferPane.vue +260 -0
  68. package/src/composables/_useItemLink.ts +19 -0
  69. package/src/composables/_useSheetMotion.ts +181 -0
  70. package/src/composables/useLocale.ts +75 -0
  71. package/src/composables/useWindowSizeClass.ts +93 -0
  72. package/src/index.ts +6 -0
  73. package/src/locales/de.ts +37 -0
  74. package/src/locales/es.ts +37 -0
  75. package/src/locales/fr.ts +37 -0
  76. package/src/locales/ja.ts +37 -0
  77. package/src/locales/ko.ts +37 -0
  78. package/src/locales/pt.ts +37 -0
  79. package/src/locales/zh.ts +37 -0
  80. package/dist/useLocale-DbREg9Zn.js.map +0 -1
@@ -0,0 +1,258 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, ref } from 'vue'
4
+ import MWindow from '../components/MWindow.vue'
5
+
6
+ // happy-dom has no layout: every parent is 1000×600.
7
+ beforeEach(() => {
8
+ vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(1000)
9
+ vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockReturnValue(600)
10
+ })
11
+ afterEach(() => {
12
+ vi.restoreAllMocks()
13
+ document.body.innerHTML = ''
14
+ })
15
+
16
+ function setup(props: Record<string, unknown> = {}) {
17
+ const w = mount(MWindow, {
18
+ props: { modelValue: true, title: 'Notes', x: 100, y: 100, width: '400px', height: '300px', ...props },
19
+ attachTo: document.body,
20
+ })
21
+ const win = () => w.find('[role="dialog"]')
22
+ const bar = () => win().find('div')
23
+ return { w, win, bar }
24
+ }
25
+ const pointer = (type: string, x: number, y: number, target: EventTarget = window) =>
26
+ target.dispatchEvent(new PointerEvent(type, { clientX: x, clientY: y, button: 0, bubbles: true }))
27
+
28
+ describe('MWindow', () => {
29
+ it('only the front window looks active; clicking another brings it forward', async () => {
30
+ const Two = defineComponent({
31
+ setup: () => () => [
32
+ h(MWindow, { modelValue: true, title: 'A' }),
33
+ h(MWindow, { modelValue: true, title: 'B' }),
34
+ ],
35
+ })
36
+ const w = mount(Two, { attachTo: document.body })
37
+ await nextTick()
38
+ const [a, b] = w.findAll('[role="dialog"]')
39
+ // The last one opened is in front.
40
+ expect(b!.classes()).toContain('shadow-elevation-3')
41
+ expect(b!.find('div').classes()).toContain('bg-surface-container-high')
42
+ expect(a!.classes()).toContain('shadow-elevation-1')
43
+ expect(a!.find('div').classes()).toContain('bg-surface-container')
44
+
45
+ await a!.trigger('pointerdown')
46
+ expect(a!.classes()).toContain('shadow-elevation-3')
47
+ expect(b!.classes()).toContain('shadow-elevation-1')
48
+ expect(Number((a!.element as HTMLElement).style.zIndex)).toBeGreaterThan(Number((b!.element as HTMLElement).style.zIndex))
49
+ w.unmount()
50
+ })
51
+
52
+ it('title bar buttons are labelled buttons', () => {
53
+ const { win, w } = setup()
54
+ const buttons = win().findAll('button')
55
+ expect(buttons.map((b) => b.attributes('aria-label'))).toEqual(['Minimize', 'Maximize', 'Close'])
56
+ expect(buttons.every((b) => b.attributes('type') === 'button')).toBe(true)
57
+ w.unmount()
58
+ })
59
+
60
+ it('lifts to elevation 4 while dragged', async () => {
61
+ const { win, bar, w } = setup()
62
+ pointer('pointerdown', 150, 110, bar().element)
63
+ await nextTick()
64
+ expect(win().classes()).toContain('shadow-elevation-4')
65
+ pointer('pointermove', 200, 160)
66
+ await nextTick()
67
+ expect((win().element as HTMLElement).style.left).toBe('150px')
68
+ expect((win().element as HTMLElement).style.top).toBe('150px')
69
+ pointer('pointerup', 200, 160)
70
+ await nextTick()
71
+ expect(win().classes()).not.toContain('shadow-elevation-4')
72
+ w.unmount()
73
+ })
74
+
75
+ it('resizes from the top edge, keeping the bottom edge in place', async () => {
76
+ const { win, w } = setup()
77
+ const top = win().findAll('.cursor-ns-resize').find((h) => h.classes().includes('top-0'))!
78
+ pointer('pointerdown', 300, 100, top.element)
79
+ pointer('pointermove', 300, 60)
80
+ await nextTick()
81
+ const style = (win().element as HTMLElement).style
82
+ expect(style.top).toBe('60px')
83
+ expect(style.height).toBe('340px')
84
+ pointer('pointerup', 300, 60)
85
+ w.unmount()
86
+ })
87
+
88
+ it('double-clicking the title bar maximizes to the parent and restores', async () => {
89
+ const { win, bar, w } = setup()
90
+ await bar().trigger('dblclick')
91
+ const style = (win().element as HTMLElement).style
92
+ expect([style.left, style.top, style.width, style.height]).toEqual(['0px', '0px', '1000px', '600px'])
93
+ expect(win().classes()).toContain('rounded-none')
94
+ expect(win().find('.cursor-ns-resize').exists()).toBe(false)
95
+ expect(win().findAll('button')[1]!.attributes('aria-label')).toBe('Restore')
96
+
97
+ await bar().trigger('dblclick')
98
+ expect([style.left, style.top, style.width, style.height]).toEqual(['100px', '100px', '400px', '300px'])
99
+ expect(w.emitted('maximize')).toEqual([[true], [false]])
100
+ w.unmount()
101
+ })
102
+
103
+ it('dragging a maximized window detaches only after 40px, then eases to its size under the pointer', async () => {
104
+ vi.useFakeTimers({ toFake: ['requestAnimationFrame', 'cancelAnimationFrame', 'performance', 'setTimeout'] })
105
+ const { win, bar, w } = setup()
106
+ await bar().trigger('dblclick')
107
+ const style = (win().element as HTMLElement).style
108
+ // Grab the bar at x=500 (half of the 1000px maximized width), 20px below its top.
109
+ pointer('pointerdown', 500, 20, bar().element)
110
+ pointer('pointermove', 510, 50) // ~32px: not far enough to detach
111
+ await nextTick()
112
+ expect([style.left, style.width]).toEqual(['0px', '1000px'])
113
+
114
+ pointer('pointermove', 520, 60) // ~45px: detaches
115
+ vi.advanceTimersByTime(100)
116
+ await nextTick()
117
+ const midWidth = parseFloat(style.width)
118
+ expect(midWidth).toBeLessThan(1000)
119
+ expect(midWidth).toBeGreaterThan(400)
120
+ // Still anchored mid-animation: half the current width left of the pointer.
121
+ expect(parseFloat(style.left)).toBeCloseTo(520 - midWidth / 2, 0)
122
+
123
+ vi.advanceTimersByTime(300)
124
+ await nextTick()
125
+ expect([style.width, style.height]).toEqual(['400px', '300px'])
126
+ expect(style.left).toBe('320px')
127
+ // The bar was grabbed 20px below its top edge, and still is.
128
+ expect(style.top).toBe('40px')
129
+ expect(win().classes()).toContain('rounded-xl')
130
+ expect(w.emitted('maximize')).toEqual([[true], [false]])
131
+
132
+ // Keeps following the pointer afterwards.
133
+ pointer('pointermove', 600, 100)
134
+ await nextTick()
135
+ expect([style.left, style.top]).toEqual(['400px', '80px'])
136
+ pointer('pointerup', 600, 100)
137
+ vi.useRealTimers()
138
+ w.unmount()
139
+ })
140
+
141
+ it('minimize folds the height to the title bar, keeping the body until the animation ends', async () => {
142
+ vi.useFakeTimers()
143
+ const { win, w } = setup()
144
+ const body = () => win().findAll(':scope > div')[1]!.element as HTMLElement
145
+ await win().findAll('button')[0]!.trigger('click')
146
+ expect((win().element as HTMLElement).style.height).toBe('40px')
147
+ expect(win().classes()).toContain('m3-window-geometry')
148
+ expect(body().style.display).not.toBe('none')
149
+ vi.advanceTimersByTime(350)
150
+ await nextTick()
151
+ expect(body().style.display).toBe('none')
152
+ vi.useRealTimers()
153
+ w.unmount()
154
+ })
155
+
156
+ it('snap: the top edge previews and maximizes, a side edge fills that half; off by default', async () => {
157
+ const drag = async (bar: ReturnType<ReturnType<typeof setup>['bar']>, toX: number, toY: number) => {
158
+ pointer('pointerdown', 200, 110, bar.element)
159
+ pointer('pointermove', toX, toY)
160
+ await nextTick()
161
+ }
162
+ const off = setup()
163
+ await drag(off.bar(), 400, 5)
164
+ expect(document.querySelector('.m3-window-snap-preview')).toBeNull()
165
+ pointer('pointerup', 400, 5)
166
+ await nextTick()
167
+ expect((off.win().element as HTMLElement).style.width).toBe('400px')
168
+ off.w.unmount()
169
+
170
+ const { win, bar, w } = setup({ snap: true })
171
+ const style = (win().element as HTMLElement).style
172
+ await drag(bar(), 400, 5)
173
+ expect(document.querySelector('.m3-window-snap-preview')).not.toBeNull()
174
+ pointer('pointerup', 400, 5)
175
+ await nextTick()
176
+ expect([style.left, style.top, style.width, style.height]).toEqual(['0px', '0px', '1000px', '600px'])
177
+ expect(w.emitted('maximize')).toEqual([[true]])
178
+
179
+ // Dragging it off the top restores, then the right edge snaps it to the right half.
180
+ pointer('pointerdown', 500, 20, bar().element)
181
+ pointer('pointermove', 600, 200)
182
+ pointer('pointermove', 995, 200)
183
+ await nextTick()
184
+ pointer('pointerup', 995, 200)
185
+ await nextTick()
186
+ expect([style.left, style.top, style.width, style.height]).toEqual(['500px', '0px', '500px', '600px'])
187
+ expect(win().find('.cursor-ns-resize').exists()).toBe(false)
188
+
189
+ // And double-click from the half restores the original size, not the half.
190
+ await bar().trigger('dblclick')
191
+ await bar().trigger('dblclick')
192
+ expect([style.width, style.height]).toEqual(['400px', '300px'])
193
+ w.unmount()
194
+ })
195
+
196
+ it('offscreen lets it leave the container up to 3/4 of its width, keeping the title bar; off by default', async () => {
197
+ const dragBy = async (bar: Element, dx: number, dy: number) => {
198
+ pointer('pointerdown', 200, 110, bar)
199
+ pointer('pointermove', 200 + dx, 110 + dy)
200
+ pointer('pointerup', 200 + dx, 110 + dy)
201
+ await nextTick()
202
+ }
203
+ const inside = setup()
204
+ const s1 = (inside.win().element as HTMLElement).style
205
+ await dragBy(inside.bar().element, -1000, 1000)
206
+ expect([s1.left, s1.top]).toEqual(['0px', '300px']) // 600 - 300: fully inside
207
+ inside.w.unmount()
208
+
209
+ const { win, bar, w } = setup({ offscreen: true })
210
+ const style = (win().element as HTMLElement).style
211
+ await dragBy(bar().element, -1000, 1000)
212
+ expect([style.left, style.top]).toEqual(['-300px', '560px']) // 1/4 of 400 visible, only the 40px bar
213
+ await dragBy(bar().element, 2000, -2000)
214
+ expect([style.left, style.top]).toEqual(['900px', '0px']) // never above the top
215
+ w.unmount()
216
+ })
217
+
218
+ it('snap: a top corner snaps to that side, not maximize', async () => {
219
+ const { win, bar, w } = setup({ snap: true })
220
+ const style = (win().element as HTMLElement).style
221
+ pointer('pointerdown', 200, 110, bar().element)
222
+ pointer('pointermove', 4, 3)
223
+ await nextTick()
224
+ pointer('pointerup', 4, 3)
225
+ await nextTick()
226
+ expect([style.left, style.top, style.width, style.height]).toEqual(['0px', '0px', '500px', '600px'])
227
+ expect(w.emitted('maximize')).toBeUndefined()
228
+ w.unmount()
229
+ })
230
+
231
+ it('focus() brings a window to the front and emits focus once per activation', async () => {
232
+ const Two = defineComponent({
233
+ setup() {
234
+ const a = ref<InstanceType<typeof MWindow>>()
235
+ return { a }
236
+ },
237
+ render() {
238
+ return [
239
+ h(MWindow, { ref: 'a', modelValue: true, title: 'A', onFocus: () => this.$emit('focusA') }),
240
+ h(MWindow, { modelValue: true, title: 'B' }),
241
+ ]
242
+ },
243
+ emits: ['focusA'],
244
+ })
245
+ const w = mount(Two, { attachTo: document.body })
246
+ await nextTick()
247
+ const [a] = w.findAll('[role="dialog"]')
248
+ expect(a!.classes()).toContain('shadow-elevation-1')
249
+ const focusesBefore = w.emitted('focusA')?.length ?? 0
250
+ ;(w.vm.a as any).focus()
251
+ await nextTick()
252
+ expect(a!.classes()).toContain('shadow-elevation-3')
253
+ expect(w.emitted('focusA')!.length).toBe(focusesBefore + 1)
254
+ ;(w.vm.a as any).focus()
255
+ expect(w.emitted('focusA')!.length).toBe(focusesBefore + 1)
256
+ w.unmount()
257
+ })
258
+ })
@@ -0,0 +1,191 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { nextTick } from 'vue'
4
+ import { widthSizeClass, heightSizeClass } from '../composables/useWindowSizeClass'
5
+ import MNavigationSuite from '../components/MNavigationSuite.vue'
6
+ import MListDetail from '../components/MListDetail.vue'
7
+ import MSupportingPane from '../components/MSupportingPane.vue'
8
+
9
+ // happy-dom has no layout: every element reports the size below, and resizing fires the
10
+ // mocked ResizeObserver callbacks the way the browser would.
11
+ let size = { w: 1000, h: 800 }
12
+ const observers = new Set<() => void>()
13
+ class FakeResizeObserver {
14
+ constructor(private cb: () => void) {}
15
+ observe() { observers.add(this.cb) }
16
+ disconnect() { observers.delete(this.cb) }
17
+ }
18
+ async function resize(w: number, h = 800) {
19
+ size = { w, h }
20
+ observers.forEach((cb) => cb())
21
+ await nextTick()
22
+ }
23
+
24
+ beforeEach(() => {
25
+ size = { w: 1000, h: 800 }
26
+ vi.stubGlobal('ResizeObserver', FakeResizeObserver)
27
+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockImplementation(() => size.w)
28
+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(() => size.h)
29
+ })
30
+ afterEach(() => {
31
+ vi.unstubAllGlobals()
32
+ vi.restoreAllMocks()
33
+ observers.clear()
34
+ })
35
+
36
+ describe('window size classes', () => {
37
+ it('uses the androidx WindowSizeClass breakpoints', () => {
38
+ expect([599, 600, 839, 840, 1199, 1200, 1599, 1600].map(widthSizeClass)).toEqual([
39
+ 'compact', 'medium', 'medium', 'expanded', 'expanded', 'large', 'large', 'extra-large',
40
+ ])
41
+ expect([479, 480, 899, 900].map(heightSizeClass)).toEqual(['compact', 'medium', 'medium', 'expanded'])
42
+ })
43
+ })
44
+
45
+ const navItems = [
46
+ { value: 'home', label: 'Home', icon: 'home' },
47
+ { value: 'search', label: 'Search', icon: 'search' },
48
+ ]
49
+
50
+ describe('MNavigationSuite', () => {
51
+ const setup = (props = {}) =>
52
+ mount(MNavigationSuite, {
53
+ props: { modelValue: 'home', items: navItems, ...props },
54
+ slots: { default: `<template #default="{ layout }"><p class="content">{{ layout }}</p></template>` },
55
+ attachTo: document.body,
56
+ })
57
+
58
+ it('picks bar, landscape bar or rail from its own size, keeping the content element', async () => {
59
+ const w = setup()
60
+ await nextTick()
61
+ expect(w.find('.content').text()).toBe('rail')
62
+ expect(w.findComponent({ name: 'MNavigationRail' }).exists()).toBe(true)
63
+ const content = w.find('.content').element
64
+
65
+ await resize(400)
66
+ expect(w.find('.content').text()).toBe('bar')
67
+ expect(w.findComponent({ name: 'MNavigationRail' }).exists()).toBe(false)
68
+ expect(w.findComponent({ name: 'MShortNavigationBar' }).props()).toMatchObject({ arrangement: 'equal', iconPosition: 'top' })
69
+
70
+ await resize(800, 400)
71
+ expect(w.find('.content').text()).toBe('bar-medium')
72
+ expect(w.findComponent({ name: 'MShortNavigationBar' }).props()).toMatchObject({ arrangement: 'centered', iconPosition: 'start' })
73
+ expect(w.find('.content').element).toBe(content)
74
+ w.unmount()
75
+ })
76
+
77
+ it('layout forces a navigation and selection goes through v-model', async () => {
78
+ const w = setup({ layout: 'rail-expanded' })
79
+ await resize(400)
80
+ expect(w.findComponent({ name: 'MNavigationRail' }).props('expanded')).toBe(true)
81
+ w.findComponent({ name: 'MNavigationRail' }).vm.$emit('update:modelValue', 'search')
82
+ expect(w.emitted('update:modelValue')![0]).toEqual(['search'])
83
+ w.unmount()
84
+ })
85
+ })
86
+
87
+ describe('MListDetail', () => {
88
+ const setup = (props = {}) =>
89
+ mount(MListDetail, {
90
+ props: { ...props },
91
+ slots: {
92
+ list: `<template #list="{ twoPane }"><p class="list">{{ twoPane }}</p></template>`,
93
+ detail: `<template #detail="{ back }"><button class="back" @click="back">back</button></template>`,
94
+ },
95
+ attachTo: document.body,
96
+ })
97
+ const panes = (w: ReturnType<typeof setup>) => w.element.children as HTMLCollectionOf<HTMLElement>
98
+
99
+ it('shows both panes from 840px, with the list at 360px (412px from 1200px)', async () => {
100
+ const w = setup()
101
+ await nextTick()
102
+ expect(w.find('.list').text()).toBe('true')
103
+ expect(panes(w)[0]!.style.width).toBe('360px')
104
+ expect(panes(w)[1]!.hasAttribute('inert')).toBe(false)
105
+ await resize(1300)
106
+ expect(panes(w)[0]!.style.width).toBe('412px')
107
+ w.unmount()
108
+ })
109
+
110
+ it('one pane: shows the list or the detail, and back() returns to the list', async () => {
111
+ const w = setup()
112
+ await resize(700)
113
+ expect(w.find('.list').text()).toBe('false')
114
+ expect(panes(w)[1]!.hasAttribute('inert')).toBe(true)
115
+ expect(panes(w)[0]!.hasAttribute('inert')).toBe(false)
116
+
117
+ await w.setProps({ showDetail: true })
118
+ expect(panes(w)[0]!.hasAttribute('inert')).toBe(true)
119
+ expect(panes(w)[1]!.hasAttribute('inert')).toBe(false)
120
+ await w.find('.back').trigger('click')
121
+ expect(w.emitted('update:showDetail')![0]).toEqual([false])
122
+ w.unmount()
123
+ })
124
+ })
125
+
126
+ describe('MSupportingPane', () => {
127
+ const setup = (props = {}) =>
128
+ mount(MSupportingPane, {
129
+ props: { ...props },
130
+ slots: { main: '<p class="main">main</p>', supporting: '<p class="sup">sup</p>' },
131
+ attachTo: document.body,
132
+ })
133
+
134
+ it('puts the supporting pane beside, then below the main one', async () => {
135
+ const w = setup()
136
+ await nextTick()
137
+ expect(w.classes()).not.toContain('flex-col')
138
+ expect((w.element.children[1] as HTMLElement).style.width).toBe('360px')
139
+ await resize(500)
140
+ expect(w.classes()).toContain('flex-col')
141
+ expect(w.find('.sup').exists()).toBe(true)
142
+ w.unmount()
143
+ })
144
+
145
+ it("singlePane='sheet' moves it into a bottom sheet and closes it once it fits again", async () => {
146
+ const w = setup({ singlePane: 'sheet', open: true, 'onUpdate:open': (v: boolean) => w.setProps({ open: v }) })
147
+ await resize(500)
148
+ const sheet = w.findComponent({ name: 'MBottomSheet' })
149
+ expect(sheet.exists()).toBe(true)
150
+ expect(sheet.props('modelValue')).toBe(true)
151
+ await resize(1000)
152
+ expect(w.emitted('update:open')![0]).toEqual([false])
153
+ expect(w.findComponent({ name: 'MBottomSheet' }).exists()).toBe(false)
154
+ expect(w.find('.sup').exists()).toBe(true)
155
+ w.unmount()
156
+ })
157
+ })
158
+
159
+ describe('navigation items with `to`', () => {
160
+ const linkItems = [
161
+ { value: 'home', label: 'Home', icon: 'home', to: '/home' },
162
+ { value: 'search', label: 'Search', icon: 'search' },
163
+ { value: 'off', label: 'Off', icon: 'block', to: '/off', disabled: true },
164
+ ]
165
+
166
+ it('render as plain links without vue-router, buttons otherwise, and still update v-model', async () => {
167
+ for (const [layout, name] of [['rail', 'MNavigationRail'], ['bar', 'MShortNavigationBar']] as const) {
168
+ const w = mount(MNavigationSuite, { props: { modelValue: 'search', items: linkItems, layout } })
169
+ const nav = w.findComponent({ name })
170
+ const els = nav.findAll('nav a, nav button').filter((e) => e.text().match(/Home|Search|Off/))
171
+ expect(els.map((e) => e.element.tagName)).toEqual(['A', 'BUTTON', 'BUTTON'])
172
+ expect(els[0]!.attributes('href')).toBe('/home')
173
+ await els[0]!.trigger('click')
174
+ expect(w.emitted('update:modelValue')![0]).toEqual(['home'])
175
+ w.unmount()
176
+ }
177
+ })
178
+
179
+ it('render as RouterLink when vue-router is installed', () => {
180
+ const RouterLink = { name: 'RouterLink', props: ['to'], template: '<a class="rl" :href="String(to)"><slot /></a>' }
181
+ const w = mount(MNavigationSuite, {
182
+ props: { modelValue: 'home', items: linkItems, layout: 'rail' },
183
+ global: { components: { RouterLink } },
184
+ })
185
+ const links = w.findAllComponents({ name: 'RouterLink' })
186
+ expect(links).toHaveLength(1)
187
+ expect(links[0]!.props('to')).toBe('/home')
188
+ w.unmount()
189
+ })
190
+ })
191
+
@@ -0,0 +1,76 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { nextTick } from 'vue'
4
+ import MBottomSheet from '../components/MBottomSheet.vue'
5
+ import MSideSheet from '../components/MSideSheet.vue'
6
+
7
+ // happy-dom has no layout: the panel is 400px tall / 320px wide. Fake timers drive rAF.
8
+ beforeEach(() => {
9
+ vi.useFakeTimers({ toFake: ['requestAnimationFrame', 'cancelAnimationFrame', 'performance', 'setTimeout', 'Date'] })
10
+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(400)
11
+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(320)
12
+ })
13
+ afterEach(() => {
14
+ vi.useRealTimers()
15
+ vi.restoreAllMocks()
16
+ document.body.innerHTML = ''
17
+ })
18
+
19
+ const panel = () => document.querySelector<HTMLElement>('[data-sheet-panel]')
20
+ const scrim = () => document.querySelector<HTMLElement>('[data-sheet-scrim]')
21
+
22
+ describe.each([
23
+ ['MBottomSheet', MBottomSheet, 'translateY', 400],
24
+ ['MSideSheet', MSideSheet, 'translateX', 320],
25
+ ] as const)('%s motion', (_, Comp, fn, size) => {
26
+ it('slides in from fully off-screen with the scrim fading in, then out on close', async () => {
27
+ const w = mount(Comp as any, { props: { modelValue: false }, attachTo: document.body, global: { stubs: { transition: false } } })
28
+ await w.setProps({ modelValue: true })
29
+ await nextTick()
30
+ expect(panel()!.style.transform).toBe(`${fn}(${size}px)`)
31
+ expect(scrim()!.style.opacity).toBe('0')
32
+
33
+ vi.advanceTimersByTime(80)
34
+ const mid = parseFloat(panel()!.style.transform.match(/\(([-\d.]+)px/)![1]!)
35
+ expect(mid).toBeGreaterThan(0)
36
+ expect(mid).toBeLessThan(size)
37
+
38
+ vi.advanceTimersByTime(1500)
39
+ expect(panel()!.style.transform).toBe(`${fn}(0px)`)
40
+ expect(scrim()!.style.opacity).toBe('1')
41
+
42
+ await w.setProps({ modelValue: false })
43
+ vi.advanceTimersByTime(1500)
44
+ await nextTick()
45
+ expect(panel()).toBeNull()
46
+ w.unmount()
47
+ })
48
+
49
+ it('a short drag springs back; a long one dismisses', async () => {
50
+ const w = mount(Comp as any, { props: { modelValue: true }, attachTo: document.body, global: { stubs: { transition: false } } })
51
+ await nextTick()
52
+ vi.advanceTimersByTime(1500)
53
+ const handle = (fn === 'translateY'
54
+ ? panel()!.querySelector('.cursor-grab')
55
+ : panel()!.querySelector('.cursor-ew-resize')) as HTMLElement
56
+ handle.setPointerCapture = () => {}
57
+ const at = (v: number) => (fn === 'translateY' ? { clientY: v, clientX: 0 } : { clientX: v, clientY: 0 })
58
+
59
+ handle.dispatchEvent(new PointerEvent('pointerdown', { ...at(0), pointerId: 1 }))
60
+ vi.advanceTimersByTime(16)
61
+ handle.dispatchEvent(new PointerEvent('pointermove', { ...at(40), pointerId: 1 }))
62
+ expect(panel()!.style.transform).toBe(`${fn}(40px)`)
63
+ vi.advanceTimersByTime(200)
64
+ handle.dispatchEvent(new PointerEvent('pointerup', { pointerId: 1 }))
65
+ vi.advanceTimersByTime(1500)
66
+ expect(panel()!.style.transform).toBe(`${fn}(0px)`)
67
+ expect(w.emitted('update:modelValue')).toBeUndefined()
68
+
69
+ handle.dispatchEvent(new PointerEvent('pointerdown', { ...at(0), pointerId: 1 }))
70
+ vi.advanceTimersByTime(16)
71
+ handle.dispatchEvent(new PointerEvent('pointermove', { ...at(150), pointerId: 1 }))
72
+ handle.dispatchEvent(new PointerEvent('pointerup', { pointerId: 1 }))
73
+ expect(w.emitted('update:modelValue')![0]).toEqual([false])
74
+ w.unmount()
75
+ })
76
+ })
@@ -1,6 +1,6 @@
1
1
  <script setup lang="ts">
2
- import { computed, ref } from 'vue'
3
2
  import MIcon from './MIcon.vue'
3
+ import { useSheetMotion } from '../composables/_useSheetMotion'
4
4
 
5
5
  withDefaults(defineProps<{
6
6
  modelValue: boolean
@@ -11,52 +11,30 @@ withDefaults(defineProps<{
11
11
  const emit = defineEmits<{ 'update:modelValue': [boolean] }>()
12
12
  const close = () => emit('update:modelValue', false)
13
13
 
14
- // Drag-to-dismiss state
15
- const dragY = ref(0)
16
- const dragging = ref(false)
17
- let startY = 0
18
-
19
- function onHandlePointerDown(e: PointerEvent) {
20
- dragging.value = true
21
- startY = e.clientY
22
- dragY.value = 0
23
- ;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
24
- }
25
- function onHandlePointerMove(e: PointerEvent) {
26
- if (!dragging.value) return
27
- dragY.value = Math.max(0, e.clientY - startY)
28
- }
29
- function onHandlePointerUp() {
30
- if (dragY.value > 100) close()
31
- dragging.value = false
32
- dragY.value = 0
33
- }
34
-
35
- const sheetStyle = computed(() => ({
36
- transform: `translateY(${dragY.value}px)`,
37
- transition: dragging.value ? 'none' : undefined,
38
- }))
14
+ // Slide in/out and drag-to-dismiss, as springs (see _useSheetMotion).
15
+ const motion = useSheetMotion('y', close)
39
16
  </script>
40
17
 
41
18
  <template>
42
19
  <Teleport to="body">
43
- <Transition name="bs" :duration="{ enter: 320, leave: 280 }">
20
+ <Transition :css="false" @enter="motion.onEnter" @leave="motion.onLeave">
44
21
  <div v-if="modelValue" class="fixed inset-0 z-200 flex flex-col justify-end">
45
22
  <!-- Scrim -->
46
- <div class="bs-scrim absolute inset-0 bg-scrim/32" @click="close" />
23
+ <div data-sheet-scrim class="absolute inset-0 bg-scrim/32" @click="close" />
47
24
 
48
25
  <!-- Panel -->
49
26
  <div
50
- class="bs-panel relative mx-auto flex w-full max-w-[640px] flex-col rounded-t-[28px] bg-surface-container-low shadow-elevation-1"
27
+ data-sheet-panel
28
+ class="relative mx-auto flex w-full max-w-[640px] flex-col rounded-t-[28px] bg-surface-container-low shadow-elevation-1"
51
29
  :class="fullHeight ? 'max-h-[92vh]' : 'max-h-[60vh]'"
52
- :style="sheetStyle"
53
30
  >
54
31
  <!-- Drag handle -->
55
32
  <div
56
33
  class="flex h-12 shrink-0 cursor-grab touch-none select-none items-center justify-center active:cursor-grabbing"
57
- @pointerdown="onHandlePointerDown"
58
- @pointermove="onHandlePointerMove"
59
- @pointerup="onHandlePointerUp"
34
+ @pointerdown="motion.onDragStart"
35
+ @pointermove="motion.onDragMove"
36
+ @pointerup="motion.onDragEnd"
37
+ @pointercancel="motion.onDragEnd"
60
38
  >
61
39
  <div class="h-1 w-8 rounded-full bg-on-surface-variant" />
62
40
  </div>
@@ -87,27 +65,3 @@ const sheetStyle = computed(() => ({
87
65
  </Transition>
88
66
  </Teleport>
89
67
  </template>
90
-
91
- <style scoped>
92
- .bs-scrim {
93
- transition: opacity 300ms ease;
94
- }
95
- .bs-enter-from .bs-scrim,
96
- .bs-leave-to .bs-scrim {
97
- opacity: 0;
98
- }
99
-
100
- .bs-panel {
101
- transition:
102
- transform 320ms cubic-bezier(0.2, 0, 0, 1),
103
- opacity 240ms ease;
104
- }
105
- .bs-enter-from .bs-panel {
106
- transform: translateY(40%);
107
- opacity: 0;
108
- }
109
- .bs-leave-to .bs-panel {
110
- transform: translateY(100%) !important;
111
- opacity: 0;
112
- }
113
- </style>
@@ -40,7 +40,8 @@ onMounted(async () => {
40
40
  m3Colors.value = getM3Colors()
41
41
 
42
42
  themeObserver = new MutationObserver(() => { m3Colors.value = getM3Colors() })
43
- themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class', 'style'] })
43
+ // `class` flips light/dark, `data-palette` switches the palette (useColorPalette / createM3UI).
44
+ themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class', 'style', 'data-palette'] })
44
45
  // Canvas text drawn before a web font finishes loading keeps the fallback, so redraw once it has
45
46
  document.fonts?.ready.then(() => { m3Colors.value = getM3Colors() })
46
47