@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
|
@@ -1,22 +1,36 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
/**
|
|
3
3
|
* A top app bar in four M3 layouts — center-aligned, small, medium, and large — with a leading navigation icon
|
|
4
|
-
* and trailing actions.
|
|
4
|
+
* and trailing actions. Medium and large can be M3 Expressive's flexible bars (bigger titles, a subtitle), and
|
|
5
|
+
* any of them can react to scrolling: change color, hide, or collapse to the small bar.
|
|
5
6
|
*
|
|
6
7
|
* @slot navigation Custom leading area (default: an MIconButton built from the navigationIcon prop)
|
|
7
8
|
* @slot title Custom title content (default: renders the title prop)
|
|
9
|
+
* @slot subtitle Custom subtitle content (default: renders the subtitle prop)
|
|
8
10
|
* @slot actions Trailing action buttons
|
|
9
11
|
* @event navigation Emitted when the default navigation icon button is clicked (not emitted when the
|
|
10
12
|
* #navigation slot is overridden — that content is fully custom)
|
|
11
13
|
*/
|
|
14
|
+
import { computed, onMounted, onUnmounted, ref, useSlots, watch } from 'vue'
|
|
12
15
|
import MIconButton from './MIconButton.vue'
|
|
13
16
|
import { useLocale } from '../composables/useLocale'
|
|
17
|
+
import { DEFAULT_EFFECTS } from '../composables/_useSpring'
|
|
18
|
+
import { cubicBezier } from '../composables/motion'
|
|
14
19
|
|
|
15
|
-
withDefaults(defineProps<{
|
|
20
|
+
const props = withDefaults(defineProps<{
|
|
16
21
|
/** App bar title */
|
|
17
22
|
title?: string
|
|
23
|
+
/** A line below the title (Compose's `subtitle`): small, center-aligned and flexible medium/large bars */
|
|
24
|
+
subtitle?: string
|
|
18
25
|
/** 64dp small/center-aligned row, or a 112dp medium / 152dp large two-row layout with the title below */
|
|
19
26
|
variant?: 'center' | 'small' | 'medium' | 'large'
|
|
27
|
+
/**
|
|
28
|
+
* Medium/large only: M3 Expressive's MediumFlexibleTopAppBar / LargeFlexibleTopAppBar, with a
|
|
29
|
+
* headline-medium / display-small title and room for a subtitle (112/136dp and 120/152dp tall)
|
|
30
|
+
*/
|
|
31
|
+
flexible?: boolean
|
|
32
|
+
/** Medium/large only: where the title row sits (Compose's `titleHorizontalAlignment`) */
|
|
33
|
+
titleAlignment?: 'start' | 'center'
|
|
20
34
|
/** Leading icon (e.g. menu, arrow_back) */
|
|
21
35
|
navigationIcon?: string
|
|
22
36
|
/** Accessible label for the navigation icon button — defaults to the
|
|
@@ -30,30 +44,216 @@ withDefaults(defineProps<{
|
|
|
30
44
|
elevated?: boolean
|
|
31
45
|
/** Add a bottom border (border-outline-variant) */
|
|
32
46
|
bordered?: boolean
|
|
33
|
-
|
|
47
|
+
/**
|
|
48
|
+
* How the bar reacts to `scrollTarget` scrolling (Compose's TopAppBarDefaults scroll behaviors). `pinned`
|
|
49
|
+
* stays put and takes the scrolled container color once the content moves under it; `enter-always` hides
|
|
50
|
+
* (a medium/large bar collapses to the small one) while the content scrolls down and comes back as soon as
|
|
51
|
+
* it scrolls up; `exit-until-collapsed` collapses a medium/large bar as the content scrolls and expands it
|
|
52
|
+
* only back at the top. The last two make the bar sticky over the content: put it at the top of the
|
|
53
|
+
* scrolling element (or of the page)
|
|
54
|
+
*/
|
|
55
|
+
scrollBehavior?: 'pinned' | 'enter-always' | 'exit-until-collapsed'
|
|
56
|
+
/** What `scrollBehavior` follows: an element or a CSS selector for it. Defaults to the window */
|
|
57
|
+
scrollTarget?: HTMLElement | string | null
|
|
58
|
+
}>(), { variant: 'small', flexible: false, titleAlignment: 'start', scrollTarget: null })
|
|
34
59
|
|
|
35
60
|
defineEmits<{ navigation: [] }>()
|
|
36
61
|
|
|
37
62
|
const locale = useLocale()
|
|
63
|
+
const slots = useSlots()
|
|
64
|
+
|
|
65
|
+
const twoRow = computed(() => props.variant === 'medium' || props.variant === 'large')
|
|
66
|
+
const hasSubtitle = computed(() => !!(props.subtitle || slots.subtitle))
|
|
67
|
+
const COLLAPSED = 64
|
|
68
|
+
// AppBarMedium/Large(Flexible)Tokens container heights
|
|
69
|
+
const expandedHeight = computed(() => {
|
|
70
|
+
if (!twoRow.value) return COLLAPSED
|
|
71
|
+
if (!props.flexible) return props.variant === 'large' ? 152 : 112
|
|
72
|
+
if (props.variant === 'large') return hasSubtitle.value ? 152 : 120
|
|
73
|
+
return hasSubtitle.value ? 136 : 112
|
|
74
|
+
})
|
|
75
|
+
const titleClass = computed(() => {
|
|
76
|
+
if (props.variant === 'large') return props.flexible ? 'text-display-small' : 'text-headline-medium'
|
|
77
|
+
return props.flexible ? 'text-headline-medium' : 'text-headline-small'
|
|
78
|
+
})
|
|
79
|
+
const subtitleClass = computed(() => (props.variant === 'large' ? 'text-title-medium' : 'text-label-large'))
|
|
80
|
+
|
|
81
|
+
// ── Scroll behaviors ─────────────────────────────────────────────────────────────────────────────
|
|
82
|
+
// TopAppBarState: heightOffset from 0 (expanded) to heightOffsetLimit (the bottom row gone for a two-row bar,
|
|
83
|
+
// the whole bar for a single row). Compose shrinks the bar in the layout and consumes the scroll first (the
|
|
84
|
+
// nested scroll's pre-scroll), so the content doesn't move while the bar collapses. A web page scrolls
|
|
85
|
+
// natively, so instead the bar is sticky over the content and, as it shrinks, its bottom margin grows by the
|
|
86
|
+
// same amount: it keeps taking its expanded height in the layout, and its bottom edge stays on the content's
|
|
87
|
+
// top edge, one-to-one with the finger.
|
|
88
|
+
const overlay = computed(() => props.scrollBehavior === 'enter-always' || props.scrollBehavior === 'exit-until-collapsed')
|
|
89
|
+
const limit = () => -(twoRow.value ? expandedHeight.value - COLLAPSED : COLLAPSED)
|
|
90
|
+
|
|
91
|
+
const barEl = ref<HTMLElement>()
|
|
92
|
+
const bottomRowEl = ref<HTMLElement>()
|
|
93
|
+
const smallTitleEl = ref<HTMLElement>()
|
|
94
|
+
const bigTitleEl = ref<HTMLElement>()
|
|
95
|
+
const scrolled = ref(false)
|
|
96
|
+
const collapsedHalf = ref(false)
|
|
97
|
+
|
|
98
|
+
let offset = 0
|
|
99
|
+
let vel = 0
|
|
100
|
+
let snapTarget: number | null = null
|
|
101
|
+
let raf = 0
|
|
102
|
+
let last = 0
|
|
103
|
+
let settleTimer = 0
|
|
104
|
+
let lastScroll = 0
|
|
105
|
+
let target: HTMLElement | Window | null = null
|
|
106
|
+
|
|
107
|
+
// TopTitleAlphaEasing = CubicBezierEasing(.8, 0, .8, .15); the container lerps on FastOutLinearInEasing
|
|
108
|
+
const topTitleAlpha = cubicBezier(0.8, 0, 0.8, 0.15)
|
|
109
|
+
const fastOutLinearIn = cubicBezier(0.4, 0, 1, 1)
|
|
110
|
+
|
|
111
|
+
function write() {
|
|
112
|
+
const f = offset === 0 ? 0 : offset / limit()
|
|
113
|
+
collapsedHalf.value = f >= 0.5
|
|
114
|
+
if (barEl.value) {
|
|
115
|
+
barEl.value.style.height = props.scrollBehavior && offset !== 0 ? `${expandedHeight.value + offset}px` : ''
|
|
116
|
+
barEl.value.style.marginBottom = overlay.value && offset !== 0 ? `${-offset}px` : ''
|
|
117
|
+
}
|
|
118
|
+
if (!twoRow.value) return
|
|
119
|
+
if (bottomRowEl.value) bottomRowEl.value.style.height = `${expandedHeight.value - COLLAPSED + offset}px`
|
|
120
|
+
if (smallTitleEl.value) smallTitleEl.value.style.opacity = String(topTitleAlpha(f))
|
|
121
|
+
if (bigTitleEl.value) bigTitleEl.value.style.opacity = String(1 - f)
|
|
122
|
+
// A two-row bar's container follows the collapse continuously (TwoRowsTopAppBar's colorTransitionFraction)
|
|
123
|
+
if (barEl.value) {
|
|
124
|
+
barEl.value.style.backgroundColor = props.elevated || f === 0
|
|
125
|
+
? ''
|
|
126
|
+
: `color-mix(in srgb, var(--color-surface-container) ${(fastOutLinearIn(f) * 100).toFixed(1)}%, var(--color-surface))`
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const scrollTop = () => (target instanceof Window ? target.scrollY : target?.scrollTop ?? 0)
|
|
131
|
+
|
|
132
|
+
function stopSnap() {
|
|
133
|
+
cancelAnimationFrame(raf)
|
|
134
|
+
raf = 0
|
|
135
|
+
snapTarget = null
|
|
136
|
+
vel = 0
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function onScroll() {
|
|
140
|
+
const y = Math.max(0, scrollTop())
|
|
141
|
+
const dy = y - lastScroll
|
|
142
|
+
lastScroll = y
|
|
143
|
+
// SingleRowTopAppBar: the scrolled color as soon as content sits under the bar (overlappedFraction)
|
|
144
|
+
scrolled.value = y > 0
|
|
145
|
+
if (props.scrollBehavior === 'exit-until-collapsed') {
|
|
146
|
+
// Collapses with the content and expands only as the content comes back to its top
|
|
147
|
+
offset = Math.max(limit(), -y)
|
|
148
|
+
write()
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
if (props.scrollBehavior !== 'enter-always' || !dy) return
|
|
152
|
+
stopSnap()
|
|
153
|
+
offset = Math.min(0, Math.max(limit(), offset - dy))
|
|
154
|
+
write()
|
|
155
|
+
clearTimeout(settleTimer)
|
|
156
|
+
settleTimer = window.setTimeout(settle, 150)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// settleAppBar: a bar left half-way snaps to the closer end (TopAppBarDefaults.snapAnimationSpec, DefaultEffects)
|
|
160
|
+
function settle() {
|
|
161
|
+
const lim = limit()
|
|
162
|
+
if (offset === 0 || offset === lim) return
|
|
163
|
+
snapTarget = offset / lim < 0.5 ? 0 : lim
|
|
164
|
+
last = performance.now()
|
|
165
|
+
raf = requestAnimationFrame(frame)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function frame(now: number) {
|
|
169
|
+
if (snapTarget === null) return
|
|
170
|
+
let dt = Math.min((now - last) / 1000, 1 / 30)
|
|
171
|
+
last = now
|
|
172
|
+
const k = DEFAULT_EFFECTS.stiffness
|
|
173
|
+
const c = 2 * DEFAULT_EFFECTS.dampingRatio * Math.sqrt(k)
|
|
174
|
+
while (dt > 0) {
|
|
175
|
+
const h = Math.min(dt, 0.004)
|
|
176
|
+
dt -= h
|
|
177
|
+
vel += (-k * (offset - snapTarget) - c * vel) * h
|
|
178
|
+
offset += vel * h
|
|
179
|
+
}
|
|
180
|
+
if (Math.abs(offset - snapTarget) < 0.5 && Math.abs(vel) < 0.5) {
|
|
181
|
+
offset = snapTarget
|
|
182
|
+
write()
|
|
183
|
+
stopSnap()
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
write()
|
|
187
|
+
raf = requestAnimationFrame(frame)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function resolveTarget(): HTMLElement | Window | null {
|
|
191
|
+
if (typeof window === 'undefined') return null
|
|
192
|
+
if (!props.scrollTarget) return window
|
|
193
|
+
if (typeof props.scrollTarget === 'string') return document.querySelector<HTMLElement>(props.scrollTarget)
|
|
194
|
+
return props.scrollTarget
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function attach() {
|
|
198
|
+
detach()
|
|
199
|
+
if (!props.scrollBehavior) return
|
|
200
|
+
target = resolveTarget()
|
|
201
|
+
if (!target) return
|
|
202
|
+
lastScroll = Math.max(0, scrollTop())
|
|
203
|
+
target.addEventListener('scroll', onScroll, { passive: true })
|
|
204
|
+
onScroll()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function detach() {
|
|
208
|
+
target?.removeEventListener('scroll', onScroll)
|
|
209
|
+
target = null
|
|
210
|
+
clearTimeout(settleTimer)
|
|
211
|
+
stopSnap()
|
|
212
|
+
offset = 0
|
|
213
|
+
scrolled.value = false
|
|
214
|
+
write()
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
onMounted(() => {
|
|
218
|
+
write()
|
|
219
|
+
attach()
|
|
220
|
+
})
|
|
221
|
+
onUnmounted(detach)
|
|
222
|
+
watch(() => [props.scrollBehavior, props.scrollTarget], attach)
|
|
223
|
+
watch(() => [props.variant, props.flexible, hasSubtitle.value], () => {
|
|
224
|
+
offset = Math.min(0, Math.max(limit(), offset))
|
|
225
|
+
write()
|
|
226
|
+
}, { flush: 'post' })
|
|
227
|
+
|
|
228
|
+
// Single row: elevated, or scrolled under while a scroll behavior is set
|
|
229
|
+
const scrolledLook = computed(() => props.elevated || (!twoRow.value && scrolled.value))
|
|
38
230
|
</script>
|
|
39
231
|
|
|
40
232
|
<template>
|
|
233
|
+
<!-- Direct children are the rows (consumers target `header > div > button` for the navigation button) -->
|
|
41
234
|
<header
|
|
235
|
+
ref="barEl"
|
|
42
236
|
class="flex w-full flex-col transition-[background-color,box-shadow]"
|
|
43
237
|
:class="[
|
|
44
238
|
// AppBarTokens: ContainerColor=Surface/Elevation=Level0 at rest, but
|
|
45
239
|
// OnScrollContainerColor=SurfaceContainer/OnScrollContainerElevation=Level2 together once
|
|
46
|
-
// scrolled — `elevated` swaps both, not just the shadow.
|
|
47
|
-
|
|
240
|
+
// scrolled — `elevated` swaps both, not just the shadow. Scrolling only changes the color, as
|
|
241
|
+
// Compose's TopAppBarColors do.
|
|
242
|
+
scrolledLook ? 'bg-surface-container' : 'bg-surface',
|
|
243
|
+
elevated ? 'shadow-elevation-2' : '',
|
|
48
244
|
bordered ? 'border-b border-outline-variant' : '',
|
|
245
|
+
// Clipped only while it can shrink, so nothing that overflows a static bar today gets cut
|
|
246
|
+
scrollBehavior ? 'overflow-hidden' : '',
|
|
247
|
+
overlay ? 'sticky top-0 z-10' : '',
|
|
49
248
|
]"
|
|
50
249
|
>
|
|
51
250
|
<!-- Top row — 64dp per M3's TopAppBarSmall/collapsed-Medium/collapsed-Large tokens.
|
|
52
251
|
px-2: TopAppBarHorizontalPadding (4dp) + the 4dp IconButton() gets for free from
|
|
53
252
|
Modifier.minimumInteractiveComponentSize() centering its 40dp visible container inside
|
|
54
253
|
a 48dp touch target — MIconButton doesn't implement that invisible expansion, so it's
|
|
55
|
-
folded into this row padding instead to land the icon glyph at the real 16dp inset.
|
|
56
|
-
|
|
254
|
+
folded into this row padding instead to land the icon glyph at the real 16dp inset.
|
|
255
|
+
A single-row bar's row is capped by the (shrinking) bar, keeping its content centered as it hides. -->
|
|
256
|
+
<div class="flex h-16 shrink-0 items-center gap-1 px-2" :class="twoRow ? '' : 'max-h-full'">
|
|
57
257
|
<!-- Navigation -->
|
|
58
258
|
<slot name="navigation">
|
|
59
259
|
<MIconButton
|
|
@@ -65,17 +265,36 @@ const locale = useLocale()
|
|
|
65
265
|
/>
|
|
66
266
|
</slot>
|
|
67
267
|
|
|
68
|
-
<!-- Title: center or small variant -->
|
|
69
|
-
<
|
|
70
|
-
v-if="
|
|
71
|
-
class="flex flex-1
|
|
72
|
-
:class="variant === 'center' ? '
|
|
268
|
+
<!-- Title: center or small variant, with an optional subtitle -->
|
|
269
|
+
<div
|
|
270
|
+
v-if="!twoRow"
|
|
271
|
+
class="flex min-w-0 flex-1 flex-col justify-center px-4"
|
|
272
|
+
:class="variant === 'center' ? 'items-center text-center' : ''"
|
|
73
273
|
>
|
|
74
|
-
<
|
|
75
|
-
|
|
274
|
+
<h1 class="w-full truncate text-title-large text-on-surface">
|
|
275
|
+
<slot name="title">{{ title }}</slot>
|
|
276
|
+
</h1>
|
|
277
|
+
<p v-if="hasSubtitle" class="w-full truncate text-label-medium text-on-surface-variant">
|
|
278
|
+
<slot name="subtitle">{{ subtitle }}</slot>
|
|
279
|
+
</p>
|
|
280
|
+
</div>
|
|
76
281
|
|
|
77
|
-
<!--
|
|
78
|
-
<div
|
|
282
|
+
<!-- Two-row bars: the small title fades in as the bar collapses (TopTitleAlphaEasing) -->
|
|
283
|
+
<div
|
|
284
|
+
v-else
|
|
285
|
+
ref="smallTitleEl"
|
|
286
|
+
class="flex min-w-0 flex-1 flex-col justify-center px-4"
|
|
287
|
+
:class="titleAlignment === 'center' ? 'items-center text-center' : ''"
|
|
288
|
+
style="opacity: 0"
|
|
289
|
+
:aria-hidden="!collapsedHalf || undefined"
|
|
290
|
+
>
|
|
291
|
+
<span class="w-full truncate text-title-large text-on-surface">
|
|
292
|
+
<slot name="title">{{ title }}</slot>
|
|
293
|
+
</span>
|
|
294
|
+
<span v-if="flexible && hasSubtitle" class="w-full truncate text-label-medium text-on-surface-variant">
|
|
295
|
+
<slot name="subtitle">{{ subtitle }}</slot>
|
|
296
|
+
</span>
|
|
297
|
+
</div>
|
|
79
298
|
|
|
80
299
|
<!-- Trailing actions -->
|
|
81
300
|
<div v-if="$slots.actions" class="flex items-center gap-1">
|
|
@@ -84,20 +303,25 @@ const locale = useLocale()
|
|
|
84
303
|
</div>
|
|
85
304
|
|
|
86
305
|
<!-- Large title row for medium/large variants — total container height
|
|
87
|
-
112dp (medium) / 152dp (large) per M3's AppBarMedium/LargeTokens,
|
|
88
|
-
|
|
89
|
-
|
|
306
|
+
112dp (medium) / 152dp (large) per M3's AppBarMedium/LargeTokens, 112/136dp and 120/152dp for the
|
|
307
|
+
flexible ones (without/with a subtitle). Bottom padding is MediumTitleBottomPadding=24dp /
|
|
308
|
+
LargeTitleBottomPadding=28dp. It shrinks from the top while collapsing, the title anchored to its
|
|
309
|
+
bottom and clipped as it goes. Its height is only ever written by write(): a reactive :style here
|
|
310
|
+
would be re-applied on every re-render and undo the collapse. -->
|
|
90
311
|
<div
|
|
91
|
-
v-if="
|
|
92
|
-
|
|
93
|
-
|
|
312
|
+
v-if="twoRow"
|
|
313
|
+
ref="bottomRowEl"
|
|
314
|
+
class="flex shrink-0 items-end overflow-hidden px-4"
|
|
315
|
+
:class="[variant === 'large' ? 'pb-7' : 'pb-6', titleAlignment === 'center' ? 'justify-center text-center' : '']"
|
|
94
316
|
>
|
|
95
|
-
<
|
|
96
|
-
class="truncate text-on-surface"
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
<
|
|
100
|
-
|
|
317
|
+
<div ref="bigTitleEl" class="min-w-0" :aria-hidden="collapsedHalf || undefined">
|
|
318
|
+
<h1 class="truncate text-on-surface" :class="titleClass">
|
|
319
|
+
<slot name="title">{{ title }}</slot>
|
|
320
|
+
</h1>
|
|
321
|
+
<p v-if="flexible && hasSubtitle" class="truncate text-on-surface-variant" :class="subtitleClass">
|
|
322
|
+
<slot name="subtitle">{{ subtitle }}</slot>
|
|
323
|
+
</p>
|
|
324
|
+
</div>
|
|
101
325
|
</div>
|
|
102
326
|
</header>
|
|
103
327
|
</template>
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @slot default The current view; give it a `key` that changes with it
|
|
10
10
|
*/
|
|
11
|
+
import { sampleSpring, playSamples } from '../composables/_springAnimation'
|
|
12
|
+
|
|
11
13
|
const props = withDefaults(defineProps<{
|
|
12
14
|
/** `shared-axis` (horizontal, for forward/back navigation) or `fade-through` (unrelated destinations) */
|
|
13
15
|
type?: 'shared-axis' | 'fade-through'
|
|
@@ -18,8 +20,7 @@ const props = withDefaults(defineProps<{
|
|
|
18
20
|
}>(), { type: 'shared-axis', reverse: false, appear: false })
|
|
19
21
|
|
|
20
22
|
// slideIn/OutHorizontally and scaleIn/Out default to spring(stiffness = StiffnessMediumLow), no bounce
|
|
21
|
-
const
|
|
22
|
-
const DAMPING = 2 * Math.sqrt(STIFFNESS)
|
|
23
|
+
const SPRING = { dampingRatio: 1, stiffness: 400 }
|
|
23
24
|
// HorizontalSharedAxis
|
|
24
25
|
const SLIDE_FRACTION = 0.2
|
|
25
26
|
const AXIS_FADE = { duration: 90, delay: 50 }
|
|
@@ -30,54 +31,50 @@ const INITIAL_SCALE = 0.8
|
|
|
30
31
|
const TARGET_SCALE = 1.05
|
|
31
32
|
|
|
32
33
|
interface Motion {
|
|
33
|
-
/** Spring from `from` to `to` (an x offset
|
|
34
|
+
/** Spring from `from` to `to` (an x offset as a fraction of the view's width, or a scale) */
|
|
34
35
|
from: number
|
|
35
36
|
to: number
|
|
36
|
-
|
|
37
|
+
transform: (value: number) => string
|
|
37
38
|
/** Linear opacity tween */
|
|
38
39
|
fade: { from: number; to: number; duration: number; delay: number }
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
const running = new WeakMap<Element, () => void>()
|
|
42
43
|
|
|
44
|
+
// Played as Web Animations (see _springAnimation.ts), which the compositor runs, so the entering view mounting
|
|
45
|
+
// (the busiest moment of a navigation) doesn't jank the transition
|
|
43
46
|
function run(el: HTMLElement, m: Motion, done: () => void) {
|
|
44
47
|
running.get(el)?.()
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const samples = sampleSpring(m.from, m.to, 0, SPRING, 0.001)
|
|
49
|
+
const move = playSamples(el, samples, (v) => ({ transform: m.transform(v) }))
|
|
50
|
+
const fade = typeof el.animate === 'function'
|
|
51
|
+
? el.animate([{ opacity: String(m.fade.from) }, { opacity: String(m.fade.to) }], {
|
|
52
|
+
duration: m.fade.duration,
|
|
53
|
+
delay: m.fade.delay,
|
|
54
|
+
easing: 'linear',
|
|
55
|
+
// Holds the starting opacity through the delay
|
|
56
|
+
fill: 'backwards',
|
|
57
|
+
})
|
|
58
|
+
: null
|
|
59
|
+
fade?.finished.catch(() => {})
|
|
60
|
+
// The end state goes inline underneath
|
|
61
|
+
el.style.transform = m.transform(m.to)
|
|
62
|
+
el.style.opacity = String(m.fade.to)
|
|
63
|
+
let ended = false
|
|
51
64
|
const finish = () => {
|
|
52
|
-
|
|
65
|
+
if (ended) return
|
|
66
|
+
ended = true
|
|
67
|
+
move?.cancel()
|
|
68
|
+
fade?.cancel()
|
|
53
69
|
running.delete(el)
|
|
54
70
|
done()
|
|
55
71
|
}
|
|
56
72
|
running.set(el, finish)
|
|
57
|
-
|
|
58
|
-
let dt = Math.min((now - last) / 1000, 1 / 30)
|
|
59
|
-
last = now
|
|
60
|
-
while (dt > 0) {
|
|
61
|
-
const h = Math.min(dt, 0.004)
|
|
62
|
-
dt -= h
|
|
63
|
-
velocity += (-STIFFNESS * (value - m.to) - DAMPING * velocity) * h
|
|
64
|
-
value += velocity * h
|
|
65
|
-
}
|
|
66
|
-
const springDone = Math.abs(value - m.to) < threshold && Math.abs(velocity) < threshold * 10
|
|
67
|
-
if (springDone) value = m.to
|
|
68
|
-
m.apply(value, el)
|
|
69
|
-
const t = Math.min(1, Math.max(0, (now - start - m.fade.delay) / m.fade.duration))
|
|
70
|
-
el.style.opacity = String(m.fade.from + (m.fade.to - m.fade.from) * t)
|
|
71
|
-
if (springDone && t >= 1) finish()
|
|
72
|
-
else raf = requestAnimationFrame(frame)
|
|
73
|
-
}
|
|
74
|
-
m.apply(m.from, el)
|
|
75
|
-
el.style.opacity = String(m.fade.from)
|
|
76
|
-
raf = requestAnimationFrame(frame)
|
|
73
|
+
Promise.all([move?.finished, fade?.finished]).then(finish, () => {})
|
|
77
74
|
}
|
|
78
75
|
|
|
79
|
-
const translate = (x: number
|
|
80
|
-
const scale = (
|
|
76
|
+
const translate = (x: number) => `translateX(${x * 100}%)`
|
|
77
|
+
const scale = (v: number) => `scale(${v})`
|
|
81
78
|
|
|
82
79
|
function clear(el: HTMLElement) {
|
|
83
80
|
el.style.transform = ''
|
|
@@ -89,16 +86,15 @@ function onEnter(element: Element, done: () => void) {
|
|
|
89
86
|
const el = element as HTMLElement
|
|
90
87
|
const finish = () => { clear(el); done() }
|
|
91
88
|
if (props.type === 'fade-through') {
|
|
92
|
-
run(el, { from: INITIAL_SCALE, to: 1,
|
|
89
|
+
run(el, { from: INITIAL_SCALE, to: 1, transform: scale, fade: { from: 0, to: 1, duration: FADE_IN, delay: FADE_OUT } }, finish)
|
|
93
90
|
return
|
|
94
91
|
}
|
|
95
|
-
const w = el.offsetWidth
|
|
96
92
|
// Backward: the entering view sits under the leaving one (targetContentZIndex = -10)
|
|
97
93
|
if (props.reverse) el.style.zIndex = '-1'
|
|
98
94
|
run(el, {
|
|
99
|
-
from: (props.reverse ? -1 : 1) *
|
|
95
|
+
from: (props.reverse ? -1 : 1) * SLIDE_FRACTION,
|
|
100
96
|
to: 0,
|
|
101
|
-
|
|
97
|
+
transform: translate,
|
|
102
98
|
fade: { from: 0, to: 1, ...AXIS_FADE },
|
|
103
99
|
}, finish)
|
|
104
100
|
}
|
|
@@ -107,14 +103,13 @@ function onLeave(element: Element, done: () => void) {
|
|
|
107
103
|
const el = element as HTMLElement
|
|
108
104
|
const finish = () => { clear(el); done() }
|
|
109
105
|
if (props.type === 'fade-through') {
|
|
110
|
-
run(el, { from: 1, to: TARGET_SCALE,
|
|
106
|
+
run(el, { from: 1, to: TARGET_SCALE, transform: scale, fade: { from: 1, to: 0, duration: FADE_OUT, delay: 0 } }, finish)
|
|
111
107
|
return
|
|
112
108
|
}
|
|
113
|
-
const w = el.offsetWidth
|
|
114
109
|
run(el, {
|
|
115
110
|
from: 0,
|
|
116
|
-
to: (props.reverse ? 1 : -1) *
|
|
117
|
-
|
|
111
|
+
to: (props.reverse ? 1 : -1) * SLIDE_FRACTION,
|
|
112
|
+
transform: translate,
|
|
118
113
|
fade: { from: 1, to: 0, ...AXIS_FADE },
|
|
119
114
|
}, finish)
|
|
120
115
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Internal (not exported from index.ts): what MAppBarWithSearch hands the MSearchBar inside it
|
|
2
|
+
import type { InjectionKey, Ref } from 'vue'
|
|
3
|
+
|
|
4
|
+
export interface SearchAppBarContext {
|
|
5
|
+
/** Content is scrolled under the app bar: the search bar takes its scrolled container color */
|
|
6
|
+
scrolled: Ref<boolean>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const searchAppBarKey: InjectionKey<SearchAppBarContext> = Symbol('m-search-app-bar')
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { SpringSpec } from './motion'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Internal (not exported from index.ts): springs played as Web Animations.
|
|
5
|
+
*
|
|
6
|
+
* The library's springs normally step in a rAF loop (see CLAUDE.md). That loop runs on the main thread, and an
|
|
7
|
+
* enter/exit animation starts exactly when the main thread is busiest: mounting the panel's content, re-laying
|
|
8
|
+
* out the page for a scroll lock. On phones that janked visibly (MNavigationDrawer's open, where the old CSS
|
|
9
|
+
* transition hadn't, since the compositor runs those). So enter/exit/settle springs are simulated up front, one
|
|
10
|
+
* sample per 60Hz frame, and played as a linear Web Animation of transform/opacity, which the compositor runs
|
|
11
|
+
* too. Same curve, bounce and release velocity as the loop; only a finger-driven drag writes styles directly.
|
|
12
|
+
*
|
|
13
|
+
* Pattern: write the end state inline, then play the samples over it (an animation beats inline styles), so
|
|
14
|
+
* nothing jumps when it ends; to interrupt, read `sampleAt(samples, anim.currentTime)`, write that inline and
|
|
15
|
+
* cancel.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export const SAMPLE_MS = 1000 / 60
|
|
19
|
+
// Stiff springs (FastEffects, 3800) diverge at a 16ms Euler step: integrate in 4ms substeps
|
|
20
|
+
const SUBSTEP = 0.004
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The spring from `from` to `to`, starting at `velocity` (units/s), one sample per 60Hz frame until it's within
|
|
24
|
+
* `epsilon` of `to` (and slower than `epsilon * 10` per second). The last sample is exactly `to`.
|
|
25
|
+
*/
|
|
26
|
+
export function sampleSpring(from: number, to: number, velocity: number, spring: SpringSpec, epsilon: number): number[] {
|
|
27
|
+
const out = [from]
|
|
28
|
+
let pos = from
|
|
29
|
+
let vel = velocity
|
|
30
|
+
const damping = 2 * spring.dampingRatio * Math.sqrt(spring.stiffness)
|
|
31
|
+
for (let i = 0; i < 600; i++) {
|
|
32
|
+
for (let left = SAMPLE_MS / 1000; left > 1e-9; left -= SUBSTEP) {
|
|
33
|
+
const h = Math.min(SUBSTEP, left)
|
|
34
|
+
vel += (-spring.stiffness * (pos - to) - damping * vel) * h
|
|
35
|
+
pos += vel * h
|
|
36
|
+
}
|
|
37
|
+
out.push(pos)
|
|
38
|
+
if (Math.abs(pos - to) < epsilon && Math.abs(vel) < epsilon * 10) break
|
|
39
|
+
}
|
|
40
|
+
out[out.length - 1] = to
|
|
41
|
+
return out
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The value `samples` has `time` ms into playback (an Animation's currentTime), linear between frames */
|
|
45
|
+
export function sampleAt(samples: number[], time: CSSNumberish | null | undefined): number {
|
|
46
|
+
const t = Number(time ?? 0) / SAMPLE_MS
|
|
47
|
+
const i = Math.floor(t)
|
|
48
|
+
if (i >= samples.length - 1) return samples[samples.length - 1]!
|
|
49
|
+
if (i < 0) return samples[0]!
|
|
50
|
+
return samples[i]! + (samples[i + 1]! - samples[i]!) * (t - i)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** How long `samples` play, in ms */
|
|
54
|
+
export const samplesDuration = (samples: number[]) => (samples.length - 1) * SAMPLE_MS
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Plays `samples` on `el` as a linear Web Animation, each mapped to a keyframe. Null when there's nothing to
|
|
58
|
+
* play or no Web Animations API (the inline end state then simply stands).
|
|
59
|
+
*/
|
|
60
|
+
export function playSamples(
|
|
61
|
+
el: Element | null | undefined,
|
|
62
|
+
samples: number[],
|
|
63
|
+
keyframe: (value: number) => Keyframe,
|
|
64
|
+
delay = 0,
|
|
65
|
+
): Animation | null {
|
|
66
|
+
if (!el || typeof (el as HTMLElement).animate !== 'function' || samples.length < 2) return null
|
|
67
|
+
const anim = (el as HTMLElement).animate(samples.map(keyframe), { duration: samplesDuration(samples), delay, easing: 'linear' })
|
|
68
|
+
// cancel() rejects `finished`; nothing has to wait on it
|
|
69
|
+
anim.finished.catch(() => {})
|
|
70
|
+
return anim
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const prefersReducedMotion = () =>
|
|
74
|
+
typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// role of the active palette, dark mode and seed included. Started by createM3UI({ themeColor }).
|
|
3
3
|
|
|
4
4
|
let observer: MutationObserver | undefined
|
|
5
|
+
// useColorPalette's <style> for generated palettes
|
|
6
|
+
const PALETTE_STYLE_ID = 'm3-dynamic-palette'
|
|
5
7
|
|
|
6
8
|
function metas(): HTMLMetaElement[] {
|
|
7
9
|
const found = Array.from(document.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]'))
|
|
@@ -26,8 +28,13 @@ export function syncThemeColor(role: string) {
|
|
|
26
28
|
if (typeof document === 'undefined') return
|
|
27
29
|
observer?.disconnect()
|
|
28
30
|
update(role)
|
|
29
|
-
// Palette (data-palette),
|
|
30
|
-
// a
|
|
31
|
-
|
|
32
|
-
observer
|
|
31
|
+
// Palette (data-palette), contrast (data-contrast) and dark mode (class) land on <html>; a generated palette
|
|
32
|
+
// (a seed, or the same seed at another contrast) only rewrites the <style> it lives in, inside <head>
|
|
33
|
+
const isPaletteStyle = (node: Node | null) => (node instanceof Element ? node : node?.parentElement)?.id === PALETTE_STYLE_ID
|
|
34
|
+
observer = new MutationObserver((records) => {
|
|
35
|
+
if (records.some((r) => r.target === document.documentElement || isPaletteStyle(r.target)
|
|
36
|
+
|| Array.from(r.addedNodes).some(isPaletteStyle))) update(role)
|
|
37
|
+
})
|
|
38
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class', 'data-palette', 'data-contrast'] })
|
|
39
|
+
observer.observe(document.head, { childList: true, characterData: true, subtree: true })
|
|
33
40
|
}
|