@m3ui-vue/m3ui-vue 1.4.2 → 1.4.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m3ui-vue/m3ui-vue",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "description": "Material 3 component library for Vue 3 + Tailwind CSS v4",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -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,102 @@ 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
+ function mockLayout() {
50
+ Object.defineProperty(proto, 'offsetLeft', {
51
+ configurable: true,
52
+ get(this: HTMLElement) { return isTab(this) ? [...this.parentElement!.children].indexOf(this) * 120 : 0 },
53
+ })
54
+ Object.defineProperty(proto, 'offsetWidth', { configurable: true, get(this: HTMLElement) { return isTab(this) ? 120 : 390 } })
55
+ Object.defineProperty(proto, 'clientWidth', { configurable: true, get() { return 390 } })
56
+ Object.defineProperty(proto, 'scrollWidth', {
57
+ configurable: true,
58
+ get(this: HTMLElement) { return this.querySelector(':scope > button') ? this.querySelectorAll(':scope > button').length * 120 : 390 },
59
+ })
60
+ }
61
+ function spyScroll() {
62
+ const calls: ScrollToOptions[] = []
63
+ vi.spyOn(proto, 'scrollTo').mockImplementation(function (this: HTMLElement, opts?: ScrollToOptions | number) {
64
+ if (typeof opts === 'object') { calls.push(opts); this.scrollLeft = opts.left ?? 0 }
65
+ } as typeof proto.scrollTo)
66
+ return calls
67
+ }
68
+ function mockReducedMotion(reduce: boolean) {
69
+ vi.stubGlobal('matchMedia', (q: string) => ({ matches: reduce && q.includes('reduce'), media: q, addEventListener() {}, removeEventListener() {} }))
70
+ }
71
+
72
+ afterEach(() => {
73
+ for (const [k, d] of saved) {
74
+ if (d) Object.defineProperty(proto, k, d)
75
+ else delete (proto as unknown as Record<string, unknown>)[k]
76
+ }
77
+ vi.restoreAllMocks()
78
+ vi.unstubAllGlobals()
79
+ })
80
+
81
+ it('scrolls the row (not the page) to the active tab on mount, without animation', async () => {
82
+ mockLayout(); mockReducedMotion(false)
83
+ const calls = spyScroll()
84
+ const intoView = vi.fn()
85
+ vi.spyOn(proto, 'scrollIntoView').mockImplementation(intoView)
86
+ mount(MTabs, { props: { modelValue: 4, tabs: many, scrollable: true } })
87
+ await flushPromises()
88
+ // tab 4 spans 480–600: centered → 540 - 195 = 345
89
+ expect(calls).toEqual([{ left: 345, behavior: 'auto' }])
90
+ expect(intoView).not.toHaveBeenCalled()
91
+ })
92
+
93
+ it('smooth-scrolls to the new tab when modelValue changes, clamped to the row ends', async () => {
94
+ mockLayout(); mockReducedMotion(false)
95
+ const calls = spyScroll()
96
+ const w = mount(MTabs, { props: { modelValue: 0, tabs: many, scrollable: true } })
97
+ await flushPromises()
98
+ expect(calls).toEqual([]) // first tab already in view
99
+ await w.setProps({ modelValue: 7 })
100
+ await flushPromises()
101
+ expect(calls).toEqual([{ left: 960 - 390, behavior: 'smooth' }])
102
+ await w.setProps({ modelValue: 1 })
103
+ await flushPromises()
104
+ expect(calls[1]).toEqual({ left: 0, behavior: 'smooth' })
105
+ })
106
+
107
+ it('jumps instead of animating with prefers-reduced-motion', async () => {
108
+ mockLayout(); mockReducedMotion(true)
109
+ const calls = spyScroll()
110
+ const w = mount(MTabs, { props: { modelValue: 0, tabs: many, scrollable: true } })
111
+ await flushPromises()
112
+ await w.setProps({ modelValue: 5 })
113
+ await flushPromises()
114
+ expect(calls).toEqual([{ left: 465, behavior: 'auto' }])
115
+ })
116
+
117
+ it('does not pull the row back when an unchanged tabs array is passed again', async () => {
118
+ mockLayout(); mockReducedMotion(false)
119
+ const calls = spyScroll()
120
+ const w = mount(MTabs, { props: { modelValue: 4, tabs: many, scrollable: true } })
121
+ await flushPromises()
122
+ await w.setProps({ tabs: many.map((t) => ({ ...t })) })
123
+ await flushPromises()
124
+ expect(calls).toHaveLength(1)
125
+ })
126
+
127
+ it('never scrolls a fixed (non-scrollable) row', async () => {
128
+ mockLayout(); mockReducedMotion(false)
129
+ const calls = spyScroll()
130
+ const w = mount(MTabs, { props: { modelValue: 4, tabs: many } })
131
+ await flushPromises()
132
+ await w.setProps({ modelValue: 7 })
133
+ await flushPromises()
134
+ expect(calls).toEqual([])
135
+ })
136
+ })
39
137
  })
@@ -40,6 +40,7 @@ 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)
43
44
  const indicatorLeft = ref(0)
44
45
  const indicatorWidth = ref(0)
45
46
 
@@ -98,11 +99,37 @@ function ensureRaf() {
98
99
  rafId = requestAnimationFrame(tick)
99
100
  }
100
101
 
101
- function updateIndicator(animate = true) {
102
+ // ScrollableTabRow keeps the selected tab in view: on every selection change it
103
+ // animates the row's scroll so the tab is centered, clamped to the row's ends
104
+ // (Compose's `calculateTabOffset`). Only the row scrolls, never the page, so
105
+ // no scrollIntoView.
106
+ function scrollToTab(rect: { left: number; width: number }, smooth: boolean) {
107
+ const row = rowEl.value
108
+ if (!props.scrollable || !row) return
109
+ const max = row.scrollWidth - row.clientWidth
110
+ if (max <= 0) return
111
+ const left = Math.round(Math.min(Math.max(rect.left + rect.width / 2 - row.clientWidth / 2, 0), max))
112
+ if (Math.abs(left - row.scrollLeft) < 1) return
113
+ const reduce = typeof window !== 'undefined' && window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
114
+ const behavior: ScrollBehavior = smooth && !reduce ? 'smooth' : 'auto'
115
+ if (typeof row.scrollTo === 'function') row.scrollTo({ left, behavior })
116
+ else row.scrollLeft = left
117
+ }
118
+
119
+ // The value whose tab was last brought into view: tabs that arrive after mount
120
+ // (loaded async) still get scrolled to once, without a later `:tabs` literal
121
+ // re-render pulling the row back from wherever the user scrolled it.
122
+ let shownValue: unknown = Symbol('none')
123
+
124
+ function updateIndicator(animate = true, scroll: 'none' | 'instant' | 'smooth' = 'none') {
102
125
  nextTick(() => {
103
126
  const idx = props.tabs.findIndex((t) => t.value === props.modelValue)
104
127
  const rect = getTabRect(idx)
105
128
  if (!rect) return
129
+ if (scroll !== 'none') {
130
+ shownValue = props.modelValue
131
+ scrollToTab(rect, scroll === 'smooth')
132
+ }
106
133
 
107
134
  if (props.variant === 'primary') {
108
135
  const inset = Math.min(rect.width * 0.2, 16)
@@ -124,15 +151,15 @@ function updateIndicator(animate = true) {
124
151
  })
125
152
  }
126
153
 
127
- onMounted(() => updateIndicator(false))
154
+ onMounted(() => updateIndicator(false, 'instant'))
128
155
  onUnmounted(() => { if (rafId) cancelAnimationFrame(rafId) })
129
- watch(() => props.modelValue, () => updateIndicator(true))
156
+ watch(() => props.modelValue, () => updateIndicator(true, 'smooth'))
130
157
  // Animated (not a hard snap) here too: a consumer passing an inline `:tabs="[...]"`
131
158
  // literal gets a brand-new array reference on every unrelated parent re-render,
132
159
  // which would otherwise race with — and instantly override — an in-flight
133
160
  // modelValue-triggered animation. Re-running the same animated update is a
134
161
  // no-op when the target hasn't actually moved, so this is safe either way.
135
- watch(() => props.tabs, () => updateIndicator(true), { deep: true })
162
+ watch(() => props.tabs, () => updateIndicator(true, shownValue === props.modelValue ? 'none' : 'instant'), { deep: true })
136
163
  watch(leading, () => updateIndicator(true))
137
164
 
138
165
  function select(tab: Tab) {
@@ -143,7 +170,7 @@ function select(tab: Tab) {
143
170
  <template>
144
171
  <!-- ── Primary: sliding short indicator with stretch ──────────────── -->
145
172
  <div v-if="variant === 'primary'" class="border-b border-outline-variant">
146
- <div class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
173
+ <div ref="rowEl" class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
147
174
  <button
148
175
  v-for="tab in tabs"
149
176
  :key="tab.value"
@@ -175,7 +202,7 @@ function select(tab: Tab) {
175
202
 
176
203
  <!-- ── Secondary: full-width underline indicator ─────────────────── -->
177
204
  <div v-else class="border-b border-outline-variant">
178
- <div class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
205
+ <div ref="rowEl" class="relative flex" :class="scrollable ? 'overflow-x-auto' : 'overflow-x-hidden'" style="scrollbar-width: none">
179
206
  <button
180
207
  v-for="tab in tabs"
181
208
  :key="tab.value"