@m3ui-vue/m3ui-vue 1.4.2 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API.md +1 -1
- package/dist/api.json +1 -1
- package/dist/m3ui.js +5869 -5788
- package/dist/m3ui.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/MTabs.test.ts +208 -2
- package/src/components/MTabs.vue +192 -79
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { mount } from '@vue/test-utils'
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
+
import { mount, flushPromises } from '@vue/test-utils'
|
|
3
3
|
import MTabs from '../components/MTabs.vue'
|
|
4
4
|
|
|
5
5
|
const tabs = [
|
|
@@ -36,4 +36,210 @@ describe('MTabs', () => {
|
|
|
36
36
|
await w.findAll('button')[1]!.trigger('click')
|
|
37
37
|
expect(w.emitted('update:modelValue')).toEqual([['hotels']])
|
|
38
38
|
})
|
|
39
|
+
|
|
40
|
+
describe('scrollable: keeps the active tab in view (ScrollableTabRow)', () => {
|
|
41
|
+
// A 390px row holding eight 120px tabs (960px of content): happy-dom has
|
|
42
|
+
// no layout, so the geometry is mocked on the prototype.
|
|
43
|
+
const many = Array.from({ length: 8 }, (_, i) => ({ value: i, label: `Tab ${i}` }))
|
|
44
|
+
const proto = HTMLElement.prototype
|
|
45
|
+
const saved = ['offsetLeft', 'offsetWidth', 'clientWidth', 'scrollWidth'].map(
|
|
46
|
+
(k) => [k, Object.getOwnPropertyDescriptor(proto, k)] as const,
|
|
47
|
+
)
|
|
48
|
+
const isTab = (el: HTMLElement) => el.tagName === 'BUTTON'
|
|
49
|
+
// Per-tab widths, editable mid-test to simulate a tab resizing after mount.
|
|
50
|
+
let widths: number[] = []
|
|
51
|
+
const tabIndex = (el: HTMLElement) => [...el.parentElement!.querySelectorAll(':scope > button')].indexOf(el)
|
|
52
|
+
function mockLayout(w: number[] = Array(8).fill(120)) {
|
|
53
|
+
widths = w
|
|
54
|
+
Object.defineProperty(proto, 'offsetLeft', {
|
|
55
|
+
configurable: true,
|
|
56
|
+
get(this: HTMLElement) { return isTab(this) ? widths.slice(0, tabIndex(this)).reduce((a, b) => a + b, 0) : 0 },
|
|
57
|
+
})
|
|
58
|
+
Object.defineProperty(proto, 'offsetWidth', { configurable: true, get(this: HTMLElement) { return isTab(this) ? widths[tabIndex(this)]! : 390 } })
|
|
59
|
+
Object.defineProperty(proto, 'clientWidth', { configurable: true, get() { return 390 } })
|
|
60
|
+
Object.defineProperty(proto, 'scrollWidth', {
|
|
61
|
+
configurable: true,
|
|
62
|
+
get(this: HTMLElement) { return this.querySelector('button') ? Math.max(390, widths.reduce((a, b) => a + b, 0)) : 390 },
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
function spyScroll() {
|
|
66
|
+
const calls: ScrollToOptions[] = []
|
|
67
|
+
vi.spyOn(proto, 'scrollTo').mockImplementation(function (this: HTMLElement, opts?: ScrollToOptions | number) {
|
|
68
|
+
if (typeof opts === 'object') { calls.push(opts); this.scrollLeft = opts.left ?? 0 }
|
|
69
|
+
} as typeof proto.scrollTo)
|
|
70
|
+
return calls
|
|
71
|
+
}
|
|
72
|
+
function mockReducedMotion(reduce: boolean) {
|
|
73
|
+
vi.stubGlobal('matchMedia', (q: string) => ({ matches: reduce && q.includes('reduce'), media: q, addEventListener() {}, removeEventListener() {} }))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
afterEach(() => {
|
|
77
|
+
for (const [k, d] of saved) {
|
|
78
|
+
if (d) Object.defineProperty(proto, k, d)
|
|
79
|
+
else delete (proto as unknown as Record<string, unknown>)[k]
|
|
80
|
+
}
|
|
81
|
+
vi.restoreAllMocks()
|
|
82
|
+
vi.unstubAllGlobals()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('scrolls the row (not the page) to the active tab on mount, without animation', async () => {
|
|
86
|
+
mockLayout(); mockReducedMotion(false)
|
|
87
|
+
const calls = spyScroll()
|
|
88
|
+
const intoView = vi.fn()
|
|
89
|
+
vi.spyOn(proto, 'scrollIntoView').mockImplementation(intoView)
|
|
90
|
+
mount(MTabs, { props: { modelValue: 4, tabs: many, scrollable: true } })
|
|
91
|
+
await flushPromises()
|
|
92
|
+
// tab 4 spans 480–600: centered → 540 - 195 = 345
|
|
93
|
+
expect(calls).toEqual([{ left: 345, behavior: 'auto' }])
|
|
94
|
+
expect(intoView).not.toHaveBeenCalled()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('smooth-scrolls to the new tab when modelValue changes, clamped to the row ends', async () => {
|
|
98
|
+
mockLayout(); mockReducedMotion(false)
|
|
99
|
+
const calls = spyScroll()
|
|
100
|
+
const w = mount(MTabs, { props: { modelValue: 0, tabs: many, scrollable: true } })
|
|
101
|
+
await flushPromises()
|
|
102
|
+
expect(calls).toEqual([]) // first tab already in view
|
|
103
|
+
await w.setProps({ modelValue: 7 })
|
|
104
|
+
await flushPromises()
|
|
105
|
+
expect(calls).toEqual([{ left: 960 - 390, behavior: 'smooth' }])
|
|
106
|
+
await w.setProps({ modelValue: 1 })
|
|
107
|
+
await flushPromises()
|
|
108
|
+
expect(calls[1]).toEqual({ left: 0, behavior: 'smooth' })
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('jumps instead of animating with prefers-reduced-motion', async () => {
|
|
112
|
+
mockLayout(); mockReducedMotion(true)
|
|
113
|
+
const calls = spyScroll()
|
|
114
|
+
const w = mount(MTabs, { props: { modelValue: 0, tabs: many, scrollable: true } })
|
|
115
|
+
await flushPromises()
|
|
116
|
+
await w.setProps({ modelValue: 5 })
|
|
117
|
+
await flushPromises()
|
|
118
|
+
expect(calls).toEqual([{ left: 465, behavior: 'auto' }])
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('does not pull the row back when an unchanged tabs array is passed again', async () => {
|
|
122
|
+
mockLayout(); mockReducedMotion(false)
|
|
123
|
+
const calls = spyScroll()
|
|
124
|
+
const w = mount(MTabs, { props: { modelValue: 4, tabs: many, scrollable: true } })
|
|
125
|
+
await flushPromises()
|
|
126
|
+
await w.setProps({ tabs: many.map((t) => ({ ...t })) })
|
|
127
|
+
await flushPromises()
|
|
128
|
+
expect(calls).toHaveLength(1)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('never scrolls a fixed (non-scrollable) row', async () => {
|
|
132
|
+
mockLayout(); mockReducedMotion(false)
|
|
133
|
+
const calls = spyScroll()
|
|
134
|
+
const w = mount(MTabs, { props: { modelValue: 4, tabs: many } })
|
|
135
|
+
await flushPromises()
|
|
136
|
+
await w.setProps({ modelValue: 7 })
|
|
137
|
+
await flushPromises()
|
|
138
|
+
expect(calls).toEqual([])
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
// A fake ResizeObserver the test can fire by hand (happy-dom's never reports).
|
|
142
|
+
class FakeRO {
|
|
143
|
+
static instances: FakeRO[] = []
|
|
144
|
+
targets = new Set<Element>()
|
|
145
|
+
disconnected = false
|
|
146
|
+
constructor(public cb: ResizeObserverCallback) { FakeRO.instances.push(this) }
|
|
147
|
+
observe(el: Element) { this.targets.add(el); this.fire([el]) }
|
|
148
|
+
unobserve(el: Element) { this.targets.delete(el) }
|
|
149
|
+
disconnect() { this.targets.clear(); this.disconnected = true }
|
|
150
|
+
fire(els: Element[] = [...this.targets]) {
|
|
151
|
+
const entries = els.map((target) => {
|
|
152
|
+
const el = target as HTMLElement
|
|
153
|
+
return { target, contentRect: { width: el.offsetWidth, height: 48 } } as unknown as ResizeObserverEntry
|
|
154
|
+
})
|
|
155
|
+
this.cb(entries, this as unknown as ResizeObserver)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function stubRO() { FakeRO.instances = []; vi.stubGlobal('ResizeObserver', FakeRO) }
|
|
159
|
+
const indicator = (w: ReturnType<typeof mount>) => {
|
|
160
|
+
const el = w.find('button').element.parentElement!.querySelector(':scope > div') as HTMLElement
|
|
161
|
+
return { left: parseFloat(el.style.left), width: parseFloat(el.style.width) }
|
|
162
|
+
}
|
|
163
|
+
// Icons drawn as their ligature names before the font loads: 300px tabs
|
|
164
|
+
const wide = () => [300, 300, 300, 300]
|
|
165
|
+
const iconTabs = Array.from({ length: 4 }, (_, i) => ({ value: i, label: `Tab ${i}`, icon: 'fact_check' }))
|
|
166
|
+
|
|
167
|
+
it('moves the indicator and re-centers, without animation, when the tabs resize after mount', async () => {
|
|
168
|
+
mockLayout(wide()); mockReducedMotion(false); stubRO()
|
|
169
|
+
const calls = spyScroll()
|
|
170
|
+
const w = mount(MTabs, { props: { modelValue: 3, tabs: iconTabs, scrollable: true } })
|
|
171
|
+
await flushPromises()
|
|
172
|
+
// tab 3 at 900–1200 (inset 16): indicator 916–1184; row scrolled to its end
|
|
173
|
+
expect(indicator(w)).toEqual({ left: 916, width: 268 })
|
|
174
|
+
expect(calls).toEqual([{ left: 1200 - 390, behavior: 'auto' }])
|
|
175
|
+
|
|
176
|
+
widths = [108, 108, 108, 108] // the font loaded: tabs shrink
|
|
177
|
+
FakeRO.instances[0]!.fire()
|
|
178
|
+
await flushPromises()
|
|
179
|
+
// tab 3 now at 324–432: indicator 340–416, row clamped to 432 - 390
|
|
180
|
+
expect(indicator(w)).toEqual({ left: 340, width: 76 })
|
|
181
|
+
expect(calls[1]).toEqual({ left: 42, behavior: 'auto' })
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('keeps the row where the user scrolled it by hand, but still moves the indicator', async () => {
|
|
185
|
+
mockLayout(wide()); mockReducedMotion(false); stubRO()
|
|
186
|
+
const calls = spyScroll()
|
|
187
|
+
const w = mount(MTabs, { props: { modelValue: 3, tabs: iconTabs, scrollable: true } })
|
|
188
|
+
await flushPromises()
|
|
189
|
+
const row = w.find('button').element.parentElement!.parentElement!
|
|
190
|
+
row.dispatchEvent(new Event('wheel'))
|
|
191
|
+
widths = [108, 108, 108, 108]
|
|
192
|
+
FakeRO.instances[0]!.fire()
|
|
193
|
+
await flushPromises()
|
|
194
|
+
expect(indicator(w)).toEqual({ left: 340, width: 76 })
|
|
195
|
+
expect(calls).toHaveLength(1)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('ignores the first report of a newly observed element (no snap of a running animation)', async () => {
|
|
199
|
+
mockLayout(); mockReducedMotion(false); stubRO()
|
|
200
|
+
const w = mount(MTabs, { props: { modelValue: 0, tabs: many, scrollable: true } })
|
|
201
|
+
await flushPromises()
|
|
202
|
+
const ro = FakeRO.instances[0]!
|
|
203
|
+
widths = widths.map(() => 200)
|
|
204
|
+
const report = (el: Element) => ro.cb([{ target: el, contentRect: { width: 200, height: 48 } } as unknown as ResizeObserverEntry], ro as unknown as ResizeObserver)
|
|
205
|
+
report(document.createElement('button')) // first sight of an element
|
|
206
|
+
await flushPromises()
|
|
207
|
+
expect(indicator(w)).toEqual({ left: 16, width: 88 })
|
|
208
|
+
report(w.find('button').element) // a known tab that changed size
|
|
209
|
+
await flushPromises()
|
|
210
|
+
expect(indicator(w)).toEqual({ left: 16, width: 168 })
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('re-measures once fonts are ready', async () => {
|
|
214
|
+
mockLayout(wide()); mockReducedMotion(false)
|
|
215
|
+
let resolveFonts!: () => void
|
|
216
|
+
const ready = new Promise<void>((r) => { resolveFonts = r })
|
|
217
|
+
Object.defineProperty(document, 'fonts', { configurable: true, value: { ready } })
|
|
218
|
+
try {
|
|
219
|
+
const w = mount(MTabs, { props: { modelValue: 1, tabs: iconTabs, scrollable: true } })
|
|
220
|
+
await flushPromises()
|
|
221
|
+
expect(indicator(w)).toEqual({ left: 316, width: 268 })
|
|
222
|
+
widths = [108, 108, 108, 108]
|
|
223
|
+
resolveFonts()
|
|
224
|
+
await flushPromises()
|
|
225
|
+
expect(indicator(w)).toEqual({ left: 124, width: 76 })
|
|
226
|
+
} finally {
|
|
227
|
+
delete (document as unknown as Record<string, unknown>).fonts
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('keeps the indicator inside a clipping box the width of the tabs, and disconnects on unmount', async () => {
|
|
232
|
+
mockLayout(); mockReducedMotion(false); stubRO()
|
|
233
|
+
const w = mount(MTabs, { props: { modelValue: 0, tabs: many, scrollable: true } })
|
|
234
|
+
await flushPromises()
|
|
235
|
+
const box = w.find('button').element.parentElement!
|
|
236
|
+
expect(box.querySelector(':scope > div.absolute')).not.toBeNull()
|
|
237
|
+
expect(box.classList).toContain('overflow-hidden')
|
|
238
|
+
expect(box.classList).toContain('w-max')
|
|
239
|
+
const ro = FakeRO.instances[0]!
|
|
240
|
+
expect(ro.targets.size).toBe(9) // the row + 8 tabs
|
|
241
|
+
w.unmount()
|
|
242
|
+
expect(ro.disconnected).toBe(true)
|
|
243
|
+
})
|
|
244
|
+
})
|
|
39
245
|
})
|
package/src/components/MTabs.vue
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @event update:modelValue Emitted with the value of the tab you picked.
|
|
7
7
|
*/
|
|
8
|
-
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
|
|
8
|
+
import { computed, nextTick, onMounted, onUnmounted, onUpdated, ref, watch } from 'vue'
|
|
9
9
|
import MIcon from './MIcon.vue'
|
|
10
10
|
|
|
11
11
|
interface Tab {
|
|
@@ -40,6 +40,8 @@ const leading = computed(() => (props.iconPosition ?? (props.variant === 'primar
|
|
|
40
40
|
const emit = defineEmits<{ 'update:modelValue': [string | number] }>()
|
|
41
41
|
|
|
42
42
|
const tabEls = ref<HTMLElement[]>([])
|
|
43
|
+
const rowEl = ref<HTMLElement | null>(null)
|
|
44
|
+
const contentEl = ref<HTMLElement | null>(null)
|
|
43
45
|
const indicatorLeft = ref(0)
|
|
44
46
|
const indicatorWidth = ref(0)
|
|
45
47
|
|
|
@@ -98,41 +100,146 @@ function ensureRaf() {
|
|
|
98
100
|
rafId = requestAnimationFrame(tick)
|
|
99
101
|
}
|
|
100
102
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
103
|
+
// ScrollableTabRow keeps the selected tab in view: on every selection change it
|
|
104
|
+
// animates the row's scroll so the tab is centered, clamped to the row's ends
|
|
105
|
+
// (Compose's `calculateTabOffset`). Only the row scrolls, never the page, so
|
|
106
|
+
// no scrollIntoView.
|
|
107
|
+
function scrollToTab(rect: { left: number; width: number }, smooth: boolean) {
|
|
108
|
+
const row = rowEl.value
|
|
109
|
+
if (!props.scrollable || !row) return
|
|
110
|
+
const max = row.scrollWidth - row.clientWidth
|
|
111
|
+
if (max <= 0) return
|
|
112
|
+
const left = Math.round(Math.min(Math.max(rect.left + rect.width / 2 - row.clientWidth / 2, 0), max))
|
|
113
|
+
if (Math.abs(left - row.scrollLeft) < 1) return
|
|
114
|
+
const reduce = typeof window !== 'undefined' && window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
|
|
115
|
+
const behavior: ScrollBehavior = smooth && !reduce ? 'smooth' : 'auto'
|
|
116
|
+
if (typeof row.scrollTo === 'function') row.scrollTo({ left, behavior })
|
|
117
|
+
else row.scrollLeft = left
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// The value whose tab was last brought into view: tabs that arrive after mount
|
|
121
|
+
// (loaded async) still get scrolled to once, without a later `:tabs` literal
|
|
122
|
+
// re-render pulling the row back from wherever the user scrolled it.
|
|
123
|
+
let shownValue: unknown = Symbol('none')
|
|
124
|
+
|
|
125
|
+
function measureTarget() {
|
|
126
|
+
const idx = props.tabs.findIndex((t) => t.value === props.modelValue)
|
|
127
|
+
const rect = getTabRect(idx)
|
|
128
|
+
if (!rect) return null
|
|
129
|
+
if (props.variant === 'primary') {
|
|
130
|
+
const inset = Math.min(rect.width * 0.2, 16)
|
|
131
|
+
return { rect, offset: rect.left + inset, width: rect.width - inset * 2 }
|
|
132
|
+
}
|
|
133
|
+
return { rect, offset: rect.left, width: rect.width }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function snapIndicator() {
|
|
137
|
+
offsetPos = offsetTarget; offsetVel = 0
|
|
138
|
+
widthPos = widthTarget; widthVel = 0
|
|
139
|
+
indicatorLeft.value = offsetPos
|
|
140
|
+
indicatorWidth.value = widthPos
|
|
141
|
+
}
|
|
115
142
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
143
|
+
function updateIndicator(animate = true, scroll: 'none' | 'instant' | 'smooth' = 'none') {
|
|
144
|
+
nextTick(() => {
|
|
145
|
+
const m = measureTarget()
|
|
146
|
+
if (!m) return
|
|
147
|
+
if (scroll !== 'none') {
|
|
148
|
+
shownValue = props.modelValue
|
|
149
|
+
if (scroll === 'smooth') manualScroll = false
|
|
150
|
+
scrollToTab(m.rect, scroll === 'smooth')
|
|
123
151
|
}
|
|
152
|
+
offsetTarget = m.offset
|
|
153
|
+
widthTarget = m.width
|
|
154
|
+
if (animate) ensureRaf()
|
|
155
|
+
else snapIndicator()
|
|
124
156
|
})
|
|
125
157
|
}
|
|
126
158
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
159
|
+
// Tabs can change size after mount: a Material Symbols icon is drawn as its
|
|
160
|
+
// ligature name ("fact_check") until the font loads, so the tab is far wider
|
|
161
|
+
// at first. Re-measure without animation whenever the row or a tab resizes,
|
|
162
|
+
// and re-center the active tab unless the user has scrolled the row by hand
|
|
163
|
+
// since it was last centered for them.
|
|
164
|
+
let manualScroll = false
|
|
165
|
+
function markManualScroll() { manualScroll = true }
|
|
166
|
+
const SCROLL_KEYS = new Set(['ArrowLeft', 'ArrowRight', 'Home', 'End', 'PageUp', 'PageDown'])
|
|
167
|
+
function onRowKeydown(e: KeyboardEvent) { if (SCROLL_KEYS.has(e.key)) manualScroll = true }
|
|
168
|
+
|
|
169
|
+
function relayout() {
|
|
170
|
+
const m = measureTarget()
|
|
171
|
+
if (!m) return
|
|
172
|
+
if (Math.abs(m.offset - offsetTarget) >= 0.5 || Math.abs(m.width - widthTarget) >= 0.5) {
|
|
173
|
+
offsetTarget = m.offset
|
|
174
|
+
widthTarget = m.width
|
|
175
|
+
snapIndicator()
|
|
176
|
+
}
|
|
177
|
+
if (!manualScroll) scrollToTab(m.rect, false)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let ro: ResizeObserver | null = null
|
|
181
|
+
const observed = new Set<Element>()
|
|
182
|
+
const lastSize = new WeakMap<Element, string>()
|
|
183
|
+
let listenedRow: HTMLElement | null = null
|
|
184
|
+
|
|
185
|
+
function onResize(entries: ResizeObserverEntry[]) {
|
|
186
|
+
let changed = false
|
|
187
|
+
for (const e of entries) {
|
|
188
|
+
const key = `${e.contentRect.width}x${e.contentRect.height}`
|
|
189
|
+
// The first notification of every newly observed element only reports
|
|
190
|
+
// its current size: ignore it, so it can't cut short a running animation.
|
|
191
|
+
if (lastSize.has(e.target) && lastSize.get(e.target) !== key) changed = true
|
|
192
|
+
lastSize.set(e.target, key)
|
|
193
|
+
}
|
|
194
|
+
if (changed) relayout()
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function syncObserved() {
|
|
198
|
+
const row = rowEl.value
|
|
199
|
+
if (row !== listenedRow) {
|
|
200
|
+
listenedRow?.removeEventListener('wheel', markManualScroll)
|
|
201
|
+
listenedRow?.removeEventListener('touchmove', markManualScroll)
|
|
202
|
+
listenedRow?.removeEventListener('keydown', onRowKeydown)
|
|
203
|
+
row?.addEventListener('wheel', markManualScroll, { passive: true })
|
|
204
|
+
row?.addEventListener('touchmove', markManualScroll, { passive: true })
|
|
205
|
+
row?.addEventListener('keydown', onRowKeydown)
|
|
206
|
+
listenedRow = row
|
|
207
|
+
}
|
|
208
|
+
if (!ro) return
|
|
209
|
+
const want = new Set<Element>()
|
|
210
|
+
if (row) want.add(row)
|
|
211
|
+
if (contentEl.value) for (const el of contentEl.value.querySelectorAll(':scope > button')) want.add(el)
|
|
212
|
+
for (const el of observed) if (!want.has(el)) { ro.unobserve(el); observed.delete(el) }
|
|
213
|
+
for (const el of want) if (!observed.has(el)) { ro.observe(el); observed.add(el) }
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
let mounted = false
|
|
217
|
+
onMounted(() => {
|
|
218
|
+
mounted = true
|
|
219
|
+
updateIndicator(false, 'instant')
|
|
220
|
+
if (typeof ResizeObserver !== 'undefined') ro = new ResizeObserver(onResize)
|
|
221
|
+
syncObserved()
|
|
222
|
+
// Fallback for browsers or layouts where the font swap doesn't resize an
|
|
223
|
+
// observed box (and for the first paint before the observer reports).
|
|
224
|
+
document.fonts?.ready.then(() => { if (mounted) relayout() })
|
|
225
|
+
})
|
|
226
|
+
onUpdated(syncObserved)
|
|
227
|
+
onUnmounted(() => {
|
|
228
|
+
mounted = false
|
|
229
|
+
if (rafId) cancelAnimationFrame(rafId)
|
|
230
|
+
ro?.disconnect(); ro = null; observed.clear()
|
|
231
|
+
listenedRow?.removeEventListener('wheel', markManualScroll)
|
|
232
|
+
listenedRow?.removeEventListener('touchmove', markManualScroll)
|
|
233
|
+
listenedRow?.removeEventListener('keydown', onRowKeydown)
|
|
234
|
+
listenedRow = null
|
|
235
|
+
})
|
|
236
|
+
watch(() => props.modelValue, () => updateIndicator(true, 'smooth'))
|
|
130
237
|
// Animated (not a hard snap) here too: a consumer passing an inline `:tabs="[...]"`
|
|
131
238
|
// literal gets a brand-new array reference on every unrelated parent re-render,
|
|
132
239
|
// which would otherwise race with — and instantly override — an in-flight
|
|
133
240
|
// modelValue-triggered animation. Re-running the same animated update is a
|
|
134
241
|
// no-op when the target hasn't actually moved, so this is safe either way.
|
|
135
|
-
watch(() => props.tabs, () => updateIndicator(true), { deep: true })
|
|
242
|
+
watch(() => props.tabs, () => updateIndicator(true, shownValue === props.modelValue ? 'none' : 'instant'), { deep: true })
|
|
136
243
|
watch(leading, () => updateIndicator(true))
|
|
137
244
|
|
|
138
245
|
function select(tab: Tab) {
|
|
@@ -143,64 +250,70 @@ function select(tab: Tab) {
|
|
|
143
250
|
<template>
|
|
144
251
|
<!-- ── Primary: sliding short indicator with stretch ──────────────── -->
|
|
145
252
|
<div v-if="variant === 'primary'" class="border-b border-outline-variant">
|
|
146
|
-
<div
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
? '
|
|
159
|
-
|
|
160
|
-
? '
|
|
161
|
-
:
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
253
|
+
<div ref="rowEl" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
|
|
254
|
+
<!-- Content-width box: clips the indicator so it never widens the scroll area -->
|
|
255
|
+
<div ref="contentEl" class="relative flex overflow-hidden" :class="scrollable ? 'w-max min-w-full' : 'w-full'">
|
|
256
|
+
<button
|
|
257
|
+
v-for="tab in tabs"
|
|
258
|
+
:key="tab.value"
|
|
259
|
+
:ref="(el) => { if (el) tabEls[tabs.indexOf(tab)] = el as HTMLElement }"
|
|
260
|
+
type="button"
|
|
261
|
+
class="relative flex items-center justify-center pb-1.5 text-title-small transition-colors duration-150 focus-visible:outline-none"
|
|
262
|
+
:class="[
|
|
263
|
+
scrollable ? 'shrink-0' : 'flex-1 min-w-0',
|
|
264
|
+
// LeadingIconTab: 48dp row, 16dp side padding, 8dp from the icon to the label
|
|
265
|
+
leading ? 'h-12 flex-row gap-2 px-4' : ['flex-col gap-1 px-6', tab.icon ? 'h-16' : 'h-12'],
|
|
266
|
+
tab.value === modelValue
|
|
267
|
+
? 'text-primary'
|
|
268
|
+
: tab.disabled
|
|
269
|
+
? 'cursor-not-allowed text-on-surface/38'
|
|
270
|
+
: 'cursor-pointer text-on-surface-variant hover:text-on-surface',
|
|
271
|
+
]"
|
|
272
|
+
:disabled="tab.disabled"
|
|
273
|
+
@click="select(tab)"
|
|
274
|
+
>
|
|
275
|
+
<MIcon v-if="tab.icon" :name="tab.icon" :size="24" />
|
|
276
|
+
<span>{{ tab.label }}</span>
|
|
277
|
+
</button>
|
|
278
|
+
<div
|
|
279
|
+
class="absolute bottom-0 h-[3px] rounded-t-full bg-primary"
|
|
280
|
+
:style="{ left: `${indicatorLeft}px`, width: `${indicatorWidth}px` }"
|
|
281
|
+
/>
|
|
282
|
+
</div>
|
|
173
283
|
</div>
|
|
174
284
|
</div>
|
|
175
285
|
|
|
176
286
|
<!-- ── Secondary: full-width underline indicator ─────────────────── -->
|
|
177
287
|
<div v-else class="border-b border-outline-variant">
|
|
178
|
-
<div
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
? '
|
|
190
|
-
|
|
191
|
-
? '
|
|
192
|
-
:
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
288
|
+
<div ref="rowEl" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
|
|
289
|
+
<!-- Content-width box: clips the indicator so it never widens the scroll area -->
|
|
290
|
+
<div ref="contentEl" class="relative flex overflow-hidden" :class="scrollable ? 'w-max min-w-full' : 'w-full'">
|
|
291
|
+
<button
|
|
292
|
+
v-for="tab in tabs"
|
|
293
|
+
:key="tab.value"
|
|
294
|
+
:ref="(el) => { if (el) tabEls[tabs.indexOf(tab)] = el as HTMLElement }"
|
|
295
|
+
type="button"
|
|
296
|
+
class="relative flex items-center justify-center text-title-small transition-colors duration-150 focus-visible:outline-none"
|
|
297
|
+
:class="[
|
|
298
|
+
scrollable ? 'shrink-0' : 'flex-1 min-w-0',
|
|
299
|
+
leading ? 'h-12 flex-row gap-2 px-4' : ['flex-col gap-1 px-6', tab.icon ? 'h-16' : 'h-12'],
|
|
300
|
+
tab.value === modelValue
|
|
301
|
+
? 'text-on-surface'
|
|
302
|
+
: tab.disabled
|
|
303
|
+
? 'cursor-not-allowed text-on-surface/38'
|
|
304
|
+
: 'cursor-pointer text-on-surface-variant hover:text-on-surface',
|
|
305
|
+
]"
|
|
306
|
+
:disabled="tab.disabled"
|
|
307
|
+
@click="select(tab)"
|
|
308
|
+
>
|
|
309
|
+
<MIcon v-if="tab.icon" :name="tab.icon" :size="24" />
|
|
310
|
+
<span>{{ tab.label }}</span>
|
|
311
|
+
</button>
|
|
312
|
+
<div
|
|
313
|
+
class="absolute bottom-0 h-[3px] bg-primary"
|
|
314
|
+
:style="{ left: `${indicatorLeft}px`, width: `${indicatorWidth}px` }"
|
|
315
|
+
/>
|
|
316
|
+
</div>
|
|
204
317
|
</div>
|
|
205
318
|
</div>
|
|
206
319
|
</template>
|