@m3ui-vue/m3ui-vue 0.9.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -29
- package/dist/API.md +177 -18
- package/dist/{MTextField-DKtgPymd.js → MTextField-TZ7lfr09.js} +233 -108
- package/dist/MTextField-TZ7lfr09.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/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/composables/_searchAppBar.d.ts +6 -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 +6772 -5931
- 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 +96 -0
- package/src/__tests__/MBottomAppBar.test.ts +71 -0
- package/src/__tests__/MBottomSheetScaffold.test.ts +101 -0
- package/src/__tests__/MChip.test.ts +63 -0
- package/src/__tests__/MFab.test.ts +36 -0
- package/src/__tests__/MNavigationRail.test.ts +77 -0
- package/src/__tests__/MTopAppBar.test.ts +99 -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__/themeTransition.test.ts +119 -0
- package/src/components/MAppBarWithSearch.vue +236 -0
- package/src/components/MBottomAppBar.vue +181 -0
- package/src/components/MBottomSheetScaffold.vue +296 -0
- package/src/components/MBreadcrumbs.vue +1 -1
- package/src/components/MChip.vue +137 -5
- package/src/components/MFab.vue +73 -12
- package/src/components/MNavigationDrawer.vue +2 -2
- package/src/components/MNavigationRail.vue +135 -8
- package/src/components/MSearchBar.vue +28 -12
- package/src/components/MTopAppBar.vue +252 -28
- package/src/composables/_searchAppBar.ts +9 -0
- package/src/composables/_themeColor.ts +11 -4
- package/src/composables/_themeTransition.ts +71 -0
- 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,236 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* A top app bar whose title is a search bar (a port of Compose's `AppBarWithSearch`, and with `transparent` its
|
|
4
|
+
* `TopSearchBar`): a navigation icon, the search bar and actions in one 64px row. The search bar expands like
|
|
5
|
+
* MSearchBar (full screen by default). It can darken as content scrolls under it, and hide while scrolling down.
|
|
6
|
+
*
|
|
7
|
+
* @slot navigation Custom leading area (default: an MIconButton built from the navigationIcon prop)
|
|
8
|
+
* @slot actions Trailing action buttons
|
|
9
|
+
* @slot leading Overrides the search bar's leading icon
|
|
10
|
+
* @slot trailing Trailing content inside the search bar (e.g. an avatar)
|
|
11
|
+
* @slot default Results/suggestions, shown below the input while the search is expanded
|
|
12
|
+
* @event update:modelValue Emitted on every input change — powers v-model
|
|
13
|
+
* @event update:expanded Emitted when the search expands or collapses — powers v-model:expanded
|
|
14
|
+
* @event search Emitted when Enter is pressed in the input, with the current query
|
|
15
|
+
* @event navigation Emitted when the default navigation icon button is clicked
|
|
16
|
+
*/
|
|
17
|
+
import { onMounted, onUnmounted, provide, ref, watch } from 'vue'
|
|
18
|
+
import MIconButton from './MIconButton.vue'
|
|
19
|
+
import MSearchBar from './MSearchBar.vue'
|
|
20
|
+
import { useLocale } from '../composables/useLocale'
|
|
21
|
+
import { DEFAULT_EFFECTS } from '../composables/_useSpring'
|
|
22
|
+
import { searchAppBarKey } from '../composables/_searchAppBar'
|
|
23
|
+
|
|
24
|
+
const props = withDefaults(defineProps<{
|
|
25
|
+
/** Search query (v-model) */
|
|
26
|
+
modelValue?: string
|
|
27
|
+
/** Whether the search is expanded (v-model:expanded) */
|
|
28
|
+
expanded?: boolean
|
|
29
|
+
/** Defaults to the locale's "Search..." */
|
|
30
|
+
placeholder?: string
|
|
31
|
+
/** How the search expands: full screen (default), full screen keeping the pill input, or a docked panel */
|
|
32
|
+
searchVariant?: 'fullscreen' | 'fullscreen-contained' | 'docked'
|
|
33
|
+
/** Icon shown in the collapsed search bar */
|
|
34
|
+
leadingIcon?: string
|
|
35
|
+
/** Shows a spinner in the expanded header instead of the clear button */
|
|
36
|
+
loading?: boolean
|
|
37
|
+
/** Shows a clear (×) button once there is a query */
|
|
38
|
+
clearable?: boolean
|
|
39
|
+
/** Leading icon outside the search bar (e.g. menu, arrow_back) */
|
|
40
|
+
navigationIcon?: string
|
|
41
|
+
/** Accessible label for the navigation icon button; defaults to the locale's "Menu" */
|
|
42
|
+
navigationLabel?: string
|
|
43
|
+
/** No app bar container, only the search bar (Compose's TopSearchBar) */
|
|
44
|
+
transparent?: boolean
|
|
45
|
+
/**
|
|
46
|
+
* How the bar reacts to `scrollTarget` scrolling: `pinned` stays and changes color once content is under
|
|
47
|
+
* it; `enter-always` also hides while scrolling down and comes back on any scroll up. With `enter-always`
|
|
48
|
+
* the bar is sticky over the content: put it at the top of the scrolling element (or of the page)
|
|
49
|
+
*/
|
|
50
|
+
scrollBehavior?: 'pinned' | 'enter-always'
|
|
51
|
+
/** What `scrollBehavior` follows: an element or a CSS selector for it. Defaults to the window */
|
|
52
|
+
scrollTarget?: HTMLElement | string | null
|
|
53
|
+
}>(), {
|
|
54
|
+
modelValue: '',
|
|
55
|
+
expanded: undefined,
|
|
56
|
+
searchVariant: 'fullscreen',
|
|
57
|
+
leadingIcon: 'search',
|
|
58
|
+
loading: false,
|
|
59
|
+
clearable: true,
|
|
60
|
+
transparent: false,
|
|
61
|
+
scrollTarget: null,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const emit = defineEmits<{
|
|
65
|
+
'update:modelValue': [string]
|
|
66
|
+
'update:expanded': [boolean]
|
|
67
|
+
search: [string]
|
|
68
|
+
navigation: []
|
|
69
|
+
}>()
|
|
70
|
+
|
|
71
|
+
const locale = useLocale()
|
|
72
|
+
|
|
73
|
+
// AppBarWithSearchColors: the app bar goes surface → surface-container (animated, DefaultEffects) and the
|
|
74
|
+
// search bar surface-container-high → surface-container-highest once content is under it. A transparent bar
|
|
75
|
+
// (TopSearchBar) keeps both.
|
|
76
|
+
const scrolled = ref(false)
|
|
77
|
+
const searchScrolled = ref(false)
|
|
78
|
+
provide(searchAppBarKey, { scrolled: searchScrolled })
|
|
79
|
+
|
|
80
|
+
// ── enter-always (EnterAlwaysSearchBarScrollBehavior) ───────────────────────────────────────────
|
|
81
|
+
// scrollOffset from 0 to -height: the row slides up and the bar's height shrinks with it. As in MTopAppBar
|
|
82
|
+
// the bar is sticky and grows its bottom margin as it shrinks, so content scrolls one to one under it.
|
|
83
|
+
const barEl = ref<HTMLElement>()
|
|
84
|
+
const rowEl = ref<HTMLElement>()
|
|
85
|
+
const HEIGHT = 64
|
|
86
|
+
let offset = 0
|
|
87
|
+
let vel = 0
|
|
88
|
+
let snapTarget: number | null = null
|
|
89
|
+
let raf = 0
|
|
90
|
+
let last = 0
|
|
91
|
+
let settleTimer = 0
|
|
92
|
+
let lastScroll = 0
|
|
93
|
+
let target: HTMLElement | Window | null = null
|
|
94
|
+
|
|
95
|
+
function write() {
|
|
96
|
+
if (barEl.value) {
|
|
97
|
+
barEl.value.style.height = offset ? `${HEIGHT + offset}px` : ''
|
|
98
|
+
barEl.value.style.marginBottom = offset ? `${-offset}px` : ''
|
|
99
|
+
}
|
|
100
|
+
if (rowEl.value) rowEl.value.style.transform = offset ? `translateY(${offset}px)` : ''
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const scrollTop = () => (target instanceof Window ? target.scrollY : target?.scrollTop ?? 0)
|
|
104
|
+
|
|
105
|
+
function stopSnap() {
|
|
106
|
+
cancelAnimationFrame(raf)
|
|
107
|
+
raf = 0
|
|
108
|
+
snapTarget = null
|
|
109
|
+
vel = 0
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function onScroll() {
|
|
113
|
+
const y = Math.max(0, scrollTop())
|
|
114
|
+
const dy = y - lastScroll
|
|
115
|
+
lastScroll = y
|
|
116
|
+
scrolled.value = y > 0 && !props.transparent
|
|
117
|
+
searchScrolled.value = scrolled.value
|
|
118
|
+
if (props.scrollBehavior !== 'enter-always' || !dy) return
|
|
119
|
+
stopSnap()
|
|
120
|
+
offset = Math.min(0, Math.max(-HEIGHT, offset - dy))
|
|
121
|
+
write()
|
|
122
|
+
clearTimeout(settleTimer)
|
|
123
|
+
settleTimer = window.setTimeout(settle, 150)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// settleSearchBar: half-way snaps to the closer end on DefaultEffects
|
|
127
|
+
function settle() {
|
|
128
|
+
if (offset === 0 || offset === -HEIGHT) return
|
|
129
|
+
snapTarget = offset / -HEIGHT < 0.5 ? 0 : -HEIGHT
|
|
130
|
+
last = performance.now()
|
|
131
|
+
raf = requestAnimationFrame(frame)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function frame(now: number) {
|
|
135
|
+
if (snapTarget === null) return
|
|
136
|
+
let dt = Math.min((now - last) / 1000, 1 / 30)
|
|
137
|
+
last = now
|
|
138
|
+
const k = DEFAULT_EFFECTS.stiffness
|
|
139
|
+
const c = 2 * DEFAULT_EFFECTS.dampingRatio * Math.sqrt(k)
|
|
140
|
+
while (dt > 0) {
|
|
141
|
+
const h = Math.min(dt, 0.004)
|
|
142
|
+
dt -= h
|
|
143
|
+
vel += (-k * (offset - snapTarget) - c * vel) * h
|
|
144
|
+
offset += vel * h
|
|
145
|
+
}
|
|
146
|
+
if (Math.abs(offset - snapTarget) < 0.5 && Math.abs(vel) < 0.5) {
|
|
147
|
+
offset = snapTarget
|
|
148
|
+
write()
|
|
149
|
+
stopSnap()
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
write()
|
|
153
|
+
raf = requestAnimationFrame(frame)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function resolveTarget(): HTMLElement | Window | null {
|
|
157
|
+
if (typeof window === 'undefined') return null
|
|
158
|
+
if (!props.scrollTarget) return window
|
|
159
|
+
if (typeof props.scrollTarget === 'string') return document.querySelector<HTMLElement>(props.scrollTarget)
|
|
160
|
+
return props.scrollTarget
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function attach() {
|
|
164
|
+
detach()
|
|
165
|
+
if (!props.scrollBehavior) return
|
|
166
|
+
target = resolveTarget()
|
|
167
|
+
if (!target) return
|
|
168
|
+
lastScroll = Math.max(0, scrollTop())
|
|
169
|
+
target.addEventListener('scroll', onScroll, { passive: true })
|
|
170
|
+
onScroll()
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function detach() {
|
|
174
|
+
target?.removeEventListener('scroll', onScroll)
|
|
175
|
+
target = null
|
|
176
|
+
clearTimeout(settleTimer)
|
|
177
|
+
stopSnap()
|
|
178
|
+
offset = 0
|
|
179
|
+
scrolled.value = false
|
|
180
|
+
searchScrolled.value = false
|
|
181
|
+
write()
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
onMounted(attach)
|
|
185
|
+
onUnmounted(detach)
|
|
186
|
+
watch(() => [props.scrollBehavior, props.scrollTarget, props.transparent], attach)
|
|
187
|
+
</script>
|
|
188
|
+
|
|
189
|
+
<template>
|
|
190
|
+
<header
|
|
191
|
+
ref="barEl"
|
|
192
|
+
class="w-full transition-[background-color] duration-200"
|
|
193
|
+
:class="[
|
|
194
|
+
transparent ? 'bg-transparent' : scrolled ? 'bg-surface-container' : 'bg-surface',
|
|
195
|
+
scrollBehavior ? 'overflow-hidden' : '',
|
|
196
|
+
scrollBehavior === 'enter-always' ? 'sticky top-0 z-10' : '',
|
|
197
|
+
]"
|
|
198
|
+
>
|
|
199
|
+
<!-- AppBarWithSearch: 4dp outside the navigation icon and the actions; the search bar 8dp from them and
|
|
200
|
+
4dp from the top and bottom (SearchBarAsTopBarPadding, AppBarWithSearchVerticalPadding), at most
|
|
201
|
+
720dp wide and centered -->
|
|
202
|
+
<div ref="rowEl" class="flex h-16 items-center px-1">
|
|
203
|
+
<slot name="navigation">
|
|
204
|
+
<MIconButton
|
|
205
|
+
v-if="navigationIcon"
|
|
206
|
+
:icon="navigationIcon"
|
|
207
|
+
:label="navigationLabel ?? locale.menu"
|
|
208
|
+
style="color: var(--color-on-surface)"
|
|
209
|
+
@click="emit('navigation')"
|
|
210
|
+
/>
|
|
211
|
+
</slot>
|
|
212
|
+
<div class="flex min-w-0 flex-1 justify-center px-2 py-1">
|
|
213
|
+
<MSearchBar
|
|
214
|
+
class="max-w-[720px]"
|
|
215
|
+
:model-value="modelValue"
|
|
216
|
+
:expanded="expanded"
|
|
217
|
+
:placeholder="placeholder"
|
|
218
|
+
:variant="searchVariant"
|
|
219
|
+
:leading-icon="leadingIcon"
|
|
220
|
+
:loading="loading"
|
|
221
|
+
:clearable="clearable"
|
|
222
|
+
@update:model-value="emit('update:modelValue', $event)"
|
|
223
|
+
@update:expanded="emit('update:expanded', $event)"
|
|
224
|
+
@search="emit('search', $event)"
|
|
225
|
+
>
|
|
226
|
+
<template v-if="$slots.leading" #leading><slot name="leading" /></template>
|
|
227
|
+
<template v-if="$slots.trailing" #trailing><slot name="trailing" /></template>
|
|
228
|
+
<slot />
|
|
229
|
+
</MSearchBar>
|
|
230
|
+
</div>
|
|
231
|
+
<div v-if="$slots.actions" class="flex shrink-0 items-center gap-1" style="color: var(--color-on-surface-variant)">
|
|
232
|
+
<slot name="actions" />
|
|
233
|
+
</div>
|
|
234
|
+
</div>
|
|
235
|
+
</header>
|
|
236
|
+
</template>
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* M3's bottom app bar (a port of Compose's `BottomAppBar`): navigation and key actions at the bottom of small
|
|
4
|
+
* screens, with an optional FAB at the end. With `flexible` it's M3 Expressive's `FlexibleBottomAppBar`, a
|
|
5
|
+
* shorter bar whose actions spread across it. With `scrollBehavior="exit-always"` it collapses as the content
|
|
6
|
+
* scrolls down and comes back as it scrolls up.
|
|
7
|
+
*
|
|
8
|
+
* @slot default The actions, usually MIconButtons
|
|
9
|
+
* @slot fab A FAB at the end of the standard bar; an MFab here turns secondary and drops its shadow, as M3's
|
|
10
|
+
* bottom app bar FAB (`bottomAppBarFabColor`, `bottomAppBarFabElevation`)
|
|
11
|
+
*/
|
|
12
|
+
import { computed, onMounted, onUnmounted, provide, ref, watch } from 'vue'
|
|
13
|
+
import { FAST_SPATIAL } from '../composables/_useSpring'
|
|
14
|
+
|
|
15
|
+
const props = withDefaults(defineProps<{
|
|
16
|
+
/** M3 Expressive's FlexibleBottomAppBar: 64px instead of 80px, 16px side padding, no FAB slot */
|
|
17
|
+
flexible?: boolean
|
|
18
|
+
/**
|
|
19
|
+
* How the actions are spread: `start` (the standard bar's), `between` (the flexible bar's default),
|
|
20
|
+
* `around`, `evenly`, or `fixed` (centered, 32px apart: FlexibleFixedHorizontalArrangement)
|
|
21
|
+
*/
|
|
22
|
+
arrangement?: 'start' | 'between' | 'around' | 'evenly' | 'fixed'
|
|
23
|
+
/** Container color: surface-container, or a color's container for a vibrant bar */
|
|
24
|
+
color?: 'surface' | 'primary' | 'secondary' | 'tertiary'
|
|
25
|
+
/** `pinned` stays put; `exit-always` collapses while the content scrolls down and returns when it scrolls up */
|
|
26
|
+
scrollBehavior?: 'pinned' | 'exit-always'
|
|
27
|
+
/** What `exit-always` follows: an element or a CSS selector for it. Defaults to the window */
|
|
28
|
+
scrollTarget?: HTMLElement | string | null
|
|
29
|
+
}>(), {
|
|
30
|
+
flexible: false,
|
|
31
|
+
color: 'surface',
|
|
32
|
+
scrollBehavior: 'pinned',
|
|
33
|
+
scrollTarget: null,
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// An MFab in #fab reads this (secondary, no elevation)
|
|
37
|
+
provide('m-bottom-app-bar', true)
|
|
38
|
+
|
|
39
|
+
const colorClass = computed(() => ({
|
|
40
|
+
surface: 'bg-surface-container text-on-surface',
|
|
41
|
+
primary: 'bg-primary-container text-on-primary-container',
|
|
42
|
+
secondary: 'bg-secondary-container text-on-secondary-container',
|
|
43
|
+
tertiary: 'bg-tertiary-container text-on-tertiary-container',
|
|
44
|
+
}[props.color]))
|
|
45
|
+
|
|
46
|
+
const arrangementClass = computed(() => ({
|
|
47
|
+
start: 'justify-start',
|
|
48
|
+
between: 'justify-between',
|
|
49
|
+
around: 'justify-around',
|
|
50
|
+
evenly: 'justify-evenly',
|
|
51
|
+
fixed: 'justify-center gap-8',
|
|
52
|
+
}[props.arrangement ?? (props.flexible ? 'between' : 'start')]))
|
|
53
|
+
|
|
54
|
+
// ── exit-always ──────────────────────────────────────────────────────────────────────────────────
|
|
55
|
+
// BottomAppBarState: heightOffset runs from 0 (expanded) to -height (collapsed) and is applied to the
|
|
56
|
+
// bar's height, so the bar shrinks from its top edge and gives its space back to the layout. Compose
|
|
57
|
+
// feeds it the nested scroll's consumed delta; here it's the scroll target's own scroll delta, so the
|
|
58
|
+
// browser's momentum scrolling stands in for Compose's fling decay.
|
|
59
|
+
const outerEl = ref<HTMLElement>()
|
|
60
|
+
const innerEl = ref<HTMLElement>()
|
|
61
|
+
let offset = 0
|
|
62
|
+
let velocity = 0
|
|
63
|
+
let snapTarget: number | null = null
|
|
64
|
+
let raf = 0
|
|
65
|
+
let last = 0
|
|
66
|
+
let settleTimer = 0
|
|
67
|
+
let lastScroll = 0
|
|
68
|
+
let target: HTMLElement | Window | null = null
|
|
69
|
+
|
|
70
|
+
const limit = () => -(innerEl.value?.offsetHeight ?? 0)
|
|
71
|
+
|
|
72
|
+
function write() {
|
|
73
|
+
if (!outerEl.value) return
|
|
74
|
+
outerEl.value.style.height = offset === 0 ? '' : `${Math.max(0, (innerEl.value?.offsetHeight ?? 0) + offset)}px`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const scrollTop = () => (target instanceof Window ? target.scrollY : target?.scrollTop ?? 0)
|
|
78
|
+
|
|
79
|
+
function stopSnap() {
|
|
80
|
+
cancelAnimationFrame(raf)
|
|
81
|
+
raf = 0
|
|
82
|
+
snapTarget = null
|
|
83
|
+
velocity = 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function onScroll() {
|
|
87
|
+
const y = scrollTop()
|
|
88
|
+
const dy = y - lastScroll
|
|
89
|
+
lastScroll = y
|
|
90
|
+
if (!dy) return
|
|
91
|
+
stopSnap()
|
|
92
|
+
offset = Math.min(0, Math.max(limit(), offset - dy))
|
|
93
|
+
write()
|
|
94
|
+
clearTimeout(settleTimer)
|
|
95
|
+
settleTimer = window.setTimeout(settle, 150)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// settleAppBarBottom: a bar left half-way snaps to whichever end is closer, on the FastSpatial spring
|
|
99
|
+
function settle() {
|
|
100
|
+
const lim = limit()
|
|
101
|
+
if (!lim || offset === 0 || offset === lim) return
|
|
102
|
+
snapTarget = offset / lim < 0.5 ? 0 : lim
|
|
103
|
+
last = performance.now()
|
|
104
|
+
raf = requestAnimationFrame(frame)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function frame(now: number) {
|
|
108
|
+
if (snapTarget === null) return
|
|
109
|
+
let dt = Math.min((now - last) / 1000, 1 / 30)
|
|
110
|
+
last = now
|
|
111
|
+
const k = FAST_SPATIAL.stiffness
|
|
112
|
+
const c = 2 * FAST_SPATIAL.dampingRatio * Math.sqrt(k)
|
|
113
|
+
while (dt > 0) {
|
|
114
|
+
const h = Math.min(dt, 0.004)
|
|
115
|
+
dt -= h
|
|
116
|
+
velocity += (-k * (offset - snapTarget) - c * velocity) * h
|
|
117
|
+
offset += velocity * h
|
|
118
|
+
}
|
|
119
|
+
if (Math.abs(offset - snapTarget) < 0.5 && Math.abs(velocity) < 0.5) {
|
|
120
|
+
offset = snapTarget
|
|
121
|
+
write()
|
|
122
|
+
stopSnap()
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
write()
|
|
126
|
+
raf = requestAnimationFrame(frame)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function resolveTarget(): HTMLElement | Window | null {
|
|
130
|
+
if (typeof window === 'undefined') return null
|
|
131
|
+
if (!props.scrollTarget) return window
|
|
132
|
+
if (typeof props.scrollTarget === 'string') return document.querySelector<HTMLElement>(props.scrollTarget)
|
|
133
|
+
return props.scrollTarget
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function attach() {
|
|
137
|
+
detach()
|
|
138
|
+
if (props.scrollBehavior !== 'exit-always') return
|
|
139
|
+
target = resolveTarget()
|
|
140
|
+
if (!target) return
|
|
141
|
+
lastScroll = scrollTop()
|
|
142
|
+
target.addEventListener('scroll', onScroll, { passive: true })
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function detach() {
|
|
146
|
+
target?.removeEventListener('scroll', onScroll)
|
|
147
|
+
target = null
|
|
148
|
+
clearTimeout(settleTimer)
|
|
149
|
+
stopSnap()
|
|
150
|
+
offset = 0
|
|
151
|
+
write()
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
onMounted(attach)
|
|
155
|
+
onUnmounted(detach)
|
|
156
|
+
watch(() => [props.scrollBehavior, props.scrollTarget], attach)
|
|
157
|
+
</script>
|
|
158
|
+
|
|
159
|
+
<template>
|
|
160
|
+
<div ref="outerEl" class="w-full shrink-0 overflow-hidden" :class="colorClass">
|
|
161
|
+
<!-- Bottom safe area (windowInsets) below the bar's own height, which stays fixed while it collapses -->
|
|
162
|
+
<div ref="innerEl" style="padding-bottom: env(safe-area-inset-bottom)">
|
|
163
|
+
<!-- Standard: ContentPadding 4dp start/top/end (16dp minus the icon buttons' touch target).
|
|
164
|
+
Flexible: DockedToolbarTokens, 64dp tall, 16dp leading/trailing space. -->
|
|
165
|
+
<div class="flex items-center" :class="flexible ? ['h-16 px-4', arrangementClass] : 'h-20 px-1 pt-1'">
|
|
166
|
+
<template v-if="flexible">
|
|
167
|
+
<slot />
|
|
168
|
+
</template>
|
|
169
|
+
<template v-else>
|
|
170
|
+
<div class="flex min-w-0 flex-1 items-center" :class="arrangementClass">
|
|
171
|
+
<slot />
|
|
172
|
+
</div>
|
|
173
|
+
<!-- FAB 12dp from the top and 16dp from the end edge -->
|
|
174
|
+
<div v-if="$slots.fab" class="flex h-full shrink-0 items-start pt-2 pr-3">
|
|
175
|
+
<slot name="fab" />
|
|
176
|
+
</div>
|
|
177
|
+
</template>
|
|
178
|
+
</div>
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
</template>
|