@m3ui-vue/m3ui-vue 1.1.3 → 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 (236) hide show
  1. package/README.md +1 -1
  2. package/dist/API.md +579 -40
  3. package/dist/MMenuItem-DI351kIf.js +929 -0
  4. package/dist/MMenuItem-DI351kIf.js.map +1 -0
  5. package/dist/MSlider-BnEy5ieZ.js +1500 -0
  6. package/dist/MSlider-BnEy5ieZ.js.map +1 -0
  7. package/dist/MTextField-BBmkaIbO.js +633 -0
  8. package/dist/MTextField-BBmkaIbO.js.map +1 -0
  9. package/dist/api.json +1 -1
  10. package/dist/chart.js +83 -49
  11. package/dist/chart.js.map +1 -1
  12. package/dist/code-editor.js +1 -1
  13. package/dist/components/MAnimatedNumber.vue.d.ts +3 -1
  14. package/dist/components/MButton.vue.d.ts +9 -1
  15. package/dist/components/MButtonGroup.vue.d.ts +40 -31
  16. package/dist/components/MCarousel.vue.d.ts +2 -0
  17. package/dist/components/MChart.vue.d.ts +6 -0
  18. package/dist/components/MCircleProgressBar.vue.d.ts +5 -3
  19. package/dist/components/MContainerTransform.vue.d.ts +59 -0
  20. package/dist/components/MContextMenu.vue.d.ts +2 -2
  21. package/dist/components/MCopyButton.vue.d.ts +1 -1
  22. package/dist/components/MDialog.vue.d.ts +8 -5
  23. package/dist/components/MExportButton.vue.d.ts +1 -1
  24. package/dist/components/MFab.vue.d.ts +2 -2
  25. package/dist/components/MFabMenu.vue.d.ts +1 -1
  26. package/dist/components/MFlex.vue.d.ts +1 -1
  27. package/dist/components/MIconButton.vue.d.ts +15 -1
  28. package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
  29. package/dist/components/MListItem.vue.d.ts +5 -5
  30. package/dist/components/MMap.vue.d.ts +20 -2
  31. package/dist/components/MMapBaseLayers.vue.d.ts +3 -0
  32. package/dist/components/MMapFilter.vue.d.ts +3 -0
  33. package/dist/components/MMapLayers.vue.d.ts +3 -0
  34. package/dist/components/MMenu.vue.d.ts +2 -2
  35. package/dist/components/MMenuItem.vue.d.ts +7 -3
  36. package/dist/components/MNavigationDrawer.vue.d.ts +13 -2
  37. package/dist/components/MNavigationRail.vue.d.ts +1 -1
  38. package/dist/components/MOverlay.vue.d.ts +12 -1
  39. package/dist/components/MProgressBar.vue.d.ts +5 -3
  40. package/dist/components/MPullToRefresh.vue.d.ts +7 -2
  41. package/dist/components/MRichTextEditor.vue.d.ts +7 -1
  42. package/dist/components/MSearchField.vue.d.ts +58 -0
  43. package/dist/components/MSegmentedButton.vue.d.ts +31 -23
  44. package/dist/components/MSimpleCarousel.vue.d.ts +2 -0
  45. package/dist/components/MSkeleton.vue.d.ts +6 -3
  46. package/dist/components/MSplitter.vue.d.ts +1 -1
  47. package/dist/components/MStack.vue.d.ts +1 -1
  48. package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
  49. package/dist/components/MSwitch.vue.d.ts +1 -1
  50. package/dist/components/MTextField.vue.d.ts +5 -1
  51. package/dist/components/MTimeline.vue.d.ts +85 -22
  52. package/dist/components/MToolbar.vue.d.ts +32 -5
  53. package/dist/components/MTooltip.vue.d.ts +1 -1
  54. package/dist/components/_MDrawerItemList.vue.d.ts +2 -0
  55. package/dist/components/_MToolbarFab.vue.d.ts +16 -0
  56. package/dist/composables/_containerTransform.d.ts +48 -0
  57. package/dist/composables/_mapStyle.d.ts +13 -0
  58. package/dist/composables/_themeTransition.d.ts +2 -0
  59. package/dist/composables/_tooltipTrigger.d.ts +22 -0
  60. package/dist/composables/_useMenuNav.d.ts +28 -0
  61. package/dist/composables/useAutoGrow.d.ts +21 -0
  62. package/dist/composables/useColorPalette.d.ts +1 -1
  63. package/dist/composables/useContainerTransform.d.ts +30 -0
  64. package/dist/composables/useLocale.d.ts +6 -0
  65. package/dist/composables/useMap.d.ts +19 -3
  66. package/dist/composables/useMapFilter.d.ts +4 -1
  67. package/dist/composables/useMapLayers.d.ts +5 -2
  68. package/dist/composables/useScrollReveal.d.ts +44 -0
  69. package/dist/composables/useThemeTokens.d.ts +21 -0
  70. package/dist/composables/viewportHints.d.ts +27 -0
  71. package/dist/directives/vClickOutside.d.ts +25 -0
  72. package/dist/directives/vFocusRing.d.ts +22 -0
  73. package/dist/directives/vLongPress.d.ts +27 -0
  74. package/dist/directives/vReveal.d.ts +10 -0
  75. package/dist/directives/vScrollElevation.d.ts +31 -0
  76. package/dist/directives/vSkeleton.d.ts +19 -0
  77. package/dist/directives/vStateLayer.d.ts +20 -0
  78. package/dist/directives/vSwipe.d.ts +45 -0
  79. package/dist/directives/vTooltip.d.ts +23 -0
  80. package/dist/index.d.ts +31 -1
  81. package/dist/locales/index.js +35 -7
  82. package/dist/locales/index.js.map +1 -1
  83. package/dist/m3ui-vue.css +1 -1
  84. package/dist/m3ui.js +7692 -6273
  85. package/dist/m3ui.js.map +1 -1
  86. package/dist/map.d.ts +1 -1
  87. package/dist/map.js +823 -793
  88. package/dist/map.js.map +1 -1
  89. package/dist/motion-DZiXG09o.js +150 -0
  90. package/dist/motion-DZiXG09o.js.map +1 -0
  91. package/dist/plugin.d.ts +24 -0
  92. package/dist/rich-text-editor.js +20 -16
  93. package/dist/rich-text-editor.js.map +1 -1
  94. package/dist/styles/theme.css +63 -0
  95. package/dist/styles.css +1 -1
  96. package/dist/{useLocale-SfQHnJfp.js → useLocale-JYxDDeXC.js} +6 -2
  97. package/dist/useLocale-JYxDDeXC.js.map +1 -0
  98. package/dist/useThemeTokens-Bv-Uv8eX.js +135 -0
  99. package/dist/useThemeTokens-Bv-Uv8eX.js.map +1 -0
  100. package/package.json +2 -1
  101. package/src/__tests__/MAnimatedNumber.test.ts +24 -2
  102. package/src/__tests__/MButton.test.ts +22 -0
  103. package/src/__tests__/MCarousel.test.ts +9 -0
  104. package/src/__tests__/MChart.test.ts +77 -5
  105. package/src/__tests__/MContainerTransform.test.ts +279 -0
  106. package/src/__tests__/MDialog.test.ts +27 -0
  107. package/src/__tests__/MIconButton.test.ts +33 -0
  108. package/src/__tests__/MListItemExpandable.test.ts +48 -0
  109. package/src/__tests__/MListItemShape.test.ts +9 -6
  110. package/src/__tests__/MListKeyboard.test.ts +90 -0
  111. package/src/__tests__/MMap.test.ts +53 -1
  112. package/src/__tests__/MMapLayers.test.ts +46 -2
  113. package/src/__tests__/MMapTools.test.ts +2 -1
  114. package/src/__tests__/MMenu.test.ts +104 -1
  115. package/src/__tests__/MMenuItem.test.ts +18 -0
  116. package/src/__tests__/MNavigationDrawer.test.ts +80 -0
  117. package/src/__tests__/MNavigationRail.test.ts +33 -0
  118. package/src/__tests__/MOverlay.test.ts +49 -0
  119. package/src/__tests__/MProgressBar.test.ts +41 -0
  120. package/src/__tests__/MPullToRefresh.test.ts +33 -0
  121. package/src/__tests__/MRichTextEditor.test.ts +22 -0
  122. package/src/__tests__/MSearchField.test.ts +71 -0
  123. package/src/__tests__/MSkeleton.test.ts +38 -0
  124. package/src/__tests__/MSlider.test.ts +161 -0
  125. package/src/__tests__/MTimeline.test.ts +238 -0
  126. package/src/__tests__/MToolbar.test.ts +157 -0
  127. package/src/__tests__/_fakeMaplibre.ts +3 -1
  128. package/src/__tests__/attrsFallthrough.test.ts +9 -0
  129. package/src/__tests__/escapeStack.test.ts +93 -0
  130. package/src/__tests__/hydration.test.ts +25 -0
  131. package/src/__tests__/ssr.test.ts +21 -2
  132. package/src/__tests__/useAutoGrow.test.ts +103 -0
  133. package/src/__tests__/useScrollReveal.test.ts +102 -0
  134. package/src/__tests__/useThemeTokens.test.ts +114 -0
  135. package/src/__tests__/vClickOutside.test.ts +82 -0
  136. package/src/__tests__/vFocusRing.test.ts +36 -0
  137. package/src/__tests__/vLongPress.test.ts +81 -0
  138. package/src/__tests__/vScrollElevation.test.ts +89 -0
  139. package/src/__tests__/vSkeleton.test.ts +48 -0
  140. package/src/__tests__/vStateLayer.test.ts +53 -0
  141. package/src/__tests__/vSwipe.test.ts +93 -0
  142. package/src/__tests__/vTooltip.test.ts +95 -0
  143. package/src/__tests__/viewportHints.test.ts +41 -0
  144. package/src/components/MAnimatedNumber.vue +32 -6
  145. package/src/components/MAutocomplete.vue +5 -0
  146. package/src/components/MBottomSheet.vue +4 -1
  147. package/src/components/MButton.vue +19 -3
  148. package/src/components/MButtonGroup.vue +22 -17
  149. package/src/components/MCarousel.vue +7 -1
  150. package/src/components/MChart.vue +74 -34
  151. package/src/components/MCircleProgressBar.vue +14 -9
  152. package/src/components/MCommandPalette.vue +3 -2
  153. package/src/components/MContainerTransform.vue +321 -0
  154. package/src/components/MContextMenu.vue +19 -60
  155. package/src/components/MDialog.vue +31 -6
  156. package/src/components/MDragDropList.vue +2 -0
  157. package/src/components/MFab.vue +10 -5
  158. package/src/components/MIconButton.vue +43 -2
  159. package/src/components/MKanban.vue +2 -0
  160. package/src/components/MLightbox.vue +7 -2
  161. package/src/components/MList.vue +26 -2
  162. package/src/components/MListItem.vue +138 -84
  163. package/src/components/MMap.vue +38 -5
  164. package/src/components/MMapBaseLayers.vue +7 -3
  165. package/src/components/MMapFilter.vue +7 -4
  166. package/src/components/MMapGeoJson.vue +1 -1
  167. package/src/components/MMapLayers.vue +6 -2
  168. package/src/components/MMapMeasure.vue +5 -2
  169. package/src/components/MMenu.vue +18 -59
  170. package/src/components/MMenuItem.vue +11 -2
  171. package/src/components/MMultiAutocomplete.vue +5 -0
  172. package/src/components/MNavigationDrawer.vue +96 -11
  173. package/src/components/MNavigationRail.vue +44 -25
  174. package/src/components/MOverlay.vue +32 -13
  175. package/src/components/MProgressBar.vue +25 -27
  176. package/src/components/MPullToRefresh.vue +20 -1
  177. package/src/components/MRichTextEditor.vue +19 -5
  178. package/src/components/MSearchBar.vue +5 -0
  179. package/src/components/MSearchField.vue +142 -0
  180. package/src/components/MSegmentedButton.vue +19 -17
  181. package/src/components/MSelect.vue +10 -1
  182. package/src/components/MSideSheet.vue +5 -2
  183. package/src/components/MSimpleCarousel.vue +10 -1
  184. package/src/components/MSkeleton.vue +30 -8
  185. package/src/components/MSlider.vue +161 -42
  186. package/src/components/MSpotlightSearch.vue +3 -2
  187. package/src/components/MTextField.vue +15 -2
  188. package/src/components/MTimeline.vue +370 -81
  189. package/src/components/MToolbar.vue +256 -15
  190. package/src/components/MTooltip.vue +20 -152
  191. package/src/components/_MDrawerItemList.vue +25 -6
  192. package/src/components/_MToolbarFab.vue +14 -0
  193. package/src/composables/_containerTransform.ts +179 -0
  194. package/src/composables/_mapStyle.ts +45 -0
  195. package/src/composables/_themeTransition.ts +2 -0
  196. package/src/composables/_tooltipTrigger.ts +184 -0
  197. package/src/composables/_useEscapeStack.ts +2 -0
  198. package/src/composables/_useMenuNav.ts +209 -0
  199. package/src/composables/useAutoGrow.ts +78 -0
  200. package/src/composables/useContainerTransform.ts +59 -0
  201. package/src/composables/useLocale.ts +10 -0
  202. package/src/composables/useMap.ts +43 -5
  203. package/src/composables/useMapFilter.ts +6 -5
  204. package/src/composables/useMapLayers.ts +7 -6
  205. package/src/composables/useScrollReveal.ts +151 -0
  206. package/src/composables/useThemeTokens.ts +116 -0
  207. package/src/composables/viewportHints.ts +80 -0
  208. package/src/directives/vClickOutside.ts +90 -0
  209. package/src/directives/vFocusRing.ts +60 -0
  210. package/src/directives/vLongPress.ts +134 -0
  211. package/src/directives/vReveal.ts +57 -0
  212. package/src/directives/vScrollElevation.ts +116 -0
  213. package/src/directives/vSkeleton.ts +87 -0
  214. package/src/directives/vStateLayer.ts +83 -0
  215. package/src/directives/vSwipe.ts +197 -0
  216. package/src/directives/vTooltip.ts +128 -0
  217. package/src/index.ts +31 -1
  218. package/src/locales/de.ts +4 -0
  219. package/src/locales/es.ts +4 -0
  220. package/src/locales/fr.ts +4 -0
  221. package/src/locales/ja.ts +4 -0
  222. package/src/locales/ko.ts +4 -0
  223. package/src/locales/pt.ts +4 -0
  224. package/src/locales/zh.ts +4 -0
  225. package/src/map.ts +1 -1
  226. package/src/plugin.ts +34 -0
  227. package/src/styles/theme.css +63 -0
  228. package/dist/MMenuItem-BckcR0nW.js +0 -809
  229. package/dist/MMenuItem-BckcR0nW.js.map +0 -1
  230. package/dist/MSlider-LyH_gy8D.js +0 -1411
  231. package/dist/MSlider-LyH_gy8D.js.map +0 -1
  232. package/dist/MTextField-FCPOTB0o.js +0 -672
  233. package/dist/MTextField-FCPOTB0o.js.map +0 -1
  234. package/dist/composables/_useThemeTokens.d.ts +0 -7
  235. package/dist/useLocale-SfQHnJfp.js.map +0 -1
  236. package/src/composables/_useThemeTokens.ts +0 -42
@@ -0,0 +1,90 @@
1
+ import type { Directive } from 'vue'
2
+
3
+ /** Options for `v-click-outside`. */
4
+ export interface ClickOutsideOptions {
5
+ /** Called with the event that landed outside the element. */
6
+ handler?: (event: PointerEvent | MouseEvent) => void
7
+ /**
8
+ * Elements that count as inside too: elements or CSS selectors (the button that toggles a panel, or a panel's
9
+ * own popup teleported to `<body>`).
10
+ */
11
+ exclude?: Array<HTMLElement | string | null | undefined>
12
+ /** `'pointerdown'` (default) reacts as the press starts, like menus; `'click'` once it ends, so a drag out doesn't count. */
13
+ event?: 'pointerdown' | 'click'
14
+ /** Off while true. */
15
+ disabled?: boolean
16
+ }
17
+
18
+ /** `v-click-outside` value: the handler, or an options object. */
19
+ export type ClickOutsideValue = ((event: PointerEvent | MouseEvent) => void) | ClickOutsideOptions | null | undefined
20
+
21
+ function normalize(value: ClickOutsideValue): ClickOutsideOptions {
22
+ if (value == null) return {}
23
+ return typeof value === 'function' ? { handler: value } : value
24
+ }
25
+
26
+ interface ClickOutsideState {
27
+ options: ClickOutsideOptions
28
+ listening: 'pointerdown' | 'click' | null
29
+ armTimer: ReturnType<typeof setTimeout> | undefined
30
+ onEvent: (e: Event) => void
31
+ }
32
+
33
+ const states = new WeakMap<HTMLElement, ClickOutsideState>()
34
+
35
+ function isInside(el: HTMLElement, options: ClickOutsideOptions, e: Event) {
36
+ // composedPath: the target may be gone by now (a click that removed it), and it sees into shadow roots
37
+ const path = e.composedPath()
38
+ if (path.includes(el)) return true
39
+ return (options.exclude ?? []).some((x) => {
40
+ if (!x) return false
41
+ if (typeof x !== 'string') return path.includes(x)
42
+ return path.some((node) => node instanceof Element && node.matches(x))
43
+ })
44
+ }
45
+
46
+ function listen(state: ClickOutsideState) {
47
+ const want = state.options.disabled || !state.options.handler ? null : state.options.event ?? 'pointerdown'
48
+ if (want === state.listening) return
49
+ if (state.listening) document.removeEventListener(state.listening, state.onEvent, true)
50
+ clearTimeout(state.armTimer)
51
+ state.listening = want
52
+ // Armed after the current event: the press or click that showed the element mustn't count as outside it
53
+ if (want) state.armTimer = setTimeout(() => document.addEventListener(want, state.onEvent, true))
54
+ }
55
+
56
+ /**
57
+ * Calls a handler when a press lands outside the element: `v-click-outside="close"` or
58
+ * `v-click-outside="{ handler, exclude, event, disabled }"`. `exclude` takes elements or selectors that count
59
+ * as inside (the toggle button, a teleported popup). Only the primary button counts. Starts listening after the
60
+ * event that mounted it, so the click that opened a panel doesn't close it. Registered globally as
61
+ * `v-click-outside` by `createM3UI()`; import `vClickOutside` to use it without the plugin.
62
+ */
63
+ export const vClickOutside: Directive<HTMLElement, ClickOutsideValue> = {
64
+ mounted(el, binding) {
65
+ const state: ClickOutsideState = {
66
+ options: normalize(binding.value),
67
+ listening: null,
68
+ armTimer: undefined,
69
+ onEvent(e) {
70
+ if ((e as MouseEvent).button !== 0 || isInside(el, state.options, e)) return
71
+ state.options.handler?.(e as PointerEvent | MouseEvent)
72
+ },
73
+ }
74
+ states.set(el, state)
75
+ listen(state)
76
+ },
77
+ updated(el, binding) {
78
+ const state = states.get(el)
79
+ if (!state) return
80
+ state.options = normalize(binding.value)
81
+ listen(state)
82
+ },
83
+ beforeUnmount(el) {
84
+ const state = states.get(el)
85
+ if (!state) return
86
+ clearTimeout(state.armTimer)
87
+ if (state.listening) document.removeEventListener(state.listening, state.onEvent, true)
88
+ states.delete(el)
89
+ },
90
+ }
@@ -0,0 +1,60 @@
1
+ import type { Directive } from 'vue'
2
+
3
+ /** Options for `v-focus-ring`. */
4
+ export interface FocusRingOptions {
5
+ /** No ring while true. */
6
+ disabled?: boolean
7
+ /** Any CSS color or token for the ring; defaults to `var(--color-secondary)`, as M3's focus indicator. */
8
+ color?: string
9
+ /** Draws the ring inside the element's edge instead of 2px outside it, for elements clipped by a parent. */
10
+ inset?: boolean
11
+ /** Also shows while an element inside it has keyboard focus (a card holding a link or a field). */
12
+ within?: boolean
13
+ }
14
+
15
+ /** `v-focus-ring` value: nothing or `true` (on), `false` (off), a CSS color, or an options object. */
16
+ export type FocusRingValue = boolean | string | FocusRingOptions | null | undefined
17
+
18
+ function normalize(value: FocusRingValue): FocusRingOptions {
19
+ if (value === false) return { disabled: true }
20
+ if (typeof value === 'string') return { color: value }
21
+ if (value === true || value == null) return {}
22
+ return value
23
+ }
24
+
25
+ // The ring itself is in theme.css (`[data-m3-focus-ring]`), drawn as an outline so it follows the element's
26
+ // corners and takes no layout; this only keeps the attribute and the color/offset variables in step
27
+ function apply(el: HTMLElement, options: FocusRingOptions) {
28
+ el.dataset.m3FocusRing = options.disabled ? 'off' : options.within ? 'within' : ''
29
+ if (options.color) el.style.setProperty('--m3-focus-ring-color', options.color)
30
+ else el.style.removeProperty('--m3-focus-ring-color')
31
+ if (options.inset) el.style.setProperty('--m3-focus-ring-offset', '-3px')
32
+ else el.style.removeProperty('--m3-focus-ring-offset')
33
+ }
34
+
35
+ /**
36
+ * The M3 focus indicator on any element: a 3dp ring in `secondary`, 2dp outside the element, shown on keyboard
37
+ * focus only (`:focus-visible`), never on a click or tap. `<div v-focus-ring tabindex="0">`, `v-focus-ring="false"`,
38
+ * `v-focus-ring="'#e91e63'"` or `v-focus-ring="{ disabled, color, inset, within }"`. It wins over an `outline-none`
39
+ * class on the same element. Registered globally as `v-focus-ring` by `createM3UI()`; import `vFocusRing` to use
40
+ * it without the plugin.
41
+ */
42
+ export const vFocusRing: Directive<HTMLElement, FocusRingValue> = {
43
+ // The marker only: an inline style here would make a hydration mismatch on an element with its own :style (the
44
+ // color and offset join on the client)
45
+ getSSRProps(binding) {
46
+ const o = normalize(binding.value)
47
+ return { 'data-m3-focus-ring': o.disabled ? 'off' : o.within ? 'within' : '' }
48
+ },
49
+ mounted(el, binding) {
50
+ apply(el, normalize(binding.value))
51
+ },
52
+ updated(el, binding) {
53
+ apply(el, normalize(binding.value))
54
+ },
55
+ beforeUnmount(el) {
56
+ delete el.dataset.m3FocusRing
57
+ el.style.removeProperty('--m3-focus-ring-color')
58
+ el.style.removeProperty('--m3-focus-ring-offset')
59
+ },
60
+ }
@@ -0,0 +1,134 @@
1
+ import type { Directive } from 'vue'
2
+
3
+ /** Options for `v-long-press`. */
4
+ export interface LongPressOptions {
5
+ /** Called once the press has been held for `duration`, with the pointerdown that started it. */
6
+ handler?: (event: PointerEvent) => void
7
+ /** How long to hold, in ms (default 500, as MTooltip's long press). */
8
+ duration?: number
9
+ /** How far (px) the pointer may move before it's a scroll or a drag, not a press (default 10). */
10
+ tolerance?: number
11
+ /** A short vibration when it fires, where the device supports it (default true). */
12
+ haptic?: boolean
13
+ /** Also with a mouse (default true); false leaves the mouse alone, for touch and pen only. */
14
+ mouse?: boolean
15
+ /** Off while true. */
16
+ disabled?: boolean
17
+ }
18
+
19
+ /** `v-long-press` value: the handler, or an options object. */
20
+ export type LongPressValue = ((event: PointerEvent) => void) | LongPressOptions | null | undefined
21
+
22
+ function normalize(value: LongPressValue): LongPressOptions {
23
+ if (value == null) return {}
24
+ return typeof value === 'function' ? { handler: value } : value
25
+ }
26
+
27
+ interface LongPressState {
28
+ options: LongPressOptions
29
+ press: { id: number, x: number, y: number, timer: ReturnType<typeof setTimeout> } | null
30
+ fired: boolean
31
+ onPointerDown: (e: PointerEvent) => void
32
+ onMove: (e: PointerEvent) => void
33
+ onEnd: (e: PointerEvent) => void
34
+ onClick: (e: MouseEvent) => void
35
+ onContextMenu: (e: Event) => void
36
+ }
37
+
38
+ const states = new WeakMap<HTMLElement, LongPressState>()
39
+
40
+ function endPress(el: HTMLElement, state: LongPressState) {
41
+ if (state.press) clearTimeout(state.press.timer)
42
+ state.press = null
43
+ delete el.dataset.longPressing
44
+ window.removeEventListener('pointermove', state.onMove)
45
+ window.removeEventListener('pointerup', state.onEnd)
46
+ window.removeEventListener('pointercancel', state.onEnd)
47
+ }
48
+
49
+ /**
50
+ * Calls a handler when the element is pressed and held: `v-long-press="onHold"` or
51
+ * `v-long-press="{ handler, duration, tolerance, haptic, mouse, disabled }"`. Moving past the tolerance (a scroll
52
+ * or a drag) cancels it; once it fires, the click the press ends in and the browser's own long-press menu are
53
+ * swallowed, so a long-pressed button doesn't also click. While held, the element has `data-long-pressing`
54
+ * (`data-long-pressing:scale-95`). Registered globally as `v-long-press` by `createM3UI()`; import `vLongPress`
55
+ * to use it without the plugin.
56
+ */
57
+ export const vLongPress: Directive<HTMLElement, LongPressValue> = {
58
+ mounted(el, binding) {
59
+ const state: LongPressState = {
60
+ options: normalize(binding.value),
61
+ press: null,
62
+ fired: false,
63
+ onPointerDown(e) {
64
+ const { disabled, handler, mouse = true, duration = 500, haptic = true } = state.options
65
+ if (disabled || !handler || e.button !== 0 || (!mouse && e.pointerType === 'mouse')) return
66
+ endPress(el, state)
67
+ state.fired = false
68
+ el.dataset.longPressing = ''
69
+ state.press = {
70
+ id: e.pointerId,
71
+ x: e.clientX,
72
+ y: e.clientY,
73
+ timer: setTimeout(() => {
74
+ state.fired = true
75
+ endPress(el, state)
76
+ // The click comes right after the finger lifts, if at all (not over a non-clickable element): stop
77
+ // waiting for it a moment later, or a later keyboard click would be swallowed
78
+ const release = () => {
79
+ window.removeEventListener('pointerup', release)
80
+ window.removeEventListener('pointercancel', release)
81
+ setTimeout(() => { state.fired = false }, 400)
82
+ }
83
+ window.addEventListener('pointerup', release)
84
+ window.addEventListener('pointercancel', release)
85
+ if (haptic) navigator.vibrate?.(10)
86
+ state.options.handler?.(e)
87
+ }, duration),
88
+ }
89
+ window.addEventListener('pointermove', state.onMove)
90
+ window.addEventListener('pointerup', state.onEnd)
91
+ window.addEventListener('pointercancel', state.onEnd)
92
+ },
93
+ onMove(e) {
94
+ const p = state.press
95
+ if (p && e.pointerId === p.id && Math.hypot(e.clientX - p.x, e.clientY - p.y) > (state.options.tolerance ?? 10)) endPress(el, state)
96
+ },
97
+ onEnd(e) {
98
+ if (state.press && e.pointerId === state.press.id) endPress(el, state)
99
+ },
100
+ // The click a fired press ends in belongs to it
101
+ onClick(e) {
102
+ if (!state.fired) return
103
+ state.fired = false
104
+ e.preventDefault()
105
+ e.stopPropagation()
106
+ },
107
+ onContextMenu(e) {
108
+ if (state.press || state.fired) e.preventDefault()
109
+ },
110
+ }
111
+ states.set(el, state)
112
+ // No iOS callout (copy / share) over the press
113
+ el.style.setProperty('-webkit-touch-callout', 'none')
114
+ el.addEventListener('pointerdown', state.onPointerDown)
115
+ el.addEventListener('click', state.onClick, true)
116
+ el.addEventListener('contextmenu', state.onContextMenu)
117
+ },
118
+ updated(el, binding) {
119
+ const state = states.get(el)
120
+ if (!state) return
121
+ state.options = normalize(binding.value)
122
+ if (state.options.disabled) endPress(el, state)
123
+ },
124
+ beforeUnmount(el) {
125
+ const state = states.get(el)
126
+ if (!state) return
127
+ endPress(el, state)
128
+ el.removeEventListener('pointerdown', state.onPointerDown)
129
+ el.removeEventListener('click', state.onClick, true)
130
+ el.removeEventListener('contextmenu', state.onContextMenu)
131
+ el.style.removeProperty('-webkit-touch-callout')
132
+ states.delete(el)
133
+ },
134
+ }
@@ -0,0 +1,57 @@
1
+ import type { Directive } from 'vue'
2
+ import { createReveal, type RevealEffect, type ScrollRevealOptions } from '../composables/useScrollReveal'
3
+
4
+ /** `v-reveal` value: nothing or `true` (fade up), `false` (off), an effect name, or options. */
5
+ export type RevealValue = boolean | RevealEffect | ScrollRevealOptions | null | undefined
6
+
7
+ function normalize(value: RevealValue): ScrollRevealOptions | null {
8
+ if (value === false) return null
9
+ if (typeof value === 'string') return { effect: value }
10
+ if (value === true || value == null) return {}
11
+ return value
12
+ }
13
+
14
+ // Elements with the same options share one observer, so a list of `v-reveal` items cascades in together
15
+ const engines = new Map<string, ReturnType<typeof createReveal>>()
16
+ function engineFor(options: ScrollRevealOptions) {
17
+ const key = JSON.stringify(options)
18
+ let engine = engines.get(key)
19
+ if (!engine) engines.set(key, (engine = createReveal(options)))
20
+ return engine
21
+ }
22
+
23
+ const state = new WeakMap<Element, ReturnType<typeof createReveal>>()
24
+
25
+ function attach(el: Element, value: RevealValue) {
26
+ const options = normalize(value)
27
+ if (!options) return
28
+ const engine = engineFor(options)
29
+ state.set(el, engine)
30
+ engine.observe(el)
31
+ }
32
+ function detach(el: Element) {
33
+ state.get(el)?.unobserve(el)
34
+ state.delete(el)
35
+ }
36
+
37
+ /**
38
+ * Reveals the element as it scrolls into view, on M3's springs (see `useScrollReveal` for the options):
39
+ * `v-reveal`, `v-reveal="'scale'"`, `v-reveal="{ effect: 'slide-left', delay: 100 }"`. Items of a `v-for` with
40
+ * the same options cascade in. Off with reduced motion. Registered by `createM3UI()`, or import `vReveal`.
41
+ */
42
+ export const vReveal: Directive<HTMLElement, RevealValue> = {
43
+ mounted(el, { value }) {
44
+ attach(el, value)
45
+ },
46
+ updated(el, { value, oldValue }) {
47
+ // Only a change of options re-arms it (an inline object literal is a new object on every render)
48
+ if (JSON.stringify(normalize(value)) === JSON.stringify(normalize(oldValue))) return
49
+ detach(el)
50
+ attach(el, value)
51
+ },
52
+ beforeUnmount(el) {
53
+ detach(el)
54
+ },
55
+ // Rendered on the server as is: it hides only once mounted in the browser
56
+ getSSRProps: () => ({}),
57
+ }
@@ -0,0 +1,116 @@
1
+ import type { Directive } from 'vue'
2
+
3
+ /** Options for `v-scroll-elevation`. */
4
+ export interface ScrollElevationOptions {
5
+ /**
6
+ * What scrolls: an element, a CSS selector for it, or the window (when left out). A template ref that's still
7
+ * empty (`target: list` before the list mounts) waits for it instead of following the window.
8
+ */
9
+ target?: HTMLElement | Window | string | null
10
+ /** How far (px) the content has to scroll before the element counts as scrolled (default 0: any scroll). */
11
+ threshold?: number
12
+ /**
13
+ * The background while scrolled, any CSS color or token; default `var(--color-surface-container)`, M3's
14
+ * scrolled container color for a top app bar. `false` changes no color: style `[data-scrolled]` yourself.
15
+ */
16
+ color?: string | false
17
+ /** Called when the scrolled state changes. */
18
+ onChange?: (scrolled: boolean) => void
19
+ /** Stops following the scroll while true (and clears the scrolled state). */
20
+ disabled?: boolean
21
+ }
22
+
23
+ /** `v-scroll-elevation` value: nothing or `true` (the window), `false` (off), a selector, or an options object. */
24
+ export type ScrollElevationValue = boolean | string | ScrollElevationOptions | null | undefined
25
+
26
+ function normalize(value: ScrollElevationValue): ScrollElevationOptions {
27
+ if (value === false) return { disabled: true }
28
+ if (typeof value === 'string') return { target: value }
29
+ if (value === true || value == null) return {}
30
+ return value
31
+ }
32
+
33
+ const DEFAULT_COLOR = 'var(--color-surface-container)'
34
+
35
+ interface ScrollElevationState {
36
+ options: ScrollElevationOptions
37
+ target: HTMLElement | Window | null
38
+ scrolled: boolean
39
+ onScroll: () => void
40
+ }
41
+
42
+ const states = new WeakMap<HTMLElement, ScrollElevationState>()
43
+
44
+ function resolve(options: ScrollElevationOptions): HTMLElement | Window | null {
45
+ if (!('target' in options)) return typeof window === 'undefined' ? null : window
46
+ const { target } = options
47
+ if (target == null) return null
48
+ if (typeof target === 'string') return document.querySelector<HTMLElement>(target)
49
+ return target
50
+ }
51
+
52
+ function scrollTop(target: HTMLElement | Window) {
53
+ return target instanceof Window ? target.scrollY : target.scrollTop
54
+ }
55
+
56
+ function setScrolled(el: HTMLElement, state: ScrollElevationState, scrolled: boolean) {
57
+ const { color = DEFAULT_COLOR } = state.options
58
+ // Inline, so it wins over the element's own bg-* class while scrolled and gives it back after
59
+ el.style.backgroundColor = scrolled && color !== false ? color : ''
60
+ if (scrolled) el.dataset.scrolled = ''
61
+ else delete el.dataset.scrolled
62
+ if (scrolled !== state.scrolled) {
63
+ state.scrolled = scrolled
64
+ state.options.onChange?.(scrolled)
65
+ }
66
+ }
67
+
68
+ function attach(el: HTMLElement, state: ScrollElevationState) {
69
+ const target = state.options.disabled ? null : resolve(state.options)
70
+ if (target === state.target) return
71
+ state.target?.removeEventListener('scroll', state.onScroll)
72
+ state.target = target
73
+ if (target) {
74
+ target.addEventListener('scroll', state.onScroll, { passive: true })
75
+ state.onScroll()
76
+ }
77
+ else setScrolled(el, state, false)
78
+ }
79
+
80
+ /**
81
+ * Marks an element while the content under it has scrolled, as M3's top app bar does on scroll: it takes the
82
+ * scrolled container color (`surface-container`) and gets a `data-scrolled` attribute for your own styles
83
+ * (`data-scrolled:shadow-elevation-2`). Follows the window, or `v-scroll-elevation="'#list'"` /
84
+ * `{ target, threshold, color, onChange, disabled }`. Add `transition-colors` to the element for a fade.
85
+ * Registered globally as `v-scroll-elevation` by `createM3UI()`; import `vScrollElevation` to use it without the
86
+ * plugin.
87
+ */
88
+ export const vScrollElevation: Directive<HTMLElement, ScrollElevationValue> = {
89
+ mounted(el, binding) {
90
+ const state: ScrollElevationState = {
91
+ options: normalize(binding.value),
92
+ target: null,
93
+ scrolled: false,
94
+ onScroll: () => {
95
+ if (state.target) setScrolled(el, state, scrollTop(state.target) > (state.options.threshold ?? 0))
96
+ },
97
+ }
98
+ states.set(el, state)
99
+ attach(el, state)
100
+ },
101
+ updated(el, binding) {
102
+ const state = states.get(el)
103
+ if (!state) return
104
+ state.options = normalize(binding.value)
105
+ // A target that changed, or a selector / ref that only resolves now
106
+ attach(el, state)
107
+ if (state.target) state.onScroll()
108
+ },
109
+ beforeUnmount(el) {
110
+ const state = states.get(el)
111
+ state?.target?.removeEventListener('scroll', state.onScroll)
112
+ el.style.backgroundColor = ''
113
+ delete el.dataset.scrolled
114
+ states.delete(el)
115
+ },
116
+ }
@@ -0,0 +1,87 @@
1
+ import type { Directive } from 'vue'
2
+
3
+ /** Options for `v-skeleton`. */
4
+ export interface SkeletonOptions {
5
+ /** Shows the placeholder while true. */
6
+ loading?: boolean
7
+ /** MSkeleton's animations (default `'pulse'`). */
8
+ animation?: 'pulse' | 'wave' | 'none'
9
+ }
10
+
11
+ /** `v-skeleton` value: whether it's loading, or an options object. */
12
+ export type SkeletonValue = boolean | SkeletonOptions | null | undefined
13
+
14
+ function normalize(value: SkeletonValue): Required<SkeletonOptions> {
15
+ if (value == null || typeof value === 'boolean') return { loading: !!value, animation: 'pulse' }
16
+ return { loading: !!value.loading, animation: value.animation ?? 'pulse' }
17
+ }
18
+
19
+ interface SkeletonState {
20
+ shimmer: HTMLSpanElement | null
21
+ madeRelative: boolean
22
+ }
23
+
24
+ const states = new WeakMap<HTMLElement, SkeletonState>()
25
+
26
+ // The look is in theme.css (`[data-m3-skeleton]`): the element keeps its size and corners, its content turns
27
+ // invisible (but still lays out), and its background becomes MSkeleton's tint
28
+ function apply(el: HTMLElement, state: SkeletonState, value: SkeletonValue) {
29
+ const { loading, animation } = normalize(value)
30
+ if (!loading) return clear(el, state)
31
+ el.dataset.m3Skeleton = animation
32
+ el.setAttribute('aria-busy', 'true')
33
+ if (animation === 'wave') {
34
+ const { position } = getComputedStyle(el)
35
+ if (!state.madeRelative && (!position || position === 'static')) {
36
+ el.style.position = 'relative'
37
+ state.madeRelative = true
38
+ }
39
+ if (!state.shimmer) {
40
+ state.shimmer = document.createElement('span')
41
+ state.shimmer.className = 'm3-skeleton-shimmer'
42
+ state.shimmer.setAttribute('aria-hidden', 'true')
43
+ }
44
+ if (state.shimmer.parentNode !== el) el.appendChild(state.shimmer)
45
+ }
46
+ else state.shimmer?.remove()
47
+ }
48
+
49
+ function clear(el: HTMLElement, state: SkeletonState) {
50
+ delete el.dataset.m3Skeleton
51
+ el.removeAttribute('aria-busy')
52
+ state.shimmer?.remove()
53
+ if (state.madeRelative) {
54
+ el.style.position = ''
55
+ state.madeRelative = false
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Turns an element into its own loading placeholder: `v-skeleton="loading"` or
61
+ * `v-skeleton="{ loading, animation }"`. While loading it keeps its size and corners, hides its content (which
62
+ * still sizes it: placeholder text like `{{ user?.name ?? 'Loading name' }}` gives an empty element a width),
63
+ * takes MSkeleton's tint and animation (`pulse`, `wave` or `none`; still with reduced motion), ignores the pointer
64
+ * and is `aria-busy`. Registered globally as `v-skeleton` by `createM3UI()`; import `vSkeleton` to use it
65
+ * without the plugin.
66
+ */
67
+ export const vSkeleton: Directive<HTMLElement, SkeletonValue> = {
68
+ // Server-rendered as a placeholder already (the wave's shimmer layer joins on the client)
69
+ getSSRProps(binding) {
70
+ const { loading, animation } = normalize(binding.value)
71
+ return loading ? { 'data-m3-skeleton': animation, 'aria-busy': 'true' } : {}
72
+ },
73
+ mounted(el, binding) {
74
+ const state: SkeletonState = { shimmer: null, madeRelative: false }
75
+ states.set(el, state)
76
+ apply(el, state, binding.value)
77
+ },
78
+ updated(el, binding) {
79
+ const state = states.get(el)
80
+ if (state) apply(el, state, binding.value)
81
+ },
82
+ beforeUnmount(el) {
83
+ const state = states.get(el)
84
+ if (state) clear(el, state)
85
+ states.delete(el)
86
+ },
87
+ }
@@ -0,0 +1,83 @@
1
+ import type { Directive } from 'vue'
2
+
3
+ /** Options for `v-state-layer`. */
4
+ export interface StateLayerOptions {
5
+ /** No state layer while true (e.g. bound to the element's disabled state). `disabled` / `aria-disabled="true"` hosts get none either way. */
6
+ disabled?: boolean
7
+ /** Any CSS color for the layer; defaults to the element's text color (`currentColor`). */
8
+ color?: string
9
+ /** Holds the dragged state (16%) while true, e.g. while the element is being dragged. */
10
+ dragged?: boolean
11
+ }
12
+
13
+ /** `v-state-layer` value: nothing or `true` (on), `false` (off), a CSS color, or an options object. */
14
+ export type StateLayerValue = boolean | string | StateLayerOptions | null | undefined
15
+
16
+ function normalize(value: StateLayerValue): StateLayerOptions {
17
+ if (value === false) return { disabled: true }
18
+ if (typeof value === 'string') return { color: value }
19
+ if (value === true || value == null) return {}
20
+ return value
21
+ }
22
+
23
+ interface StateLayerState {
24
+ layer: HTMLSpanElement
25
+ ensurePositioned: () => void
26
+ }
27
+
28
+ const states = new WeakMap<HTMLElement, StateLayerState>()
29
+
30
+ // The opacities (hover 8%, focus 10%, pressed 10%, dragged 16%) live in theme.css, keyed on the host's
31
+ // `data-m3-state-layer`; this only keeps the layer, its color and that attribute in step with the value.
32
+ function apply(el: HTMLElement, layer: HTMLElement, options: StateLayerOptions) {
33
+ el.dataset.m3StateLayer = options.disabled ? 'off' : options.dragged ? 'dragged' : ''
34
+ layer.style.background = options.color ?? ''
35
+ }
36
+
37
+ /**
38
+ * The M3 state layer for any element: a tint in its text color at 8% on hover, 10% on keyboard focus and press,
39
+ * 16% while dragged. `<div v-state-layer>`, `v-state-layer="false"`, `v-state-layer="'#e91e63'"` or
40
+ * `v-state-layer="{ disabled, color, dragged }"`. Pairs with `v-ripple`. A statically positioned element is made
41
+ * `position: relative`. Registered globally as `v-state-layer` by `createM3UI()`; import `vStateLayer` to use it
42
+ * without the plugin.
43
+ */
44
+ export const vStateLayer: Directive<HTMLElement, StateLayerValue> = {
45
+ // The marker only (the layer itself is added on the client)
46
+ getSSRProps(binding) {
47
+ const o = normalize(binding.value)
48
+ return { 'data-m3-state-layer': o.disabled ? 'off' : o.dragged ? 'dragged' : '' }
49
+ },
50
+ mounted(el, binding) {
51
+ const layer = document.createElement('span')
52
+ layer.className = 'm3-state-layer'
53
+ layer.setAttribute('aria-hidden', 'true')
54
+ el.appendChild(layer)
55
+ const ensurePositioned = () => {
56
+ const { position } = getComputedStyle(el)
57
+ if (!position || position === 'static') el.style.position = 'relative'
58
+ }
59
+ // On mount, and again on the first hover or focus in case the element wasn't in the document yet
60
+ ensurePositioned()
61
+ el.addEventListener('pointerenter', ensurePositioned, { once: true })
62
+ el.addEventListener('focusin', ensurePositioned, { once: true })
63
+ states.set(el, { layer, ensurePositioned })
64
+ apply(el, layer, normalize(binding.value))
65
+ },
66
+ updated(el, binding) {
67
+ const state = states.get(el)
68
+ if (!state) return
69
+ // A re-render that replaced the host's children can drop the layer: put it back
70
+ if (state.layer.parentNode !== el) el.appendChild(state.layer)
71
+ apply(el, state.layer, normalize(binding.value))
72
+ },
73
+ beforeUnmount(el) {
74
+ const state = states.get(el)
75
+ if (state) {
76
+ el.removeEventListener('pointerenter', state.ensurePositioned)
77
+ el.removeEventListener('focusin', state.ensurePositioned)
78
+ state.layer.remove()
79
+ }
80
+ delete el.dataset.m3StateLayer
81
+ states.delete(el)
82
+ },
83
+ }