@moises.ai/design-system 3.10.4 → 3.10.6

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.
@@ -0,0 +1,118 @@
1
+ import React, { createContext, useCallback, useContext, useMemo, useState } from 'react'
2
+ import { Toast } from 'radix-ui'
3
+ import { Callout } from '../Callout/Callout'
4
+ import styles from './ToastProvider.module.css'
5
+
6
+ const ToastContext = createContext(null)
7
+ let toastId = 0
8
+
9
+ const normalizeToastPayload = (payload, variant) => {
10
+ if (typeof payload === 'string') {
11
+ return { title: payload, description: '', variant }
12
+ }
13
+ return {
14
+ title: payload?.title ?? '',
15
+ description: payload?.description ?? '',
16
+ variant: payload?.variant ?? variant,
17
+ duration: payload?.duration,
18
+ }
19
+ }
20
+
21
+ export const ToastProvider = ({
22
+ children,
23
+ duration = 10000,
24
+ maxToasts = 4,
25
+ swipeDirection,
26
+ }) => {
27
+ const getCalloutColor = useCallback((variant) => {
28
+ if (variant === 'success') return 'success'
29
+ if (variant === 'error') return 'error'
30
+ return 'neutral'
31
+ }, [])
32
+
33
+ const [toasts, setToasts] = useState([])
34
+
35
+ const dismiss = useCallback((id) => {
36
+ setToasts((prev) => prev.filter((toastItem) => toastItem.id !== id))
37
+ }, [])
38
+
39
+ const show = useCallback(
40
+ (payload) => {
41
+ const normalized = normalizeToastPayload(payload, 'neutral')
42
+ const nextToast = {
43
+ ...normalized,
44
+ id: `${Date.now()}-${toastId++}`,
45
+ }
46
+ setToasts((prev) => [...prev.slice(-(maxToasts - 1)), nextToast])
47
+ return nextToast.id
48
+ },
49
+ [maxToasts],
50
+ )
51
+
52
+ const success = useCallback(
53
+ (payload) => {
54
+ return show(normalizeToastPayload(payload, 'success'))
55
+ },
56
+ [show],
57
+ )
58
+
59
+ const error = useCallback(
60
+ (payload) => {
61
+ return show(normalizeToastPayload(payload, 'error'))
62
+ },
63
+ [show],
64
+ )
65
+
66
+ const value = useMemo(
67
+ () => ({
68
+ show,
69
+ success,
70
+ error,
71
+ dismiss,
72
+ }),
73
+ [show, success, error, dismiss],
74
+ )
75
+
76
+ return (
77
+ <ToastContext.Provider value={value}>
78
+ <Toast.Provider
79
+ duration={duration}
80
+ {...(swipeDirection ? { swipeDirection } : {})}
81
+ >
82
+ {children}
83
+ {toasts.map((toastItem) => (
84
+ <Toast.Root
85
+ key={toastItem.id}
86
+ className={styles.toastRoot}
87
+ duration={toastItem.duration}
88
+ onOpenChange={(isOpen) => {
89
+ if (!isOpen) dismiss(toastItem.id)
90
+ }}
91
+ >
92
+ <Toast.Description asChild>
93
+ <div className={styles.toastCalloutWrapper}>
94
+ <Callout
95
+ size="1"
96
+ hideIcon
97
+ color={getCalloutColor(toastItem.variant)}
98
+ title={toastItem.title}
99
+ text={toastItem.description}
100
+ className={styles.toastCallout}
101
+ />
102
+ </div>
103
+ </Toast.Description>
104
+ </Toast.Root>
105
+ ))}
106
+ <Toast.Viewport className={styles.toastViewport} />
107
+ </Toast.Provider>
108
+ </ToastContext.Provider>
109
+ )
110
+ }
111
+
112
+ export const useToast = () => {
113
+ const context = useContext(ToastContext)
114
+ if (!context) {
115
+ throw new Error('useToast must be used inside ToastProvider')
116
+ }
117
+ return context
118
+ }
@@ -0,0 +1,33 @@
1
+ .toastViewport {
2
+ position: fixed;
3
+ left: 50%;
4
+ transform: translateX(-50%);
5
+ bottom: 16px;
6
+ display: flex;
7
+ flex-direction: column;
8
+ gap: 8px;
9
+ width: min(360px, calc(100vw - 24px));
10
+ margin: 0;
11
+ padding: 0;
12
+ list-style: none;
13
+ z-index: 2000;
14
+ outline: none;
15
+ }
16
+
17
+ .toastRoot {
18
+ display: block;
19
+ padding: 0;
20
+ box-shadow: 0 12px 24px -16px rgba(0, 0, 0, 0.6);
21
+ }
22
+
23
+ .toastCalloutWrapper {
24
+ position: relative;
25
+ min-width: 0;
26
+ }
27
+
28
+ .toastCallout {
29
+ width: 100%;
30
+ min-width: 0;
31
+ border-radius: 8px;
32
+ padding-right: 8px;
33
+ }
@@ -0,0 +1,74 @@
1
+ import { Flex, Text, Button } from '../../index'
2
+ import { ToastProvider, useToast } from './ToastProvider'
3
+
4
+ const ToastTester = () => {
5
+ const { success, error, show } = useToast()
6
+
7
+ return (
8
+ <Flex direction="column" gap="3" style={{ width: 360 }}>
9
+ <Text size="2" style={{ color: 'var(--neutral-alpha-11)' }}>
10
+ Use the buttons below to test canonical DS toasts.
11
+ </Text>
12
+ <Flex gap="2">
13
+ <Button
14
+ onClick={() =>
15
+ success({
16
+ title: 'Tracks added',
17
+ description: '3 tracks were added to your setlist.',
18
+ })
19
+ }
20
+ >
21
+ Trigger success
22
+ </Button>
23
+ <Button
24
+ color="red"
25
+ variant="soft"
26
+ onClick={() =>
27
+ error({
28
+ title: 'Could not add tracks',
29
+ description: 'Try again in a few seconds.',
30
+ })
31
+ }
32
+ >
33
+ Trigger error
34
+ </Button>
35
+ </Flex>
36
+ <Button
37
+ variant="ghost"
38
+ onClick={() =>
39
+ show({
40
+ title: 'Neutral message',
41
+ description: 'This is a custom toast using show().',
42
+ })
43
+ }
44
+ >
45
+ Trigger neutral
46
+ </Button>
47
+ </Flex>
48
+ )
49
+ }
50
+
51
+ export default {
52
+ title: 'Components/ToastProvider',
53
+ component: ToastProvider,
54
+ parameters: {
55
+ layout: 'centered',
56
+ },
57
+ tags: ['autodocs'],
58
+ }
59
+
60
+ export const Playground = {
61
+ render: () => (
62
+ <ToastProvider>
63
+ <ToastTester />
64
+ </ToastProvider>
65
+ ),
66
+ }
67
+
68
+ export const ShortDuration = {
69
+ render: () => (
70
+ <ToastProvider duration={1500}>
71
+ <ToastTester />
72
+ </ToastProvider>
73
+ ),
74
+ }
package/src/index.jsx CHANGED
@@ -132,6 +132,7 @@ export { Text } from './components/Text/Text'
132
132
  export { TextArea } from './components/TextArea/TextArea'
133
133
  export { TextField } from './components/TextField/TextField'
134
134
  export { Theme } from './components/theme/Theme'
135
+ export { ToastProvider, useToast } from './components/ToastProvider/ToastProvider'
135
136
  export { ThumbnailPicker } from './components/ThumbnailPicker/ThumbnailPicker'
136
137
  export { Tooltip } from './components/Tooltip/Tooltip'
137
138
  export { TooltipWithInfoIcon } from './components/TooltipWithInfoIcon/TooltipWithInfoIcon'