@getcortexlabs/mavis-core 0.1.0-alpha.2 → 0.1.0-alpha.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.
Files changed (152) hide show
  1. package/README.md +2 -2
  2. package/dist/accordion.d.ts +17 -0
  3. package/dist/accordion.d.ts.map +1 -0
  4. package/dist/accordion.js +42 -0
  5. package/dist/accordion.js.map +1 -0
  6. package/dist/alert.d.ts +21 -0
  7. package/dist/alert.d.ts.map +1 -0
  8. package/dist/alert.js +13 -0
  9. package/dist/alert.js.map +1 -0
  10. package/dist/avatar.d.ts +10 -0
  11. package/dist/avatar.d.ts.map +1 -0
  12. package/dist/avatar.js +11 -0
  13. package/dist/avatar.js.map +1 -0
  14. package/dist/badge.d.ts +7 -0
  15. package/dist/badge.d.ts.map +1 -0
  16. package/dist/badge.js +11 -0
  17. package/dist/badge.js.map +1 -0
  18. package/dist/chip.d.ts +9 -0
  19. package/dist/chip.d.ts.map +1 -0
  20. package/dist/chip.js +16 -0
  21. package/dist/chip.js.map +1 -0
  22. package/dist/choice.d.ts +15 -0
  23. package/dist/choice.d.ts.map +1 -0
  24. package/dist/choice.js +27 -0
  25. package/dist/choice.js.map +1 -0
  26. package/dist/dialog.d.ts +4 -0
  27. package/dist/dialog.d.ts.map +1 -0
  28. package/dist/dialog.js +20 -0
  29. package/dist/dialog.js.map +1 -0
  30. package/dist/empty-state.d.ts +2 -0
  31. package/dist/empty-state.d.ts.map +1 -0
  32. package/dist/empty-state.js +5 -0
  33. package/dist/empty-state.js.map +1 -0
  34. package/dist/experimental.d.ts +122 -0
  35. package/dist/experimental.d.ts.map +1 -0
  36. package/dist/experimental.js +372 -0
  37. package/dist/experimental.js.map +1 -0
  38. package/dist/index.d.ts +30 -3
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +17 -1
  41. package/dist/index.js.map +1 -1
  42. package/dist/input.d.ts +10 -0
  43. package/dist/input.d.ts.map +1 -1
  44. package/dist/input.js +12 -0
  45. package/dist/input.js.map +1 -1
  46. package/dist/list-item.d.ts +3 -0
  47. package/dist/list-item.d.ts.map +1 -0
  48. package/dist/list-item.js +7 -0
  49. package/dist/list-item.js.map +1 -0
  50. package/dist/motion.d.ts +33 -0
  51. package/dist/motion.d.ts.map +1 -0
  52. package/dist/motion.js +21 -0
  53. package/dist/motion.js.map +1 -0
  54. package/dist/progress.d.ts +19 -0
  55. package/dist/progress.d.ts.map +1 -0
  56. package/dist/progress.js +19 -0
  57. package/dist/progress.js.map +1 -0
  58. package/dist/segmented.d.ts +11 -0
  59. package/dist/segmented.d.ts.map +1 -0
  60. package/dist/segmented.js +30 -0
  61. package/dist/segmented.js.map +1 -0
  62. package/dist/sheet.d.ts +12 -0
  63. package/dist/sheet.d.ts.map +1 -0
  64. package/dist/sheet.js +15 -0
  65. package/dist/sheet.js.map +1 -0
  66. package/dist/tabs.d.ts +11 -0
  67. package/dist/tabs.d.ts.map +1 -0
  68. package/dist/tabs.js +28 -0
  69. package/dist/tabs.js.map +1 -0
  70. package/dist/theme/themes.d.ts.map +1 -1
  71. package/dist/theme/themes.js +84 -11
  72. package/dist/theme/themes.js.map +1 -1
  73. package/dist/theme/types.d.ts +52 -1
  74. package/dist/theme/types.d.ts.map +1 -1
  75. package/dist/theme/typography.d.ts +10 -0
  76. package/dist/theme/typography.d.ts.map +1 -0
  77. package/dist/theme/typography.js +90 -0
  78. package/dist/theme/typography.js.map +1 -0
  79. package/dist/toast.d.ts +43 -0
  80. package/dist/toast.d.ts.map +1 -0
  81. package/dist/toast.js +228 -0
  82. package/dist/toast.js.map +1 -0
  83. package/dist/vue/button-content.d.ts +10 -0
  84. package/dist/vue/button-content.d.ts.map +1 -0
  85. package/dist/vue/button-content.js +54 -0
  86. package/dist/vue/button-content.js.map +1 -0
  87. package/dist/vue/cached-activity.d.ts +3 -0
  88. package/dist/vue/cached-activity.d.ts.map +1 -0
  89. package/dist/vue/cached-activity.js +13 -0
  90. package/dist/vue/cached-activity.js.map +1 -0
  91. package/dist/vue/dialog.d.ts +21 -0
  92. package/dist/vue/dialog.d.ts.map +1 -0
  93. package/dist/vue/dialog.js +58 -0
  94. package/dist/vue/dialog.js.map +1 -0
  95. package/dist/vue/global-components.d.ts +102 -2
  96. package/dist/vue/global-components.d.ts.map +1 -1
  97. package/dist/vue/index.d.ts +17 -1
  98. package/dist/vue/index.d.ts.map +1 -1
  99. package/dist/vue/index.js +15 -1
  100. package/dist/vue/index.js.map +1 -1
  101. package/dist/vue/inline-content.d.ts +52 -0
  102. package/dist/vue/inline-content.d.ts.map +1 -0
  103. package/dist/vue/inline-content.js +64 -0
  104. package/dist/vue/inline-content.js.map +1 -0
  105. package/dist/vue/props.d.ts +2113 -533
  106. package/dist/vue/props.d.ts.map +1 -1
  107. package/dist/vue/props.js +272 -3
  108. package/dist/vue/props.js.map +1 -1
  109. package/dist/vue/theme-provider.d.ts +58 -0
  110. package/dist/vue/theme-provider.d.ts.map +1 -0
  111. package/dist/vue/theme-provider.js +69 -0
  112. package/dist/vue/theme-provider.js.map +1 -0
  113. package/dist/vue/theme.d.ts +962 -12
  114. package/dist/vue/theme.d.ts.map +1 -1
  115. package/dist/vue/theme.js +17 -7
  116. package/dist/vue/theme.js.map +1 -1
  117. package/dist/vue/toast.d.ts +29 -0
  118. package/dist/vue/toast.d.ts.map +1 -0
  119. package/dist/vue/toast.js +79 -0
  120. package/dist/vue/toast.js.map +1 -0
  121. package/package.json +5 -1
  122. package/src/accordion.ts +66 -0
  123. package/src/alert.ts +16 -0
  124. package/src/avatar.ts +11 -0
  125. package/src/badge.ts +11 -0
  126. package/src/chip.ts +20 -0
  127. package/src/choice.ts +41 -0
  128. package/src/dialog.ts +20 -0
  129. package/src/empty-state.ts +4 -0
  130. package/src/experimental.ts +501 -0
  131. package/src/index.ts +48 -2
  132. package/src/input.ts +22 -0
  133. package/src/list-item.ts +5 -0
  134. package/src/motion.ts +22 -0
  135. package/src/progress.ts +22 -0
  136. package/src/segmented.ts +39 -0
  137. package/src/sheet.ts +20 -0
  138. package/src/tabs.ts +35 -0
  139. package/src/theme/themes.ts +88 -11
  140. package/src/theme/types.ts +42 -1
  141. package/src/theme/typography.ts +84 -0
  142. package/src/toast.ts +249 -0
  143. package/src/vue/button-content.ts +60 -0
  144. package/src/vue/cached-activity.ts +13 -0
  145. package/src/vue/dialog.ts +63 -0
  146. package/src/vue/global-components.ts +84 -2
  147. package/src/vue/index.ts +35 -0
  148. package/src/vue/inline-content.ts +71 -0
  149. package/src/vue/props.ts +290 -5
  150. package/src/vue/theme-provider.ts +74 -0
  151. package/src/vue/theme.ts +17 -6
  152. package/src/vue/toast.ts +99 -0
package/src/sheet.ts ADDED
@@ -0,0 +1,20 @@
1
+ /** Why a sheet closed: the component's own control, a platform dismissal, or `open` set to false. */
2
+ export type SheetCloseReason = 'button' | 'backdrop' | 'escape' | 'back' | 'swipe' | 'program'
3
+
4
+ export type SheetName =
5
+ { title: string; accessibilityLabel?: string } | { title?: string; accessibilityLabel: string }
6
+
7
+ export const sheetCloseReasons: readonly SheetCloseReason[] = [
8
+ 'button',
9
+ 'backdrop',
10
+ 'escape',
11
+ 'back',
12
+ 'swipe',
13
+ 'program',
14
+ ]
15
+
16
+ export function checkSheetName(title: string | undefined, label: string | undefined) {
17
+ const name = title?.trim() || label?.trim()
18
+ if (!name) throw new RangeError('A sheet needs a nonempty title or accessibilityLabel')
19
+ return name
20
+ }
package/src/tabs.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { validateChoices, type ChoiceOption, type ChoiceValue } from './choice.js'
2
+
3
+ export type TabOption = Pick<ChoiceOption, 'value' | 'label' | 'disabled'>
4
+ export interface TabPanelProps {
5
+ value: ChoiceValue
6
+ }
7
+
8
+ export function validateTabs(tabs: readonly TabOption[], value: ChoiceValue | null, label: string) {
9
+ validateChoices(tabs, value)
10
+ if (typeof label !== 'string' || !label.trim())
11
+ throw new TypeError('Tabs need a non-empty label.')
12
+ }
13
+
14
+ /** Keyboard navigation moves focus; activation remains an explicit action. */
15
+ export function tabFocusTarget(
16
+ tabs: readonly TabOption[],
17
+ value: ChoiceValue,
18
+ key: string,
19
+ rtl = false,
20
+ ): TabOption | undefined {
21
+ const enabled = tabs.filter((tab) => !tab.disabled)
22
+ if (key === 'Home') return enabled[0]
23
+ if (key === 'End') return enabled[enabled.length - 1]
24
+ if (key !== 'ArrowLeft' && key !== 'ArrowRight') return
25
+ const step = (key === 'ArrowRight' ? 1 : -1) * (rtl ? -1 : 1)
26
+ const index = enabled.findIndex((tab) => tab.value === value)
27
+ return enabled[(index + step + enabled.length) % enabled.length]
28
+ }
29
+
30
+ /** Reveal the nearest edge without changing a surrounding vertical scroll view. */
31
+ export function tabScrollOffset(left: number, width: number, offset: number, viewport: number) {
32
+ if (left < offset || width >= viewport) return Math.max(0, left)
33
+ if (left + width > offset + viewport) return Math.max(0, left + width - viewport)
34
+ return offset
35
+ }
@@ -1,4 +1,11 @@
1
1
  import type { MavisTheme, ThemeOptions } from './types.js'
2
+ import {
3
+ validateTypography,
4
+ validateComponentTheme,
5
+ validateShadow,
6
+ freezeFamily,
7
+ } from './typography.js'
8
+ import { resolvePressScale } from '../motion.js'
2
9
 
3
10
  const foundation: MavisTheme = {
4
11
  name: 'mavis',
@@ -16,24 +23,72 @@ const foundation: MavisTheme = {
16
23
  onAccent: '#FFFFFF',
17
24
  accentSoft: '#E8EEFF',
18
25
  border: '#EAE3ED',
26
+ warning: '#9A5A10',
19
27
  success: '#237354',
20
28
  danger: '#B33545',
21
29
  },
22
30
  space: { none: 0, xs: 4, sm: 8, md: 16, lg: 24, xl: 32, '2xl': 48 },
23
31
  radius: { none: 0, sm: 10, md: 16, lg: 24, full: 999 },
24
32
  typography: {
25
- display: { fontSize: 56, fontWeight: '600', letterSpacing: -0.04 },
26
- title: { fontSize: 34, fontWeight: '600', letterSpacing: -0.035 },
27
- heading: { fontSize: 20, fontWeight: '600', letterSpacing: -0.02 },
28
- body: { fontSize: 16, fontWeight: 'normal', letterSpacing: 0 },
29
- caption: { fontSize: 13, fontWeight: 'normal', letterSpacing: 0 },
30
- overline: { fontSize: 11, fontWeight: '600', letterSpacing: 0.12 },
33
+ display: {
34
+ fontSize: 56,
35
+ fontWeight: '600',
36
+ letterSpacing: -0.04,
37
+ fontFamily: 'system',
38
+ lineHeight: 1.3,
39
+ },
40
+ title: {
41
+ fontSize: 34,
42
+ fontWeight: '600',
43
+ letterSpacing: -0.035,
44
+ fontFamily: 'system',
45
+ lineHeight: 1.3,
46
+ },
47
+ heading: {
48
+ fontSize: 20,
49
+ fontWeight: '600',
50
+ letterSpacing: -0.02,
51
+ fontFamily: 'system',
52
+ lineHeight: 1.3,
53
+ },
54
+ body: {
55
+ fontSize: 16,
56
+ fontWeight: 'normal',
57
+ letterSpacing: 0,
58
+ fontFamily: 'system',
59
+ lineHeight: 1.3,
60
+ },
61
+ caption: {
62
+ fontSize: 13,
63
+ fontWeight: 'normal',
64
+ letterSpacing: 0,
65
+ fontFamily: 'system',
66
+ lineHeight: 1.3,
67
+ },
68
+ overline: {
69
+ fontSize: 11,
70
+ fontWeight: '600',
71
+ letterSpacing: 0.12,
72
+ fontFamily: 'system',
73
+ lineHeight: 1.3,
74
+ },
31
75
  },
32
76
  control: {
33
77
  sm: { height: 44, fontSize: 14, padding: 16 },
34
78
  md: { height: 52, fontSize: 16, padding: 20 },
35
79
  lg: { height: 60, fontSize: 17, padding: 24 },
36
80
  },
81
+ elevation: {
82
+ none: { x: 0, y: 0, blur: 0, spread: 0, color: 'transparent' },
83
+ sm: { x: 0, y: 2, blur: 6, spread: 0, color: 'rgba(37, 30, 50, 0.10)' },
84
+ md: { x: 0, y: 8, blur: 24, spread: 0, color: 'rgba(37, 30, 50, 0.14)' },
85
+ lg: { x: 0, y: 16, blur: 40, spread: 0, color: 'rgba(37, 30, 50, 0.20)' },
86
+ },
87
+ components: {
88
+ button: { fontWeight: '600', lineHeight: 1.3, paddingY: 12, radius: 'md', elevation: 'none' },
89
+ input: { fontWeight: 'normal', lineHeight: 1.5, paddingX: 16, paddingY: 14, radius: 'md' },
90
+ card: { padding: 'lg', radius: 'lg', elevation: 'none' },
91
+ },
37
92
  motion: { pressScale: 0.97 },
38
93
  }
39
94
 
@@ -41,15 +96,32 @@ const foundation: MavisTheme = {
41
96
  export function defineTheme(options: ThemeOptions, base: MavisTheme = foundation): MavisTheme {
42
97
  const typography = { ...base.typography }
43
98
  for (const variant of Object.keys(typography) as (keyof typeof typography)[]) {
44
- typography[variant] = Object.freeze({
99
+ const type = {
45
100
  ...typography[variant],
46
101
  ...options.typography?.[variant],
47
- })
102
+ }
103
+ validateTypography(type)
104
+ typography[variant] = Object.freeze({ ...type, fontFamily: freezeFamily(type.fontFamily) })
48
105
  }
49
106
  const control = { ...base.control }
50
107
  for (const size of Object.keys(control) as (keyof typeof control)[]) {
51
108
  control[size] = Object.freeze({ ...control[size], ...options.control?.[size] })
52
109
  }
110
+ const elevation = { ...base.elevation }
111
+ for (const level of Object.keys(elevation) as (keyof typeof elevation)[]) {
112
+ const shadow = { ...elevation[level], ...options.elevation?.[level] }
113
+ validateShadow(shadow)
114
+ elevation[level] = Object.freeze(shadow)
115
+ }
116
+ const components = {
117
+ button: { ...base.components.button, ...options.components?.button },
118
+ input: { ...base.components.input, ...options.components?.input },
119
+ card: { ...base.components.card, ...options.components?.card },
120
+ }
121
+ validateComponentTheme(components)
122
+ for (const component of [components.button, components.input]) {
123
+ if (component.fontFamily) component.fontFamily = freezeFamily(component.fontFamily)
124
+ }
53
125
  const theme: MavisTheme = {
54
126
  name: options.name,
55
127
  appearance: options.appearance ?? base.appearance,
@@ -58,14 +130,18 @@ export function defineTheme(options: ThemeOptions, base: MavisTheme = foundation
58
130
  radius: Object.freeze({ ...base.radius, ...options.radius }),
59
131
  typography: Object.freeze(typography),
60
132
  control: Object.freeze(control),
133
+ elevation: Object.freeze(elevation),
134
+ components: Object.freeze({
135
+ button: Object.freeze(components.button),
136
+ input: Object.freeze(components.input),
137
+ card: Object.freeze(components.card),
138
+ }),
61
139
  motion: Object.freeze({ ...base.motion, ...options.motion }),
62
140
  }
63
141
  for (const [key, value] of [...Object.entries(theme.space), ...Object.entries(theme.radius)]) {
64
142
  if (!Number.isFinite(value) || value < 0) throw new RangeError(`Invalid theme token: ${key}`)
65
143
  }
66
- if (!(theme.motion.pressScale > 0 && theme.motion.pressScale <= 1)) {
67
- throw new RangeError('pressScale must be greater than 0 and at most 1')
68
- }
144
+ resolvePressScale(theme.motion.pressScale)
69
145
  return Object.freeze(theme)
70
146
  }
71
147
 
@@ -87,6 +163,7 @@ export const midnight = defineTheme({
87
163
  onAccent: '#172247',
88
164
  accentSoft: '#242E4A',
89
165
  border: '#3C3046',
166
+ warning: '#F4C579',
90
167
  success: '#9FD5BE',
91
168
  danger: '#FFA8A8',
92
169
  },
@@ -18,13 +18,50 @@ export interface Palette {
18
18
  onAccent: string
19
19
  accentSoft: string
20
20
  border: string
21
+ warning: string
21
22
  success: string
22
23
  danger: string
23
24
  }
24
25
 
26
+ export type FontWeight =
27
+ 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'
28
+ /** A shared installed family, or explicit platform names/stacks. Empty native names use the system font. */
29
+ export type FontFamily = string | Readonly<{ web: string; ios: string; android: string }>
30
+ export type Elevation = 'none' | 'sm' | 'md' | 'lg'
31
+ export interface Shadow {
32
+ x: number
33
+ y: number
34
+ blur: number
35
+ spread: number
36
+ color: string
37
+ }
38
+ export interface ComponentTheme {
39
+ button: {
40
+ fontFamily?: FontFamily
41
+ fontWeight: FontWeight
42
+ lineHeight: number
43
+ paddingY: number
44
+ radius: Radius | number
45
+ elevation: Elevation
46
+ }
47
+ input: {
48
+ fontFamily?: FontFamily
49
+ fontSize?: number
50
+ fontWeight: FontWeight
51
+ lineHeight: number
52
+ paddingX: number
53
+ paddingY: number
54
+ radius: Radius | number
55
+ }
56
+ card: { padding: Spacing; radius: Radius | number; elevation: Elevation }
57
+ }
58
+
25
59
  export interface Typography {
26
60
  fontSize: number
27
- fontWeight: 'normal' | '500' | '600' | 'bold'
61
+ fontWeight: FontWeight
62
+ fontFamily: FontFamily
63
+ /** Unitless line rhythm, at least 1; native renderers add leading to font metrics. */
64
+ lineHeight: number
28
65
  letterSpacing: number
29
66
  }
30
67
 
@@ -38,6 +75,8 @@ export interface MavisTheme {
38
75
  readonly control: Readonly<
39
76
  Record<Size, Readonly<{ height: number; fontSize: number; padding: number }>>
40
77
  >
78
+ readonly elevation: Readonly<Record<Elevation, Readonly<Shadow>>>
79
+ readonly components: { readonly [K in keyof ComponentTheme]: Readonly<ComponentTheme[K]> }
41
80
  readonly motion: Readonly<{ pressScale: number }>
42
81
  }
43
82
 
@@ -49,5 +88,7 @@ export interface ThemeOptions {
49
88
  radius?: Partial<MavisTheme['radius']>
50
89
  typography?: Partial<Record<TextVariant, Partial<Typography>>>
51
90
  control?: Partial<Record<Size, Partial<MavisTheme['control'][Size]>>>
91
+ elevation?: Partial<Record<Elevation, Partial<Shadow>>>
92
+ components?: { [K in keyof ComponentTheme]?: Partial<ComponentTheme[K]> }
52
93
  motion?: Partial<MavisTheme['motion']>
53
94
  }
@@ -0,0 +1,84 @@
1
+ import type { ComponentTheme, FontFamily, FontWeight, Shadow, Typography } from './types.js'
2
+
3
+ export const fontWeights: readonly FontWeight[] = [
4
+ 'normal',
5
+ 'bold',
6
+ '100',
7
+ '200',
8
+ '300',
9
+ '400',
10
+ '500',
11
+ '600',
12
+ '700',
13
+ '800',
14
+ '900',
15
+ ]
16
+ export function validateWeight(value: FontWeight) {
17
+ if (!fontWeights.includes(value)) throw new RangeError(`Invalid font weight: ${String(value)}`)
18
+ return value
19
+ }
20
+ export function freezeFamily(value: FontFamily): FontFamily {
21
+ if (typeof value === 'string') {
22
+ if (!value.trim()) throw new RangeError('A font family must not be blank')
23
+ return value
24
+ }
25
+ if (
26
+ !value ||
27
+ typeof value.web !== 'string' ||
28
+ !value.web.trim() ||
29
+ typeof value.ios !== 'string' ||
30
+ typeof value.android !== 'string'
31
+ ) {
32
+ throw new TypeError('A font family needs web, ios and android names')
33
+ }
34
+ return Object.freeze({ ...value })
35
+ }
36
+ export function fontFamily(value: FontFamily, platform: 'web' | 'ios' | 'android') {
37
+ if (value === 'system')
38
+ return platform === 'web'
39
+ ? 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif'
40
+ : ''
41
+ return typeof value === 'string' ? value : value[platform]
42
+ }
43
+ function finite(value: number, name: string, minimum = 0) {
44
+ if (!Number.isFinite(value) || value < minimum) throw new RangeError(`Invalid ${name}: ${value}`)
45
+ }
46
+ export function validateTypography(value: Typography) {
47
+ finite(value.fontSize, 'fontSize', Number.EPSILON)
48
+ finite(value.lineHeight, 'lineHeight', 1)
49
+ if (!Number.isFinite(value.letterSpacing)) throw new RangeError('Invalid letterSpacing')
50
+ validateWeight(value.fontWeight)
51
+ freezeFamily(value.fontFamily)
52
+ }
53
+ export function validateShadow(value: Shadow) {
54
+ for (const key of ['x', 'y', 'spread'] as const) finite(value[key], `shadow ${key}`, -Infinity)
55
+ finite(value.blur, 'shadow blur')
56
+ if (typeof value.color !== 'string' || !value.color.trim())
57
+ throw new RangeError('A shadow needs a color')
58
+ }
59
+ export function validateComponentTheme(value: ComponentTheme) {
60
+ for (const component of [value.button, value.input]) {
61
+ validateWeight(component.fontWeight)
62
+ finite(component.lineHeight, 'lineHeight', 1)
63
+ finite(component.paddingY, 'paddingY')
64
+ if (component.fontFamily !== undefined) freezeFamily(component.fontFamily)
65
+ }
66
+ finite(value.input.paddingX, 'paddingX')
67
+ if (value.input.fontSize !== undefined) finite(value.input.fontSize, 'fontSize', Number.EPSILON)
68
+ for (const component of [value.button, value.input, value.card]) {
69
+ if (typeof component.radius === 'number') finite(component.radius, 'radius')
70
+ else if (!['none', 'sm', 'md', 'lg', 'full'].includes(component.radius))
71
+ throw new RangeError('Invalid component radius')
72
+ }
73
+ for (const component of [value.button, value.card]) {
74
+ if (!['none', 'sm', 'md', 'lg'].includes(component.elevation))
75
+ throw new RangeError('Invalid elevation')
76
+ }
77
+ if (typeof value.card.padding === 'number') finite(value.card.padding, 'padding')
78
+ else if (!['none', 'xs', 'sm', 'md', 'lg', 'xl', '2xl'].includes(value.card.padding))
79
+ throw new RangeError('Invalid card padding')
80
+ }
81
+ export function shadowStyle(value: Shadow, platform: 'web' | 'native' = 'web') {
82
+ const unit = platform === 'web' ? 'px' : 'dip'
83
+ return `${value.x}${unit} ${value.y}${unit} ${value.blur}${unit} ${value.spread}${unit} ${value.color}`
84
+ }
package/src/toast.ts ADDED
@@ -0,0 +1,249 @@
1
+ export type ToastTone = 'success' | 'error' | 'warning' | 'info'
2
+
3
+ export interface ToastOptions {
4
+ message: string
5
+ title?: string
6
+ tone?: ToastTone
7
+ /** Milliseconds after entry. Zero keeps the notification until dismissed. */
8
+ duration?: number
9
+ }
10
+ export interface ToastMessage {
11
+ readonly id: number
12
+ readonly title?: string
13
+ readonly message: string
14
+ readonly tone: ToastTone
15
+ readonly duration: number
16
+ }
17
+ export interface ToastController {
18
+ /** Returns undefined after disposal, or after a local host has left. */
19
+ show(options: ToastOptions): number | undefined
20
+ dismiss(id?: number): void
21
+ clear(): void
22
+ }
23
+ export interface ToastSnapshot {
24
+ readonly message?: ToastMessage
25
+ readonly visible: boolean
26
+ }
27
+ export interface ToastConnection {
28
+ activate(): void
29
+ deactivate(): void
30
+ pause(reason: string, paused: boolean): void
31
+ begin(id: number): void
32
+ entered(id: number): void
33
+ left(id: number): void
34
+ dispose(): void
35
+ }
36
+
37
+ const queues = new WeakMap<ToastController, ToastQueue>()
38
+ let nextId = 0
39
+
40
+ function notification(options: ToastOptions): ToastMessage {
41
+ if (!options || typeof options.message !== 'string' || !options.message.trim())
42
+ throw new TypeError('Toast message must be a non-empty string')
43
+ if (options.title !== undefined && (typeof options.title !== 'string' || !options.title.trim()))
44
+ throw new TypeError('Toast title must be a non-empty string when supplied')
45
+ const tone = options.tone ?? 'info'
46
+ if (!['success', 'error', 'warning', 'info'].includes(tone))
47
+ throw new RangeError(`Unknown Toast tone: ${String(tone)}`)
48
+ const duration = options.duration ?? 5000
49
+ if (!Number.isFinite(duration) || duration < 0 || duration > 2_147_483_647)
50
+ throw new RangeError('Toast duration must be between 0 and 2147483647 milliseconds')
51
+ return Object.freeze({
52
+ id: ++nextId,
53
+ title: options.title,
54
+ message: options.message,
55
+ tone,
56
+ duration,
57
+ })
58
+ }
59
+
60
+ class ToastQueue {
61
+ items: ToastMessage[] = []
62
+ listener?: (snapshot: ToastSnapshot) => void
63
+ bound = 0
64
+ owner?: object
65
+
66
+ constructor(readonly persistent = false) {}
67
+ active = false
68
+ accepting = true
69
+ disposed = false
70
+ presenting = false
71
+ entered = false
72
+ closing = false
73
+ remaining = 0
74
+ started = 0
75
+ timer?: ReturnType<typeof setTimeout>
76
+ pauses = new Set<string>()
77
+
78
+ publish() {
79
+ this.listener?.({ message: this.items[0], visible: this.active && !this.closing })
80
+ }
81
+ stop() {
82
+ if (this.timer === undefined) return
83
+ clearTimeout(this.timer)
84
+ this.timer = undefined
85
+ this.remaining = Math.max(0, this.remaining - Math.max(0, Date.now() - this.started))
86
+ }
87
+ run() {
88
+ const item = this.items[0]
89
+ if (
90
+ !item ||
91
+ !item.duration ||
92
+ !this.active ||
93
+ !this.entered ||
94
+ this.closing ||
95
+ this.pauses.size
96
+ )
97
+ return
98
+ if (this.timer !== undefined) return
99
+ this.started = Date.now()
100
+ this.timer = setTimeout(() => {
101
+ this.timer = undefined
102
+ if (this.items[0]?.id === item.id) this.dismiss(item.id)
103
+ }, this.remaining)
104
+ }
105
+ advance() {
106
+ this.stop()
107
+ this.presenting = this.entered = this.closing = false
108
+ this.remaining = this.items[0]?.duration ?? 0
109
+ this.publish()
110
+ }
111
+ show(options: ToastOptions) {
112
+ const item = notification(options)
113
+ if (!this.accepting || this.disposed) return undefined
114
+ this.items.push(item)
115
+ if (this.items.length === 1) this.advance()
116
+ return item.id
117
+ }
118
+ dismiss(id = this.items[0]?.id) {
119
+ const index = this.items.findIndex((item) => item.id === id)
120
+ if (index < 0) return
121
+ if (index > 0) {
122
+ this.items.splice(index, 1)
123
+ return
124
+ }
125
+ if (this.closing) return
126
+ this.stop()
127
+ if (!this.presenting || !this.active) {
128
+ this.items.shift()
129
+ this.advance()
130
+ } else {
131
+ this.closing = true
132
+ this.publish()
133
+ }
134
+ }
135
+ clear() {
136
+ this.items.splice(1)
137
+ this.dismiss()
138
+ }
139
+ deactivate() {
140
+ this.stop()
141
+ this.active = false
142
+ this.pauses.clear()
143
+ if (this.persistent && !this.disposed) {
144
+ if (this.closing) {
145
+ this.items.shift()
146
+ this.remaining = this.items[0]?.duration ?? 0
147
+ }
148
+ this.presenting = this.entered = this.closing = false
149
+ this.publish()
150
+ } else {
151
+ this.accepting = false
152
+ this.items = []
153
+ this.advance()
154
+ }
155
+ }
156
+ }
157
+
158
+ /** Hosts own presentation; a persistent queue survives host replacement until disposed. */
159
+ export function createToastController(options: { persistent?: boolean } = {}): ToastController {
160
+ const queue = new ToastQueue(options.persistent)
161
+ const controller: ToastController = Object.freeze({
162
+ show: (options: ToastOptions) => queue.show(options),
163
+ dismiss: (id?: number) => queue.dismiss(id),
164
+ clear: () => queue.clear(),
165
+ })
166
+ queues.set(controller, queue)
167
+ return controller
168
+ }
169
+
170
+ /** @internal Local queues have one host; persistent queues hand off to the latest active host. */
171
+ export function connectToast(
172
+ controller: ToastController,
173
+ listener: (snapshot: ToastSnapshot) => void,
174
+ ): ToastConnection {
175
+ const queue = queues.get(controller)
176
+ if (!queue || queue.disposed) throw new Error('MToast needs an active Mavis Toast controller')
177
+ if (queue.bound && !queue.persistent)
178
+ throw new Error('A Toast controller can only have one MToast host')
179
+ queue.bound++
180
+ const owner = {}
181
+ const pauses = new Set<string>()
182
+ let released = false
183
+ const owns = () => !released && !queue.disposed && queue.owner === owner
184
+ const current = (id: number) => owns() && queue.items[0]?.id === id
185
+ const detach = () => {
186
+ if (queue.owner !== owner) return
187
+ queue.deactivate()
188
+ listener({ visible: false })
189
+ queue.owner = queue.listener = undefined
190
+ }
191
+ listener({ visible: false })
192
+ return {
193
+ activate() {
194
+ if (released || queue.disposed || owns()) return
195
+ if (queue.owner) {
196
+ queue.deactivate()
197
+ queue.listener?.({ visible: false })
198
+ }
199
+ queue.owner = owner
200
+ queue.listener = listener
201
+ queue.pauses = new Set(pauses)
202
+ queue.active = queue.accepting = true
203
+ queue.publish()
204
+ queue.run()
205
+ },
206
+ deactivate() {
207
+ if (released) return
208
+ detach()
209
+ pauses.clear()
210
+ },
211
+ pause(reason, paused) {
212
+ if (released) return
213
+ if (paused) pauses.add(reason)
214
+ else pauses.delete(reason)
215
+ if (!owns()) return
216
+ if (paused) queue.stop()
217
+ queue.pauses = new Set(pauses)
218
+ queue.run()
219
+ },
220
+ begin(id) {
221
+ if (current(id)) queue.presenting = true
222
+ },
223
+ entered(id) {
224
+ if (!current(id) || queue.closing) return
225
+ queue.entered = true
226
+ queue.run()
227
+ },
228
+ left(id) {
229
+ if (!current(id) || !queue.closing) return
230
+ queue.items.shift()
231
+ queue.advance()
232
+ },
233
+ dispose() {
234
+ if (released) return
235
+ detach()
236
+ released = true
237
+ queue.bound--
238
+ pauses.clear()
239
+ },
240
+ }
241
+ }
242
+
243
+ /** @internal Vue scope cleanup also rejects late asynchronous notifications. */
244
+ export function disposeToast(controller: ToastController) {
245
+ const queue = queues.get(controller)
246
+ if (!queue) return
247
+ queue.disposed = true
248
+ queue.deactivate()
249
+ }
@@ -0,0 +1,60 @@
1
+ import { defineComponent, inject, provide, watchEffect } from '@vue/runtime-core'
2
+ import { Comment, Fragment, isVNode, type VNode, type VNodeChild } from '@vue/runtime-core'
3
+
4
+ /** Adornments are decorative. Put any meaning in the button label, never another action. */
5
+ export function buttonContent(content: VNodeChild[] | undefined): VNodeChild[] {
6
+ const visit = (node: VNodeChild): void => {
7
+ if (Array.isArray(node)) {
8
+ node.forEach(visit)
9
+ return
10
+ }
11
+ if (!isVNode(node) || node.type === Comment) return
12
+ const name =
13
+ typeof node.type === 'string'
14
+ ? node.type
15
+ : typeof node.type === 'object'
16
+ ? 'name' in node.type
17
+ ? String(node.type.name)
18
+ : ''
19
+ : ''
20
+ const props = node.props ?? {}
21
+ if (
22
+ /^(button|input|textarea|select|a|Button|TextField|TextView|Switch|Slider|WebView|MButton|MInput|MSwitch|MCheckbox|MRadioGroup|MSelect|MSlider|MTabs|MChip|MAccordion|MSegmentedControl|MSheet|MDialog|MToast)$/i.test(
23
+ name,
24
+ ) ||
25
+ props.interactive ||
26
+ props.contenteditable ||
27
+ props.contentEditable ||
28
+ props.tabindex !== undefined ||
29
+ props.tabIndex !== undefined ||
30
+ Object.keys(props).some((key) => /^on(Click|Tap|Touch|Pointer|Key|Focus|LongPress)/.test(key))
31
+ ) {
32
+ throw new TypeError(
33
+ 'Button leading/trailing content must be decorative, without nested controls or interaction handlers.',
34
+ )
35
+ }
36
+ if (Array.isArray(node.children)) node.children.forEach((child) => visit(child as VNode))
37
+ else if (node.type === Fragment && node.children) visit(node.children as VNodeChild)
38
+ }
39
+ content?.forEach(visit)
40
+ return content ?? []
41
+ }
42
+
43
+ const decorationKey = Symbol('mavis.buttonDecoration')
44
+
45
+ /** @internal Also guards Mavis controls rendered inside custom decorative components. */
46
+ export const ButtonAdornment = defineComponent({
47
+ setup(_props, { slots }) {
48
+ provide(decorationKey, true)
49
+ return () => slots.default?.()
50
+ },
51
+ })
52
+
53
+ /** @internal Stateful controls cannot become descendants of a decorative button slot. */
54
+ export function useButtonAction(interactive: () => boolean = () => true) {
55
+ if (!inject(decorationKey, false)) return
56
+ watchEffect(() => {
57
+ if (interactive())
58
+ throw new TypeError('Button adornments cannot contain interactive Mavis components.')
59
+ })
60
+ }
@@ -0,0 +1,13 @@
1
+ import { onActivated, onDeactivated, ref } from '@vue/runtime-core'
2
+
3
+ /** @internal Vue caches instances, but application observers still need an explicit pause. */
4
+ export function useCachedActivity() {
5
+ const active = ref(true)
6
+ onActivated(() => {
7
+ active.value = true
8
+ })
9
+ onDeactivated(() => {
10
+ active.value = false
11
+ })
12
+ return active
13
+ }