@m3ui-vue/m3ui-vue 1.1.4 → 1.2.1
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.
- package/dist/API.md +511 -34
- package/dist/MMenuItem-DI351kIf.js +929 -0
- package/dist/MMenuItem-DI351kIf.js.map +1 -0
- package/dist/{MSlider-JflHJ70q.js → MSlider-BnEy5ieZ.js} +295 -320
- package/dist/MSlider-BnEy5ieZ.js.map +1 -0
- package/dist/MTextField-BBmkaIbO.js +633 -0
- package/dist/MTextField-BBmkaIbO.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/chart.js +83 -49
- package/dist/chart.js.map +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MButton.vue.d.ts +7 -1
- package/dist/components/MButtonGroup.vue.d.ts +40 -31
- package/dist/components/MCarousel.vue.d.ts +2 -0
- package/dist/components/MChart.vue.d.ts +6 -0
- package/dist/components/MCircleProgressBar.vue.d.ts +5 -3
- package/dist/components/MContextMenu.vue.d.ts +2 -2
- package/dist/components/MCopyButton.vue.d.ts +1 -1
- package/dist/components/MExportButton.vue.d.ts +1 -1
- package/dist/components/MFab.vue.d.ts +2 -2
- package/dist/components/MFabMenu.vue.d.ts +1 -1
- package/dist/components/MIconButton.vue.d.ts +13 -1
- package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
- package/dist/components/MMap.vue.d.ts +20 -2
- package/dist/components/MMapBaseLayers.vue.d.ts +3 -0
- package/dist/components/MMapFilter.vue.d.ts +3 -0
- package/dist/components/MMapLayers.vue.d.ts +3 -0
- package/dist/components/MMenu.vue.d.ts +2 -2
- package/dist/components/MMenuItem.vue.d.ts +7 -3
- package/dist/components/MNavigationDrawer.vue.d.ts +13 -2
- package/dist/components/MOverlay.vue.d.ts +12 -1
- package/dist/components/MProgressBar.vue.d.ts +5 -3
- package/dist/components/MPullToRefresh.vue.d.ts +7 -2
- package/dist/components/MRichTextEditor.vue.d.ts +7 -1
- package/dist/components/MSearchField.vue.d.ts +58 -0
- package/dist/components/MSegmentedButton.vue.d.ts +31 -23
- package/dist/components/MSimpleCarousel.vue.d.ts +2 -0
- package/dist/components/MSkeleton.vue.d.ts +6 -3
- package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
- package/dist/components/MSwitch.vue.d.ts +1 -1
- package/dist/components/MTextField.vue.d.ts +5 -1
- package/dist/components/MTimeline.vue.d.ts +85 -22
- package/dist/components/MTooltip.vue.d.ts +1 -1
- package/dist/components/_MDrawerItemList.vue.d.ts +2 -0
- package/dist/composables/_mapStyle.d.ts +13 -0
- package/dist/composables/_themeTransition.d.ts +2 -0
- package/dist/composables/_tooltipTrigger.d.ts +22 -0
- package/dist/composables/_useMenuNav.d.ts +28 -0
- package/dist/composables/useAutoGrow.d.ts +21 -0
- package/dist/composables/useColorPalette.d.ts +1 -1
- package/dist/composables/useLocale.d.ts +3 -0
- package/dist/composables/useMap.d.ts +19 -3
- package/dist/composables/useMapFilter.d.ts +4 -1
- package/dist/composables/useMapLayers.d.ts +5 -2
- package/dist/composables/useScrollReveal.d.ts +44 -0
- package/dist/composables/useThemeTokens.d.ts +21 -0
- package/dist/composables/viewportHints.d.ts +27 -0
- package/dist/directives/vClickOutside.d.ts +25 -0
- package/dist/directives/vFocusRing.d.ts +22 -0
- package/dist/directives/vLongPress.d.ts +27 -0
- package/dist/directives/vReveal.d.ts +10 -0
- package/dist/directives/vScrollElevation.d.ts +31 -0
- package/dist/directives/vSkeleton.d.ts +19 -0
- package/dist/directives/vStateLayer.d.ts +20 -0
- package/dist/directives/vSwipe.d.ts +45 -0
- package/dist/directives/vTooltip.d.ts +23 -0
- package/dist/index.d.ts +28 -1
- package/dist/locales/index.js +21 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +7132 -6217
- package/dist/m3ui.js.map +1 -1
- package/dist/map.d.ts +1 -1
- package/dist/map.js +821 -791
- package/dist/map.js.map +1 -1
- package/dist/motion-DZiXG09o.js +150 -0
- package/dist/motion-DZiXG09o.js.map +1 -0
- package/dist/plugin.d.ts +24 -0
- package/dist/rich-text-editor.js +13 -9
- package/dist/rich-text-editor.js.map +1 -1
- package/dist/styles/theme.css +63 -0
- package/dist/styles.css +1 -1
- package/dist/{useLocale-CXibICQA.js → useLocale-JYxDDeXC.js} +4 -2
- package/dist/useLocale-JYxDDeXC.js.map +1 -0
- package/dist/useThemeTokens-Bv-Uv8eX.js +135 -0
- package/dist/useThemeTokens-Bv-Uv8eX.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MButton.test.ts +22 -0
- package/src/__tests__/MCarousel.test.ts +9 -0
- package/src/__tests__/MChart.test.ts +77 -5
- package/src/__tests__/MIconButton.test.ts +33 -0
- package/src/__tests__/MMap.test.ts +53 -1
- package/src/__tests__/MMapLayers.test.ts +46 -2
- package/src/__tests__/MMenu.test.ts +104 -1
- package/src/__tests__/MMenuItem.test.ts +18 -0
- package/src/__tests__/MNavigationDrawer.test.ts +80 -0
- package/src/__tests__/MOverlay.test.ts +49 -0
- package/src/__tests__/MProgressBar.test.ts +41 -0
- package/src/__tests__/MPullToRefresh.test.ts +33 -0
- package/src/__tests__/MRichTextEditor.test.ts +22 -0
- package/src/__tests__/MSearchField.test.ts +71 -0
- package/src/__tests__/MSegmentedButton.test.ts +10 -0
- package/src/__tests__/MSkeleton.test.ts +38 -0
- package/src/__tests__/MTimeline.test.ts +238 -0
- package/src/__tests__/_fakeMaplibre.ts +3 -1
- package/src/__tests__/attrsFallthrough.test.ts +3 -0
- package/src/__tests__/escapeStack.test.ts +93 -0
- package/src/__tests__/ssr.test.ts +21 -2
- package/src/__tests__/useAutoGrow.test.ts +103 -0
- package/src/__tests__/useScrollReveal.test.ts +102 -0
- package/src/__tests__/useThemeTokens.test.ts +114 -0
- package/src/__tests__/vClickOutside.test.ts +82 -0
- package/src/__tests__/vFocusRing.test.ts +36 -0
- package/src/__tests__/vLongPress.test.ts +81 -0
- package/src/__tests__/vScrollElevation.test.ts +89 -0
- package/src/__tests__/vSkeleton.test.ts +48 -0
- package/src/__tests__/vStateLayer.test.ts +53 -0
- package/src/__tests__/vSwipe.test.ts +93 -0
- package/src/__tests__/vTooltip.test.ts +95 -0
- package/src/__tests__/viewportHints.test.ts +41 -0
- package/src/components/MAutocomplete.vue +5 -0
- package/src/components/MBottomSheet.vue +4 -1
- package/src/components/MButton.vue +15 -1
- package/src/components/MButtonGroup.vue +22 -17
- package/src/components/MCarousel.vue +7 -1
- package/src/components/MChart.vue +74 -34
- package/src/components/MCircleProgressBar.vue +14 -9
- package/src/components/MCommandPalette.vue +3 -2
- package/src/components/MContextMenu.vue +19 -60
- package/src/components/MDragDropList.vue +2 -0
- package/src/components/MIconButton.vue +40 -1
- package/src/components/MKanban.vue +2 -0
- package/src/components/MLightbox.vue +7 -2
- package/src/components/MMap.vue +38 -5
- package/src/components/MMapBaseLayers.vue +7 -3
- package/src/components/MMapFilter.vue +7 -4
- package/src/components/MMapGeoJson.vue +1 -1
- package/src/components/MMapLayers.vue +6 -2
- package/src/components/MMapMeasure.vue +5 -2
- package/src/components/MMenu.vue +18 -59
- package/src/components/MMenuItem.vue +11 -2
- package/src/components/MMultiAutocomplete.vue +5 -0
- package/src/components/MNavigationDrawer.vue +96 -11
- package/src/components/MNavigationRail.vue +4 -15
- package/src/components/MOverlay.vue +32 -13
- package/src/components/MProgressBar.vue +25 -27
- package/src/components/MPullToRefresh.vue +20 -1
- package/src/components/MRichTextEditor.vue +19 -5
- package/src/components/MSearchBar.vue +5 -0
- package/src/components/MSearchField.vue +142 -0
- package/src/components/MSegmentedButton.vue +20 -18
- package/src/components/MSelect.vue +10 -1
- package/src/components/MSideSheet.vue +5 -2
- package/src/components/MSimpleCarousel.vue +10 -1
- package/src/components/MSkeleton.vue +30 -8
- package/src/components/MSpotlightSearch.vue +3 -2
- package/src/components/MTextField.vue +15 -2
- package/src/components/MTimeline.vue +370 -81
- package/src/components/MTooltip.vue +20 -152
- package/src/components/_MDrawerItemList.vue +25 -6
- package/src/composables/_mapStyle.ts +45 -0
- package/src/composables/_themeTransition.ts +2 -0
- package/src/composables/_tooltipTrigger.ts +184 -0
- package/src/composables/_useMenuNav.ts +209 -0
- package/src/composables/useAutoGrow.ts +78 -0
- package/src/composables/useLocale.ts +5 -0
- package/src/composables/useMap.ts +43 -5
- package/src/composables/useMapFilter.ts +6 -5
- package/src/composables/useMapLayers.ts +7 -6
- package/src/composables/useScrollReveal.ts +151 -0
- package/src/composables/useThemeTokens.ts +116 -0
- package/src/composables/viewportHints.ts +80 -0
- package/src/directives/vClickOutside.ts +90 -0
- package/src/directives/vFocusRing.ts +60 -0
- package/src/directives/vLongPress.ts +134 -0
- package/src/directives/vReveal.ts +57 -0
- package/src/directives/vScrollElevation.ts +116 -0
- package/src/directives/vSkeleton.ts +87 -0
- package/src/directives/vStateLayer.ts +83 -0
- package/src/directives/vSwipe.ts +197 -0
- package/src/directives/vTooltip.ts +128 -0
- package/src/index.ts +28 -1
- package/src/locales/de.ts +2 -0
- package/src/locales/es.ts +2 -0
- package/src/locales/fr.ts +2 -0
- package/src/locales/ja.ts +2 -0
- package/src/locales/ko.ts +2 -0
- package/src/locales/pt.ts +2 -0
- package/src/locales/zh.ts +2 -0
- package/src/map.ts +1 -1
- package/src/plugin.ts +34 -0
- package/src/styles/theme.css +63 -0
- package/dist/MMenuItem-BfjqwcYP.js +0 -830
- package/dist/MMenuItem-BfjqwcYP.js.map +0 -1
- package/dist/MSlider-JflHJ70q.js.map +0 -1
- package/dist/MTextField-NaIB6RbM.js +0 -673
- package/dist/MTextField-NaIB6RbM.js.map +0 -1
- package/dist/composables/_useThemeTokens.d.ts +0 -7
- package/dist/useLocale-CXibICQA.js.map +0 -1
- package/src/composables/_useThemeTokens.ts +0 -42
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { onBeforeUnmount, onMounted, ref, type Ref } from 'vue'
|
|
2
|
+
import { STYLE_ID } from './_storageKeys'
|
|
3
|
+
import { THEME_TRANSITION_MS } from './_themeTransition'
|
|
4
|
+
import { cubicBezier } from './motion'
|
|
5
|
+
|
|
6
|
+
export interface ThemeTokensOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Updates every animation frame while the theme switch's colors fade, so a canvas redrawn on change fades with
|
|
9
|
+
* the page instead of jumping to the new colors. Off by default: what animates color changes itself (Chart.js,
|
|
10
|
+
* MChart, maps) only needs the final values, and would otherwise redraw ~15 times per switch.
|
|
11
|
+
*/
|
|
12
|
+
live?: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// The CSS `ease` the switch uses (_themeTransition.ts, and `.theme-transitioning` in theme.css)
|
|
16
|
+
const ease = cubicBezier(0.25, 0.1, 0.25, 1)
|
|
17
|
+
|
|
18
|
+
type Rgba = [number, number, number, number]
|
|
19
|
+
|
|
20
|
+
// Hex (#rgb, #rgba, #rrggbb, #rrggbbaa) or rgb()/rgba(), the forms tokens are written in or computed to; null for
|
|
21
|
+
// anything else (that token then switches at once)
|
|
22
|
+
function parseColor(value: string): Rgba | null {
|
|
23
|
+
const v = value.trim()
|
|
24
|
+
const hex = /^#([0-9a-f]{3,8})$/i.exec(v)?.[1]
|
|
25
|
+
if (hex && hex.length !== 5 && hex.length !== 7) {
|
|
26
|
+
const full = hex.length <= 4 ? [...hex].map((c) => c + c).join('') : hex
|
|
27
|
+
const n = (i: number) => parseInt(full.slice(i, i + 2), 16)
|
|
28
|
+
return [n(0), n(2), n(4), full.length === 8 ? n(6) / 255 : 1]
|
|
29
|
+
}
|
|
30
|
+
const fn = /^rgba?\(([^)]+)\)$/i.exec(v)?.[1]
|
|
31
|
+
if (!fn) return null
|
|
32
|
+
const parts = fn.split(/[\s,/]+/).filter(Boolean)
|
|
33
|
+
if (parts.length < 3) return null
|
|
34
|
+
const channel = (p: string) => (p.endsWith('%') ? parseFloat(p) * 2.55 : parseFloat(p))
|
|
35
|
+
const alpha = parts[3] == null ? 1 : parts[3].endsWith('%') ? parseFloat(parts[3]) / 100 : parseFloat(parts[3])
|
|
36
|
+
const rgba = [channel(parts[0]!), channel(parts[1]!), channel(parts[2]!), alpha] as Rgba
|
|
37
|
+
return rgba.every(Number.isFinite) ? rgba : null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function mix(from: string, to: string, t: number): string {
|
|
41
|
+
const a = parseColor(from)
|
|
42
|
+
const b = parseColor(to)
|
|
43
|
+
if (!a || !b) return t < 1 ? from : to
|
|
44
|
+
const [r, g, bl, al] = a.map((x, i) => x + (b[i]! - x) * t)
|
|
45
|
+
return `rgba(${Math.round(r!)}, ${Math.round(g!)}, ${Math.round(bl!)}, ${+al!.toFixed(3)})`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The current values of some color tokens (`['primary', 'surface']` → `--color-primary`…), kept up to date across
|
|
50
|
+
* light/dark, palette, contrast and generated (seed) palette changes: for what can't use CSS variables, like a
|
|
51
|
+
* Chart.js dataset, a canvas or a MapLibre layer's paint. Values settle after the theme switch's color transition;
|
|
52
|
+
* with `{ live: true }` they also follow it frame by frame (as `rgba()` midway). Empty strings on the server.
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* const colors = useThemeTokens(['primary', 'tertiary', 'outline-variant'] as const)
|
|
56
|
+
* const data = computed(() => ({ datasets: [{ data: sales, borderColor: colors.value.primary }] }))
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function useThemeTokens<T extends string>(names: readonly T[], options: ThemeTokensOptions = {}): Ref<Record<T, string>> {
|
|
60
|
+
const read = () => {
|
|
61
|
+
const style = getComputedStyle(document.documentElement)
|
|
62
|
+
return Object.fromEntries(names.map((n) => [n, style.getPropertyValue(`--color-${n}`).trim()])) as Record<T, string>
|
|
63
|
+
}
|
|
64
|
+
const tokens = ref(typeof document === 'undefined' ? Object.fromEntries(names.map((n) => [n, ''])) as Record<T, string> : read()) as Ref<Record<T, string>>
|
|
65
|
+
let observer: MutationObserver | undefined
|
|
66
|
+
let settle: ReturnType<typeof setTimeout> | undefined
|
|
67
|
+
let frame = 0
|
|
68
|
+
|
|
69
|
+
const set = (next: Record<T, string>) => {
|
|
70
|
+
if (names.some((n) => next[n] !== tokens.value[n])) tokens.value = next
|
|
71
|
+
}
|
|
72
|
+
const update = () => set(read())
|
|
73
|
+
|
|
74
|
+
// Follows a switch already under way. Chromium/WebKit animate the tokens themselves (`theme-switching`), so
|
|
75
|
+
// reading them each frame gives the page's own colors; Firefox fades each element's colors instead
|
|
76
|
+
// (`theme-transitioning`) while the tokens jump, so the same `ease` is replayed here from the old values
|
|
77
|
+
function follow(root: HTMLElement) {
|
|
78
|
+
cancelAnimationFrame(frame)
|
|
79
|
+
const native = root.classList.contains('theme-switching')
|
|
80
|
+
const from = { ...tokens.value }
|
|
81
|
+
const to = native ? null : read()
|
|
82
|
+
const start = performance.now()
|
|
83
|
+
const step = (now: number) => {
|
|
84
|
+
const t = Math.min(1, (now - start) / THEME_TRANSITION_MS)
|
|
85
|
+
// The last frame lands on the values as written, not their rgba() form
|
|
86
|
+
if (to) set(t < 1 ? Object.fromEntries(names.map((n) => [n, mix(from[n], to[n], ease(t))])) as Record<T, string> : to)
|
|
87
|
+
else update()
|
|
88
|
+
if (t < 1) frame = requestAnimationFrame(step)
|
|
89
|
+
}
|
|
90
|
+
frame = requestAnimationFrame(step)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
onMounted(() => {
|
|
94
|
+
const root = document.documentElement
|
|
95
|
+
// Dark mode (class), a palette or contrast (attributes), or a generated palette rewriting its <style> in <head>
|
|
96
|
+
const isPaletteStyle = (node: Node | null) => (node instanceof Element ? node : node?.parentElement)?.id === STYLE_ID
|
|
97
|
+
observer = new MutationObserver((records) => {
|
|
98
|
+
if (!records.some((r) => r.target === root || isPaletteStyle(r.target)
|
|
99
|
+
|| Array.from(r.addedNodes).some(isPaletteStyle))) return
|
|
100
|
+
if (options.live && (root.classList.contains('theme-switching') || root.classList.contains('theme-transitioning'))) follow(root)
|
|
101
|
+
else update()
|
|
102
|
+
// Chromium animates the tokens on a theme switch (_themeTransition, 250ms): read the settled values too
|
|
103
|
+
clearTimeout(settle)
|
|
104
|
+
settle = setTimeout(update, THEME_TRANSITION_MS + 50)
|
|
105
|
+
})
|
|
106
|
+
observer.observe(root, { attributes: true, attributeFilter: ['class', 'style', 'data-palette', 'data-contrast'] })
|
|
107
|
+
observer.observe(document.head, { childList: true, characterData: true, subtree: true })
|
|
108
|
+
})
|
|
109
|
+
onBeforeUnmount(() => {
|
|
110
|
+
observer?.disconnect()
|
|
111
|
+
clearTimeout(settle)
|
|
112
|
+
cancelAnimationFrame(frame)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
return tokens
|
|
116
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { VIEWPORT_KEY } from './_storageKeys'
|
|
2
|
+
|
|
3
|
+
/** A request's headers as server frameworks hand them over: a `Headers`, or a plain object (Node's, h3's). */
|
|
4
|
+
export type RequestHeaders = Headers | Record<string, string | string[] | undefined>
|
|
5
|
+
|
|
6
|
+
/** A window size, for `createM3UI({ initialState: { viewport } })` */
|
|
7
|
+
export interface ViewportSize {
|
|
8
|
+
width: number
|
|
9
|
+
height: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The client hints that carry the window's size (Chromium), for a server's `Accept-CH` and `Critical-CH` response
|
|
14
|
+
* headers. With `Critical-CH`, Chromium repeats the very first request once to send them; later requests carry them
|
|
15
|
+
* after `Accept-CH` alone.
|
|
16
|
+
*/
|
|
17
|
+
export const VIEWPORT_HINTS = 'Sec-CH-Viewport-Width, Sec-CH-Viewport-Height'
|
|
18
|
+
|
|
19
|
+
// Sizes standing for a kind of device when that's all a request tells: a phone (compact), a tablet held upright
|
|
20
|
+
// (medium), a laptop (large)
|
|
21
|
+
const GUESSES = {
|
|
22
|
+
phone: { width: 412, height: 915 },
|
|
23
|
+
tablet: { width: 820, height: 1180 },
|
|
24
|
+
desktop: { width: 1280, height: 800 },
|
|
25
|
+
} as const
|
|
26
|
+
|
|
27
|
+
function header(headers: RequestHeaders, name: string): string | undefined {
|
|
28
|
+
if (typeof Headers !== 'undefined' && headers instanceof Headers) return headers.get(name) ?? undefined
|
|
29
|
+
const record = headers as Record<string, string | string[] | undefined>
|
|
30
|
+
const value = record[name] ?? record[Object.keys(record).find((k) => k.toLowerCase() === name) ?? '']
|
|
31
|
+
return Array.isArray(value) ? value[0] : value
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const size = (n: number) => Number.isFinite(n) && n > 0 && n < 20000
|
|
35
|
+
|
|
36
|
+
function fromCookie(cookies: string | undefined): ViewportSize | undefined {
|
|
37
|
+
const raw = cookies?.match(new RegExp(`(?:^|;\\s*)${VIEWPORT_KEY}=([^;]*)`))?.[1]
|
|
38
|
+
if (!raw) return undefined
|
|
39
|
+
let value = raw
|
|
40
|
+
try { value = decodeURIComponent(raw) } catch { /* as it is */ }
|
|
41
|
+
const [width, height] = value.split('x').map(Number)
|
|
42
|
+
return size(width!) && size(height!) ? { width: width!, height: height! } : undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The kind of device a request comes from: Chromium says whether it's mobile; otherwise the user agent */
|
|
46
|
+
function guess(headers: RequestHeaders): ViewportSize | undefined {
|
|
47
|
+
const ua = header(headers, 'user-agent') ?? ''
|
|
48
|
+
const mobileHint = header(headers, 'sec-ch-ua-mobile')
|
|
49
|
+
// Chromium on an Android tablet says ?0 and leaves "Mobile" out of its user agent, so the hint goes first only for
|
|
50
|
+
// phones
|
|
51
|
+
if (mobileHint === '?1') return { ...GUESSES.phone }
|
|
52
|
+
if (/iPad|Tablet|PlayBook|Silk|Kindle|Android(?!.*Mobile)/i.test(ua)) return { ...GUESSES.tablet }
|
|
53
|
+
if (mobileHint === '?0') return { ...GUESSES.desktop }
|
|
54
|
+
if (/Mobi|iPhone|iPod|Android|BlackBerry|Opera Mini|IEMobile/i.test(ua)) return { ...GUESSES.phone }
|
|
55
|
+
if (ua) return { ...GUESSES.desktop }
|
|
56
|
+
return undefined
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The window size to server-render the adaptive layouts at, from a request's headers: the size `createM3UI({
|
|
61
|
+
* cookies: true })` keeps in the `m3-viewport` cookie (any visit after the first); the viewport client hints
|
|
62
|
+
* (Chromium, once the server asked for them with `VIEWPORT_HINTS`); or, on a first visit, a size standing for the
|
|
63
|
+
* kind of device (`Sec-CH-UA-Mobile`, then the user agent: a phone, a tablet or a desktop), unless `guess: false`.
|
|
64
|
+
* Undefined when nothing tells: the layouts then render compact.
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* createM3UI({ cookies: true, initialState: { viewport: viewportFromHeaders(request.headers) } })
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function viewportFromHeaders(headers: RequestHeaders, options: { guess?: boolean } = {}): ViewportSize | undefined {
|
|
71
|
+
const cookie = fromCookie(header(headers, 'cookie'))
|
|
72
|
+
if (cookie) return cookie
|
|
73
|
+
const width = Number(header(headers, 'sec-ch-viewport-width'))
|
|
74
|
+
if (size(width)) {
|
|
75
|
+
const height = Number(header(headers, 'sec-ch-viewport-height'))
|
|
76
|
+
// Older Chromium sends only the width
|
|
77
|
+
return { width, height: size(height) ? height : Math.round(width * (width < 600 ? 2.2 : 0.625)) }
|
|
78
|
+
}
|
|
79
|
+
return options.guess === false ? undefined : guess(headers)
|
|
80
|
+
}
|
|
@@ -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
|
+
}
|