@getcortexlabs/mavis-web 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.
- package/README.md +4 -4
- package/dist/components/MAccordion.d.ts +109 -0
- package/dist/components/MAccordion.d.ts.map +1 -0
- package/dist/components/MAccordion.js +180 -0
- package/dist/components/MAccordion.js.map +1 -0
- package/dist/components/MAlert.d.ts +99 -0
- package/dist/components/MAlert.d.ts.map +1 -0
- package/dist/components/MAlert.js +88 -0
- package/dist/components/MAlert.js.map +1 -0
- package/dist/components/MAvatar.d.ts +74 -0
- package/dist/components/MAvatar.d.ts.map +1 -0
- package/dist/components/MAvatar.js +88 -0
- package/dist/components/MAvatar.js.map +1 -0
- package/dist/components/MBadge.d.ts +64 -0
- package/dist/components/MBadge.d.ts.map +1 -0
- package/dist/components/MBadge.js +35 -0
- package/dist/components/MBadge.js.map +1 -0
- package/dist/components/MButton.d.ts +59 -7
- package/dist/components/MButton.d.ts.map +1 -1
- package/dist/components/MButton.js +51 -18
- package/dist/components/MButton.js.map +1 -1
- package/dist/components/MCard.d.ts +23 -5
- package/dist/components/MCard.d.ts.map +1 -1
- package/dist/components/MCard.js +28 -6
- package/dist/components/MCard.js.map +1 -1
- package/dist/components/MCheckbox.d.ts +93 -0
- package/dist/components/MCheckbox.d.ts.map +1 -0
- package/dist/components/MCheckbox.js +108 -0
- package/dist/components/MCheckbox.js.map +1 -0
- package/dist/components/MChip.d.ts +107 -0
- package/dist/components/MChip.d.ts.map +1 -0
- package/dist/components/MChip.js +103 -0
- package/dist/components/MChip.js.map +1 -0
- package/dist/components/MDialog.d.ts +94 -0
- package/dist/components/MDialog.d.ts.map +1 -0
- package/dist/components/MDialog.js +214 -0
- package/dist/components/MDialog.js.map +1 -0
- package/dist/components/MEmptyState.d.ts +107 -0
- package/dist/components/MEmptyState.d.ts.map +1 -0
- package/dist/components/MEmptyState.js +79 -0
- package/dist/components/MEmptyState.js.map +1 -0
- package/dist/components/MIcon.d.ts +1 -1
- package/dist/components/MInput.d.ts +66 -8
- package/dist/components/MInput.d.ts.map +1 -1
- package/dist/components/MInput.js +96 -20
- package/dist/components/MInput.js.map +1 -1
- package/dist/components/MListItem.d.ts +92 -0
- package/dist/components/MListItem.d.ts.map +1 -0
- package/dist/components/MListItem.js +53 -0
- package/dist/components/MListItem.js.map +1 -0
- package/dist/components/MProgress.d.ts +112 -0
- package/dist/components/MProgress.d.ts.map +1 -0
- package/dist/components/MProgress.js +69 -0
- package/dist/components/MProgress.js.map +1 -0
- package/dist/components/MRadioGroup.d.ts +94 -0
- package/dist/components/MRadioGroup.d.ts.map +1 -0
- package/dist/components/MRadioGroup.js +167 -0
- package/dist/components/MRadioGroup.js.map +1 -0
- package/dist/components/MReveal.d.ts.map +1 -1
- package/dist/components/MReveal.js +28 -3
- package/dist/components/MReveal.js.map +1 -1
- package/dist/components/MScreen.d.ts +19 -1
- package/dist/components/MScreen.d.ts.map +1 -1
- package/dist/components/MScreen.js +30 -1
- package/dist/components/MScreen.js.map +1 -1
- package/dist/components/MSegmentedControl.d.ts +85 -0
- package/dist/components/MSegmentedControl.d.ts.map +1 -0
- package/dist/components/MSegmentedControl.js +128 -0
- package/dist/components/MSegmentedControl.js.map +1 -0
- package/dist/components/MSelect.d.ts +130 -0
- package/dist/components/MSelect.d.ts.map +1 -0
- package/dist/components/MSelect.js +308 -0
- package/dist/components/MSelect.js.map +1 -0
- package/dist/components/MSeparator.d.ts +54 -0
- package/dist/components/MSeparator.d.ts.map +1 -0
- package/dist/components/MSeparator.js +36 -0
- package/dist/components/MSeparator.js.map +1 -0
- package/dist/components/MSheet.d.ts +102 -0
- package/dist/components/MSheet.d.ts.map +1 -0
- package/dist/components/MSheet.js +265 -0
- package/dist/components/MSheet.js.map +1 -0
- package/dist/components/MSkeleton.d.ts +70 -0
- package/dist/components/MSkeleton.d.ts.map +1 -0
- package/dist/components/MSkeleton.js +43 -0
- package/dist/components/MSkeleton.js.map +1 -0
- package/dist/components/MSlider.d.ts +1 -1
- package/dist/components/MSlider.d.ts.map +1 -1
- package/dist/components/MSlider.js +2 -1
- package/dist/components/MSlider.js.map +1 -1
- package/dist/components/MSwitch.d.ts +79 -0
- package/dist/components/MSwitch.d.ts.map +1 -0
- package/dist/components/MSwitch.js +87 -0
- package/dist/components/MSwitch.js.map +1 -0
- package/dist/components/MTabs.d.ts +91 -0
- package/dist/components/MTabs.d.ts.map +1 -0
- package/dist/components/MTabs.js +228 -0
- package/dist/components/MTabs.js.map +1 -0
- package/dist/components/MText.d.ts +18 -9
- package/dist/components/MText.d.ts.map +1 -1
- package/dist/components/MText.js +26 -7
- package/dist/components/MText.js.map +1 -1
- package/dist/components/MToast.d.ts +24 -0
- package/dist/components/MToast.d.ts.map +1 -0
- package/dist/components/MToast.js +196 -0
- package/dist/components/MToast.js.map +1 -0
- package/dist/index.d.ts +27 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +46 -4
- package/dist/index.js.map +1 -1
- package/dist/input-attrs.d.ts +15 -0
- package/dist/input-attrs.d.ts.map +1 -0
- package/dist/input-attrs.js +18 -0
- package/dist/input-attrs.js.map +1 -0
- package/dist/motion-surface.d.ts +8 -0
- package/dist/motion-surface.d.ts.map +1 -0
- package/dist/motion-surface.js +79 -0
- package/dist/motion-surface.js.map +1 -0
- package/dist/motion.d.ts +20 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +227 -0
- package/dist/motion.js.map +1 -0
- package/dist/screen-viewport.d.ts.map +1 -1
- package/dist/screen-viewport.js +3 -0
- package/dist/screen-viewport.js.map +1 -1
- package/dist/scroll-motion.d.ts +5 -0
- package/dist/scroll-motion.d.ts.map +1 -0
- package/dist/scroll-motion.js +69 -0
- package/dist/scroll-motion.js.map +1 -0
- package/dist/sheet-motion.d.ts +8 -0
- package/dist/sheet-motion.d.ts.map +1 -0
- package/dist/sheet-motion.js +148 -0
- package/dist/sheet-motion.js.map +1 -0
- package/dist/style.d.ts.map +1 -1
- package/dist/style.js +24 -9
- package/dist/style.js.map +1 -1
- package/package.json +4 -3
- package/src/components/MAccordion.ts +231 -0
- package/src/components/MAlert.ts +109 -0
- package/src/components/MAvatar.ts +97 -0
- package/src/components/MBadge.ts +40 -0
- package/src/components/MButton.ts +68 -15
- package/src/components/MCard.ts +43 -5
- package/src/components/MCheckbox.ts +127 -0
- package/src/components/MChip.ts +109 -0
- package/src/components/MDialog.ts +245 -0
- package/src/components/MEmptyState.ts +96 -0
- package/src/components/MInput.ts +166 -71
- package/src/components/MListItem.ts +66 -0
- package/src/components/MProgress.ts +82 -0
- package/src/components/MRadioGroup.ts +184 -0
- package/src/components/MReveal.ts +40 -3
- package/src/components/MScreen.ts +31 -1
- package/src/components/MSegmentedControl.ts +151 -0
- package/src/components/MSelect.ts +362 -0
- package/src/components/MSeparator.ts +40 -0
- package/src/components/MSheet.ts +317 -0
- package/src/components/MSkeleton.ts +52 -0
- package/src/components/MSlider.ts +7 -1
- package/src/components/MSwitch.ts +108 -0
- package/src/components/MTabs.ts +275 -0
- package/src/components/MText.ts +30 -7
- package/src/components/MToast.ts +241 -0
- package/src/index.ts +82 -4
- package/src/input-attrs.ts +41 -0
- package/src/motion-surface.ts +86 -0
- package/src/motion.ts +275 -0
- package/src/screen-viewport.ts +2 -0
- package/src/scroll-motion.ts +75 -0
- package/src/sheet-motion.ts +152 -0
- package/src/style.css +861 -39
- package/src/style.ts +20 -9
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { fontFamily } from '@getcortexlabs/mavis-core'
|
|
2
|
+
import {
|
|
3
|
+
computed,
|
|
4
|
+
KeepAlive,
|
|
5
|
+
defineComponent,
|
|
6
|
+
h,
|
|
7
|
+
mergeProps,
|
|
8
|
+
nextTick,
|
|
9
|
+
onBeforeUnmount,
|
|
10
|
+
onActivated,
|
|
11
|
+
onDeactivated,
|
|
12
|
+
onMounted,
|
|
13
|
+
ref,
|
|
14
|
+
useId,
|
|
15
|
+
watch,
|
|
16
|
+
type SlotsType,
|
|
17
|
+
} from 'vue'
|
|
18
|
+
import {
|
|
19
|
+
canChoose,
|
|
20
|
+
choiceKey,
|
|
21
|
+
tabFocusTarget,
|
|
22
|
+
validateTabs,
|
|
23
|
+
type TabOption,
|
|
24
|
+
type TabPanelProps,
|
|
25
|
+
} from '@getcortexlabs/mavis-core'
|
|
26
|
+
import { useButtonAction, tabsProps, tabsEmits, useMavisTheme } from '@getcortexlabs/mavis-core/vue'
|
|
27
|
+
import { MColumn } from './layout.js'
|
|
28
|
+
|
|
29
|
+
const TabPanel = defineComponent({
|
|
30
|
+
name: 'MTabPanel',
|
|
31
|
+
setup(_props, { slots }) {
|
|
32
|
+
useButtonAction()
|
|
33
|
+
return () => h('div', {}, slots.default?.())
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
export const MTabs = defineComponent({
|
|
38
|
+
name: 'MTabs',
|
|
39
|
+
inheritAttrs: false,
|
|
40
|
+
props: tabsProps,
|
|
41
|
+
emits: tabsEmits,
|
|
42
|
+
slots: Object as SlotsType<{ default: TabPanelProps }>,
|
|
43
|
+
setup(props, { attrs, emit, slots }) {
|
|
44
|
+
const { theme } = useMavisTheme()
|
|
45
|
+
const generated = `m-tabs-${useId()}`
|
|
46
|
+
const controls = new Map<string, HTMLButtonElement>()
|
|
47
|
+
const track = ref<HTMLElement>(),
|
|
48
|
+
scroller = ref<HTMLElement>()
|
|
49
|
+
const marker = ref<{ left: number; width: number }>()
|
|
50
|
+
const focusValue = ref(props.modelValue)
|
|
51
|
+
const selected = computed(() => {
|
|
52
|
+
validateTabs(props.tabs, props.modelValue, props.label)
|
|
53
|
+
return props.tabs.find((tab) => tab.value === props.modelValue)
|
|
54
|
+
})
|
|
55
|
+
let observer: ResizeObserver | undefined
|
|
56
|
+
let frame: number | undefined
|
|
57
|
+
let alive = false
|
|
58
|
+
function reveal(button: HTMLButtonElement) {
|
|
59
|
+
if (!scroller.value) return
|
|
60
|
+
const bounds = button.getBoundingClientRect(),
|
|
61
|
+
viewport = scroller.value.getBoundingClientRect()
|
|
62
|
+
const delta =
|
|
63
|
+
bounds.left < viewport.left
|
|
64
|
+
? bounds.left - viewport.left
|
|
65
|
+
: bounds.right > viewport.right
|
|
66
|
+
? bounds.right - viewport.right
|
|
67
|
+
: 0
|
|
68
|
+
if (delta) scroller.value.scrollBy({ left: delta, behavior: 'instant' })
|
|
69
|
+
}
|
|
70
|
+
function measure(revealSelection = false) {
|
|
71
|
+
if (!alive) return
|
|
72
|
+
if (frame !== undefined) cancelAnimationFrame(frame)
|
|
73
|
+
frame = requestAnimationFrame(() => {
|
|
74
|
+
frame = undefined
|
|
75
|
+
const button = selected.value && controls.get(choiceKey(selected.value.value))
|
|
76
|
+
marker.value = button ? { left: button.offsetLeft, width: button.offsetWidth } : undefined
|
|
77
|
+
if (button && revealSelection) reveal(button)
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
function observe() {
|
|
81
|
+
observer?.disconnect()
|
|
82
|
+
if (!alive) return
|
|
83
|
+
if (track.value) observer?.observe(track.value)
|
|
84
|
+
if (scroller.value) observer?.observe(scroller.value)
|
|
85
|
+
for (const button of controls.values()) observer?.observe(button)
|
|
86
|
+
measure(true)
|
|
87
|
+
}
|
|
88
|
+
const start = () => {
|
|
89
|
+
if (alive) return
|
|
90
|
+
alive = true
|
|
91
|
+
observer = new ResizeObserver(() => measure(true))
|
|
92
|
+
observe()
|
|
93
|
+
}
|
|
94
|
+
const stop = () => {
|
|
95
|
+
alive = false
|
|
96
|
+
observer?.disconnect()
|
|
97
|
+
if (frame !== undefined) cancelAnimationFrame(frame)
|
|
98
|
+
frame = undefined
|
|
99
|
+
}
|
|
100
|
+
onMounted(start)
|
|
101
|
+
onActivated(start)
|
|
102
|
+
onDeactivated(stop)
|
|
103
|
+
watch(
|
|
104
|
+
() => [props.modelValue, props.tabs, props.disabled],
|
|
105
|
+
() => {
|
|
106
|
+
const focused = typeof document !== 'undefined' ? document.activeElement : null
|
|
107
|
+
const ownedFocus = focused && track.value?.contains(focused)
|
|
108
|
+
focusValue.value = props.modelValue
|
|
109
|
+
void nextTick(() => {
|
|
110
|
+
observe()
|
|
111
|
+
if (!alive || !ownedFocus || props.disabled) return
|
|
112
|
+
const stillAvailable = props.tabs.some(
|
|
113
|
+
(tab) => !tab.disabled && controls.get(choiceKey(tab.value)) === focused,
|
|
114
|
+
)
|
|
115
|
+
if (stillAvailable) return
|
|
116
|
+
const next =
|
|
117
|
+
props.tabs.find((tab) => !tab.disabled && tab.value === props.modelValue) ??
|
|
118
|
+
props.tabs.find((tab) => !tab.disabled)
|
|
119
|
+
const button = next && controls.get(choiceKey(next.value))
|
|
120
|
+
button?.focus({ preventScroll: true })
|
|
121
|
+
if (button) reveal(button)
|
|
122
|
+
})
|
|
123
|
+
},
|
|
124
|
+
{ deep: true, flush: 'pre' },
|
|
125
|
+
)
|
|
126
|
+
onBeforeUnmount(() => {
|
|
127
|
+
alive = false
|
|
128
|
+
observer?.disconnect()
|
|
129
|
+
if (frame !== undefined) cancelAnimationFrame(frame)
|
|
130
|
+
controls.clear()
|
|
131
|
+
})
|
|
132
|
+
function request(tab: TabOption) {
|
|
133
|
+
if (canChoose(tab, props.modelValue, props.disabled)) emit('update:modelValue', tab.value)
|
|
134
|
+
}
|
|
135
|
+
function keydown(event: KeyboardEvent, tab: TabOption) {
|
|
136
|
+
if (props.disabled || tab.disabled || event.altKey || event.ctrlKey || event.metaKey) return
|
|
137
|
+
const rtl = track.value && getComputedStyle(track.value).direction === 'rtl'
|
|
138
|
+
const next = tabFocusTarget(props.tabs, tab.value, event.key, rtl)
|
|
139
|
+
if (!next) return
|
|
140
|
+
event.preventDefault()
|
|
141
|
+
focusValue.value = next.value
|
|
142
|
+
const button = controls.get(choiceKey(next.value))
|
|
143
|
+
button?.focus({ preventScroll: true })
|
|
144
|
+
if (button) reveal(button)
|
|
145
|
+
}
|
|
146
|
+
return () => {
|
|
147
|
+
const active = selected.value
|
|
148
|
+
const { colors, typography } = theme.value
|
|
149
|
+
const id = props.id ?? generated
|
|
150
|
+
const tabId = (tab: TabOption) => `${id}-${encodeURIComponent(choiceKey(tab.value))}`
|
|
151
|
+
const focus =
|
|
152
|
+
props.tabs.find((tab) => !tab.disabled && tab.value === focusValue.value) ??
|
|
153
|
+
props.tabs.find((tab) => !tab.disabled)
|
|
154
|
+
return h(
|
|
155
|
+
MColumn,
|
|
156
|
+
mergeProps(attrs, {
|
|
157
|
+
id,
|
|
158
|
+
width: props.width,
|
|
159
|
+
grow: props.grow,
|
|
160
|
+
shrink: props.shrink,
|
|
161
|
+
accessibilityHidden: props.accessibilityHidden,
|
|
162
|
+
gap: 'md',
|
|
163
|
+
}),
|
|
164
|
+
{
|
|
165
|
+
default: () => [
|
|
166
|
+
h(
|
|
167
|
+
'div',
|
|
168
|
+
{ ref: scroller, class: 'm-tabs-scroll', style: { '--m-focus': colors.primary } },
|
|
169
|
+
[
|
|
170
|
+
h(
|
|
171
|
+
'div',
|
|
172
|
+
{
|
|
173
|
+
ref: track,
|
|
174
|
+
class: 'm-tabs-track',
|
|
175
|
+
role: 'tablist',
|
|
176
|
+
'aria-label': props.label,
|
|
177
|
+
'aria-orientation': 'horizontal',
|
|
178
|
+
'aria-disabled': props.disabled || undefined,
|
|
179
|
+
style: {
|
|
180
|
+
borderBottomColor: colors.border,
|
|
181
|
+
fontFamily: fontFamily(theme.value.typography.body.fontFamily, 'web'),
|
|
182
|
+
fontSize: `${typography.body.fontSize}px`,
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
[
|
|
186
|
+
...props.tabs.map((tab) =>
|
|
187
|
+
h(
|
|
188
|
+
'button',
|
|
189
|
+
{
|
|
190
|
+
key: choiceKey(tab.value),
|
|
191
|
+
id: tabId(tab),
|
|
192
|
+
class: 'm-tab',
|
|
193
|
+
type: 'button',
|
|
194
|
+
role: 'tab',
|
|
195
|
+
ref: (element: unknown) => {
|
|
196
|
+
if (element)
|
|
197
|
+
controls.set(choiceKey(tab.value), element as HTMLButtonElement)
|
|
198
|
+
else controls.delete(choiceKey(tab.value))
|
|
199
|
+
},
|
|
200
|
+
disabled: props.disabled || tab.disabled,
|
|
201
|
+
tabindex: !props.disabled && focus?.value === tab.value ? 0 : -1,
|
|
202
|
+
'aria-selected': active?.value === tab.value,
|
|
203
|
+
'aria-controls': active?.value === tab.value ? `${id}-panel` : undefined,
|
|
204
|
+
style: {
|
|
205
|
+
color: active?.value === tab.value ? colors.primary : colors.muted,
|
|
206
|
+
'--m-tab-active': colors.primarySoft,
|
|
207
|
+
},
|
|
208
|
+
onFocus: () => {
|
|
209
|
+
focusValue.value = tab.value
|
|
210
|
+
},
|
|
211
|
+
onClick: () => request(tab),
|
|
212
|
+
onKeydown: (event: KeyboardEvent) => keydown(event, tab),
|
|
213
|
+
},
|
|
214
|
+
tab.label,
|
|
215
|
+
),
|
|
216
|
+
),
|
|
217
|
+
h('span', {
|
|
218
|
+
class: 'm-tabs-indicator',
|
|
219
|
+
'aria-hidden': 'true',
|
|
220
|
+
style: {
|
|
221
|
+
backgroundColor: colors.primary,
|
|
222
|
+
opacity: marker.value ? 1 : 0,
|
|
223
|
+
width: `${marker.value?.width ?? 0}px`,
|
|
224
|
+
transform: `translateX(${marker.value?.left ?? 0}px)`,
|
|
225
|
+
},
|
|
226
|
+
}),
|
|
227
|
+
],
|
|
228
|
+
),
|
|
229
|
+
],
|
|
230
|
+
),
|
|
231
|
+
props.keepAlive
|
|
232
|
+
? props.tabs.map((tab) =>
|
|
233
|
+
h(
|
|
234
|
+
KeepAlive,
|
|
235
|
+
{ key: choiceKey(tab.value), max: 1 },
|
|
236
|
+
{
|
|
237
|
+
default: () =>
|
|
238
|
+
active?.value === tab.value
|
|
239
|
+
? h(
|
|
240
|
+
TabPanel,
|
|
241
|
+
{
|
|
242
|
+
id: `${id}-panel`,
|
|
243
|
+
class: 'm-tab-panel',
|
|
244
|
+
role: 'tabpanel',
|
|
245
|
+
tabindex: 0,
|
|
246
|
+
'aria-labelledby': tabId(tab),
|
|
247
|
+
style: { '--m-focus': colors.primary },
|
|
248
|
+
},
|
|
249
|
+
{ default: () => slots.default?.({ value: tab.value }) },
|
|
250
|
+
)
|
|
251
|
+
: null,
|
|
252
|
+
},
|
|
253
|
+
),
|
|
254
|
+
)
|
|
255
|
+
: active
|
|
256
|
+
? h(
|
|
257
|
+
'div',
|
|
258
|
+
{
|
|
259
|
+
key: choiceKey(active.value),
|
|
260
|
+
id: `${id}-panel`,
|
|
261
|
+
class: 'm-tab-panel',
|
|
262
|
+
role: 'tabpanel',
|
|
263
|
+
tabindex: 0,
|
|
264
|
+
'aria-labelledby': tabId(active),
|
|
265
|
+
style: { '--m-focus': colors.primary },
|
|
266
|
+
},
|
|
267
|
+
slots.default?.({ value: active.value }),
|
|
268
|
+
)
|
|
269
|
+
: null,
|
|
270
|
+
],
|
|
271
|
+
},
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
})
|
package/src/components/MText.ts
CHANGED
|
@@ -2,11 +2,11 @@ import { defineComponent, h, mergeProps } from 'vue'
|
|
|
2
2
|
import {
|
|
3
3
|
textProps,
|
|
4
4
|
useMavisTheme,
|
|
5
|
-
|
|
5
|
+
inlineContent,
|
|
6
6
|
useDefaultShrink,
|
|
7
7
|
viewAttrs,
|
|
8
8
|
} from '@getcortexlabs/mavis-core/vue'
|
|
9
|
-
import { nonNegative } from '@getcortexlabs/mavis-core'
|
|
9
|
+
import { fontFamily, nonNegative, validateWeight } from '@getcortexlabs/mavis-core'
|
|
10
10
|
import { webAttrs } from '../style.js'
|
|
11
11
|
|
|
12
12
|
export const MText = defineComponent({
|
|
@@ -18,16 +18,22 @@ export const MText = defineComponent({
|
|
|
18
18
|
const shrink = useDefaultShrink()
|
|
19
19
|
return () => {
|
|
20
20
|
const type = theme.value.typography[props.variant]
|
|
21
|
+
const runs =
|
|
22
|
+
props.text !== undefined ? [{ text: props.text }] : inlineContent(slots.default?.())
|
|
21
23
|
return h(
|
|
22
24
|
props.as,
|
|
23
25
|
mergeProps(
|
|
24
26
|
{
|
|
27
|
+
class: 'm-text',
|
|
25
28
|
style: {
|
|
26
29
|
margin: 0,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
fontFamily: `var(--m-text-font-family, ${fontFamily(type.fontFamily, 'web')})`,
|
|
31
|
+
fontSize: `var(--m-text-font-size, ${type.fontSize}px)`,
|
|
32
|
+
fontWeight: props.weight
|
|
33
|
+
? validateWeight(props.weight)
|
|
34
|
+
: `var(--m-text-font-weight, ${type.fontWeight})`,
|
|
35
|
+
letterSpacing: `var(--m-text-letter-spacing, ${type.letterSpacing}em)`,
|
|
36
|
+
lineHeight: `var(--m-text-line-height, ${type.lineHeight})`,
|
|
31
37
|
color: theme.value.colors[props.tone],
|
|
32
38
|
textAlign: props.align,
|
|
33
39
|
whiteSpace: props.wrap ? 'normal' : 'nowrap',
|
|
@@ -39,7 +45,24 @@ export const MText = defineComponent({
|
|
|
39
45
|
},
|
|
40
46
|
webAttrs({ ...attrs, ...viewAttrs(props) }),
|
|
41
47
|
),
|
|
42
|
-
|
|
48
|
+
runs.every((run) => !run.weight && !run.tone && !run.strong && !run.italic)
|
|
49
|
+
? runs.map((run) => run.text).join('')
|
|
50
|
+
: runs.map((run) => {
|
|
51
|
+
if (!run.weight && !run.tone && !run.strong && !run.italic) return run.text
|
|
52
|
+
if (run.tone && !(run.tone in theme.value.colors))
|
|
53
|
+
throw new RangeError(`Unknown text tone: ${run.tone}`)
|
|
54
|
+
const content = run.strong && run.italic ? h('em', {}, run.text) : run.text
|
|
55
|
+
return h(
|
|
56
|
+
run.strong ? 'strong' : run.italic ? 'em' : 'span',
|
|
57
|
+
{
|
|
58
|
+
style: {
|
|
59
|
+
fontWeight: run.weight ?? 'inherit',
|
|
60
|
+
color: run.tone ? theme.value.colors[run.tone] : undefined,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
content,
|
|
64
|
+
)
|
|
65
|
+
}),
|
|
43
66
|
)
|
|
44
67
|
}
|
|
45
68
|
},
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { fontFamily } from '@getcortexlabs/mavis-core'
|
|
2
|
+
import {
|
|
3
|
+
defineComponent,
|
|
4
|
+
h,
|
|
5
|
+
onBeforeUnmount,
|
|
6
|
+
onActivated,
|
|
7
|
+
onDeactivated,
|
|
8
|
+
onMounted,
|
|
9
|
+
ref,
|
|
10
|
+
Teleport,
|
|
11
|
+
Transition,
|
|
12
|
+
watch,
|
|
13
|
+
} from 'vue'
|
|
14
|
+
import {
|
|
15
|
+
useButtonAction,
|
|
16
|
+
toastProps,
|
|
17
|
+
useMavisTheme,
|
|
18
|
+
useToastHost,
|
|
19
|
+
} from '@getcortexlabs/mavis-core/vue'
|
|
20
|
+
import type { IconName, ToastTone } from '@getcortexlabs/mavis-core'
|
|
21
|
+
import { MIcon } from './MIcon.js'
|
|
22
|
+
|
|
23
|
+
const icons: Record<ToastTone, IconName> = {
|
|
24
|
+
success: 'circle-check',
|
|
25
|
+
error: 'circle-alert',
|
|
26
|
+
warning: 'triangle-alert',
|
|
27
|
+
info: 'info',
|
|
28
|
+
}
|
|
29
|
+
const tones = { success: 'success', error: 'danger', warning: 'warning', info: 'accent' } as const
|
|
30
|
+
|
|
31
|
+
export const MToast = defineComponent({
|
|
32
|
+
name: 'MToast',
|
|
33
|
+
inheritAttrs: false,
|
|
34
|
+
props: toastProps,
|
|
35
|
+
setup(props) {
|
|
36
|
+
useButtonAction()
|
|
37
|
+
const { theme } = useMavisTheme()
|
|
38
|
+
const host = useToastHost(() => props.controller)
|
|
39
|
+
const mounted = ref(false)
|
|
40
|
+
const covered = ref(false)
|
|
41
|
+
const top = ref(0)
|
|
42
|
+
const announcement = ref('')
|
|
43
|
+
let enteredId: number | undefined
|
|
44
|
+
function announce() {
|
|
45
|
+
const item = host.snapshot.value.message
|
|
46
|
+
if (!covered.value && item && item.id === enteredId)
|
|
47
|
+
announcement.value = [item.title, item.message].filter(Boolean).join('. ')
|
|
48
|
+
}
|
|
49
|
+
watch(covered, announce)
|
|
50
|
+
let opener: HTMLElement | undefined
|
|
51
|
+
let observer: MutationObserver | undefined
|
|
52
|
+
let viewport: VisualViewport | null = null
|
|
53
|
+
function updateVisibility() {
|
|
54
|
+
host.pause('background', document.hidden)
|
|
55
|
+
}
|
|
56
|
+
function updateModal() {
|
|
57
|
+
covered.value = Boolean(document.querySelector('dialog:modal'))
|
|
58
|
+
host.pause('modal', covered.value)
|
|
59
|
+
}
|
|
60
|
+
function updateViewport() {
|
|
61
|
+
top.value = viewport?.scale === 1 ? viewport.offsetTop : 0
|
|
62
|
+
}
|
|
63
|
+
function restoreFocus(element: Element) {
|
|
64
|
+
if (element.contains(document.activeElement) && opener?.isConnected)
|
|
65
|
+
opener.focus({ preventScroll: true })
|
|
66
|
+
}
|
|
67
|
+
let listening = false
|
|
68
|
+
const start = () => {
|
|
69
|
+
if (listening) return
|
|
70
|
+
listening = true
|
|
71
|
+
if (typeof document === 'undefined') return
|
|
72
|
+
mounted.value = true
|
|
73
|
+
updateVisibility()
|
|
74
|
+
updateModal()
|
|
75
|
+
document.addEventListener('visibilitychange', updateVisibility)
|
|
76
|
+
observer = new MutationObserver(updateModal)
|
|
77
|
+
observer.observe(document.body, {
|
|
78
|
+
subtree: true,
|
|
79
|
+
childList: true,
|
|
80
|
+
attributes: true,
|
|
81
|
+
attributeFilter: ['open'],
|
|
82
|
+
})
|
|
83
|
+
viewport = window.visualViewport
|
|
84
|
+
viewport?.addEventListener('resize', updateViewport)
|
|
85
|
+
viewport?.addEventListener('scroll', updateViewport)
|
|
86
|
+
updateViewport()
|
|
87
|
+
host.activate()
|
|
88
|
+
}
|
|
89
|
+
const stop = () => {
|
|
90
|
+
listening = false
|
|
91
|
+
mounted.value = false
|
|
92
|
+
observer?.disconnect()
|
|
93
|
+
if (typeof document !== 'undefined')
|
|
94
|
+
document.removeEventListener('visibilitychange', updateVisibility)
|
|
95
|
+
viewport?.removeEventListener('resize', updateViewport)
|
|
96
|
+
viewport?.removeEventListener('scroll', updateViewport)
|
|
97
|
+
host.deactivate()
|
|
98
|
+
}
|
|
99
|
+
onMounted(start)
|
|
100
|
+
onActivated(start)
|
|
101
|
+
onDeactivated(stop)
|
|
102
|
+
onBeforeUnmount(stop)
|
|
103
|
+
return () => {
|
|
104
|
+
if (!mounted.value) return null
|
|
105
|
+
const { colors, typography, radius } = theme.value
|
|
106
|
+
const { message, visible } = host.snapshot.value
|
|
107
|
+
const tone = message ? tones[message.tone] : 'accent'
|
|
108
|
+
return h(Teleport, { to: 'body' }, [
|
|
109
|
+
h(
|
|
110
|
+
'div',
|
|
111
|
+
{
|
|
112
|
+
class: 'm-toast-host',
|
|
113
|
+
hidden: covered.value,
|
|
114
|
+
style: {
|
|
115
|
+
top: `calc(${top.value + 16}px + env(safe-area-inset-top, 0px))`,
|
|
116
|
+
'--m-focus': colors.primary,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
[
|
|
120
|
+
// Keep the live region mounted before its text changes. The close control stays outside it.
|
|
121
|
+
h(
|
|
122
|
+
'span',
|
|
123
|
+
{
|
|
124
|
+
class: 'm-toast-announcement',
|
|
125
|
+
role: 'status',
|
|
126
|
+
'aria-live': 'polite',
|
|
127
|
+
'aria-atomic': 'true',
|
|
128
|
+
},
|
|
129
|
+
announcement.value,
|
|
130
|
+
),
|
|
131
|
+
h(
|
|
132
|
+
Transition,
|
|
133
|
+
{
|
|
134
|
+
name: 'm-toast',
|
|
135
|
+
appear: true,
|
|
136
|
+
onBeforeEnter: () => {
|
|
137
|
+
if (!message) return
|
|
138
|
+
host.begin(message.id)
|
|
139
|
+
opener =
|
|
140
|
+
document.activeElement instanceof HTMLElement
|
|
141
|
+
? document.activeElement
|
|
142
|
+
: undefined
|
|
143
|
+
enteredId = undefined
|
|
144
|
+
announcement.value = ''
|
|
145
|
+
},
|
|
146
|
+
onAfterEnter: () => {
|
|
147
|
+
if (!message) return
|
|
148
|
+
enteredId = message.id
|
|
149
|
+
announce()
|
|
150
|
+
host.entered(message.id)
|
|
151
|
+
},
|
|
152
|
+
onBeforeLeave: (element: Element) => {
|
|
153
|
+
restoreFocus(element)
|
|
154
|
+
element.setAttribute('inert', '')
|
|
155
|
+
element.setAttribute('aria-hidden', 'true')
|
|
156
|
+
announcement.value = ''
|
|
157
|
+
host.pause('hover', false)
|
|
158
|
+
host.pause('focus', false)
|
|
159
|
+
},
|
|
160
|
+
onAfterLeave: () => {
|
|
161
|
+
if (message) host.left(message.id)
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
default: () =>
|
|
166
|
+
message && visible
|
|
167
|
+
? h(
|
|
168
|
+
'div',
|
|
169
|
+
{
|
|
170
|
+
key: message.id,
|
|
171
|
+
class: 'm-toast',
|
|
172
|
+
'data-tone': message.tone,
|
|
173
|
+
style: {
|
|
174
|
+
backgroundColor: colors.surface,
|
|
175
|
+
color: colors.text,
|
|
176
|
+
borderColor: colors.border,
|
|
177
|
+
borderRadius: `${radius.lg}px`,
|
|
178
|
+
fontFamily: fontFamily(theme.value.typography.body.fontFamily, 'web'),
|
|
179
|
+
fontSize: `${typography.body.fontSize}px`,
|
|
180
|
+
lineHeight: typography.body.lineHeight,
|
|
181
|
+
},
|
|
182
|
+
onPointerenter: () => host.pause('hover', true),
|
|
183
|
+
onPointerleave: () => host.pause('hover', false),
|
|
184
|
+
onFocusin: () => host.pause('focus', true),
|
|
185
|
+
onFocusout: (event: FocusEvent) => {
|
|
186
|
+
if (
|
|
187
|
+
!(event.currentTarget as Element).contains(
|
|
188
|
+
event.relatedTarget as Node | null,
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
host.pause('focus', false)
|
|
192
|
+
},
|
|
193
|
+
onKeydown: (event: KeyboardEvent) => {
|
|
194
|
+
if (event.key === 'Escape') {
|
|
195
|
+
event.preventDefault()
|
|
196
|
+
event.stopPropagation()
|
|
197
|
+
props.controller.dismiss(message.id)
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
[
|
|
202
|
+
h(
|
|
203
|
+
'div',
|
|
204
|
+
{ class: 'm-toast-icon', style: { backgroundColor: colors.elevated } },
|
|
205
|
+
[h(MIcon, { name: icons[message.tone], tone })],
|
|
206
|
+
),
|
|
207
|
+
h('div', { class: 'm-toast-copy' }, [
|
|
208
|
+
message.title
|
|
209
|
+
? h('p', { class: 'm-toast-title' }, message.title)
|
|
210
|
+
: null,
|
|
211
|
+
h(
|
|
212
|
+
'p',
|
|
213
|
+
{
|
|
214
|
+
class: 'm-toast-message',
|
|
215
|
+
style: { color: message.title ? colors.muted : colors.text },
|
|
216
|
+
},
|
|
217
|
+
message.message,
|
|
218
|
+
),
|
|
219
|
+
]),
|
|
220
|
+
h(
|
|
221
|
+
'button',
|
|
222
|
+
{
|
|
223
|
+
type: 'button',
|
|
224
|
+
class: 'm-toast-close',
|
|
225
|
+
'aria-label': props.closeLabel,
|
|
226
|
+
style: { color: colors.muted },
|
|
227
|
+
onClick: () => props.controller.dismiss(message.id),
|
|
228
|
+
},
|
|
229
|
+
[h(MIcon, { name: 'x', tone: 'muted' })],
|
|
230
|
+
),
|
|
231
|
+
],
|
|
232
|
+
)
|
|
233
|
+
: null,
|
|
234
|
+
},
|
|
235
|
+
),
|
|
236
|
+
],
|
|
237
|
+
),
|
|
238
|
+
])
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
})
|