@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.
Files changed (84) hide show
  1. package/README.md +24 -29
  2. package/dist/API.md +177 -18
  3. package/dist/{MTextField-DKtgPymd.js → MTextField-TZ7lfr09.js} +233 -108
  4. package/dist/MTextField-TZ7lfr09.js.map +1 -0
  5. package/dist/api.json +1 -1
  6. package/dist/code-editor.js +1 -1
  7. package/dist/components/MAppBarWithSearch.vue.d.ts +70 -0
  8. package/dist/components/MBottomAppBar.vue.d.ts +37 -0
  9. package/dist/components/MBottomSheetScaffold.vue.d.ts +40 -0
  10. package/dist/components/MChatBubble.vue.d.ts +1 -1
  11. package/dist/components/MChip.vue.d.ts +19 -2
  12. package/dist/components/MContainer.vue.d.ts +1 -1
  13. package/dist/components/MDataTable.vue.d.ts +1 -1
  14. package/dist/components/MDialog.vue.d.ts +1 -1
  15. package/dist/components/MFab.vue.d.ts +13 -4
  16. package/dist/components/MListItem.vue.d.ts +2 -2
  17. package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
  18. package/dist/components/MNavigationRail.vue.d.ts +6 -0
  19. package/dist/components/MSearchBar.vue.d.ts +5 -3
  20. package/dist/components/MShortNavigationBar.vue.d.ts +1 -1
  21. package/dist/components/MTable.vue.d.ts +1 -1
  22. package/dist/components/MTopAppBar.vue.d.ts +33 -2
  23. package/dist/composables/_searchAppBar.d.ts +6 -0
  24. package/dist/composables/_themeTransition.d.ts +2 -0
  25. package/dist/composables/_useSpring.d.ts +3 -5
  26. package/dist/composables/motion.d.ts +101 -0
  27. package/dist/composables/useColorPalette.d.ts +10 -0
  28. package/dist/composables/useLocale.d.ts +3 -0
  29. package/dist/index.d.ts +8 -1
  30. package/dist/locales/index.js +21 -0
  31. package/dist/locales/index.js.map +1 -1
  32. package/dist/m3ui-vue.css +1 -1
  33. package/dist/m3ui.js +6772 -5931
  34. package/dist/m3ui.js.map +1 -1
  35. package/dist/plugin.d.ts +7 -0
  36. package/dist/rich-text-editor.js +2 -2
  37. package/dist/styles/palettes-contrast.css +7764 -0
  38. package/dist/styles/theme.css +46 -2
  39. package/dist/styles.css +1 -1
  40. package/dist/{useLocale-Cn9MVIdw.js → useLocale-CuvWRZ1h.js} +4 -1
  41. package/dist/useLocale-CuvWRZ1h.js.map +1 -0
  42. package/package.json +3 -1
  43. package/src/__tests__/MAppBarWithSearch.test.ts +96 -0
  44. package/src/__tests__/MBottomAppBar.test.ts +71 -0
  45. package/src/__tests__/MBottomSheetScaffold.test.ts +101 -0
  46. package/src/__tests__/MChip.test.ts +63 -0
  47. package/src/__tests__/MFab.test.ts +36 -0
  48. package/src/__tests__/MNavigationRail.test.ts +77 -0
  49. package/src/__tests__/MTopAppBar.test.ts +99 -0
  50. package/src/__tests__/contrast.test.ts +64 -0
  51. package/src/__tests__/dynamicColor.test.ts +17 -0
  52. package/src/__tests__/motion.test.ts +37 -0
  53. package/src/__tests__/themeTransition.test.ts +119 -0
  54. package/src/components/MAppBarWithSearch.vue +236 -0
  55. package/src/components/MBottomAppBar.vue +181 -0
  56. package/src/components/MBottomSheetScaffold.vue +296 -0
  57. package/src/components/MBreadcrumbs.vue +1 -1
  58. package/src/components/MChip.vue +137 -5
  59. package/src/components/MFab.vue +73 -12
  60. package/src/components/MNavigationDrawer.vue +2 -2
  61. package/src/components/MNavigationRail.vue +135 -8
  62. package/src/components/MSearchBar.vue +28 -12
  63. package/src/components/MTopAppBar.vue +252 -28
  64. package/src/composables/_searchAppBar.ts +9 -0
  65. package/src/composables/_themeColor.ts +11 -4
  66. package/src/composables/_themeTransition.ts +71 -0
  67. package/src/composables/_useSpring.ts +11 -21
  68. package/src/composables/motion.ts +73 -0
  69. package/src/composables/useColorPalette.ts +103 -20
  70. package/src/composables/useLocale.ts +6 -0
  71. package/src/composables/useTheme.ts +4 -12
  72. package/src/index.ts +8 -1
  73. package/src/locales/de.ts +3 -0
  74. package/src/locales/es.ts +3 -0
  75. package/src/locales/fr.ts +3 -0
  76. package/src/locales/ja.ts +3 -0
  77. package/src/locales/ko.ts +3 -0
  78. package/src/locales/pt.ts +3 -0
  79. package/src/locales/zh.ts +3 -0
  80. package/src/plugin.ts +9 -1
  81. package/src/styles/palettes-contrast.css +7764 -0
  82. package/src/styles/theme.css +46 -2
  83. package/dist/MTextField-DKtgPymd.js.map +0 -1
  84. package/dist/useLocale-Cn9MVIdw.js.map +0 -1
@@ -0,0 +1,63 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { nextTick } from 'vue'
4
+ import MChip from '../components/MChip.vue'
5
+
6
+ const chip = (props: Record<string, unknown>, slots: Record<string, string> = {}) =>
7
+ mount(MChip, { props, slots: { default: 'Label', ...slots } })
8
+
9
+ describe('MChip', () => {
10
+ it('keeps the generic tone chip without a type', () => {
11
+ const w = chip({ tone: 'primary' })
12
+ expect(w.element.tagName).toBe('SPAN')
13
+ expect(w.classes()).toContain('bg-primary-container')
14
+ expect(w.find('button').exists()).toBe(false)
15
+ })
16
+
17
+ it('labels the generic remove button from the locale', () => {
18
+ expect(chip({ removable: true }).find('button').attributes('aria-label')).toBe('Remove')
19
+ })
20
+
21
+ it('draws an outlined assist chip with a primary icon and 8px corners', () => {
22
+ const w = chip({ type: 'assist', icon: 'event' })
23
+ expect(w.classes()).toEqual(expect.arrayContaining(['border', 'border-outline-variant', 'text-on-surface', 'rounded-sm']))
24
+ expect(w.find('.material-symbols-outlined').classes()).toContain('text-primary')
25
+ expect(w.find('button').classes()).toContain('pl-2')
26
+ })
27
+
28
+ it('draws an elevated suggestion chip without an outline', () => {
29
+ const w = chip({ type: 'suggestion', elevated: true })
30
+ expect(w.classes()).toEqual(expect.arrayContaining(['bg-surface-container-low', 'shadow-elevation-1', 'text-on-surface-variant']))
31
+ expect(w.classes()).not.toContain('border')
32
+ expect(w.find('button').classes()).toEqual(expect.arrayContaining(['pl-4', 'pr-4']))
33
+ })
34
+
35
+ it('toggles a filter chip with v-model:selected, filling it and morphing to a pill', async () => {
36
+ const w = chip({ type: 'filter', selected: false })
37
+ await w.find('button').trigger('click')
38
+ expect(w.emitted('update:selected')).toEqual([[true]])
39
+ expect(w.emitted('click')).toHaveLength(1)
40
+ await w.setProps({ selected: true })
41
+ expect(w.classes()).toContain('bg-secondary-container')
42
+ expect(w.find('button').attributes('aria-pressed')).toBe('true')
43
+ expect(w.find('[aria-hidden="true"]').text()).toBe('check')
44
+ })
45
+
46
+ it('makes an input chip with an avatar and a separate remove button', async () => {
47
+ const w = chip({ type: 'input', removable: true }, { avatar: '<img alt="" src="a.png">' })
48
+ const buttons = w.findAll('button')
49
+ expect(buttons).toHaveLength(2)
50
+ expect(buttons[0]!.find('img').exists()).toBe(true)
51
+ expect(buttons[0]!.classes()).toEqual(expect.arrayContaining(['pl-1', 'pr-2']))
52
+ await buttons[1]!.trigger('click')
53
+ expect(w.emitted('remove')).toHaveLength(1)
54
+ expect(w.emitted('click')).toBeUndefined()
55
+ })
56
+
57
+ it('disables a typed chip', async () => {
58
+ const w = chip({ type: 'filter', disabled: true })
59
+ expect(w.find('button').attributes('disabled')).toBeDefined()
60
+ expect(w.classes()).toContain('text-on-surface/38')
61
+ await nextTick()
62
+ })
63
+ })
@@ -0,0 +1,36 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { nextTick } from 'vue'
4
+ import MFab from '../components/MFab.vue'
5
+
6
+ const fab = (props: Record<string, unknown>) => mount(MFab, { props: { icon: 'edit', ...props } })
7
+
8
+ describe('MFab', () => {
9
+ it('keeps the baseline extended FAB for the regular size', () => {
10
+ const b = fab({ label: 'Compose' }).find('button')
11
+ expect(b.classes()).toEqual(expect.arrayContaining(['h-14', 'min-w-20', 'pl-4', 'pr-5']))
12
+ expect(b.find('span.whitespace-nowrap').classes()).toContain('text-label-large')
13
+ })
14
+
15
+ it.each([
16
+ ['small', 'h-14', 'text-title-medium', '8px'],
17
+ ['medium', 'h-20', 'text-title-large', '12px'],
18
+ ['large', 'h-24', 'text-headline-small', '16px'],
19
+ ])('sizes the %s extended FAB from ExtendedFab tokens', (size, height, text, gap) => {
20
+ const b = fab({ label: 'Compose', size }).find('button')
21
+ expect(b.classes()).toContain(height)
22
+ const label = b.find('span.whitespace-nowrap')
23
+ expect(label.classes()).toContain(text)
24
+ expect((label.element as HTMLElement).style.paddingLeft).toBe(gap)
25
+ })
26
+
27
+ it('collapses to the icon with expanded false, keeping the label as its accessible name', async () => {
28
+ const w = fab({ label: 'Compose', expanded: false })
29
+ await nextTick()
30
+ const b = w.find('button')
31
+ expect(b.attributes('aria-label')).toBe('Compose')
32
+ const label = b.find('span.whitespace-nowrap').element as HTMLElement
33
+ expect(label.style.display).toBe('none')
34
+ expect((b.element as HTMLElement).style.width).toBe('56px')
35
+ })
36
+ })
@@ -0,0 +1,77 @@
1
+ import { describe, it, expect, afterEach } from 'vitest'
2
+ import { mount, type VueWrapper } from '@vue/test-utils'
3
+ import MNavigationRail from '../components/MNavigationRail.vue'
4
+
5
+ const items = [
6
+ { value: 'inbox', label: 'Inbox', icon: 'inbox' },
7
+ { value: 'sent', label: 'Sent', icon: 'send' },
8
+ ]
9
+
10
+ let wrapper: VueWrapper | undefined
11
+ afterEach(() => {
12
+ wrapper?.unmount()
13
+ wrapper = undefined
14
+ document.body.innerHTML = ''
15
+ })
16
+
17
+ function open(props: Record<string, unknown> = {}) {
18
+ wrapper = mount(MNavigationRail, {
19
+ props: { modelValue: 'inbox', items, modal: true, hideOnCollapse: true, expanded: true, ...props },
20
+ attachTo: document.body,
21
+ global: { stubs: { transition: false } },
22
+ })
23
+ return document.body.querySelector<HTMLElement>('.wnr-panel')!
24
+ }
25
+
26
+ // Moves 25px per step, `msPerStep` apart (the events' timeStamp is what the fling velocity is measured from)
27
+ function swipe(panel: HTMLElement, from: number, to: number, msPerStep = 100) {
28
+ let t = 1000
29
+ const send = (type: string, x: number) => {
30
+ const e = new PointerEvent(type, { pointerId: 1, button: 0, bubbles: true, clientY: 300, clientX: x })
31
+ Object.defineProperty(e, 'timeStamp', { value: t })
32
+ panel.dispatchEvent(e)
33
+ }
34
+ send('pointerdown', from)
35
+ for (let x = from; x >= to; x -= 25) {
36
+ t += msPerStep
37
+ send('pointermove', x)
38
+ }
39
+ send('pointerup', to)
40
+ }
41
+
42
+ describe('MNavigationRail', () => {
43
+ it('shows no collapsed rail with hideOnCollapse, only the modal panel', () => {
44
+ const panel = open()
45
+ expect(panel).not.toBeNull()
46
+ expect(wrapper!.find('nav').exists()).toBe(false)
47
+ })
48
+
49
+ it('keeps the collapsed rail on screen in the default modal', () => {
50
+ open({ hideOnCollapse: false })
51
+ expect(wrapper!.find('nav').exists()).toBe(true)
52
+ })
53
+
54
+ it('closes when slowly swiped past half its width, and springs back from a short swipe', () => {
55
+ const panel = open()
56
+ swipe(panel, 200, 150, 400)
57
+ expect(wrapper!.emitted('update:expanded')).toBeUndefined()
58
+ swipe(panel, 200, 50, 400)
59
+ expect(wrapper!.emitted('update:expanded')).toEqual([[false]])
60
+ })
61
+
62
+ it('closes on a quick flick even when it moved less than half', () => {
63
+ const panel = open()
64
+ swipe(panel, 200, 150, 16)
65
+ expect(wrapper!.emitted('update:expanded')).toEqual([[false]])
66
+ })
67
+
68
+ it('ignores a vertical drag (the items scroll)', () => {
69
+ const panel = open()
70
+ const opts = { pointerId: 1, button: 0, bubbles: true, clientX: 200 }
71
+ panel.dispatchEvent(new PointerEvent('pointerdown', { ...opts, clientY: 300 }))
72
+ panel.dispatchEvent(new PointerEvent('pointermove', { ...opts, clientX: 190, clientY: 200 }))
73
+ panel.dispatchEvent(new PointerEvent('pointermove', { ...opts, clientX: 40, clientY: 200 }))
74
+ panel.dispatchEvent(new PointerEvent('pointerup', { ...opts, clientX: 40, clientY: 200 }))
75
+ expect(wrapper!.emitted('update:expanded')).toBeUndefined()
76
+ })
77
+ })
@@ -0,0 +1,99 @@
1
+ import { describe, it, expect, afterEach } from 'vitest'
2
+ import { mount, type VueWrapper } from '@vue/test-utils'
3
+ import { nextTick } from 'vue'
4
+ import MTopAppBar from '../components/MTopAppBar.vue'
5
+
6
+ let wrapper: VueWrapper | undefined
7
+ afterEach(() => {
8
+ wrapper?.unmount()
9
+ wrapper = undefined
10
+ document.body.innerHTML = ''
11
+ })
12
+
13
+ function bar(props: Record<string, unknown>) {
14
+ wrapper = mount(MTopAppBar, { props: { title: 'Inbox', ...props }, attachTo: document.body })
15
+ const bottomRow = () => wrapper!.find('.items-end').element as HTMLElement
16
+ return { bar: wrapper.find('header').element as HTMLElement, bottomRow }
17
+ }
18
+
19
+ function scroller() {
20
+ const el = document.createElement('div')
21
+ document.body.appendChild(el)
22
+ return {
23
+ el,
24
+ to(y: number) {
25
+ el.scrollTop = y
26
+ el.dispatchEvent(new Event('scroll'))
27
+ },
28
+ }
29
+ }
30
+
31
+ describe('MTopAppBar', () => {
32
+ it.each([
33
+ [{ variant: 'medium' }, '48px'],
34
+ [{ variant: 'large' }, '88px'],
35
+ [{ variant: 'medium', flexible: true }, '48px'],
36
+ [{ variant: 'medium', flexible: true, subtitle: 'Work' }, '72px'],
37
+ [{ variant: 'large', flexible: true }, '56px'],
38
+ [{ variant: 'large', flexible: true, subtitle: 'Work' }, '88px'],
39
+ ])('sizes the title row of %o from the app bar tokens', (props, height) => {
40
+ expect(bar(props).bottomRow().style.height).toBe(height)
41
+ })
42
+
43
+ it('uses the flexible title and subtitle styles', () => {
44
+ bar({ variant: 'large', flexible: true, subtitle: 'Work' })
45
+ expect(wrapper!.find('h1').classes()).toContain('text-display-small')
46
+ expect(wrapper!.find('.items-end p').classes()).toContain('text-title-medium')
47
+ })
48
+
49
+ it('shows a subtitle under a small title', () => {
50
+ bar({ subtitle: '3 unread' })
51
+ expect(wrapper!.find('p').text()).toBe('3 unread')
52
+ expect(wrapper!.find('p').classes()).toContain('text-label-medium')
53
+ })
54
+
55
+ it('collapses a medium bar with the content and swaps the titles (exit-until-collapsed)', async () => {
56
+ const s = scroller()
57
+ const { bar: el, bottomRow } = bar({ variant: 'medium', scrollBehavior: 'exit-until-collapsed', scrollTarget: s.el })
58
+ expect(wrapper!.find('header').classes()).toContain('sticky')
59
+ s.to(30)
60
+ expect(el.style.height).toBe('82px')
61
+ // The bottom margin keeps the expanded 112px in the layout
62
+ expect(el.style.marginBottom).toBe('30px')
63
+ expect(bottomRow().style.height).toBe('18px')
64
+ s.to(200)
65
+ expect(el.style.height).toBe('64px')
66
+ const [small, big] = wrapper!.findAll('.min-w-0').map((w) => w.element as HTMLElement)
67
+ expect(small!.style.opacity).toBe('1')
68
+ expect(big!.style.opacity).toBe('0')
69
+ // Only back at the top does it expand again
70
+ s.to(10)
71
+ expect(el.style.height).toBe('102px')
72
+ })
73
+
74
+ it('hides a small bar while scrolling down and brings it back scrolling up (enter-always)', () => {
75
+ const s = scroller()
76
+ const { bar: el } = bar({ scrollBehavior: 'enter-always', scrollTarget: s.el })
77
+ s.to(40)
78
+ expect(el.style.height).toBe('24px')
79
+ s.to(300)
80
+ expect(el.style.height).toBe('0px')
81
+ s.to(280)
82
+ expect(el.style.height).toBe('20px')
83
+ })
84
+
85
+ it('keeps the navigation button a grandchild of the header (consumers select header > div > button)', () => {
86
+ bar({ navigationIcon: 'menu' })
87
+ expect(wrapper!.find('header > div > button').exists()).toBe(true)
88
+ })
89
+
90
+ it('takes the scrolled color when content moves under a pinned bar', async () => {
91
+ const s = scroller()
92
+ const { bar: el } = bar({ scrollBehavior: 'pinned', scrollTarget: s.el })
93
+ expect(el.classList).toContain('bg-surface')
94
+ s.to(5)
95
+ await nextTick()
96
+ expect(el.classList).toContain('bg-surface-container')
97
+ expect(el.style.height).toBe('')
98
+ })
99
+ })
@@ -0,0 +1,64 @@
1
+ import { describe, it, expect, afterEach } from 'vitest'
2
+ import { mount } from '@vue/test-utils'
3
+ import { defineComponent, h, nextTick } from 'vue'
4
+ import { readFileSync } from 'node:fs'
5
+ import { resolve } from 'node:path'
6
+ import { useColorPalette, palettes } from '../composables/useColorPalette'
7
+
8
+ function withSetup() {
9
+ let result!: ReturnType<typeof useColorPalette>
10
+ mount(defineComponent({
11
+ setup() {
12
+ result = useColorPalette()
13
+ return () => h('div')
14
+ },
15
+ }))
16
+ return result
17
+ }
18
+
19
+ describe('contrast levels', () => {
20
+ afterEach(async () => {
21
+ withSetup().setContrast('standard')
22
+ await nextTick()
23
+ })
24
+
25
+ it('sets data-contrast on <html> and remembers the level', async () => {
26
+ const { setContrast, contrast, activeContrast } = withSetup()
27
+ setContrast('high')
28
+ await nextTick()
29
+ expect(contrast.value).toBe('high')
30
+ expect(activeContrast.value).toBe('high')
31
+ expect(document.documentElement.getAttribute('data-contrast')).toBe('high')
32
+ expect(localStorage.getItem('m3-contrast')).toBe('high')
33
+ setContrast('standard')
34
+ await nextTick()
35
+ expect(document.documentElement.hasAttribute('data-contrast')).toBe(false)
36
+ })
37
+
38
+ it('resolves system to the OS preference (standard without prefers-contrast: more)', async () => {
39
+ const { setContrast, activeContrast } = withSetup()
40
+ setContrast('system')
41
+ await nextTick()
42
+ expect(activeContrast.value).toBe('standard')
43
+ })
44
+ })
45
+
46
+ describe('palettes-contrast.css', () => {
47
+ const css = readFileSync(resolve(__dirname, '../styles/palettes-contrast.css'), 'utf8')
48
+
49
+ it('has medium and high, light and dark, for every built-in palette', () => {
50
+ for (const { id } of palettes) {
51
+ const base = id === 'purple' ? 'html:not([data-palette])' : `[data-palette='${id}']`
52
+ for (const level of ['medium', 'high']) {
53
+ expect(css).toContain(`${base}[data-contrast='${level}']:not(.dark) {`)
54
+ expect(css).toContain(`${base}[data-contrast='${level}'].dark {`)
55
+ }
56
+ }
57
+ })
58
+
59
+ it('raises contrast: high-contrast on-surface is black in light and white in dark', () => {
60
+ const block = (selector: string) => css.slice(css.indexOf(`${selector} {`)).split('}')[0]!
61
+ expect(block("[data-palette='teal'][data-contrast='high']:not(.dark)")).toContain('--color-on-surface: #000000;')
62
+ expect(block("[data-palette='teal'][data-contrast='high'].dark")).toContain('--color-on-surface: #FFFFFF;')
63
+ })
64
+ })
@@ -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
+ })