@m3ui-vue/m3ui-vue 1.0.0 → 1.0.2
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 +42 -15
- package/dist/{MTextField-TZ7lfr09.js → MTextField-BPbFeI9M.js} +76 -73
- package/dist/MTextField-BPbFeI9M.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/chart.js +1 -1
- package/dist/chart.js.map +1 -1
- package/dist/components/MNavigationDrawer.vue.d.ts +9 -6
- package/dist/components/MShader.vue.d.ts +9 -3
- package/dist/components/MTransition.vue.d.ts +0 -9
- package/dist/composables/_hydration.d.ts +4 -0
- package/dist/composables/_springAnimation.d.ts +17 -0
- package/dist/composables/themeInitScript.d.ts +21 -0
- package/dist/composables/useColorPalette.d.ts +4 -0
- package/dist/composables/useTheme.d.ts +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +3017 -2782
- package/dist/m3ui.js.map +1 -1
- package/dist/plugin.d.ts +7 -0
- package/dist/rich-text-editor.js +1 -1
- package/dist/styles.css +1 -1
- package/package.json +1 -1
- package/src/__tests__/MAppBarWithSearch.test.ts +12 -0
- package/src/__tests__/MBottomSheetScaffold.test.ts +21 -0
- package/src/__tests__/MNavigationDrawer.test.ts +185 -0
- package/src/__tests__/MNavigationRail.test.ts +17 -1
- package/src/__tests__/MPullToRefresh.test.ts +18 -1
- package/src/__tests__/MShader.test.ts +16 -0
- package/src/__tests__/MTransition.test.ts +12 -0
- package/src/__tests__/hydration.test.ts +86 -0
- package/src/__tests__/sheets.test.ts +19 -15
- package/src/__tests__/ssr.test.ts +109 -0
- package/src/__tests__/themeInitScript.test.ts +104 -0
- package/src/components/MBottomSheetScaffold.vue +23 -2
- package/src/components/MChart.vue +3 -2
- package/src/components/MContextMenu.vue +34 -33
- package/src/components/MLightbox.vue +6 -1
- package/src/components/MMenu.vue +34 -33
- package/src/components/MNavigationDrawer.vue +297 -37
- package/src/components/MNavigationRail.vue +69 -15
- package/src/components/MPullToRefresh.vue +18 -0
- package/src/components/MSearchBar.vue +8 -4
- package/src/components/MShader.vue +11 -3
- package/src/components/MTransition.vue +36 -41
- package/src/composables/_hydration.ts +10 -0
- package/src/composables/_springAnimation.ts +74 -0
- package/src/composables/_useSheetMotion.ts +101 -88
- package/src/composables/themeInitScript.ts +49 -0
- package/src/composables/useColorPalette.ts +10 -6
- package/src/composables/useTheme.ts +12 -6
- package/src/composables/useWindowSizeClass.ts +14 -4
- package/src/index.ts +2 -0
- package/src/plugin.ts +24 -11
- package/dist/MTextField-TZ7lfr09.js.map +0 -1
|
@@ -3,7 +3,12 @@ import { themeTransition } from './_themeTransition'
|
|
|
3
3
|
|
|
4
4
|
export type Theme = 'light' | 'dark' | 'system'
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
export const THEME_KEY = 'm3-theme'
|
|
7
|
+
|
|
8
|
+
// No DOM on the server (SSR): nothing is read or applied there, the client does it when the module loads.
|
|
9
|
+
const inBrowser = typeof window !== 'undefined'
|
|
10
|
+
|
|
11
|
+
const theme = ref<Theme>((inBrowser ? localStorage.getItem(THEME_KEY) as Theme : null) ?? 'system')
|
|
7
12
|
|
|
8
13
|
let _appliedDark: boolean | null = null
|
|
9
14
|
|
|
@@ -20,22 +25,23 @@ function applyTheme(t: Theme) {
|
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
// Apply immediately on module load (before any component mounts) — no animation
|
|
23
|
-
applyTheme(theme.value)
|
|
28
|
+
if (inBrowser) applyTheme(theme.value)
|
|
24
29
|
|
|
25
30
|
export function useTheme() {
|
|
26
|
-
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
|
|
31
|
+
const mediaQuery = inBrowser ? window.matchMedia('(prefers-color-scheme: dark)') : null
|
|
27
32
|
|
|
28
33
|
function onSystemChange() {
|
|
29
34
|
if (theme.value === 'system') applyTheme('system')
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
watchEffect(() => {
|
|
33
|
-
|
|
38
|
+
if (!inBrowser) return
|
|
39
|
+
localStorage.setItem(THEME_KEY, theme.value)
|
|
34
40
|
applyTheme(theme.value)
|
|
35
41
|
})
|
|
36
42
|
|
|
37
|
-
onMounted(() => mediaQuery
|
|
38
|
-
onUnmounted(() => mediaQuery
|
|
43
|
+
onMounted(() => mediaQuery?.addEventListener('change', onSystemChange))
|
|
44
|
+
onUnmounted(() => mediaQuery?.removeEventListener('change', onSystemChange))
|
|
39
45
|
|
|
40
46
|
function cycle() {
|
|
41
47
|
const order: Theme[] = ['light', 'dark', 'system']
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { computed, onMounted, onUnmounted, ref, toValue, watch, type MaybeRefOrGetter, type Ref } from 'vue'
|
|
2
|
+
import { isHydrating } from './_hydration'
|
|
2
3
|
|
|
3
4
|
/*
|
|
4
5
|
* M3 window size classes, with the breakpoints from androidx `WindowSizeClass`
|
|
@@ -48,10 +49,19 @@ function updateWindow() {
|
|
|
48
49
|
export function useWindowSizeClass(target?: MaybeRefOrGetter<HTMLElement | null | undefined>): WindowSizeClassInfo {
|
|
49
50
|
let width: Ref<number>
|
|
50
51
|
let height: Ref<number>
|
|
52
|
+
// Hydrating server-rendered HTML: start from the server's size (none, so compact) to match it, then measure
|
|
53
|
+
const hydrating = isHydrating()
|
|
51
54
|
|
|
52
55
|
if (target === undefined) {
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
if (hydrating) {
|
|
57
|
+
const mounted = ref(false)
|
|
58
|
+
onMounted(() => { mounted.value = true })
|
|
59
|
+
width = computed(() => (mounted.value ? winWidth.value : 0)) as Ref<number>
|
|
60
|
+
height = computed(() => (mounted.value ? winHeight.value : 0)) as Ref<number>
|
|
61
|
+
} else {
|
|
62
|
+
width = winWidth
|
|
63
|
+
height = winHeight
|
|
64
|
+
}
|
|
55
65
|
onMounted(() => {
|
|
56
66
|
if (winListeners++ === 0) {
|
|
57
67
|
updateWindow()
|
|
@@ -63,8 +73,8 @@ export function useWindowSizeClass(target?: MaybeRefOrGetter<HTMLElement | null
|
|
|
63
73
|
})
|
|
64
74
|
} else {
|
|
65
75
|
// Until the element is measured, guess from the window so the first paint is usually right.
|
|
66
|
-
width = ref(winWidth.value)
|
|
67
|
-
height = ref(winHeight.value)
|
|
76
|
+
width = ref(hydrating ? 0 : winWidth.value)
|
|
77
|
+
height = ref(hydrating ? 0 : winHeight.value)
|
|
68
78
|
let observer: ResizeObserver | undefined
|
|
69
79
|
const observe = (el: HTMLElement | null | undefined) => {
|
|
70
80
|
observer?.disconnect()
|
package/src/index.ts
CHANGED
|
@@ -8,6 +8,8 @@ export type { Theme } from './composables/useTheme'
|
|
|
8
8
|
export { useColorPalette, palettes } from './composables/useColorPalette'
|
|
9
9
|
export type { Palette, ActiveSeed, ContrastLevel } from './composables/useColorPalette'
|
|
10
10
|
export { SEED_PALETTE } from './composables/useColorPalette'
|
|
11
|
+
export { themeInitScript } from './composables/themeInitScript'
|
|
12
|
+
export type { ThemeInitOptions } from './composables/themeInitScript'
|
|
11
13
|
export { generateScheme, schemeToCss, schemeRoles } from './composables/dynamicColor'
|
|
12
14
|
export type { SchemeVariant, SeedOptions, GeneratedScheme } from './composables/dynamicColor'
|
|
13
15
|
export { useToast } from './composables/useToast'
|
package/src/plugin.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { syncThemeColor } from './composables/_themeColor'
|
|
|
7
7
|
import type { M3Locale } from './composables/useLocale'
|
|
8
8
|
import { M3_LOCALE_KEY, __setAppLocale } from './composables/useLocale'
|
|
9
9
|
import { vRipple } from './directives/vRipple'
|
|
10
|
+
import { M3_HYDRATING_KEY } from './composables/_hydration'
|
|
10
11
|
|
|
11
12
|
export interface M3UIOptions {
|
|
12
13
|
palette?: string
|
|
@@ -50,29 +51,41 @@ export interface M3UIOptions {
|
|
|
50
51
|
* mode (they keep their own dark bar); installed apps (PWA) still get it.
|
|
51
52
|
*/
|
|
52
53
|
themeColor?: 'surface' | 'surface-container' | 'primary' | (string & {})
|
|
54
|
+
/**
|
|
55
|
+
* Server-side rendering: tells the components whether the app is still hydrating the server's HTML (Nuxt's
|
|
56
|
+
* `() => nuxtApp.isHydrating`, which `@m3ui-vue/nuxt` passes). The adaptive layouts (`MNavigationSuite`,
|
|
57
|
+
* `MListDetail`, `MSupportingPane`) and `useWindowSizeClass()` then render their first frame at the server's size
|
|
58
|
+
* (compact: it has no window) so it matches the HTML, and adapt once mounted.
|
|
59
|
+
*/
|
|
60
|
+
isHydrating?: () => boolean
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
export function createM3UI(options: M3UIOptions = {}) {
|
|
56
64
|
return {
|
|
57
65
|
install(app: App) {
|
|
58
66
|
app.directive('ripple', vRipple)
|
|
67
|
+
if (options.isHydrating) app.provide(M3_HYDRATING_KEY, options.isHydrating)
|
|
59
68
|
for (const custom of options.customPalettes ?? []) {
|
|
60
69
|
const i = palettes.findIndex((p) => p.id === custom.id)
|
|
61
70
|
if (i === -1) palettes.push(custom)
|
|
62
71
|
else palettes[i] = custom
|
|
63
72
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
// Palette, seed, contrast and theme-color live on the page and in localStorage: on the server (SSR) there's
|
|
74
|
+
// neither, and the client applies them when it installs the plugin
|
|
75
|
+
if (typeof window !== 'undefined') {
|
|
76
|
+
if (options.persistPalette === false) {
|
|
77
|
+
if (options.palette) __forcePalette(options.palette, false)
|
|
78
|
+
else if (options.seed) void __defaultSeed(options.seed, options.seedOptions, false)
|
|
79
|
+
} else if (!localStorage.getItem('m3-palette')) {
|
|
80
|
+
// `palette`/`seed` are only the first-visit default — once the user picks a palette via
|
|
81
|
+
// `useColorPalette().set()` (or a seed via `setSeed()`) it's persisted to localStorage, and that choice
|
|
82
|
+
// must win on every later boot instead of being clobbered back to the plugin's static default.
|
|
83
|
+
if (options.palette && options.palette !== 'purple') __defaultPalette(options.palette)
|
|
84
|
+
else if (!options.palette && options.seed) void __defaultSeed(options.seed, options.seedOptions, true)
|
|
85
|
+
}
|
|
86
|
+
if (options.contrast) __defaultContrast(options.contrast, options.persistPalette !== false)
|
|
87
|
+
if (options.themeColor) syncThemeColor(options.themeColor)
|
|
73
88
|
}
|
|
74
|
-
if (options.contrast) __defaultContrast(options.contrast, options.persistPalette !== false)
|
|
75
|
-
if (options.themeColor) syncThemeColor(options.themeColor)
|
|
76
89
|
if (options.locale) {
|
|
77
90
|
app.provide(M3_LOCALE_KEY, options.locale)
|
|
78
91
|
__setAppLocale(options.locale)
|