@m3ui-vue/m3ui-vue 0.9.2 → 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.
Files changed (139) hide show
  1. package/README.md +26 -29
  2. package/dist/API.md +242 -22
  3. package/dist/{MIcon-CaEooCmZ.js → MIcon-DLOkQduI.js} +10 -3
  4. package/dist/MIcon-DLOkQduI.js.map +1 -0
  5. package/dist/{MTextField-Ck0oWDMk.js → MTextField-TZ7lfr09.js} +551 -216
  6. package/dist/MTextField-TZ7lfr09.js.map +1 -0
  7. package/dist/THIRD_PARTY_LICENSES.md +547 -0
  8. package/dist/api.json +1 -1
  9. package/dist/code-editor.js +2 -2
  10. package/dist/components/MAppBarWithSearch.vue.d.ts +70 -0
  11. package/dist/components/MBottomAppBar.vue.d.ts +37 -0
  12. package/dist/components/MBottomSheetScaffold.vue.d.ts +40 -0
  13. package/dist/components/MButton.vue.d.ts +4 -2
  14. package/dist/components/MButtonGroup.vue.d.ts +2 -0
  15. package/dist/components/MChatBubble.vue.d.ts +1 -1
  16. package/dist/components/MChip.vue.d.ts +19 -2
  17. package/dist/components/MContainer.vue.d.ts +1 -1
  18. package/dist/components/MDataTable.vue.d.ts +1 -1
  19. package/dist/components/MDialog.vue.d.ts +1 -1
  20. package/dist/components/MFab.vue.d.ts +15 -4
  21. package/dist/components/MIcon.vue.d.ts +3 -0
  22. package/dist/components/MIconButton.vue.d.ts +15 -1
  23. package/dist/components/MListItem.vue.d.ts +2 -2
  24. package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
  25. package/dist/components/MMenuItem.vue.d.ts +17 -4
  26. package/dist/components/MNavigationRail.vue.d.ts +6 -0
  27. package/dist/components/MSearchBar.vue.d.ts +5 -3
  28. package/dist/components/MShortNavigationBar.vue.d.ts +1 -1
  29. package/dist/components/MSplitButton.vue.d.ts +2 -0
  30. package/dist/components/MTable.vue.d.ts +1 -1
  31. package/dist/components/MTabs.vue.d.ts +6 -0
  32. package/dist/components/MTimePickerModal.vue.d.ts +3 -0
  33. package/dist/components/MTimeScroll.vue.d.ts +22 -0
  34. package/dist/components/MToggleButton.vue.d.ts +2 -0
  35. package/dist/components/MTopAppBar.vue.d.ts +33 -2
  36. package/dist/components/MTree.vue.d.ts +2 -2
  37. package/dist/components/_MDrawerItemList.vue.d.ts +3 -0
  38. package/dist/composables/_searchAppBar.d.ts +6 -0
  39. package/dist/composables/_themeTransition.d.ts +2 -0
  40. package/dist/composables/_useSpring.d.ts +5 -5
  41. package/dist/composables/motion.d.ts +101 -0
  42. package/dist/composables/useColorPalette.d.ts +10 -0
  43. package/dist/composables/useLocale.d.ts +9 -0
  44. package/dist/directives/vRipple.d.ts +16 -0
  45. package/dist/emoji-names-de-BphO5XpV.js.map +1 -1
  46. package/dist/emoji-names-en-9ZvMRvzi.js.map +1 -1
  47. package/dist/emoji-names-es-DvfWU3Pj.js.map +1 -1
  48. package/dist/emoji-names-fr-B-jhUlsP.js.map +1 -1
  49. package/dist/emoji-names-ja-l8cMFJo1.js.map +1 -1
  50. package/dist/emoji-names-ko-YP-o9G0c.js.map +1 -1
  51. package/dist/emoji-names-pt-DGj_Js4M.js.map +1 -1
  52. package/dist/emoji-names-zh-Dzecwtw_.js.map +1 -1
  53. package/dist/index.d.ts +11 -1
  54. package/dist/locales/index.js +63 -0
  55. package/dist/locales/index.js.map +1 -1
  56. package/dist/m3ui-vue.css +1 -1
  57. package/dist/m3ui.js +7716 -6768
  58. package/dist/m3ui.js.map +1 -1
  59. package/dist/plugin.d.ts +7 -0
  60. package/dist/rich-text-editor.js +22 -22
  61. package/dist/styles/palettes-contrast.css +7764 -0
  62. package/dist/styles/theme.css +59 -2
  63. package/dist/styles.css +1 -1
  64. package/dist/terminal.js +1 -1
  65. package/dist/{useLocale-CFW8cIzL.js → useLocale-CuvWRZ1h.js} +10 -1
  66. package/dist/useLocale-CuvWRZ1h.js.map +1 -0
  67. package/package.json +4 -2
  68. package/src/__tests__/MAppBarWithSearch.test.ts +96 -0
  69. package/src/__tests__/MBottomAppBar.test.ts +71 -0
  70. package/src/__tests__/MBottomSheetScaffold.test.ts +101 -0
  71. package/src/__tests__/MChip.test.ts +63 -0
  72. package/src/__tests__/MFab.test.ts +36 -0
  73. package/src/__tests__/MIconButton.test.ts +60 -0
  74. package/src/__tests__/MMenuItem.test.ts +84 -0
  75. package/src/__tests__/MNavigationRail.test.ts +77 -0
  76. package/src/__tests__/MScrollField.test.ts +28 -0
  77. package/src/__tests__/MTabs.test.ts +39 -0
  78. package/src/__tests__/MTimePickerModal.test.ts +52 -0
  79. package/src/__tests__/MTimeScroll.test.ts +44 -0
  80. package/src/__tests__/MTopAppBar.test.ts +99 -0
  81. package/src/__tests__/contrast.test.ts +64 -0
  82. package/src/__tests__/dynamicColor.test.ts +17 -0
  83. package/src/__tests__/motion.test.ts +37 -0
  84. package/src/__tests__/themeTransition.test.ts +119 -0
  85. package/src/__tests__/vRipple.test.ts +92 -0
  86. package/src/components/MAppBarWithSearch.vue +236 -0
  87. package/src/components/MBottomAppBar.vue +181 -0
  88. package/src/components/MBottomSheetScaffold.vue +296 -0
  89. package/src/components/MBreadcrumbs.vue +1 -1
  90. package/src/components/MButton.vue +4 -13
  91. package/src/components/MButtonGroup.vue +4 -11
  92. package/src/components/MChip.vue +137 -5
  93. package/src/components/MFab.vue +81 -27
  94. package/src/components/MIcon.vue +4 -2
  95. package/src/components/MIconButton.vue +96 -24
  96. package/src/components/MMenuItem.vue +115 -13
  97. package/src/components/MNavigationDrawer.vue +2 -2
  98. package/src/components/MNavigationRail.vue +135 -8
  99. package/src/components/MScrollField.vue +10 -2
  100. package/src/components/MSearchBar.vue +28 -12
  101. package/src/components/MSplitButton.vue +5 -12
  102. package/src/components/MTabs.vue +16 -5
  103. package/src/components/MTimePickerModal.vue +48 -8
  104. package/src/components/MTimeScroll.vue +161 -0
  105. package/src/components/MToggleButton.vue +5 -12
  106. package/src/components/MTopAppBar.vue +252 -28
  107. package/src/composables/_carouselStrategy.ts +3 -0
  108. package/src/composables/_searchAppBar.ts +9 -0
  109. package/src/composables/_themeColor.ts +11 -4
  110. package/src/composables/_themeTransition.ts +71 -0
  111. package/src/composables/_useSpring.ts +12 -20
  112. package/src/composables/motion.ts +73 -0
  113. package/src/composables/useColorPalette.ts +103 -20
  114. package/src/composables/useLocale.ts +18 -0
  115. package/src/composables/useTheme.ts +4 -12
  116. package/src/data/emoji-names/emoji-names-de.ts +1 -0
  117. package/src/data/emoji-names/emoji-names-en.ts +1 -0
  118. package/src/data/emoji-names/emoji-names-es.ts +1 -0
  119. package/src/data/emoji-names/emoji-names-fr.ts +1 -0
  120. package/src/data/emoji-names/emoji-names-ja.ts +1 -0
  121. package/src/data/emoji-names/emoji-names-ko.ts +1 -0
  122. package/src/data/emoji-names/emoji-names-pt.ts +1 -0
  123. package/src/data/emoji-names/emoji-names-zh.ts +1 -0
  124. package/src/data/emojis.ts +1 -0
  125. package/src/directives/vRipple.ts +73 -0
  126. package/src/index.ts +13 -1
  127. package/src/locales/de.ts +9 -0
  128. package/src/locales/es.ts +9 -0
  129. package/src/locales/fr.ts +9 -0
  130. package/src/locales/ja.ts +9 -0
  131. package/src/locales/ko.ts +9 -0
  132. package/src/locales/pt.ts +9 -0
  133. package/src/locales/zh.ts +9 -0
  134. package/src/plugin.ts +11 -1
  135. package/src/styles/palettes-contrast.css +7764 -0
  136. package/src/styles/theme.css +59 -2
  137. package/dist/MIcon-CaEooCmZ.js.map +0 -1
  138. package/dist/MTextField-Ck0oWDMk.js.map +0 -1
  139. package/dist/useLocale-CFW8cIzL.js.map +0 -1
@@ -162,6 +162,23 @@ describe('createM3UI themeColor', () => {
162
162
  document.documentElement.style.setProperty('--color-surface', '#fef7ff')
163
163
  })
164
164
 
165
+ // Reproduces the old observer's miss only when run alone (-t): the seed tests above leave theme-transitioning
166
+ // timers that touch <html> later and update the meta anyway
167
+ it('follows a generated palette rewritten in place (same seed id, new contrast)', async () => {
168
+ const { createM3UI } = await fresh()
169
+ createApp({}).use(createM3UI({ themeColor: 'surface' }))
170
+ const meta = () => document.querySelector<HTMLMetaElement>('meta[name="theme-color"]')!
171
+ const style = document.createElement('style')
172
+ style.id = 'm3-dynamic-palette'
173
+ document.head.appendChild(style)
174
+ await settle()
175
+ // Only the <style>'s text changes; <html>'s attributes stay the same
176
+ document.documentElement.style.setProperty('--color-surface', '#faf8ff')
177
+ style.textContent = '[data-palette="seed"] { --color-surface: #faf8ff }'
178
+ await settle()
179
+ expect(meta().content).toBe('#faf8ff')
180
+ })
181
+
165
182
  it('creates the meta and follows the palette', async () => {
166
183
  const { createM3UI } = await fresh()
167
184
  createApp({}).use(createM3UI({ themeColor: 'surface' }))
@@ -0,0 +1,37 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { readFileSync } from 'node:fs'
3
+ import { resolve } from 'node:path'
4
+ import { cubicBezier, motionDurations, motionEasings, motionSprings } from '../composables/motion'
5
+ import { DEFAULT_SPATIAL, FAST_SPATIAL, fastOutSlowIn } from '../composables/_useSpring'
6
+
7
+ describe('motion tokens', () => {
8
+ it('evaluates cubic-bezier easings', () => {
9
+ const linear = cubicBezier(...motionEasings.linear)
10
+ expect(linear(0.3)).toBeCloseTo(0.3, 5)
11
+ const emphasized = cubicBezier(...motionEasings.emphasized)
12
+ expect(emphasized(0)).toBe(0)
13
+ expect(emphasized(1)).toBe(1)
14
+ // Front-loaded: well past halfway by the middle of the duration
15
+ expect(emphasized(0.5)).toBeGreaterThan(0.85)
16
+ expect(fastOutSlowIn(0.5)).toBeCloseTo(cubicBezier(0.4, 0, 0.2, 1)(0.5), 10)
17
+ })
18
+
19
+ it('keeps the internal spring shorthands on the Standard scheme', () => {
20
+ expect(FAST_SPATIAL).toEqual(motionSprings.standard.fastSpatial)
21
+ expect(DEFAULT_SPATIAL).toEqual({ dampingRatio: 0.9, stiffness: 700 })
22
+ expect(motionSprings.expressive.fastSpatial).toEqual({ dampingRatio: 0.6, stiffness: 800 })
23
+ })
24
+
25
+ it('matches the CSS variables in theme.css', () => {
26
+ const css = readFileSync(resolve(__dirname, '../styles/theme.css'), 'utf8')
27
+ for (const [name, ms] of Object.entries(motionDurations)) {
28
+ const cssName = name.replace(/([A-Z])/g, '-$1').toLowerCase().replace(/(\d)$/, '-$1')
29
+ expect(css).toContain(`--duration-${cssName}: ${ms}ms;`)
30
+ }
31
+ for (const [name, [x1, y1, x2, y2]] of Object.entries(motionEasings)) {
32
+ if (name === 'linear') continue
33
+ const cssName = name.replace(/([A-Z])/g, '-$1').toLowerCase()
34
+ expect(css).toContain(`--ease-${cssName}: cubic-bezier(${x1}, ${y1}, ${x2}, ${y2});`)
35
+ }
36
+ })
37
+ })
@@ -0,0 +1,119 @@
1
+ import { describe, it, expect, vi, afterEach } from 'vitest'
2
+ import { themeTransition } from '../composables/_themeTransition'
3
+
4
+ const root = document.documentElement
5
+ const style = () => document.getElementById('m3-theme-transition') as HTMLStyleElement | null
6
+
7
+ function mockReducedMotion(reduce: boolean) {
8
+ vi.spyOn(window, 'matchMedia').mockImplementation((query: string) => ({
9
+ matches: reduce && query.includes('reduce'),
10
+ media: query,
11
+ onchange: null,
12
+ addListener: () => {},
13
+ removeListener: () => {},
14
+ addEventListener: () => {},
15
+ removeEventListener: () => {},
16
+ dispatchEvent: () => false,
17
+ }) as MediaQueryList)
18
+ }
19
+
20
+ // happy-dom answers every CSS.supports() with true and has no CSS.registerProperty
21
+ function mockBrowser(browser: 'chromium' | 'firefox' | 'old') {
22
+ vi.stubGlobal('CSS', {
23
+ supports: (prop: string) => browser === 'firefox' && prop.startsWith('-moz-'),
24
+ registerProperty: browser === 'old' ? undefined : () => {},
25
+ })
26
+ }
27
+
28
+ describe('themeTransition', () => {
29
+ afterEach(() => {
30
+ vi.restoreAllMocks()
31
+ vi.unstubAllGlobals()
32
+ vi.useRealTimers()
33
+ root.classList.remove('theme-transitioning', 'theme-switching')
34
+ style()?.remove()
35
+ })
36
+
37
+ it('transitions the registered color tokens on <html> in Chromium, with components\' own transitions off', () => {
38
+ mockReducedMotion(false)
39
+ mockBrowser('chromium')
40
+ vi.useFakeTimers()
41
+ const apply = vi.fn(() => {
42
+ expect(root.classList.contains('theme-switching')).toBe(true)
43
+ expect(root.classList.contains('theme-transitioning')).toBe(false)
44
+ const css = style()!.textContent!
45
+ expect(css).toContain("@property --color-primary { syntax: '<color>'")
46
+ expect(css).toContain('@property --color-on-tertiary-fixed-variant')
47
+ expect(css).toContain('html.theme-switching *, html.theme-switching *::before, html.theme-switching *::after { transition: none !important; }')
48
+ })
49
+
50
+ themeTransition(apply)
51
+ expect(apply).toHaveBeenCalledOnce()
52
+ vi.advanceTimersByTime(300)
53
+ // Unregistered afterwards, so the tokens read back as written
54
+ expect(root.classList.contains('theme-switching')).toBe(false)
55
+ expect(style()).toBeNull()
56
+ })
57
+
58
+ it('keeps one transition going when switches overlap', () => {
59
+ mockReducedMotion(false)
60
+ mockBrowser('chromium')
61
+ vi.useFakeTimers()
62
+ themeTransition(() => {})
63
+ vi.advanceTimersByTime(200)
64
+ themeTransition(() => {})
65
+ vi.advanceTimersByTime(200)
66
+ expect(root.classList.contains('theme-switching')).toBe(true)
67
+ expect(document.querySelectorAll('#m3-theme-transition')).toHaveLength(1)
68
+ vi.advanceTimersByTime(100)
69
+ expect(root.classList.contains('theme-switching')).toBe(false)
70
+ })
71
+
72
+ for (const browser of ['firefox', 'old'] as const) {
73
+ it(`uses the theme-transitioning class ${browser === 'firefox' ? 'in Firefox' : 'without @property'}`, () => {
74
+ mockReducedMotion(false)
75
+ mockBrowser(browser)
76
+ vi.useFakeTimers()
77
+ const apply = vi.fn(() => {
78
+ expect(root.classList.contains('theme-transitioning')).toBe(true)
79
+ })
80
+
81
+ themeTransition(apply)
82
+ expect(apply).toHaveBeenCalledOnce()
83
+ expect(style()).toBeNull()
84
+ vi.advanceTimersByTime(300)
85
+ expect(root.classList.contains('theme-transitioning')).toBe(false)
86
+ })
87
+ }
88
+
89
+ it('takes Firefox by its user agent too', () => {
90
+ mockReducedMotion(false)
91
+ mockBrowser('chromium')
92
+ vi.spyOn(navigator, 'userAgent', 'get').mockReturnValue('Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0')
93
+ vi.useFakeTimers()
94
+ themeTransition(() => {})
95
+ expect(root.classList.contains('theme-transitioning')).toBe(true)
96
+ expect(style()).toBeNull()
97
+ })
98
+
99
+ it('does not take Chrome\'s "like Gecko" for Firefox', () => {
100
+ mockReducedMotion(false)
101
+ mockBrowser('chromium')
102
+ vi.spyOn(navigator, 'userAgent', 'get').mockReturnValue('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36')
103
+ vi.useFakeTimers()
104
+ themeTransition(() => {})
105
+ expect(root.classList.contains('theme-switching')).toBe(true)
106
+ })
107
+
108
+ it('applies directly with reduced motion', () => {
109
+ mockReducedMotion(true)
110
+ mockBrowser('chromium')
111
+ const apply = vi.fn()
112
+
113
+ themeTransition(apply)
114
+ expect(apply).toHaveBeenCalledOnce()
115
+ expect(style()).toBeNull()
116
+ expect(root.classList.contains('theme-switching')).toBe(false)
117
+ expect(root.classList.contains('theme-transitioning')).toBe(false)
118
+ })
119
+ })
@@ -0,0 +1,92 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, ref, withDirectives } from 'vue'
4
+ import { vRipple, type RippleValue } from '../directives/vRipple'
5
+ import MButton from '../components/MButton.vue'
6
+
7
+ function host(value: RippleValue | (() => RippleValue)) {
8
+ return mount(defineComponent({
9
+ setup() {
10
+ return () => withDirectives(h('div', { class: 'host' }), [[vRipple, typeof value === 'function' ? value() : value]])
11
+ },
12
+ }))
13
+ }
14
+
15
+ function press(el: Element, button = 0) {
16
+ el.dispatchEvent(new PointerEvent('pointerdown', { button, clientX: 10, clientY: 10, bubbles: true }))
17
+ }
18
+
19
+ describe('vRipple', () => {
20
+ it('spawns a ripple inside a clipping layer on press', () => {
21
+ const w = host(undefined)
22
+ press(w.element)
23
+ const layer = w.element.querySelector('.m3-ripple-layer')
24
+ expect(layer).not.toBeNull()
25
+ expect(layer!.querySelector('.m3-ripple')).not.toBeNull()
26
+ })
27
+
28
+ it('removes the layer when the animation ends', () => {
29
+ const w = host(true)
30
+ press(w.element)
31
+ w.element.querySelector('.m3-ripple')!.dispatchEvent(new Event('animationend'))
32
+ expect(w.element.querySelector('.m3-ripple-layer')).toBeNull()
33
+ })
34
+
35
+ it('makes a statically positioned host relative', () => {
36
+ const w = mount(defineComponent({
37
+ setup: () => () => withDirectives(h('div'), [[vRipple, true]]),
38
+ }), { attachTo: document.body })
39
+ press(w.element)
40
+ expect((w.element as HTMLElement).style.position).toBe('relative')
41
+ w.unmount()
42
+ })
43
+
44
+ it('does nothing when false or disabled', () => {
45
+ for (const value of [false, { disabled: true }] as RippleValue[]) {
46
+ const w = host(value)
47
+ press(w.element)
48
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
49
+ }
50
+ })
51
+
52
+ it('ignores secondary buttons', () => {
53
+ const w = host(true)
54
+ press(w.element, 2)
55
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
56
+ })
57
+
58
+ it('takes a color as a string or an option', () => {
59
+ for (const value of ['rgb(255, 0, 0)', { color: 'rgb(255, 0, 0)' }] as RippleValue[]) {
60
+ const w = host(value)
61
+ press(w.element)
62
+ expect((w.element.querySelector('.m3-ripple') as HTMLElement).style.background).toBe('rgb(255, 0, 0)')
63
+ }
64
+ })
65
+
66
+ it('follows binding updates', async () => {
67
+ const disabled = ref(true)
68
+ const w = mount(defineComponent({
69
+ setup() {
70
+ return () => withDirectives(h('div'), [[vRipple, { disabled: disabled.value }]])
71
+ },
72
+ }))
73
+ press(w.element)
74
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
75
+ disabled.value = false
76
+ await w.vm.$nextTick()
77
+ press(w.element)
78
+ expect(w.element.querySelector('.m3-ripple')).not.toBeNull()
79
+ })
80
+ })
81
+
82
+ describe('MButton ripple', () => {
83
+ it('uses rippleColor and skips the ripple while loading', async () => {
84
+ const w = mount(MButton, { props: { rippleColor: 'rgb(0, 0, 255)' }, slots: { default: 'Go' } })
85
+ press(w.element)
86
+ expect((w.element.querySelector('.m3-ripple') as HTMLElement).style.background).toBe('rgb(0, 0, 255)')
87
+ await w.setProps({ loading: true })
88
+ w.element.querySelectorAll('.m3-ripple-layer').forEach((l) => l.remove())
89
+ press(w.element)
90
+ expect(w.element.querySelector('.m3-ripple')).toBeNull()
91
+ })
92
+ })
@@ -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>