@m3ui-vue/m3ui-vue 1.0.1 → 1.0.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.
Files changed (115) hide show
  1. package/dist/API.md +636 -20
  2. package/dist/MMenuItem-DFyXflk9.js +707 -0
  3. package/dist/MMenuItem-DFyXflk9.js.map +1 -0
  4. package/dist/MSlider-CdNIH7jc.js +1376 -0
  5. package/dist/MSlider-CdNIH7jc.js.map +1 -0
  6. package/dist/MTextField-B4X92vH_.js +671 -0
  7. package/dist/MTextField-B4X92vH_.js.map +1 -0
  8. package/dist/api.json +1 -1
  9. package/dist/chart.js +1 -1
  10. package/dist/chart.js.map +1 -1
  11. package/dist/code-editor.js +1 -1
  12. package/dist/components/MIconButton.vue.d.ts +1 -1
  13. package/dist/components/MMap.vue.d.ts +74 -0
  14. package/dist/components/MMapBaseLayers.vue.d.ts +15 -0
  15. package/dist/components/MMapControl.vue.d.ts +23 -0
  16. package/dist/components/MMapFeatureInfo.vue.d.ts +25 -0
  17. package/dist/components/MMapFilter.vue.d.ts +19 -0
  18. package/dist/components/MMapGeoJson.vue.d.ts +34 -0
  19. package/dist/components/MMapHover.vue.d.ts +17 -0
  20. package/dist/components/MMapIdentify.vue.d.ts +37 -0
  21. package/dist/components/MMapLayers.vue.d.ts +31 -0
  22. package/dist/components/MMapMarker.vue.d.ts +42 -0
  23. package/dist/components/MMapMeasure.vue.d.ts +43 -0
  24. package/dist/components/MMapPopup.vue.d.ts +43 -0
  25. package/dist/components/MMapSearch.vue.d.ts +28 -0
  26. package/dist/components/MShader.vue.d.ts +9 -3
  27. package/dist/components/_MMapLayerItem.vue.d.ts +25 -0
  28. package/dist/composables/_hydration.d.ts +4 -0
  29. package/dist/composables/_mapGeometry.d.ts +13 -0
  30. package/dist/composables/_ssrState.d.ts +30 -0
  31. package/dist/composables/_storageKeys.d.ts +3 -0
  32. package/dist/composables/_useThemeTokens.d.ts +7 -0
  33. package/dist/composables/mapAdapters.d.ts +111 -0
  34. package/dist/composables/mapSearch.d.ts +31 -0
  35. package/dist/composables/themeInitScript.d.ts +22 -0
  36. package/dist/composables/useColorPalette.d.ts +176 -6
  37. package/dist/composables/useLocale.d.ts +30 -0
  38. package/dist/composables/useMap.d.ts +47 -0
  39. package/dist/composables/useMapFilter.d.ts +10 -0
  40. package/dist/composables/useMapLayers.d.ts +30 -0
  41. package/dist/composables/useTheme.d.ts +2 -0
  42. package/dist/index.d.ts +4 -1
  43. package/dist/locales/index.js +217 -7
  44. package/dist/locales/index.js.map +1 -1
  45. package/dist/m3ui-vue.css +1 -1
  46. package/dist/m3ui.js +6742 -7961
  47. package/dist/m3ui.js.map +1 -1
  48. package/dist/map.d.ts +24 -0
  49. package/dist/map.js +1931 -0
  50. package/dist/map.js.map +1 -0
  51. package/dist/plugin.d.ts +25 -2
  52. package/dist/rich-text-editor.js +27 -26
  53. package/dist/rich-text-editor.js.map +1 -1
  54. package/dist/styles.css +1 -1
  55. package/dist/{useLocale-CuvWRZ1h.js → useLocale-b5-hzsL3.js} +32 -2
  56. package/dist/useLocale-b5-hzsL3.js.map +1 -0
  57. package/package.json +11 -2
  58. package/src/__tests__/MFab.test.ts +52 -6
  59. package/src/__tests__/MMap.test.ts +292 -0
  60. package/src/__tests__/MMapLayers.test.ts +257 -0
  61. package/src/__tests__/MMapTools.test.ts +233 -0
  62. package/src/__tests__/MShader.test.ts +16 -0
  63. package/src/__tests__/_fakeMaplibre.ts +158 -0
  64. package/src/__tests__/hydration.test.ts +119 -0
  65. package/src/__tests__/ssr.test.ts +171 -0
  66. package/src/__tests__/ssrState.test.ts +155 -0
  67. package/src/__tests__/themeInitScript.test.ts +122 -0
  68. package/src/components/MChart.vue +3 -2
  69. package/src/components/MFab.vue +29 -7
  70. package/src/components/MIconButton.vue +1 -1
  71. package/src/components/MLightbox.vue +10 -1
  72. package/src/components/MMap.vue +522 -0
  73. package/src/components/MMapBaseLayers.vue +67 -0
  74. package/src/components/MMapControl.vue +61 -0
  75. package/src/components/MMapFeatureInfo.vue +41 -0
  76. package/src/components/MMapFilter.vue +80 -0
  77. package/src/components/MMapGeoJson.vue +136 -0
  78. package/src/components/MMapHover.vue +131 -0
  79. package/src/components/MMapIdentify.vue +103 -0
  80. package/src/components/MMapLayers.vue +111 -0
  81. package/src/components/MMapMarker.vue +102 -0
  82. package/src/components/MMapMeasure.vue +452 -0
  83. package/src/components/MMapPopup.vue +99 -0
  84. package/src/components/MMapSearch.vue +162 -0
  85. package/src/components/MSearchBar.vue +4 -1
  86. package/src/components/MShader.vue +11 -3
  87. package/src/components/_MMapLayerItem.vue +77 -0
  88. package/src/composables/_hydration.ts +10 -0
  89. package/src/composables/_mapGeometry.ts +75 -0
  90. package/src/composables/_ssrState.ts +80 -0
  91. package/src/composables/_storageKeys.ts +6 -0
  92. package/src/composables/_useThemeTokens.ts +42 -0
  93. package/src/composables/mapAdapters.ts +208 -0
  94. package/src/composables/mapSearch.ts +56 -0
  95. package/src/composables/themeInitScript.ts +51 -0
  96. package/src/composables/useColorPalette.ts +32 -14
  97. package/src/composables/useLocale.ts +62 -0
  98. package/src/composables/useMap.ts +98 -0
  99. package/src/composables/useMapFilter.ts +45 -0
  100. package/src/composables/useMapLayers.ts +124 -0
  101. package/src/composables/useTheme.ts +18 -7
  102. package/src/composables/useWindowSizeClass.ts +23 -4
  103. package/src/index.ts +4 -1
  104. package/src/locales/de.ts +31 -1
  105. package/src/locales/es.ts +31 -1
  106. package/src/locales/fr.ts +31 -1
  107. package/src/locales/ja.ts +31 -1
  108. package/src/locales/ko.ts +31 -1
  109. package/src/locales/pt.ts +31 -1
  110. package/src/locales/zh.ts +31 -1
  111. package/src/map.ts +32 -0
  112. package/src/plugin.ts +77 -13
  113. package/dist/MTextField-BPbFeI9M.js +0 -1368
  114. package/dist/MTextField-BPbFeI9M.js.map +0 -1
  115. package/dist/useLocale-CuvWRZ1h.js.map +0 -1
package/src/plugin.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { App, MaybeRef } from 'vue'
2
- import type { Palette } from './composables/useColorPalette'
2
+ import type { Palette, PaletteId } from './composables/useColorPalette'
3
3
  import { __defaultContrast, __defaultPalette, __defaultSeed, __forcePalette, palettes } from './composables/useColorPalette'
4
4
  import type { ContrastLevel } from './composables/useColorPalette'
5
5
  import type { SeedOptions } from './composables/dynamicColor'
@@ -7,9 +7,14 @@ 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'
11
+ import { M3_INITIAL_STATE_KEY, enableCookies, readCookie, trackViewport, writeCookie, type M3InitialState, type ResolvedInitialState } from './composables/_ssrState'
12
+ import { THEME_KEY } from './composables/_storageKeys'
13
+ import type { Theme } from './composables/useTheme'
14
+ import { CONTRAST_KEY, PALETTE_KEY, SEED_PALETTE } from './composables/useColorPalette'
10
15
 
11
16
  export interface M3UIOptions {
12
- palette?: string
17
+ palette?: PaletteId | (string & {})
13
18
  /**
14
19
  * Set to `false` to make `palette` a hard default that always wins on boot, ignoring
15
20
  * whatever is already stored in localStorage, and to stop future `useColorPalette().set()`
@@ -50,29 +55,88 @@ export interface M3UIOptions {
50
55
  * mode (they keep their own dark bar); installed apps (PWA) still get it.
51
56
  */
52
57
  themeColor?: 'surface' | 'surface-container' | 'primary' | (string & {})
58
+ /**
59
+ * Server-side rendering: tells the components whether the app is still hydrating the server's HTML (Nuxt's
60
+ * `() => nuxtApp.isHydrating`, which `@m3ui-vue/nuxt` passes). The adaptive layouts (`MNavigationSuite`,
61
+ * `MListDetail`, `MSupportingPane`) and `useWindowSizeClass()` then render their first frame at the server's size
62
+ * (compact: it has no window) so it matches the HTML, and adapt once mounted.
63
+ */
64
+ isHydrating?: () => boolean
65
+ /**
66
+ * Also keep the theme, the palette and the contrast level in cookies (`m3-theme`, `m3-palette`, `m3-contrast`,
67
+ * a year, `SameSite=Lax`), and the window's size (`m3-viewport`, `<width>x<height>`), so a server can read them
68
+ * and render with the user's choice and for their screen through `initialState`.
69
+ */
70
+ cookies?: boolean
71
+ /**
72
+ * Server-side rendering: the user's theme, palette and contrast as the server knows them (from the cookies that
73
+ * `cookies` writes), on the server and on the client alike. `useTheme()` and `useColorPalette()` report them on
74
+ * the server, and on the client while hydrating (with `isHydrating`), so what's rendered from them matches the
75
+ * server's HTML. What's missing falls back to this plugin's defaults, like a first visit. With `viewport`, the
76
+ * adaptive layouts (`MNavigationSuite`, `MListDetail`, `MSupportingPane`) and `useWindowSizeClass()` render for
77
+ * that window size instead of compact.
78
+ */
79
+ initialState?: M3InitialState
80
+ }
81
+
82
+ const THEMES: readonly Theme[] = ['light', 'dark', 'system']
83
+ const CONTRASTS: readonly string[] = ['standard', 'medium', 'high', 'system']
84
+
85
+ /** `initialState` with what it lacks (or holds invalid, cookies being user input) taken from the plugin's defaults,
86
+ * by the same rules as the client: a stored choice wins unless `persistPalette` is false. */
87
+ function resolveInitialState(options: M3UIOptions): ResolvedInitialState {
88
+ const state = options.initialState ?? {}
89
+ const forced = options.persistPalette === false
90
+ const fallbackPalette = options.palette ?? (options.seed ? SEED_PALETTE : 'purple')
91
+ const contrast = state.contrast && CONTRASTS.includes(state.contrast) ? state.contrast : undefined
92
+ const size = (n: unknown) => typeof n === 'number' && Number.isFinite(n) && n > 0 && n < 100_000
93
+ const viewport = state.viewport && size(state.viewport.width) && size(state.viewport.height) ? state.viewport : undefined
94
+ return {
95
+ viewport,
96
+ theme: state.theme && THEMES.includes(state.theme) ? state.theme : 'system',
97
+ palette: forced ? fallbackPalette : state.palette || fallbackPalette,
98
+ contrast: (forced && options.contrast) || contrast || options.contrast || 'standard',
99
+ }
53
100
  }
54
101
 
55
102
  export function createM3UI(options: M3UIOptions = {}) {
56
103
  return {
57
104
  install(app: App) {
58
105
  app.directive('ripple', vRipple)
106
+ if (options.isHydrating) app.provide(M3_HYDRATING_KEY, options.isHydrating)
107
+ if (options.initialState) app.provide(M3_INITIAL_STATE_KEY, resolveInitialState(options))
108
+ if (options.cookies && typeof window !== 'undefined') enableCookies()
59
109
  for (const custom of options.customPalettes ?? []) {
60
110
  const i = palettes.findIndex((p) => p.id === custom.id)
61
111
  if (i === -1) palettes.push(custom)
62
112
  else palettes[i] = custom
63
113
  }
64
- if (options.persistPalette === false) {
65
- if (options.palette) __forcePalette(options.palette, false)
66
- else if (options.seed) void __defaultSeed(options.seed, options.seedOptions, false)
67
- } else if (!localStorage.getItem('m3-palette')) {
68
- // `palette`/`seed` are only the first-visit default — once the user picks a palette via
69
- // `useColorPalette().set()` (or a seed via `setSeed()`) it's persisted to localStorage, and that choice
70
- // must win on every later boot instead of being clobbered back to the plugin's static default.
71
- if (options.palette && options.palette !== 'purple') __defaultPalette(options.palette)
72
- else if (!options.palette && options.seed) void __defaultSeed(options.seed, options.seedOptions, true)
114
+ // Palette, seed, contrast and theme-color live on the page and in localStorage: on the server (SSR) there's
115
+ // neither, and the client applies them when it installs the plugin
116
+ if (typeof window !== 'undefined') {
117
+ if (options.persistPalette === false) {
118
+ if (options.palette) __forcePalette(options.palette, false)
119
+ else if (options.seed) void __defaultSeed(options.seed, options.seedOptions, false)
120
+ } else if (!localStorage.getItem('m3-palette') && !readCookie('m3-palette')) {
121
+ // `palette`/`seed` are only the first-visit default — once the user picks a palette via
122
+ // `useColorPalette().set()` (or a seed via `setSeed()`) it's persisted to localStorage, and that choice
123
+ // must win on every later boot instead of being clobbered back to the plugin's static default.
124
+ if (options.palette && options.palette !== 'purple') __defaultPalette(options.palette)
125
+ else if (!options.palette && options.seed) void __defaultSeed(options.seed, options.seedOptions, true)
126
+ }
127
+ if (options.contrast) __defaultContrast(options.contrast, options.persistPalette !== false)
128
+ if (options.themeColor) syncThemeColor(options.themeColor)
129
+ // Cookies for what's already stored (a user from before `cookies`, or the defaults just applied); a cookie
130
+ // without a stored value (cleared storage, or set by a server) stays
131
+ if (options.cookies) {
132
+ for (const key of [THEME_KEY, PALETTE_KEY, CONTRAST_KEY]) {
133
+ let value: string | null = null
134
+ try { value = localStorage.getItem(key) } catch { /* storage blocked */ }
135
+ if (value != null) writeCookie(key, value)
136
+ }
137
+ trackViewport()
138
+ }
73
139
  }
74
- if (options.contrast) __defaultContrast(options.contrast, options.persistPalette !== false)
75
- if (options.themeColor) syncThemeColor(options.themeColor)
76
140
  if (options.locale) {
77
141
  app.provide(M3_LOCALE_KEY, options.locale)
78
142
  __setAppLocale(options.locale)