@m3ui-vue/m3ui-vue 1.1.4 → 1.2.0

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 (199) hide show
  1. package/dist/API.md +511 -34
  2. package/dist/MMenuItem-DI351kIf.js +929 -0
  3. package/dist/MMenuItem-DI351kIf.js.map +1 -0
  4. package/dist/{MSlider-JflHJ70q.js → MSlider-BnEy5ieZ.js} +295 -320
  5. package/dist/MSlider-BnEy5ieZ.js.map +1 -0
  6. package/dist/MTextField-BBmkaIbO.js +633 -0
  7. package/dist/MTextField-BBmkaIbO.js.map +1 -0
  8. package/dist/api.json +1 -1
  9. package/dist/chart.js +83 -49
  10. package/dist/chart.js.map +1 -1
  11. package/dist/code-editor.js +1 -1
  12. package/dist/components/MButton.vue.d.ts +7 -1
  13. package/dist/components/MButtonGroup.vue.d.ts +40 -31
  14. package/dist/components/MCarousel.vue.d.ts +2 -0
  15. package/dist/components/MChart.vue.d.ts +6 -0
  16. package/dist/components/MCircleProgressBar.vue.d.ts +5 -3
  17. package/dist/components/MContextMenu.vue.d.ts +2 -2
  18. package/dist/components/MCopyButton.vue.d.ts +1 -1
  19. package/dist/components/MExportButton.vue.d.ts +1 -1
  20. package/dist/components/MFab.vue.d.ts +2 -2
  21. package/dist/components/MFabMenu.vue.d.ts +1 -1
  22. package/dist/components/MIconButton.vue.d.ts +13 -1
  23. package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
  24. package/dist/components/MMap.vue.d.ts +20 -2
  25. package/dist/components/MMapBaseLayers.vue.d.ts +3 -0
  26. package/dist/components/MMapFilter.vue.d.ts +3 -0
  27. package/dist/components/MMapLayers.vue.d.ts +3 -0
  28. package/dist/components/MMenu.vue.d.ts +2 -2
  29. package/dist/components/MMenuItem.vue.d.ts +7 -3
  30. package/dist/components/MNavigationDrawer.vue.d.ts +13 -2
  31. package/dist/components/MOverlay.vue.d.ts +12 -1
  32. package/dist/components/MProgressBar.vue.d.ts +5 -3
  33. package/dist/components/MPullToRefresh.vue.d.ts +7 -2
  34. package/dist/components/MRichTextEditor.vue.d.ts +7 -1
  35. package/dist/components/MSearchField.vue.d.ts +58 -0
  36. package/dist/components/MSegmentedButton.vue.d.ts +31 -23
  37. package/dist/components/MSimpleCarousel.vue.d.ts +2 -0
  38. package/dist/components/MSkeleton.vue.d.ts +6 -3
  39. package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
  40. package/dist/components/MSwitch.vue.d.ts +1 -1
  41. package/dist/components/MTextField.vue.d.ts +5 -1
  42. package/dist/components/MTimeline.vue.d.ts +85 -22
  43. package/dist/components/MTooltip.vue.d.ts +1 -1
  44. package/dist/components/_MDrawerItemList.vue.d.ts +2 -0
  45. package/dist/composables/_mapStyle.d.ts +13 -0
  46. package/dist/composables/_themeTransition.d.ts +2 -0
  47. package/dist/composables/_tooltipTrigger.d.ts +22 -0
  48. package/dist/composables/_useMenuNav.d.ts +28 -0
  49. package/dist/composables/useAutoGrow.d.ts +21 -0
  50. package/dist/composables/useColorPalette.d.ts +1 -1
  51. package/dist/composables/useLocale.d.ts +3 -0
  52. package/dist/composables/useMap.d.ts +19 -3
  53. package/dist/composables/useMapFilter.d.ts +4 -1
  54. package/dist/composables/useMapLayers.d.ts +5 -2
  55. package/dist/composables/useScrollReveal.d.ts +44 -0
  56. package/dist/composables/useThemeTokens.d.ts +21 -0
  57. package/dist/composables/viewportHints.d.ts +27 -0
  58. package/dist/directives/vClickOutside.d.ts +25 -0
  59. package/dist/directives/vFocusRing.d.ts +22 -0
  60. package/dist/directives/vLongPress.d.ts +27 -0
  61. package/dist/directives/vReveal.d.ts +10 -0
  62. package/dist/directives/vScrollElevation.d.ts +31 -0
  63. package/dist/directives/vSkeleton.d.ts +19 -0
  64. package/dist/directives/vStateLayer.d.ts +20 -0
  65. package/dist/directives/vSwipe.d.ts +45 -0
  66. package/dist/directives/vTooltip.d.ts +23 -0
  67. package/dist/index.d.ts +28 -1
  68. package/dist/locales/index.js +21 -7
  69. package/dist/locales/index.js.map +1 -1
  70. package/dist/m3ui-vue.css +1 -1
  71. package/dist/m3ui.js +7131 -6216
  72. package/dist/m3ui.js.map +1 -1
  73. package/dist/map.d.ts +1 -1
  74. package/dist/map.js +821 -791
  75. package/dist/map.js.map +1 -1
  76. package/dist/motion-DZiXG09o.js +150 -0
  77. package/dist/motion-DZiXG09o.js.map +1 -0
  78. package/dist/plugin.d.ts +24 -0
  79. package/dist/rich-text-editor.js +13 -9
  80. package/dist/rich-text-editor.js.map +1 -1
  81. package/dist/styles/theme.css +63 -0
  82. package/dist/styles.css +1 -1
  83. package/dist/{useLocale-CXibICQA.js → useLocale-JYxDDeXC.js} +4 -2
  84. package/dist/useLocale-JYxDDeXC.js.map +1 -0
  85. package/dist/useThemeTokens-Bv-Uv8eX.js +135 -0
  86. package/dist/useThemeTokens-Bv-Uv8eX.js.map +1 -0
  87. package/package.json +1 -1
  88. package/src/__tests__/MButton.test.ts +22 -0
  89. package/src/__tests__/MCarousel.test.ts +9 -0
  90. package/src/__tests__/MChart.test.ts +77 -5
  91. package/src/__tests__/MIconButton.test.ts +33 -0
  92. package/src/__tests__/MMap.test.ts +53 -1
  93. package/src/__tests__/MMapLayers.test.ts +46 -2
  94. package/src/__tests__/MMenu.test.ts +104 -1
  95. package/src/__tests__/MMenuItem.test.ts +18 -0
  96. package/src/__tests__/MNavigationDrawer.test.ts +80 -0
  97. package/src/__tests__/MOverlay.test.ts +49 -0
  98. package/src/__tests__/MProgressBar.test.ts +41 -0
  99. package/src/__tests__/MPullToRefresh.test.ts +33 -0
  100. package/src/__tests__/MRichTextEditor.test.ts +22 -0
  101. package/src/__tests__/MSearchField.test.ts +71 -0
  102. package/src/__tests__/MSkeleton.test.ts +38 -0
  103. package/src/__tests__/MTimeline.test.ts +238 -0
  104. package/src/__tests__/_fakeMaplibre.ts +3 -1
  105. package/src/__tests__/attrsFallthrough.test.ts +3 -0
  106. package/src/__tests__/escapeStack.test.ts +93 -0
  107. package/src/__tests__/ssr.test.ts +21 -2
  108. package/src/__tests__/useAutoGrow.test.ts +103 -0
  109. package/src/__tests__/useScrollReveal.test.ts +102 -0
  110. package/src/__tests__/useThemeTokens.test.ts +114 -0
  111. package/src/__tests__/vClickOutside.test.ts +82 -0
  112. package/src/__tests__/vFocusRing.test.ts +36 -0
  113. package/src/__tests__/vLongPress.test.ts +81 -0
  114. package/src/__tests__/vScrollElevation.test.ts +89 -0
  115. package/src/__tests__/vSkeleton.test.ts +48 -0
  116. package/src/__tests__/vStateLayer.test.ts +53 -0
  117. package/src/__tests__/vSwipe.test.ts +93 -0
  118. package/src/__tests__/vTooltip.test.ts +95 -0
  119. package/src/__tests__/viewportHints.test.ts +41 -0
  120. package/src/components/MAutocomplete.vue +5 -0
  121. package/src/components/MBottomSheet.vue +4 -1
  122. package/src/components/MButton.vue +15 -1
  123. package/src/components/MButtonGroup.vue +22 -17
  124. package/src/components/MCarousel.vue +7 -1
  125. package/src/components/MChart.vue +74 -34
  126. package/src/components/MCircleProgressBar.vue +14 -9
  127. package/src/components/MCommandPalette.vue +3 -2
  128. package/src/components/MContextMenu.vue +19 -60
  129. package/src/components/MDragDropList.vue +2 -0
  130. package/src/components/MIconButton.vue +40 -1
  131. package/src/components/MKanban.vue +2 -0
  132. package/src/components/MLightbox.vue +7 -2
  133. package/src/components/MMap.vue +38 -5
  134. package/src/components/MMapBaseLayers.vue +7 -3
  135. package/src/components/MMapFilter.vue +7 -4
  136. package/src/components/MMapGeoJson.vue +1 -1
  137. package/src/components/MMapLayers.vue +6 -2
  138. package/src/components/MMapMeasure.vue +5 -2
  139. package/src/components/MMenu.vue +18 -59
  140. package/src/components/MMenuItem.vue +11 -2
  141. package/src/components/MMultiAutocomplete.vue +5 -0
  142. package/src/components/MNavigationDrawer.vue +96 -11
  143. package/src/components/MNavigationRail.vue +4 -15
  144. package/src/components/MOverlay.vue +32 -13
  145. package/src/components/MProgressBar.vue +25 -27
  146. package/src/components/MPullToRefresh.vue +20 -1
  147. package/src/components/MRichTextEditor.vue +19 -5
  148. package/src/components/MSearchBar.vue +5 -0
  149. package/src/components/MSearchField.vue +142 -0
  150. package/src/components/MSegmentedButton.vue +19 -17
  151. package/src/components/MSelect.vue +10 -1
  152. package/src/components/MSideSheet.vue +5 -2
  153. package/src/components/MSimpleCarousel.vue +10 -1
  154. package/src/components/MSkeleton.vue +30 -8
  155. package/src/components/MSpotlightSearch.vue +3 -2
  156. package/src/components/MTextField.vue +15 -2
  157. package/src/components/MTimeline.vue +370 -81
  158. package/src/components/MTooltip.vue +20 -152
  159. package/src/components/_MDrawerItemList.vue +25 -6
  160. package/src/composables/_mapStyle.ts +45 -0
  161. package/src/composables/_themeTransition.ts +2 -0
  162. package/src/composables/_tooltipTrigger.ts +184 -0
  163. package/src/composables/_useMenuNav.ts +209 -0
  164. package/src/composables/useAutoGrow.ts +78 -0
  165. package/src/composables/useLocale.ts +5 -0
  166. package/src/composables/useMap.ts +43 -5
  167. package/src/composables/useMapFilter.ts +6 -5
  168. package/src/composables/useMapLayers.ts +7 -6
  169. package/src/composables/useScrollReveal.ts +151 -0
  170. package/src/composables/useThemeTokens.ts +116 -0
  171. package/src/composables/viewportHints.ts +80 -0
  172. package/src/directives/vClickOutside.ts +90 -0
  173. package/src/directives/vFocusRing.ts +60 -0
  174. package/src/directives/vLongPress.ts +134 -0
  175. package/src/directives/vReveal.ts +57 -0
  176. package/src/directives/vScrollElevation.ts +116 -0
  177. package/src/directives/vSkeleton.ts +87 -0
  178. package/src/directives/vStateLayer.ts +83 -0
  179. package/src/directives/vSwipe.ts +197 -0
  180. package/src/directives/vTooltip.ts +128 -0
  181. package/src/index.ts +28 -1
  182. package/src/locales/de.ts +2 -0
  183. package/src/locales/es.ts +2 -0
  184. package/src/locales/fr.ts +2 -0
  185. package/src/locales/ja.ts +2 -0
  186. package/src/locales/ko.ts +2 -0
  187. package/src/locales/pt.ts +2 -0
  188. package/src/locales/zh.ts +2 -0
  189. package/src/map.ts +1 -1
  190. package/src/plugin.ts +34 -0
  191. package/src/styles/theme.css +63 -0
  192. package/dist/MMenuItem-BfjqwcYP.js +0 -830
  193. package/dist/MMenuItem-BfjqwcYP.js.map +0 -1
  194. package/dist/MSlider-JflHJ70q.js.map +0 -1
  195. package/dist/MTextField-NaIB6RbM.js +0 -673
  196. package/dist/MTextField-NaIB6RbM.js.map +0 -1
  197. package/dist/composables/_useThemeTokens.d.ts +0 -7
  198. package/dist/useLocale-CXibICQA.js.map +0 -1
  199. package/src/composables/_useThemeTokens.ts +0 -42
@@ -0,0 +1,114 @@
1
+ import { describe, it, expect, afterEach, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, type Ref } from 'vue'
4
+ import { useThemeTokens } from '../composables/useThemeTokens'
5
+
6
+ const root = document.documentElement
7
+ afterEach(() => {
8
+ root.style.removeProperty('--color-primary')
9
+ root.className = ''
10
+ vi.useRealTimers()
11
+ })
12
+
13
+ function use(live = false) {
14
+ let tokens!: Ref<Record<'primary', string>>
15
+ const w = mount(defineComponent({ setup: () => { tokens = useThemeTokens(['primary'] as const, { live }); return () => h('div') } }))
16
+ return { tokens: () => tokens.value, w }
17
+ }
18
+ const flush = () => new Promise((r) => setTimeout(r, 0))
19
+
20
+ describe('useThemeTokens', () => {
21
+ it('reads the tokens from <html>', () => {
22
+ root.style.setProperty('--color-primary', '#6750a4')
23
+ const { tokens, w } = use()
24
+ expect(tokens().primary).toBe('#6750a4')
25
+ w.unmount()
26
+ })
27
+
28
+ it('re-reads on a theme change, and again once the transition settles', async () => {
29
+ root.style.setProperty('--color-primary', '#6750a4')
30
+ const { tokens, w } = use()
31
+ await nextTick()
32
+ root.style.setProperty('--color-primary', '#d0bcff')
33
+ root.classList.add('dark')
34
+ await flush()
35
+ expect(tokens().primary).toBe('#d0bcff')
36
+
37
+ vi.useFakeTimers()
38
+ root.style.setProperty('--color-primary', '#ffffff')
39
+ root.classList.remove('dark')
40
+ await vi.advanceTimersByTimeAsync(0)
41
+ root.style.setProperty('--color-primary', '#000000') // the value the transition lands on
42
+ await vi.advanceTimersByTimeAsync(300)
43
+ expect(tokens().primary).toBe('#000000')
44
+ w.unmount()
45
+ })
46
+
47
+ describe('live', () => {
48
+ // A fake clock drives both performance.now() and requestAnimationFrame (16ms frames)
49
+ const frames = (ms: number) => vi.advanceTimersByTimeAsync(ms)
50
+
51
+ it('replays the fade from the old colors when the tokens jump (Firefox\'s theme-transitioning)', async () => {
52
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'requestAnimationFrame', 'cancelAnimationFrame', 'performance'] })
53
+ root.style.setProperty('--color-primary', '#000000')
54
+ const { tokens, w } = use(true)
55
+ await nextTick()
56
+ root.classList.add('theme-transitioning')
57
+ root.style.setProperty('--color-primary', '#ffffff')
58
+ await frames(0)
59
+ expect(tokens().primary).toBe('#000000')
60
+ await frames(130)
61
+ const mid = /^rgba\((\d+), (\d+), (\d+), 1\)$/.exec(tokens().primary)
62
+ expect(mid).not.toBeNull()
63
+ expect(Number(mid![1])).toBeGreaterThan(60)
64
+ expect(Number(mid![1])).toBeLessThan(250)
65
+ await frames(200)
66
+ expect(tokens().primary).toBe('#ffffff')
67
+ w.unmount()
68
+ })
69
+
70
+ // A token animating through @property changes its computed value with no DOM mutation: a stylesheet outside
71
+ // <head> (which isn't observed) stands in for that
72
+ function animatedToken() {
73
+ const sheet = document.createElement('style')
74
+ document.body.appendChild(sheet)
75
+ return { to: (v: string) => { sheet.textContent = `:root { --color-primary: ${v} }` }, remove: () => sheet.remove() }
76
+ }
77
+
78
+ it('reads the tokens every frame while they animate themselves (Chromium\'s theme-switching)', async () => {
79
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'requestAnimationFrame', 'cancelAnimationFrame', 'performance'] })
80
+ const token = animatedToken()
81
+ token.to('#000000')
82
+ const { tokens, w } = use(true)
83
+ await nextTick()
84
+ root.classList.add('theme-switching')
85
+ await frames(0)
86
+ token.to('rgb(40, 40, 40)')
87
+ await frames(50)
88
+ expect(tokens().primary).toBe('rgb(40, 40, 40)')
89
+ token.to('rgb(90, 90, 90)')
90
+ await frames(50)
91
+ expect(tokens().primary).toBe('rgb(90, 90, 90)')
92
+ w.unmount()
93
+ token.remove()
94
+ })
95
+
96
+ it('without live, waits for the switch to settle', async () => {
97
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'requestAnimationFrame', 'cancelAnimationFrame', 'performance'] })
98
+ const token = animatedToken()
99
+ token.to('#000000')
100
+ const { tokens, w } = use(false)
101
+ await nextTick()
102
+ root.classList.add('theme-switching')
103
+ await frames(0)
104
+ token.to('rgb(40, 40, 40)')
105
+ await frames(100)
106
+ expect(tokens().primary).toBe('#000000')
107
+ token.to('#ffffff')
108
+ await frames(300)
109
+ expect(tokens().primary).toBe('#ffffff')
110
+ w.unmount()
111
+ token.remove()
112
+ })
113
+ })
114
+ })
@@ -0,0 +1,82 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, ref, withDirectives } from 'vue'
4
+ import { vClickOutside, type ClickOutsideValue } from '../directives/vClickOutside'
5
+
6
+ const press = (target: EventTarget, type = 'pointerdown', button = 0) =>
7
+ target.dispatchEvent(new PointerEvent(type, { button, bubbles: true, composed: true }))
8
+
9
+ describe('vClickOutside', () => {
10
+ let outside: HTMLElement
11
+ beforeEach(() => {
12
+ vi.useFakeTimers()
13
+ document.body.innerHTML = ''
14
+ outside = document.createElement('div')
15
+ outside.className = 'outside'
16
+ document.body.appendChild(outside)
17
+ })
18
+ afterEach(() => vi.useRealTimers())
19
+
20
+ function host(value: () => ClickOutsideValue) {
21
+ const w = mount(defineComponent({
22
+ setup: () => () => withDirectives(h('div', { class: 'panel' }, [h('button', { class: 'inner' })]), [[vClickOutside, value()]]),
23
+ }), { attachTo: document.body })
24
+ vi.advanceTimersByTime(0)
25
+ return { w, el: w.element as HTMLElement }
26
+ }
27
+
28
+ it('calls the handler for a press outside, not inside or with another button', () => {
29
+ const handler = vi.fn()
30
+ const { el } = host(() => handler)
31
+ press(el.querySelector('.inner')!)
32
+ expect(handler).not.toHaveBeenCalled()
33
+ press(outside, 'pointerdown', 2)
34
+ expect(handler).not.toHaveBeenCalled()
35
+ press(outside)
36
+ expect(handler).toHaveBeenCalledTimes(1)
37
+ })
38
+
39
+ it('ignores the event that mounted it', () => {
40
+ const handler = vi.fn()
41
+ mount(defineComponent({
42
+ setup: () => () => withDirectives(h('div'), [[vClickOutside, handler]]),
43
+ }), { attachTo: document.body })
44
+ press(outside)
45
+ expect(handler).not.toHaveBeenCalled()
46
+ vi.advanceTimersByTime(0)
47
+ press(outside)
48
+ expect(handler).toHaveBeenCalledTimes(1)
49
+ })
50
+
51
+ it('treats excluded elements and selectors as inside, and can wait for the click', () => {
52
+ const handler = vi.fn()
53
+ const toggle = document.createElement('button')
54
+ document.body.appendChild(toggle)
55
+ const popup = document.createElement('div')
56
+ popup.className = 'popup'
57
+ popup.appendChild(document.createElement('span'))
58
+ document.body.appendChild(popup)
59
+ host(() => ({ handler, exclude: [toggle, '.popup'], event: 'click' }))
60
+ press(toggle, 'click')
61
+ press(popup.firstElementChild!, 'click')
62
+ press(outside, 'pointerdown')
63
+ expect(handler).not.toHaveBeenCalled()
64
+ press(outside, 'click')
65
+ expect(handler).toHaveBeenCalledTimes(1)
66
+ })
67
+
68
+ it('stops while disabled and after unmount', async () => {
69
+ const handler = vi.fn()
70
+ const disabled = ref(true)
71
+ const { w } = host(() => ({ handler, disabled: disabled.value }))
72
+ press(outside)
73
+ disabled.value = false
74
+ await nextTick()
75
+ vi.advanceTimersByTime(0)
76
+ press(outside)
77
+ expect(handler).toHaveBeenCalledTimes(1)
78
+ w.unmount()
79
+ press(outside)
80
+ expect(handler).toHaveBeenCalledTimes(1)
81
+ })
82
+ })
@@ -0,0 +1,36 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, ref, withDirectives } from 'vue'
4
+ import { vFocusRing, type FocusRingValue } from '../directives/vFocusRing'
5
+
6
+ function host(value: () => FocusRingValue) {
7
+ return mount(defineComponent({
8
+ setup: () => () => withDirectives(h('div', { tabindex: 0 }), [[vFocusRing, value()]]),
9
+ }))
10
+ }
11
+
12
+ describe('vFocusRing', () => {
13
+ it('marks the element for the ring, with no color or offset of its own by default', () => {
14
+ const el = host(() => undefined).element as HTMLElement
15
+ expect(el.dataset.m3FocusRing).toBe('')
16
+ expect(el.style.getPropertyValue('--m3-focus-ring-color')).toBe('')
17
+ expect(el.style.getPropertyValue('--m3-focus-ring-offset')).toBe('')
18
+ })
19
+
20
+ it('follows its value: a color, inset, within, off', async () => {
21
+ const value = ref<FocusRingValue>('#e91e63')
22
+ const w = host(() => value.value)
23
+ const el = w.element as HTMLElement
24
+ expect(el.style.getPropertyValue('--m3-focus-ring-color')).toBe('#e91e63')
25
+ value.value = { inset: true, within: true }
26
+ await nextTick()
27
+ expect(el.dataset.m3FocusRing).toBe('within')
28
+ expect(el.style.getPropertyValue('--m3-focus-ring-color')).toBe('')
29
+ expect(el.style.getPropertyValue('--m3-focus-ring-offset')).toBe('-3px')
30
+ value.value = false
31
+ await nextTick()
32
+ expect(el.dataset.m3FocusRing).toBe('off')
33
+ w.unmount()
34
+ expect(el.dataset.m3FocusRing).toBeUndefined()
35
+ })
36
+ })
@@ -0,0 +1,81 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, withDirectives } from 'vue'
4
+ import { vLongPress, type LongPressValue } from '../directives/vLongPress'
5
+
6
+ const pointer = (type: string, pointerType = 'touch', x = 10, y = 10) =>
7
+ new PointerEvent(type, { pointerType, pointerId: 1, button: 0, clientX: x, clientY: y, bubbles: true })
8
+
9
+ function host(value: LongPressValue, onClick = vi.fn()) {
10
+ const w = mount(defineComponent({
11
+ setup: () => () => withDirectives(h('button', { onClick }, 'Hold'), [[vLongPress, value]]),
12
+ }), { attachTo: document.body })
13
+ return { w, el: w.element as HTMLElement, onClick }
14
+ }
15
+
16
+ describe('vLongPress', () => {
17
+ beforeEach(() => vi.useFakeTimers())
18
+ afterEach(() => vi.useRealTimers())
19
+
20
+ it('fires after the duration, marks the press while held, and swallows the click it ends in', () => {
21
+ const handler = vi.fn()
22
+ const { el, onClick } = host(handler)
23
+ el.dispatchEvent(pointer('pointerdown'))
24
+ expect(el.dataset.longPressing).toBe('')
25
+ vi.advanceTimersByTime(499)
26
+ expect(handler).not.toHaveBeenCalled()
27
+ vi.advanceTimersByTime(1)
28
+ expect(handler).toHaveBeenCalledTimes(1)
29
+ expect(handler.mock.calls[0]![0]).toBeInstanceOf(PointerEvent)
30
+ expect(el.dataset.longPressing).toBeUndefined()
31
+ window.dispatchEvent(pointer('pointerup'))
32
+ el.click()
33
+ expect(onClick).not.toHaveBeenCalled()
34
+ })
35
+
36
+ it('a short press is a plain click, and moving past the tolerance cancels it', () => {
37
+ const handler = vi.fn()
38
+ const { el, onClick } = host({ handler, duration: 300 })
39
+ el.dispatchEvent(pointer('pointerdown'))
40
+ vi.advanceTimersByTime(100)
41
+ window.dispatchEvent(pointer('pointerup'))
42
+ el.click()
43
+ expect(onClick).toHaveBeenCalledTimes(1)
44
+
45
+ el.dispatchEvent(pointer('pointerdown'))
46
+ window.dispatchEvent(pointer('pointermove', 'touch', 10, 25))
47
+ vi.advanceTimersByTime(300)
48
+ expect(handler).not.toHaveBeenCalled()
49
+ })
50
+
51
+ it('leaves the mouse alone with mouse: false, and does nothing when disabled', () => {
52
+ const handler = vi.fn()
53
+ const { el } = host({ handler, mouse: false })
54
+ el.dispatchEvent(pointer('pointerdown', 'mouse'))
55
+ vi.advanceTimersByTime(600)
56
+ expect(handler).not.toHaveBeenCalled()
57
+
58
+ const off = host({ handler, disabled: true })
59
+ off.el.dispatchEvent(pointer('pointerdown'))
60
+ vi.advanceTimersByTime(600)
61
+ expect(handler).not.toHaveBeenCalled()
62
+ })
63
+
64
+ it('stops swallowing clicks a moment after a press that ended in none', () => {
65
+ const { el, onClick } = host(vi.fn())
66
+ el.dispatchEvent(pointer('pointerdown'))
67
+ vi.advanceTimersByTime(500)
68
+ window.dispatchEvent(pointer('pointerup'))
69
+ vi.advanceTimersByTime(400)
70
+ el.click()
71
+ expect(onClick).toHaveBeenCalledTimes(1)
72
+ })
73
+
74
+ it('blocks the browser\'s long-press menu while pressing', () => {
75
+ const { el } = host(vi.fn())
76
+ el.dispatchEvent(pointer('pointerdown'))
77
+ const menu = new Event('contextmenu', { cancelable: true })
78
+ el.dispatchEvent(menu)
79
+ expect(menu.defaultPrevented).toBe(true)
80
+ })
81
+ })
@@ -0,0 +1,89 @@
1
+ import { describe, it, expect, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, ref, withDirectives } from 'vue'
4
+ import { vScrollElevation, type ScrollElevationValue } from '../directives/vScrollElevation'
5
+
6
+ function scroller(top = 0) {
7
+ const el = document.createElement('div')
8
+ el.scrollTop = top
9
+ document.body.appendChild(el)
10
+ return {
11
+ el,
12
+ scrollTo(y: number) {
13
+ el.scrollTop = y
14
+ el.dispatchEvent(new Event('scroll'))
15
+ },
16
+ }
17
+ }
18
+
19
+ function host(value: () => ScrollElevationValue) {
20
+ return mount(defineComponent({
21
+ setup: () => () => withDirectives(h('header'), [[vScrollElevation, value()]]),
22
+ }))
23
+ }
24
+
25
+ describe('vScrollElevation', () => {
26
+ it('takes the scrolled color and data-scrolled once its target scrolls, and gives them back at the top', () => {
27
+ const list = scroller()
28
+ const onChange = vi.fn()
29
+ const w = host(() => ({ target: list.el, onChange }))
30
+ const el = w.element as HTMLElement
31
+ expect(el.dataset.scrolled).toBeUndefined()
32
+ list.scrollTo(30)
33
+ expect(el.dataset.scrolled).toBe('')
34
+ expect(el.style.backgroundColor).toBe('var(--color-surface-container)')
35
+ list.scrollTo(0)
36
+ expect(el.dataset.scrolled).toBeUndefined()
37
+ expect(el.style.backgroundColor).toBe('')
38
+ expect(onChange.mock.calls).toEqual([[true], [false]])
39
+ w.unmount()
40
+ })
41
+
42
+ it('takes a selector, a threshold, its own color or none', async () => {
43
+ const list = scroller()
44
+ list.el.id = 'feed'
45
+ const color = ref<string | false>('#123456')
46
+ const w = host(() => ({ target: '#feed', threshold: 50, color: color.value }))
47
+ const el = w.element as HTMLElement
48
+ list.scrollTo(40)
49
+ expect(el.dataset.scrolled).toBeUndefined()
50
+ list.scrollTo(60)
51
+ expect(el.style.backgroundColor).toMatch(/#123456|rgb\(18, 52, 86\)/)
52
+ color.value = false
53
+ await nextTick()
54
+ expect(el.dataset.scrolled).toBe('')
55
+ expect(el.style.backgroundColor).toBe('')
56
+ w.unmount()
57
+ })
58
+
59
+ it('starts scrolled if its target already is, and stops following when disabled or unmounted', async () => {
60
+ const list = scroller(100)
61
+ const disabled = ref(false)
62
+ const w = host(() => ({ target: list.el, disabled: disabled.value }))
63
+ const el = w.element as HTMLElement
64
+ expect(el.dataset.scrolled).toBe('')
65
+ disabled.value = true
66
+ await nextTick()
67
+ expect(el.dataset.scrolled).toBeUndefined()
68
+ list.scrollTo(200)
69
+ expect(el.dataset.scrolled).toBeUndefined()
70
+ w.unmount()
71
+ })
72
+ })
73
+
74
+ describe('vScrollElevation target ref', () => {
75
+ it('waits for a template ref that is still empty instead of following the window', async () => {
76
+ const target = ref<HTMLElement | null>(null)
77
+ const w = host(() => ({ target: target.value }))
78
+ const el = w.element as HTMLElement
79
+ Object.defineProperty(window, 'scrollY', { value: 500, configurable: true })
80
+ window.dispatchEvent(new Event('scroll'))
81
+ expect(el.dataset.scrolled).toBeUndefined()
82
+ const list = scroller(80)
83
+ target.value = list.el
84
+ await nextTick()
85
+ expect(el.dataset.scrolled).toBe('')
86
+ Object.defineProperty(window, 'scrollY', { value: 0, configurable: true })
87
+ w.unmount()
88
+ })
89
+ })
@@ -0,0 +1,48 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, ref, withDirectives } from 'vue'
4
+ import { vSkeleton, type SkeletonValue } from '../directives/vSkeleton'
5
+
6
+ function host(value: () => SkeletonValue) {
7
+ return mount(defineComponent({
8
+ setup: () => () => withDirectives(h('div', [h('p', 'Ada Lovelace')]), [[vSkeleton, value()]]),
9
+ }), { attachTo: document.body })
10
+ }
11
+
12
+ describe('vSkeleton', () => {
13
+ it('marks the element as a busy placeholder while loading, and clears it after', async () => {
14
+ const loading = ref(true)
15
+ const w = host(() => loading.value)
16
+ const el = w.element as HTMLElement
17
+ expect(el.dataset.m3Skeleton).toBe('pulse')
18
+ expect(el.getAttribute('aria-busy')).toBe('true')
19
+ loading.value = false
20
+ await nextTick()
21
+ expect(el.dataset.m3Skeleton).toBeUndefined()
22
+ expect(el.hasAttribute('aria-busy')).toBe(false)
23
+ w.unmount()
24
+ })
25
+
26
+ it('wave adds a shimmer layer (and positions a static element); switching away removes both', async () => {
27
+ const value = ref<SkeletonValue>({ loading: true, animation: 'wave' })
28
+ const w = host(() => value.value)
29
+ const el = w.element as HTMLElement
30
+ expect(el.dataset.m3Skeleton).toBe('wave')
31
+ expect(el.querySelectorAll('.m3-skeleton-shimmer')).toHaveLength(1)
32
+ expect(el.style.position).toBe('relative')
33
+ value.value = { loading: true, animation: 'none' }
34
+ await nextTick()
35
+ expect(el.dataset.m3Skeleton).toBe('none')
36
+ expect(el.querySelector('.m3-skeleton-shimmer')).toBeNull()
37
+ value.value = false
38
+ await nextTick()
39
+ expect(el.style.position).toBe('')
40
+ w.unmount()
41
+ })
42
+
43
+ it('keeps the content in the DOM (it sizes the placeholder)', () => {
44
+ const w = host(() => true)
45
+ expect(w.text()).toContain('Ada Lovelace')
46
+ w.unmount()
47
+ })
48
+ })
@@ -0,0 +1,53 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, ref, withDirectives } from 'vue'
4
+ import { vStateLayer, type StateLayerValue } from '../directives/vStateLayer'
5
+
6
+ function host(value: () => StateLayerValue, children?: () => unknown) {
7
+ return mount(defineComponent({
8
+ setup() {
9
+ return () => withDirectives(h('div', { class: 'host' }, children?.() as never), [[vStateLayer, value()]])
10
+ },
11
+ }), { attachTo: document.body })
12
+ }
13
+ const layerOf = (el: Element) => el.querySelector<HTMLElement>(':scope > .m3-state-layer')
14
+
15
+ describe('vStateLayer', () => {
16
+ it('adds a hidden layer and makes a static host positioned', () => {
17
+ const w = host(() => undefined)
18
+ const el = w.element as HTMLElement
19
+ expect(layerOf(el)).not.toBeNull()
20
+ expect(layerOf(el)!.getAttribute('aria-hidden')).toBe('true')
21
+ expect(el.dataset.m3StateLayer).toBe('')
22
+ expect(el.style.position).toBe('relative')
23
+ w.unmount()
24
+ })
25
+
26
+ it('follows its value: off, a color, dragged', async () => {
27
+ const value = ref<StateLayerValue>(false)
28
+ const w = host(() => value.value)
29
+ const el = w.element as HTMLElement
30
+ expect(el.dataset.m3StateLayer).toBe('off')
31
+ value.value = '#e91e63'
32
+ await nextTick()
33
+ expect(el.dataset.m3StateLayer).toBe('')
34
+ expect(layerOf(el)!.style.background).toContain('#e91e63')
35
+ value.value = { dragged: true }
36
+ await nextTick()
37
+ expect(el.dataset.m3StateLayer).toBe('dragged')
38
+ expect(layerOf(el)!.style.background).toBe('')
39
+ w.unmount()
40
+ })
41
+
42
+ it('keeps one layer across re-renders of its children, and removes it on unmount', async () => {
43
+ const items = ref(['a'])
44
+ const w = host(() => true, () => items.value.map((t) => h('span', { key: t }, t)))
45
+ const el = w.element as HTMLElement
46
+ items.value = ['a', 'b', 'c']
47
+ await nextTick()
48
+ expect(el.querySelectorAll('.m3-state-layer')).toHaveLength(1)
49
+ w.unmount()
50
+ expect(el.querySelector('.m3-state-layer')).toBeNull()
51
+ expect(el.dataset.m3StateLayer).toBeUndefined()
52
+ })
53
+ })
@@ -0,0 +1,93 @@
1
+ import { describe, it, expect, vi } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick, ref, withDirectives } from 'vue'
4
+ import { vSwipe, type SwipeValue } from '../directives/vSwipe'
5
+
6
+ function host(value: () => SwipeValue, onClick = vi.fn()) {
7
+ const w = mount(defineComponent({
8
+ setup: () => () => withDirectives(h('div', { onClick }), [[vSwipe, value()]]),
9
+ }), { attachTo: document.body })
10
+ return { w, el: w.element as HTMLElement, onClick }
11
+ }
12
+
13
+ // Moves from (sx, sy) by (dx, dy) in `steps` events `msPerStep` apart, then lifts
14
+ function drag(el: HTMLElement, dx: number, dy: number, { steps = 5, msPerStep = 16, pointerType = 'touch', sx = 100, sy = 100 } = {}) {
15
+ let t = 1000
16
+ // happy-dom stamps events with its own clock: pin timeStamp so the speed is what the test says
17
+ const ev = (type: string, x: number, y: number) => {
18
+ const e = new PointerEvent(type, { pointerId: 1, pointerType, button: 0, clientX: x, clientY: y, bubbles: true })
19
+ Object.defineProperty(e, 'timeStamp', { value: t })
20
+ return e
21
+ }
22
+ el.dispatchEvent(ev('pointerdown', sx, sy))
23
+ for (let i = 1; i <= steps; i++) {
24
+ t += msPerStep
25
+ window.dispatchEvent(ev('pointermove', sx + (dx * i) / steps, sy + (dy * i) / steps))
26
+ }
27
+ window.dispatchEvent(ev('pointerup', sx + dx, sy + dy))
28
+ }
29
+
30
+ describe('vSwipe', () => {
31
+ it('calls the handler of the direction swiped, with the distance', () => {
32
+ const left = vi.fn()
33
+ const right = vi.fn()
34
+ const { el } = host(() => ({ left, right }))
35
+ drag(el, -80, 5)
36
+ expect(left).toHaveBeenCalledTimes(1)
37
+ expect(left.mock.calls[0]![0]).toMatchObject({ direction: 'left', dx: -80, dy: 5 })
38
+ drag(el, 80, 0)
39
+ expect(right).toHaveBeenCalledTimes(1)
40
+ })
41
+
42
+ it('counts a short flick but not a short slow drag', () => {
43
+ const handler = vi.fn()
44
+ const { el } = host(() => handler)
45
+ drag(el, 0, -30, { steps: 3, msPerStep: 200 })
46
+ expect(handler).not.toHaveBeenCalled()
47
+ drag(el, 0, -30, { steps: 3, msPerStep: 10 })
48
+ expect(handler).toHaveBeenCalledTimes(1)
49
+ expect(handler.mock.calls[0]![0].direction).toBe('up')
50
+ })
51
+
52
+ it('sets touch-action from the directions listened to', async () => {
53
+ const value = ref<SwipeValue>({ left: () => {} })
54
+ const { el } = host(() => value.value)
55
+ expect(el.style.touchAction).toBe('pan-y')
56
+ value.value = { down: () => {} }
57
+ await nextTick()
58
+ expect(el.style.touchAction).toBe('pan-x')
59
+ value.value = () => {}
60
+ await nextTick()
61
+ expect(el.style.touchAction).toBe('none')
62
+ value.value = { left: () => {}, disabled: true }
63
+ await nextTick()
64
+ expect(el.style.touchAction).toBe('')
65
+ })
66
+
67
+ it('ignores a drag along an axis nobody listens to, and the mouse unless asked', () => {
68
+ const left = vi.fn()
69
+ const onMove = vi.fn()
70
+ const { el } = host(() => ({ left, onMove }))
71
+ drag(el, -10, -120)
72
+ expect(left).not.toHaveBeenCalled()
73
+ expect(onMove).not.toHaveBeenCalled()
74
+ drag(el, -120, 0, { pointerType: 'mouse' })
75
+ expect(left).not.toHaveBeenCalled()
76
+ const withMouse = host(() => ({ left, mouse: true }))
77
+ drag(withMouse.el, -120, 0, { pointerType: 'mouse' })
78
+ expect(left).toHaveBeenCalledTimes(1)
79
+ })
80
+
81
+ it('reports moves and the end, and swallows the click a swipe ends in', () => {
82
+ const onMove = vi.fn()
83
+ const onEnd = vi.fn()
84
+ const { el, onClick } = host(() => ({ right: () => {}, onMove, onEnd }), vi.fn())
85
+ drag(el, 30, 0, { msPerStep: 200 })
86
+ expect(onMove).toHaveBeenLastCalledWith(30, 0, expect.any(PointerEvent))
87
+ expect(onEnd).toHaveBeenCalledWith(false)
88
+ el.click()
89
+ expect(onClick).not.toHaveBeenCalled()
90
+ el.click()
91
+ expect(onClick).toHaveBeenCalledTimes(1)
92
+ })
93
+ })