@m3ui-vue/m3ui-vue 0.9.3 → 1.0.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/README.md +24 -29
- package/dist/API.md +179 -19
- package/dist/{MTextField-DKtgPymd.js → MTextField-BPbFeI9M.js} +305 -177
- package/dist/MTextField-BPbFeI9M.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MAppBarWithSearch.vue.d.ts +70 -0
- package/dist/components/MBottomAppBar.vue.d.ts +37 -0
- package/dist/components/MBottomSheetScaffold.vue.d.ts +40 -0
- package/dist/components/MChatBubble.vue.d.ts +1 -1
- package/dist/components/MChip.vue.d.ts +19 -2
- package/dist/components/MContainer.vue.d.ts +1 -1
- package/dist/components/MDataTable.vue.d.ts +1 -1
- package/dist/components/MDialog.vue.d.ts +1 -1
- package/dist/components/MFab.vue.d.ts +13 -4
- package/dist/components/MListItem.vue.d.ts +2 -2
- package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
- package/dist/components/MNavigationDrawer.vue.d.ts +9 -6
- package/dist/components/MNavigationRail.vue.d.ts +6 -0
- package/dist/components/MSearchBar.vue.d.ts +5 -3
- package/dist/components/MShortNavigationBar.vue.d.ts +1 -1
- package/dist/components/MTable.vue.d.ts +1 -1
- package/dist/components/MTopAppBar.vue.d.ts +33 -2
- package/dist/components/MTransition.vue.d.ts +0 -9
- package/dist/composables/_searchAppBar.d.ts +6 -0
- package/dist/composables/_springAnimation.d.ts +17 -0
- package/dist/composables/_themeTransition.d.ts +2 -0
- package/dist/composables/_useSpring.d.ts +3 -5
- package/dist/composables/motion.d.ts +101 -0
- package/dist/composables/useColorPalette.d.ts +10 -0
- package/dist/composables/useLocale.d.ts +3 -0
- package/dist/index.d.ts +8 -1
- package/dist/locales/index.js +21 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +7345 -6317
- package/dist/m3ui.js.map +1 -1
- package/dist/plugin.d.ts +7 -0
- package/dist/rich-text-editor.js +2 -2
- package/dist/styles/palettes-contrast.css +7764 -0
- package/dist/styles/theme.css +46 -2
- package/dist/styles.css +1 -1
- package/dist/{useLocale-Cn9MVIdw.js → useLocale-CuvWRZ1h.js} +4 -1
- package/dist/useLocale-CuvWRZ1h.js.map +1 -0
- package/package.json +3 -1
- package/src/__tests__/MAppBarWithSearch.test.ts +108 -0
- package/src/__tests__/MBottomAppBar.test.ts +71 -0
- package/src/__tests__/MBottomSheetScaffold.test.ts +122 -0
- package/src/__tests__/MChip.test.ts +63 -0
- package/src/__tests__/MFab.test.ts +36 -0
- package/src/__tests__/MNavigationDrawer.test.ts +185 -0
- package/src/__tests__/MNavigationRail.test.ts +93 -0
- package/src/__tests__/MPullToRefresh.test.ts +18 -1
- package/src/__tests__/MTopAppBar.test.ts +99 -0
- package/src/__tests__/MTransition.test.ts +12 -0
- package/src/__tests__/contrast.test.ts +64 -0
- package/src/__tests__/dynamicColor.test.ts +17 -0
- package/src/__tests__/motion.test.ts +37 -0
- package/src/__tests__/sheets.test.ts +19 -15
- package/src/__tests__/themeTransition.test.ts +119 -0
- package/src/components/MAppBarWithSearch.vue +236 -0
- package/src/components/MBottomAppBar.vue +181 -0
- package/src/components/MBottomSheetScaffold.vue +317 -0
- package/src/components/MBreadcrumbs.vue +1 -1
- package/src/components/MChip.vue +137 -5
- package/src/components/MContextMenu.vue +34 -33
- package/src/components/MFab.vue +73 -12
- package/src/components/MMenu.vue +34 -33
- package/src/components/MNavigationDrawer.vue +298 -38
- package/src/components/MNavigationRail.vue +189 -8
- package/src/components/MPullToRefresh.vue +18 -0
- package/src/components/MSearchBar.vue +29 -12
- package/src/components/MTopAppBar.vue +252 -28
- package/src/components/MTransition.vue +36 -41
- package/src/composables/_searchAppBar.ts +9 -0
- package/src/composables/_springAnimation.ts +74 -0
- package/src/composables/_themeColor.ts +11 -4
- package/src/composables/_themeTransition.ts +71 -0
- package/src/composables/_useSheetMotion.ts +101 -88
- package/src/composables/_useSpring.ts +11 -21
- package/src/composables/motion.ts +73 -0
- package/src/composables/useColorPalette.ts +103 -20
- package/src/composables/useLocale.ts +6 -0
- package/src/composables/useTheme.ts +4 -12
- package/src/index.ts +8 -1
- package/src/locales/de.ts +3 -0
- package/src/locales/es.ts +3 -0
- package/src/locales/fr.ts +3 -0
- package/src/locales/ja.ts +3 -0
- package/src/locales/ko.ts +3 -0
- package/src/locales/pt.ts +3 -0
- package/src/locales/zh.ts +3 -0
- package/src/plugin.ts +9 -1
- package/src/styles/palettes-contrast.css +7764 -0
- package/src/styles/theme.css +46 -2
- package/dist/MTextField-DKtgPymd.js.map +0 -1
- package/dist/useLocale-Cn9MVIdw.js.map +0 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Internal: animates a theme change (dark mode, palette, contrast).
|
|
2
|
+
//
|
|
3
|
+
// Firefox (and any browser without @property) transitions six color properties on every element
|
|
4
|
+
// (`.theme-transitioning` in theme.css): smooth there, but in Chromium it recalculates every element's transitions
|
|
5
|
+
// each frame and stutters. Chromium and WebKit instead transition the color tokens themselves, once, on <html>:
|
|
6
|
+
// registered as <color> with @property they interpolate, and every element follows them through var(). Components'
|
|
7
|
+
// own color transitions (transition-colors) are turned off meanwhile, or they would restart on every frame of the
|
|
8
|
+
// token's change and trail behind the rest (the navbar and buttons did). A view transition was tried first: it
|
|
9
|
+
// cross-fades two snapshots, which looks washed out midway compared to the colors moving.
|
|
10
|
+
|
|
11
|
+
// The tokens of theme.css, palettes.css and generated palettes
|
|
12
|
+
const COLOR_TOKENS = [
|
|
13
|
+
'background', 'on-background', 'surface', 'surface-dim', 'surface-bright', 'surface-variant', 'on-surface',
|
|
14
|
+
'on-surface-variant', 'surface-container-lowest', 'surface-container-low', 'surface-container',
|
|
15
|
+
'surface-container-high', 'surface-container-highest', 'inverse-surface', 'inverse-on-surface', 'inverse-primary',
|
|
16
|
+
'outline', 'outline-variant', 'shadow', 'scrim',
|
|
17
|
+
...['primary', 'secondary', 'tertiary', 'error', 'success'].flatMap((c) => [c, `on-${c}`, `${c}-container`, `on-${c}-container`]),
|
|
18
|
+
...['primary', 'secondary', 'tertiary'].flatMap((c) => [`${c}-fixed`, `${c}-fixed-dim`, `on-${c}-fixed`, `on-${c}-fixed-variant`]),
|
|
19
|
+
].map((t) => `--color-${t}`)
|
|
20
|
+
|
|
21
|
+
const DURATION = 250
|
|
22
|
+
const STYLE_ID = 'm3-theme-transition'
|
|
23
|
+
|
|
24
|
+
// Injected only while switching: a registered <color> computes to rgb(), and code reading a token with
|
|
25
|
+
// getPropertyValue() expects it as written
|
|
26
|
+
const css = COLOR_TOKENS.map((t) => `@property ${t} { syntax: '<color>'; inherits: true; initial-value: transparent; }`).join('\n')
|
|
27
|
+
+ `\nhtml.theme-switching { transition: ${COLOR_TOKENS.map((t) => `${t} ${DURATION}ms ease`).join(', ')}; }`
|
|
28
|
+
+ '\nhtml.theme-switching *, html.theme-switching *::before, html.theme-switching *::after { transition: none !important; }'
|
|
29
|
+
|
|
30
|
+
// Gecko: a CSS feature only it has, or its user agent ("Gecko/<version>" or "Firefox/"; Chromium and WebKit only
|
|
31
|
+
// say "like Gecko", and Firefox on iOS is WebKit and says "FxiOS")
|
|
32
|
+
const isGecko = () =>
|
|
33
|
+
(typeof CSS !== 'undefined' && !!CSS.supports?.('-moz-appearance', 'none'))
|
|
34
|
+
|| (typeof navigator !== 'undefined' && /\bGecko\/\d|\bFirefox\//.test(navigator.userAgent))
|
|
35
|
+
const hasProperty = () => typeof CSS !== 'undefined' && typeof CSS.registerProperty === 'function'
|
|
36
|
+
|
|
37
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
38
|
+
|
|
39
|
+
/** Runs `apply` (the DOM writes of a theme change) with the theme transition. */
|
|
40
|
+
export function themeTransition(apply: () => void) {
|
|
41
|
+
if (typeof document === 'undefined') return apply()
|
|
42
|
+
const reduced = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
|
43
|
+
if (reduced) return apply()
|
|
44
|
+
const root = document.documentElement
|
|
45
|
+
|
|
46
|
+
if (!isGecko() && hasProperty()) {
|
|
47
|
+
if (!document.getElementById(STYLE_ID)) {
|
|
48
|
+
const style = document.createElement('style')
|
|
49
|
+
style.id = STYLE_ID
|
|
50
|
+
style.textContent = css
|
|
51
|
+
document.head.appendChild(style)
|
|
52
|
+
}
|
|
53
|
+
root.classList.add('theme-switching')
|
|
54
|
+
// Style flush: the tokens' current values are computed as registered colors and the transition is in place
|
|
55
|
+
// before they change
|
|
56
|
+
void root.offsetHeight
|
|
57
|
+
apply()
|
|
58
|
+
clearTimeout(timer)
|
|
59
|
+
timer = setTimeout(() => {
|
|
60
|
+
root.classList.remove('theme-switching')
|
|
61
|
+
document.getElementById(STYLE_ID)?.remove()
|
|
62
|
+
}, DURATION + 50)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
root.classList.add('theme-transitioning')
|
|
67
|
+
// Force a style flush so the transition is in place before the colors change
|
|
68
|
+
void root.offsetHeight
|
|
69
|
+
apply()
|
|
70
|
+
setTimeout(() => root.classList.remove('theme-transitioning'), 300)
|
|
71
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { onBeforeUnmount } from 'vue'
|
|
2
|
+
import { motionSprings, type SpringSpec } from './motion'
|
|
3
|
+
import { sampleSpring, sampleAt, playSamples, prefersReducedMotion } from './_springAnimation'
|
|
2
4
|
|
|
3
5
|
/*
|
|
4
6
|
* Internal (not exported from index.ts): enter/exit/drag motion for modal sheets
|
|
@@ -8,99 +10,114 @@ import { onBeforeUnmount } from 'vue'
|
|
|
8
10
|
* (1.0 / 1600). The panel slides only, no opacity fade. A drag release springs from where
|
|
9
11
|
* the finger left it, carrying its velocity, either back open or out.
|
|
10
12
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
13
|
+
* The springs play as Web Animations (see _springAnimation.ts), on the compositor, so the
|
|
14
|
+
* enter isn't janked by the sheet's content mounting at the same moment; only the drag writes
|
|
15
|
+
* the transform directly. The offset is a fraction of the panel's size, so entering never reads
|
|
16
|
+
* layout. The panel's transform must never also be bound reactively in the template, which is
|
|
17
|
+
* what silently killed the old CSS enter animation.
|
|
14
18
|
*
|
|
15
19
|
* Wire it to a `<Transition :css="false" @enter @leave>` around a root holding
|
|
16
20
|
* `[data-sheet-panel]` and `[data-sheet-scrim]`.
|
|
17
21
|
*/
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const DEFAULT_EFFECTS: Spring = { damping: 1, stiffness: 1600 }
|
|
23
|
+
const DEFAULT_SPATIAL = motionSprings.standard.defaultSpatial
|
|
24
|
+
const FAST_EFFECTS = motionSprings.standard.fastEffects
|
|
25
|
+
const DEFAULT_EFFECTS = motionSprings.standard.defaultEffects
|
|
23
26
|
|
|
24
27
|
/** Past this many px, or this fast (px/s) toward closing, a released drag dismisses. */
|
|
25
28
|
const DISMISS_DISTANCE = 100
|
|
26
29
|
const DISMISS_VELOCITY = 1000
|
|
27
30
|
|
|
28
|
-
// Semi-implicit Euler in fixed 4ms substeps: FastEffects' stiffness (3800) is unstable at
|
|
29
|
-
// a dropped frame's 33ms step (it diverges instead of settling).
|
|
30
|
-
const SUBSTEP = 0.004
|
|
31
|
-
function step(s: { pos: number; vel: number }, target: number, spring: Spring, dt: number) {
|
|
32
|
-
for (let left = dt; left > 0; left -= SUBSTEP) {
|
|
33
|
-
const h = Math.min(SUBSTEP, left)
|
|
34
|
-
const accel = -spring.stiffness * (s.pos - target) - 2 * spring.damping * Math.sqrt(spring.stiffness) * s.vel
|
|
35
|
-
s.vel += accel * h
|
|
36
|
-
s.pos += s.vel * h
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
31
|
export function useSheetMotion(axis: 'x' | 'y', onDismiss: () => void) {
|
|
41
32
|
let panel: HTMLElement | null = null
|
|
42
33
|
let scrim: HTMLElement | null = null
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
let
|
|
34
|
+
// 0 = open, 1 = off-screen (a fraction of the panel's size); alpha is the scrim's opacity
|
|
35
|
+
let offset = 0
|
|
36
|
+
let alpha = 0
|
|
46
37
|
let alphaTarget = 0
|
|
47
|
-
let
|
|
38
|
+
let velocity = 0
|
|
39
|
+
let offsetAnim: Animation | null = null
|
|
40
|
+
let alphaAnim: Animation | null = null
|
|
41
|
+
let offsetSamples: number[] = []
|
|
42
|
+
let alphaSamples: number[] = []
|
|
48
43
|
let settled: (() => void) | null = null
|
|
49
|
-
let raf = 0
|
|
50
|
-
let last = 0
|
|
51
44
|
|
|
52
45
|
let dragging = false
|
|
53
46
|
let dragStart = 0
|
|
54
47
|
let dragOrigin = 0
|
|
48
|
+
let size = 1
|
|
55
49
|
let lastMove = { at: 0, pos: 0 }
|
|
50
|
+
let dragVelocity = 0
|
|
56
51
|
|
|
57
|
-
const
|
|
58
|
-
const reducedMotion = () =>
|
|
59
|
-
typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
|
52
|
+
const translate = (f: number) => (axis === 'y' ? `translateY(${f * 100}%)` : `translateX(${f * 100}%)`)
|
|
60
53
|
|
|
61
54
|
function write() {
|
|
62
|
-
if (panel) panel.style.transform =
|
|
63
|
-
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, alpha
|
|
55
|
+
if (panel) panel.style.transform = translate(offset)
|
|
56
|
+
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, alpha)))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function stopOffset() {
|
|
60
|
+
if (!offsetAnim) return
|
|
61
|
+
offset = sampleAt(offsetSamples, offsetAnim.currentTime)
|
|
62
|
+
if (panel) panel.style.transform = translate(offset)
|
|
63
|
+
offsetAnim.cancel()
|
|
64
|
+
offsetAnim = null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function stopAlpha() {
|
|
68
|
+
if (!alphaAnim) return
|
|
69
|
+
alpha = sampleAt(alphaSamples, alphaAnim.currentTime)
|
|
70
|
+
if (scrim) scrim.style.opacity = String(Math.min(1, Math.max(0, alpha)))
|
|
71
|
+
alphaAnim.cancel()
|
|
72
|
+
alphaAnim = null
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
function
|
|
67
|
-
|
|
75
|
+
function maybeSettle() {
|
|
76
|
+
if (offsetAnim || alphaAnim || dragging) return
|
|
68
77
|
const cb = settled
|
|
69
78
|
settled = null
|
|
70
79
|
cb?.()
|
|
71
80
|
}
|
|
72
81
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const offsetDone = dragging || (Math.abs(offset.pos - offsetTarget) < 0.5 && Math.abs(offset.vel) < 5)
|
|
79
|
-
const alphaDone = Math.abs(alpha.pos - alphaTarget) < 0.002 && Math.abs(alpha.vel) < 0.05
|
|
80
|
-
if (offsetDone && alphaDone) {
|
|
81
|
-
if (!dragging) offset.pos = offsetTarget
|
|
82
|
-
alpha.pos = alphaTarget
|
|
83
|
-
offset.vel = alpha.vel = 0
|
|
84
|
-
write()
|
|
85
|
-
if (!dragging) return finish()
|
|
86
|
-
raf = 0
|
|
87
|
-
return
|
|
82
|
+
// Without `done`, a pending Transition callback (an enter interrupted by a drag) is kept for this run
|
|
83
|
+
function animate(offsetTo: number, spring: SpringSpec, alphaTo: number, done?: () => void) {
|
|
84
|
+
if (done) {
|
|
85
|
+
settled?.()
|
|
86
|
+
settled = done
|
|
88
87
|
}
|
|
88
|
+
stopOffset()
|
|
89
|
+
const fromOffset = offset
|
|
90
|
+
const v = velocity
|
|
91
|
+
velocity = 0
|
|
92
|
+
// The scrim keeps its running fade when its target doesn't change (a drag released back open)
|
|
93
|
+
const restartAlpha = alphaTo !== alphaTarget || !alphaAnim
|
|
94
|
+
if (restartAlpha) stopAlpha()
|
|
95
|
+
const fromAlpha = alpha
|
|
96
|
+
alphaTarget = alphaTo
|
|
97
|
+
// End states inline underneath, so nothing jumps when the animations end
|
|
98
|
+
offset = offsetTo
|
|
99
|
+
if (restartAlpha) alpha = alphaTo
|
|
89
100
|
write()
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (reducedMotion()) {
|
|
95
|
-
offset.pos = offsetTarget
|
|
96
|
-
alpha.pos = alphaTarget
|
|
97
|
-
offset.vel = alpha.vel = 0
|
|
101
|
+
if (prefersReducedMotion()) {
|
|
102
|
+
alphaAnim?.cancel()
|
|
103
|
+
alphaAnim = null
|
|
104
|
+
alpha = alphaTo
|
|
98
105
|
write()
|
|
99
|
-
return
|
|
106
|
+
return maybeSettle()
|
|
107
|
+
}
|
|
108
|
+
if (fromOffset !== offsetTo) {
|
|
109
|
+
offsetSamples = sampleSpring(fromOffset, offsetTo, v, spring, 0.001)
|
|
110
|
+
const a = playSamples(panel, offsetSamples, (f) => ({ transform: translate(f) }))
|
|
111
|
+
offsetAnim = a
|
|
112
|
+
if (a) a.onfinish = () => { if (offsetAnim === a) { offsetAnim = null; maybeSettle() } }
|
|
113
|
+
}
|
|
114
|
+
if (restartAlpha && fromAlpha !== alphaTo) {
|
|
115
|
+
alphaSamples = sampleSpring(fromAlpha, alphaTo, 0, DEFAULT_EFFECTS, 0.002)
|
|
116
|
+
const a = playSamples(scrim, alphaSamples, (o) => ({ opacity: String(Math.min(1, Math.max(0, o))) }))
|
|
117
|
+
alphaAnim = a
|
|
118
|
+
if (a) a.onfinish = () => { if (alphaAnim === a) { alphaAnim = null; maybeSettle() } }
|
|
100
119
|
}
|
|
101
|
-
|
|
102
|
-
last = performance.now()
|
|
103
|
-
raf = requestAnimationFrame(frame)
|
|
120
|
+
maybeSettle()
|
|
104
121
|
}
|
|
105
122
|
|
|
106
123
|
function bind(el: Element) {
|
|
@@ -112,69 +129,65 @@ export function useSheetMotion(axis: 'x' | 'y', onDismiss: () => void) {
|
|
|
112
129
|
// Reopened mid-exit: let the old element go now instead of leaving it in the DOM.
|
|
113
130
|
settled?.()
|
|
114
131
|
settled = null
|
|
115
|
-
|
|
116
|
-
|
|
132
|
+
offsetAnim?.cancel()
|
|
133
|
+
alphaAnim?.cancel()
|
|
134
|
+
offsetAnim = alphaAnim = null
|
|
117
135
|
bind(el)
|
|
118
|
-
offset
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
offsetTarget = 0
|
|
123
|
-
alphaTarget = 1
|
|
124
|
-
offsetSpring = DEFAULT_SPATIAL
|
|
125
|
-
settled = done
|
|
136
|
+
offset = 1
|
|
137
|
+
alpha = 0
|
|
138
|
+
alphaTarget = 0
|
|
139
|
+
velocity = 0
|
|
126
140
|
write()
|
|
127
|
-
|
|
141
|
+
animate(0, DEFAULT_SPATIAL, 1, done)
|
|
128
142
|
}
|
|
129
143
|
|
|
130
144
|
function onLeave(el: Element, done: () => void) {
|
|
131
145
|
bind(el)
|
|
132
146
|
dragging = false
|
|
133
|
-
|
|
134
|
-
alphaTarget = 0
|
|
135
|
-
offsetSpring = FAST_EFFECTS
|
|
136
|
-
settled = done
|
|
137
|
-
run()
|
|
147
|
+
animate(1, FAST_EFFECTS, 0, done)
|
|
138
148
|
}
|
|
139
149
|
|
|
140
150
|
function onDragStart(e: PointerEvent) {
|
|
141
151
|
// A sheet mounted already open never ran onEnter: find its elements from the handle.
|
|
142
152
|
const root = (e.currentTarget as HTMLElement).closest('[data-sheet-panel]')?.parentElement
|
|
143
153
|
if (root) bind(root)
|
|
154
|
+
stopOffset()
|
|
144
155
|
dragging = true
|
|
156
|
+
size = (panel ? (axis === 'y' ? panel.offsetHeight : panel.offsetWidth) : 0) || 1
|
|
145
157
|
dragStart = axis === 'y' ? e.clientY : e.clientX
|
|
146
|
-
dragOrigin = offset
|
|
147
|
-
|
|
148
|
-
lastMove = { at: performance.now(), pos:
|
|
158
|
+
dragOrigin = offset * size
|
|
159
|
+
dragVelocity = 0
|
|
160
|
+
lastMove = { at: performance.now(), pos: dragOrigin }
|
|
149
161
|
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
|
|
150
162
|
}
|
|
151
163
|
|
|
152
164
|
function onDragMove(e: PointerEvent) {
|
|
153
165
|
if (!dragging) return
|
|
154
166
|
const now = performance.now()
|
|
155
|
-
|
|
167
|
+
const pos = Math.max(0, dragOrigin + (axis === 'y' ? e.clientY : e.clientX) - dragStart)
|
|
156
168
|
const dt = (now - lastMove.at) / 1000
|
|
157
|
-
if (dt > 0)
|
|
158
|
-
lastMove = { at: now, pos
|
|
159
|
-
|
|
169
|
+
if (dt > 0) dragVelocity = (pos - lastMove.pos) / dt
|
|
170
|
+
lastMove = { at: now, pos }
|
|
171
|
+
offset = pos / size
|
|
172
|
+
if (panel) panel.style.transform = translate(offset)
|
|
160
173
|
}
|
|
161
174
|
|
|
162
175
|
function onDragEnd() {
|
|
163
176
|
if (!dragging) return
|
|
164
177
|
dragging = false
|
|
165
178
|
// A pause before lifting the finger means no fling.
|
|
166
|
-
if (performance.now() - lastMove.at > 100)
|
|
167
|
-
|
|
179
|
+
if (performance.now() - lastMove.at > 100) dragVelocity = 0
|
|
180
|
+
velocity = dragVelocity / size
|
|
181
|
+
if (offset * size > DISMISS_DISTANCE || dragVelocity > DISMISS_VELOCITY) {
|
|
168
182
|
onDismiss()
|
|
169
183
|
} else {
|
|
170
|
-
|
|
171
|
-
offsetSpring = DEFAULT_SPATIAL
|
|
172
|
-
run()
|
|
184
|
+
animate(0, DEFAULT_SPATIAL, alphaTarget)
|
|
173
185
|
}
|
|
174
186
|
}
|
|
175
187
|
|
|
176
188
|
onBeforeUnmount(() => {
|
|
177
|
-
|
|
189
|
+
offsetAnim?.cancel()
|
|
190
|
+
alphaAnim?.cancel()
|
|
178
191
|
})
|
|
179
192
|
|
|
180
193
|
return { onEnter, onLeave, onDragStart, onDragMove, onDragEnd }
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// A set of numbers (corner radii, sizes...) that springs toward a target, written to the DOM once per
|
|
2
|
+
// frame through `apply`. Same integration as the rest of the library (semi-implicit Euler), in fixed 4ms
|
|
3
|
+
// substeps so a stiff spring stays stable on a dropped frame. `useSpring` is public (index.ts, with the
|
|
4
|
+
// motion tokens); the constants below are the library's internal shorthands for the Standard scheme.
|
|
4
5
|
import { onMounted, onUnmounted, watch } from 'vue'
|
|
6
|
+
import { cubicBezier, motionSprings, type SpringSpec } from './motion'
|
|
5
7
|
|
|
6
|
-
export
|
|
8
|
+
export type { SpringSpec }
|
|
7
9
|
|
|
8
10
|
/** MotionSchemeKeyTokens.FastSpatial in the Standard scheme (StandardMotionTokens.SpringFastSpatial*) */
|
|
9
|
-
export const FAST_SPATIAL: SpringSpec =
|
|
11
|
+
export const FAST_SPATIAL: SpringSpec = motionSprings.standard.fastSpatial
|
|
10
12
|
/** MotionSchemeKeyTokens.FastEffects in the Standard scheme */
|
|
11
|
-
export const FAST_EFFECTS: SpringSpec =
|
|
13
|
+
export const FAST_EFFECTS: SpringSpec = motionSprings.standard.fastEffects
|
|
12
14
|
/** MotionSchemeKeyTokens.DefaultEffects in the Standard scheme */
|
|
13
|
-
export const DEFAULT_EFFECTS: SpringSpec =
|
|
15
|
+
export const DEFAULT_EFFECTS: SpringSpec = motionSprings.standard.defaultEffects
|
|
14
16
|
/** MotionSchemeKeyTokens.DefaultSpatial in the Standard scheme */
|
|
15
|
-
export const DEFAULT_SPATIAL: SpringSpec =
|
|
17
|
+
export const DEFAULT_SPATIAL: SpringSpec = motionSprings.standard.defaultSpatial
|
|
16
18
|
|
|
17
19
|
const SUBSTEP = 0.004
|
|
18
20
|
|
|
@@ -76,16 +78,4 @@ export function useSpring(
|
|
|
76
78
|
}
|
|
77
79
|
|
|
78
80
|
/** Compose's FastOutSlowInEasing, cubic-bezier(0.4, 0, 0.2, 1) — the easing of a default tween() */
|
|
79
|
-
export
|
|
80
|
-
if (t <= 0) return 0
|
|
81
|
-
if (t >= 1) return 1
|
|
82
|
-
// Solve x(u) = t for the curve parameter u (Newton), then return y(u)
|
|
83
|
-
let u = t
|
|
84
|
-
for (let i = 0; i < 8; i++) {
|
|
85
|
-
const x = 3 * u * (1 - u) ** 2 * 0.4 + 3 * u ** 2 * (1 - u) * 0.2 + u ** 3 - t
|
|
86
|
-
const dx = 3 * (1 - u) ** 2 * 0.4 + 6 * u * (1 - u) * (0.2 - 0.4) + 3 * u ** 2 * 0.8
|
|
87
|
-
if (Math.abs(dx) < 1e-6) break
|
|
88
|
-
u -= x / dx
|
|
89
|
-
}
|
|
90
|
-
return 3 * u ** 2 * (1 - u) + u ** 3
|
|
91
|
-
}
|
|
81
|
+
export const fastOutSlowIn = cubicBezier(0.4, 0, 0.2, 1)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Material 3's motion tokens for your own animations, from androidx's MotionTokens, StandardMotionTokens and
|
|
2
|
+
// ExpressiveMotionTokens. The same easings and durations are CSS variables in theme.css (`--ease-*`, which
|
|
3
|
+
// Tailwind turns into `ease-*` utilities, and `--duration-*`); the springs only make sense in JS, with
|
|
4
|
+
// `useSpring` (exported next to these) or your own loop. No imports here: _useSpring builds on this file.
|
|
5
|
+
|
|
6
|
+
/** A spring: dampingRatio 1 settles without overshoot, lower bounces; higher stiffness is faster */
|
|
7
|
+
export interface SpringSpec { dampingRatio: number; stiffness: number }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Spring specs of the two motion schemes. Spatial springs move things (position, size, shape); effects springs
|
|
11
|
+
* change color and opacity and never overshoot. Standard is the default scheme; Expressive is bouncier, for
|
|
12
|
+
* Expressive components and moments that should feel lively.
|
|
13
|
+
*/
|
|
14
|
+
export const motionSprings = {
|
|
15
|
+
standard: {
|
|
16
|
+
fastSpatial: { dampingRatio: 0.9, stiffness: 1400 },
|
|
17
|
+
defaultSpatial: { dampingRatio: 0.9, stiffness: 700 },
|
|
18
|
+
slowSpatial: { dampingRatio: 0.9, stiffness: 300 },
|
|
19
|
+
fastEffects: { dampingRatio: 1, stiffness: 3800 },
|
|
20
|
+
defaultEffects: { dampingRatio: 1, stiffness: 1600 },
|
|
21
|
+
slowEffects: { dampingRatio: 1, stiffness: 800 },
|
|
22
|
+
},
|
|
23
|
+
expressive: {
|
|
24
|
+
fastSpatial: { dampingRatio: 0.6, stiffness: 800 },
|
|
25
|
+
defaultSpatial: { dampingRatio: 0.8, stiffness: 380 },
|
|
26
|
+
slowSpatial: { dampingRatio: 0.8, stiffness: 200 },
|
|
27
|
+
fastEffects: { dampingRatio: 1, stiffness: 3800 },
|
|
28
|
+
defaultEffects: { dampingRatio: 1, stiffness: 1600 },
|
|
29
|
+
slowEffects: { dampingRatio: 1, stiffness: 800 },
|
|
30
|
+
},
|
|
31
|
+
} as const satisfies Record<'standard' | 'expressive', Record<string, SpringSpec>>
|
|
32
|
+
|
|
33
|
+
/** Easing curves as cubic-bezier control points [x1, y1, x2, y2] (theme.css has them as `--ease-*`) */
|
|
34
|
+
export const motionEasings = {
|
|
35
|
+
emphasized: [0.2, 0, 0, 1],
|
|
36
|
+
emphasizedDecelerate: [0.05, 0.7, 0.1, 1],
|
|
37
|
+
emphasizedAccelerate: [0.3, 0, 0.8, 0.15],
|
|
38
|
+
standard: [0.2, 0, 0, 1],
|
|
39
|
+
standardDecelerate: [0, 0, 0, 1],
|
|
40
|
+
standardAccelerate: [0.3, 0, 1, 1],
|
|
41
|
+
legacy: [0.4, 0, 0.2, 1],
|
|
42
|
+
legacyDecelerate: [0, 0, 0.2, 1],
|
|
43
|
+
legacyAccelerate: [0.4, 0, 1, 1],
|
|
44
|
+
linear: [0, 0, 1, 1],
|
|
45
|
+
} as const satisfies Record<string, readonly [number, number, number, number]>
|
|
46
|
+
|
|
47
|
+
/** Durations in ms (theme.css has them as `--duration-short-1` … `--duration-extra-long-4`) */
|
|
48
|
+
export const motionDurations = {
|
|
49
|
+
short1: 50, short2: 100, short3: 150, short4: 200,
|
|
50
|
+
medium1: 250, medium2: 300, medium3: 350, medium4: 400,
|
|
51
|
+
long1: 450, long2: 500, long3: 550, long4: 600,
|
|
52
|
+
extraLong1: 700, extraLong2: 800, extraLong3: 900, extraLong4: 1000,
|
|
53
|
+
} as const
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A cubic-bezier easing as a function of progress (0–1), e.g. `cubicBezier(...motionEasings.emphasized)`, for
|
|
57
|
+
* JS-driven tweens and for mapping a fraction through a curve
|
|
58
|
+
*/
|
|
59
|
+
export function cubicBezier(x1: number, y1: number, x2: number, y2: number): (t: number) => number {
|
|
60
|
+
return (t: number) => {
|
|
61
|
+
if (t <= 0) return 0
|
|
62
|
+
if (t >= 1) return 1
|
|
63
|
+
// Solve x(u) = t for the curve parameter u (Newton), then return y(u)
|
|
64
|
+
let u = t
|
|
65
|
+
for (let i = 0; i < 8; i++) {
|
|
66
|
+
const x = 3 * u * (1 - u) ** 2 * x1 + 3 * u ** 2 * (1 - u) * x2 + u ** 3 - t
|
|
67
|
+
const dx = 3 * (1 - u) ** 2 * x1 + 6 * u * (1 - u) * (x2 - x1) + 3 * u ** 2 * (1 - x2)
|
|
68
|
+
if (Math.abs(dx) < 1e-6) break
|
|
69
|
+
u -= x / dx
|
|
70
|
+
}
|
|
71
|
+
return 3 * u * (1 - u) ** 2 * y1 + 3 * u ** 2 * (1 - u) * y2 + u ** 3
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { readonly, ref, watchEffect } from 'vue'
|
|
1
|
+
import { computed, readonly, ref, watch, watchEffect } from 'vue'
|
|
2
2
|
import { generateScheme, schemeToCss, type SeedOptions } from './dynamicColor'
|
|
3
|
+
import { themeTransition } from './_themeTransition'
|
|
3
4
|
|
|
4
5
|
export interface Palette {
|
|
5
6
|
id: string
|
|
@@ -66,13 +67,14 @@ export interface ActiveSeed extends SeedOptions {
|
|
|
66
67
|
|
|
67
68
|
const PALETTE_KEY = 'm3-palette'
|
|
68
69
|
const SEED_KEY = 'm3-seed'
|
|
70
|
+
const CONTRAST_KEY = 'm3-contrast'
|
|
69
71
|
// The generated CSS of the active palette, so a reload applies it before the generator has loaded
|
|
70
72
|
const CSS_KEY = 'm3-palette-css'
|
|
71
73
|
|
|
72
74
|
function read<T>(key: string): T | null {
|
|
73
75
|
try {
|
|
74
76
|
const v = localStorage.getItem(key)
|
|
75
|
-
return v == null ? null : (key === PALETTE_KEY ? v : JSON.parse(v)) as T
|
|
77
|
+
return v == null ? null : (key === PALETTE_KEY || key === CONTRAST_KEY ? v : JSON.parse(v)) as T
|
|
76
78
|
} catch {
|
|
77
79
|
return null
|
|
78
80
|
}
|
|
@@ -87,6 +89,37 @@ function write(key: string, value: unknown) {
|
|
|
87
89
|
const current = ref(read<string>(PALETTE_KEY) ?? 'purple')
|
|
88
90
|
const activeSeed = ref<ActiveSeed | null>(read<ActiveSeed>(SEED_KEY))
|
|
89
91
|
|
|
92
|
+
// ── Contrast ─────────────────────────────────────────────────────────────────────────────────────
|
|
93
|
+
/**
|
|
94
|
+
* M3's contrast levels. `system` follows the OS (`prefers-contrast: more` → high). Built-in palettes need
|
|
95
|
+
* `@m3ui-vue/m3ui-vue/palettes-contrast` imported for medium and high; seed palettes are regenerated.
|
|
96
|
+
*/
|
|
97
|
+
export type ContrastLevel = 'standard' | 'medium' | 'high' | 'system'
|
|
98
|
+
const CONTRAST_LEVELS: readonly ContrastLevel[] = ['standard', 'medium', 'high', 'system']
|
|
99
|
+
const storedContrast = read<string>(CONTRAST_KEY)
|
|
100
|
+
const contrast = ref<ContrastLevel>(CONTRAST_LEVELS.includes(storedContrast as ContrastLevel) ? storedContrast as ContrastLevel : 'standard')
|
|
101
|
+
const systemMore = ref(false)
|
|
102
|
+
if (typeof window !== 'undefined' && window.matchMedia) {
|
|
103
|
+
const mq = window.matchMedia('(prefers-contrast: more)')
|
|
104
|
+
systemMore.value = mq.matches
|
|
105
|
+
mq.addEventListener?.('change', (e) => { systemMore.value = e.matches })
|
|
106
|
+
}
|
|
107
|
+
/** The level in effect: `system` resolved to standard or high. */
|
|
108
|
+
const activeContrast = computed<Exclude<ContrastLevel, 'system'>>(() =>
|
|
109
|
+
contrast.value === 'system' ? (systemMore.value ? 'high' : 'standard') : contrast.value)
|
|
110
|
+
const CONTRAST_AMOUNT = { standard: 0, medium: 0.5, high: 1 } as const
|
|
111
|
+
|
|
112
|
+
/** A seed's options with the active contrast level applied (standard keeps the seed's own `contrast`). */
|
|
113
|
+
function withContrast(seed: ActiveSeed): ActiveSeed {
|
|
114
|
+
return activeContrast.value === 'standard' ? seed : { ...seed, contrast: CONTRAST_AMOUNT[activeContrast.value] }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function applyContrast(level: Exclude<ContrastLevel, 'system'>) {
|
|
118
|
+
if (typeof document === 'undefined') return
|
|
119
|
+
if (level === 'standard') document.documentElement.removeAttribute('data-contrast')
|
|
120
|
+
else document.documentElement.setAttribute('data-contrast', level)
|
|
121
|
+
}
|
|
122
|
+
|
|
90
123
|
// Whether palette changes are written to localStorage. Defaults to true (existing behavior).
|
|
91
124
|
// An app can disable it via `createM3UI({ persistPalette: false })` or `useColorPalette().setPersistPalette(false)`
|
|
92
125
|
// so its chosen palette can't be overridden by a stored value on the next boot.
|
|
@@ -136,25 +169,30 @@ function hasPaletteCss(id: string): boolean {
|
|
|
136
169
|
|
|
137
170
|
let generation = 0
|
|
138
171
|
|
|
139
|
-
/** Generates
|
|
140
|
-
async function
|
|
172
|
+
/** Generates the CSS for a palette id from a seed. Resolves null if a newer request won. */
|
|
173
|
+
async function generateCss(id: string, seed: ActiveSeed): Promise<string | null> {
|
|
141
174
|
const mine = ++generation
|
|
142
|
-
const
|
|
143
|
-
|
|
175
|
+
const options = withContrast(seed)
|
|
176
|
+
const css = schemeToCss(await generateScheme(options.seed, options), id)
|
|
177
|
+
return mine === generation ? css : null
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Puts a generated palette's CSS in the page and caches it for the next load. */
|
|
181
|
+
function applyCss(id: string, css: string) {
|
|
144
182
|
injectCss(css)
|
|
145
183
|
if (persistEnabled.value) write(CSS_KEY, { id, css })
|
|
146
|
-
return true
|
|
147
184
|
}
|
|
148
185
|
|
|
149
|
-
/** Switches to a palette id with the theme transition
|
|
150
|
-
* watcher, so it also works when no component uses the composable
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
void document.documentElement.offsetHeight
|
|
154
|
-
current.value = id
|
|
155
|
-
applyAttribute(id)
|
|
186
|
+
/** Switches to a palette id with the theme transition, injecting its generated CSS (if any) inside it. Applied
|
|
187
|
+
* directly, not only through useColorPalette()'s watcher, so it also works when no component uses the composable
|
|
188
|
+
* (createM3UI's defaults). */
|
|
189
|
+
function switchTo(id: string, css?: string) {
|
|
156
190
|
if (persistEnabled.value) write(PALETTE_KEY, id)
|
|
157
|
-
|
|
191
|
+
themeTransition(() => {
|
|
192
|
+
if (css) applyCss(id, css)
|
|
193
|
+
current.value = id
|
|
194
|
+
applyAttribute(id)
|
|
195
|
+
})
|
|
158
196
|
}
|
|
159
197
|
|
|
160
198
|
/** Picks a palette by id; a custom one with a seed and no CSS of its own is generated first. */
|
|
@@ -162,20 +200,46 @@ async function selectPalette(id: string) {
|
|
|
162
200
|
generation++
|
|
163
201
|
const custom = palettes.find((p) => p.id === id)
|
|
164
202
|
if (custom && !builtInIds.has(id) && custom.seed && !hasPaletteCss(id)) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
203
|
+
const css = await generateCss(id, { seed: custom.seed })
|
|
204
|
+
if (css === null) return
|
|
205
|
+
switchTo(id, css)
|
|
206
|
+
} else switchTo(id)
|
|
168
207
|
}
|
|
169
208
|
|
|
170
209
|
/** Activates a seed color: generates its scheme and switches to it. */
|
|
171
210
|
async function selectSeed(seed: string, options: SeedOptions = {}) {
|
|
172
211
|
const next: ActiveSeed = { seed, ...options }
|
|
173
|
-
|
|
212
|
+
const css = await generateCss(SEED_PALETTE, next)
|
|
213
|
+
if (css === null) return
|
|
174
214
|
activeSeed.value = next
|
|
175
215
|
if (persistEnabled.value) write(SEED_KEY, next)
|
|
176
|
-
switchTo(SEED_PALETTE)
|
|
216
|
+
switchTo(SEED_PALETTE, css)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Sets the contrast level, with the theme transition; a generated palette (seed, custom) is regenerated. */
|
|
220
|
+
async function selectContrast(level: ContrastLevel, persist = persistEnabled.value) {
|
|
221
|
+
contrast.value = level
|
|
222
|
+
if (persist) write(CONTRAST_KEY, level)
|
|
177
223
|
}
|
|
178
224
|
|
|
225
|
+
// Applied on every change of the effective level (a setting or the OS preference), for every palette kind. A
|
|
226
|
+
// generated palette is regenerated first, then swapped in with the attribute in one transition.
|
|
227
|
+
watch(activeContrast, async () => {
|
|
228
|
+
const id = current.value
|
|
229
|
+
let css: string | null | undefined
|
|
230
|
+
if (id === SEED_PALETTE && activeSeed.value) css = await generateCss(SEED_PALETTE, activeSeed.value)
|
|
231
|
+
else {
|
|
232
|
+
const custom = palettes.find((p) => p.id === id)
|
|
233
|
+
if (custom && !builtInIds.has(id) && custom.seed && !hasPaletteCss(id)) css = await generateCss(id, { seed: custom.seed })
|
|
234
|
+
}
|
|
235
|
+
// A newer request (a palette, a seed, another level) superseded the generation: it generates at the active
|
|
236
|
+
// level itself, but the attribute still has to follow
|
|
237
|
+
themeTransition(() => {
|
|
238
|
+
applyContrast(activeContrast.value)
|
|
239
|
+
if (css) applyCss(id, css)
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
|
|
179
243
|
export function useColorPalette() {
|
|
180
244
|
watchEffect(() => {
|
|
181
245
|
const id = current.value
|
|
@@ -202,12 +266,24 @@ export function useColorPalette() {
|
|
|
202
266
|
if (enabled) write(PALETTE_KEY, current.value)
|
|
203
267
|
}
|
|
204
268
|
|
|
269
|
+
/**
|
|
270
|
+
* Sets M3's contrast level: `standard`, `medium`, `high`, or `system` (follows `prefers-contrast`). Built-in
|
|
271
|
+
* palettes need `@m3ui-vue/m3ui-vue/palettes-contrast` imported; a seed palette is regenerated at that level.
|
|
272
|
+
* Remembered like the palette.
|
|
273
|
+
*/
|
|
274
|
+
function setContrast(level: ContrastLevel) {
|
|
275
|
+
void selectContrast(level)
|
|
276
|
+
}
|
|
277
|
+
|
|
205
278
|
return {
|
|
206
279
|
palette: current,
|
|
207
280
|
palettes,
|
|
208
281
|
set,
|
|
209
282
|
seed: readonly(activeSeed),
|
|
210
283
|
setSeed,
|
|
284
|
+
contrast: readonly(contrast),
|
|
285
|
+
activeContrast,
|
|
286
|
+
setContrast,
|
|
211
287
|
persistPalette: persistEnabled,
|
|
212
288
|
setPersistPalette,
|
|
213
289
|
}
|
|
@@ -219,6 +295,7 @@ export function useColorPalette() {
|
|
|
219
295
|
const cached = read<{ id: string; css: string }>(CSS_KEY)
|
|
220
296
|
if (saved && cached && cached.id === saved) injectCss(cached.css)
|
|
221
297
|
if (saved && saved !== 'purple') document.documentElement.setAttribute('data-palette', saved)
|
|
298
|
+
applyContrast(activeContrast.value)
|
|
222
299
|
}
|
|
223
300
|
|
|
224
301
|
/**
|
|
@@ -251,3 +328,9 @@ export function __defaultSeed(seed: string, options: SeedOptions | undefined, pe
|
|
|
251
328
|
persistEnabled.value = persist
|
|
252
329
|
return selectSeed(seed, options)
|
|
253
330
|
}
|
|
331
|
+
|
|
332
|
+
/** Internal: `createM3UI({ contrast })`, the first-visit default (or forced with `persistPalette: false`). */
|
|
333
|
+
export function __defaultContrast(level: ContrastLevel, persist: boolean) {
|
|
334
|
+
if (persist && read<string>(CONTRAST_KEY)) return
|
|
335
|
+
void selectContrast(level, persist)
|
|
336
|
+
}
|