@dt-dds/react-toast 1.0.0-beta.100

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Daimler Truck AG.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # Toast Package
2
+
3
+ Toast is a pop-up message used to display relevant information to our users.
4
+
5
+ ## Usage
6
+
7
+ ```jsx
8
+ import { Toaster, ToastType, emitToast } from '@dt-dds/react';
9
+
10
+ const notify = () =>
11
+ emitToast({
12
+ title: 'success',
13
+ message: 'myMEssage' + count,
14
+ type: ToastType.Success,
15
+ children: (
16
+ <Tooltip>
17
+ <Button onClick={() => console.log('clicked')}>Action 2</Button>
18
+ <Tooltip.Content>Some tooltip</Tooltip.Content>
19
+ </Tooltip>
20
+ ),
21
+ });
22
+
23
+ const App = () => {
24
+ return (
25
+ <div>
26
+ <button onClick={notify}>Click me</button>
27
+ <Toaster />
28
+ </div>
29
+ );
30
+ };
31
+ ```
32
+
33
+ ### Customizable
34
+
35
+ It's a very customizable component. We are using [react-hot-toast](https://react-hot-toast.com/) for control our provider. You can check the `react-hot-toast` documentation for more knowledge on this. However, you can create your provider if you wish. For example, let's say you want to create a new prop called `autoHide`. If this prop isn't true, you want to have the toast indefinitely on the screen.
36
+
37
+ ```tsx
38
+ interface CallToastArgs {
39
+ title: string;
40
+ message: string;
41
+ type: ToastType;
42
+ autoHide: boolean;
43
+ }
44
+
45
+ export const ToasterContext = createContext({
46
+ callToast: (args: CallToastArgs) => null,
47
+ });
48
+
49
+ const Toaster = ({ children }: { children: React.ReactNode }) => {
50
+ const [toast, setToast] = useState<
51
+ Partial<ToastProps> & { autoHide: boolean }
52
+ >(null);
53
+
54
+ const callToast = ({ title, message, type, autoHide }: CallToastArgs) =>
55
+ setToast({
56
+ id: Date.now().toString(),
57
+ title,
58
+ message,
59
+ type,
60
+ autoHide,
61
+ });
62
+
63
+ const close = () =>
64
+ setToast((prevState) => ({ ...prevState, isVisible: false }));
65
+
66
+ useEffect(() => {
67
+ if (toast && toast.autoHide) {
68
+ const timer = setTimeout(() => close(), 1000);
69
+ return () => clearTimeout(timer);
70
+ }
71
+ }, [toast]);
72
+
73
+ return (
74
+ <ToasterContext.Provider value={{ callToast }}>
75
+ {children}
76
+ <div style={{ position: 'fixed', bottom: 10, left: 10 }}>
77
+ {toast && (
78
+ <Toast
79
+ key={toast.id}
80
+ id={toast.id}
81
+ type={toast.type}
82
+ title={toast.title}
83
+ message={toast.message}
84
+ isVisible={toast.isVisible}
85
+ onClose={close}
86
+ />
87
+ )}
88
+ </div>
89
+ </ToasterContext.Provider>
90
+ );
91
+ };
92
+
93
+ const App = () => {
94
+ return (
95
+ <Toaster>
96
+ <MyComponent />
97
+ </Toaster>
98
+ );
99
+ };
100
+
101
+ const MyComponent = () => {
102
+ const { callToast } = useContext(ToasterContext);
103
+
104
+ return (
105
+ <Button
106
+ onClick={() =>
107
+ callToast({
108
+ message: 'myMEssage' + count,
109
+ type: ToastType.Success,
110
+ title: 'success',
111
+ autoHide: false,
112
+ })
113
+ }
114
+ >
115
+ Clicked Me
116
+ </Button>
117
+ );
118
+ };
119
+ ```
120
+
121
+ ## API
122
+
123
+ ### Toaster
124
+
125
+ | Property | Type | Default | Description |
126
+ | -------------------- | :------------------------------ | :-------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
127
+ | `toastOptions` | `DefaultToastOptions` | - | These will act as default options for all toasts. See the documentation [here](https://react-hot-toast.com/docs/toast). |
128
+ | `reverseOrder` | `boolean` | - | Toasts spawn at top by default. Set to true if you want new toasts at the end. |
129
+ | `gutter` | `number` | 8 | Changes the gap between each toast. |
130
+ | `containerStyle` | `React.CSSProperties` | - | Customize the style of toaster div. This can be used to change the offset of all toasts |
131
+ | `containerClassName` | `string` | undefined | Add a custom CSS class name to toaster div. |
132
+ | `children` | `(toast: Toast) => JSX.Element` | - | You can customize how each toast is displayed by passing your own render function to the Toaster as its children. This function will be called for every toast, allowing you to render any component based on the toast’s state. Note: If the rendered component is a button, its styles will be overridden by the default styles. |
133
+
134
+ ### emitToast
135
+
136
+ | Property | Type | Default | Description |
137
+ | ------------- | :---------------- | :------ | :------------------------------------------------------------------------ |
138
+ | `type` | `enum<ToastType>` | - | Sets the variant toast ("success", ..., "N") |
139
+ | `title` | `string` | - | The title to be displayed |
140
+ | `message` | `string` | - | The message to be displayed |
141
+ | `dismissable` | `boolean` | true | Sets the visibility of a close button that will fire the onClose callback |
142
+ | `children` | `ReactNode` | - | A child that will be rendered at the end of the content section |
143
+
144
+ The emitToast extends from ToastOptions. They will overwrite all options received from `<Toaster />`. For example:
145
+
146
+ | Property | Type | Default | Description |
147
+ | ---------- | :-------------- | :--------------------------------- | :---------------------------------- |
148
+ | `duration` | `number` | For success: 4000, error: Infinity | Show the toast for a duration in ms |
149
+ | `style` | `CSSProperties` | - | Add css directly to the toast. |
150
+
151
+ You can check all ToastOptions [here](https://react-hot-toast.com/docs/toast).
152
+ The Toast position is fixed, on small screens it will be on the bottom center and on large screens it will be on the bottom right.
153
+ The default duration for any type of toast is 4000ms. For the error type, the duration is infinite.
154
+
155
+ ### dismissToast
156
+
157
+ | Property | Type | Default | Description |
158
+ | --------- | :------- | :------ | :------------------------- |
159
+ | `toastId` | `string` | - | ID of the toast to dismiss |
160
+
161
+ ### Toast
162
+
163
+ | Property | Type | Default | Description |
164
+ | ------------- | :---------------- | :------ | :------------------------------------------------------------------------------------------------------------ |
165
+ | `id` | `string` | - | Toast identifier |
166
+ | `dataTest` | `string` | - | Defines a value for the data test |
167
+ | `title` | `string` | - | The title to be displayed |
168
+ | `message` | `string` | - | The message to be displayed |
169
+ | `onClose` | `string` | - | The callback when the close button is fired. |
170
+ | `type` | `enum<ToastType>` | - | Sets the variant toast ("success", ..., "N") |
171
+ | `isVisible` | `boolean` | true | Sets the visibility of the toast and triggers the fade out effect |
172
+ | `dismissable` | `boolean` | true | Sets the visibility of a close button that will fire the onClose callback |
173
+ | `children` | `ReactNode` | - | Child components to be rendered at the end of the content section, this can be used to receive action buttons |
174
+
175
+ ### Stack
176
+
177
+ - [TypeScript](https://www.typescriptlang.org/) for static type checking
178
+ - [React](https://reactjs.org/) — JavaScript library for user interfaces
179
+ - [Emotion](https://emotion.sh/docs/introduction) — for writing css styles with JavaScript
180
+ - [Storybook](https://storybook.js.org/) — UI component environment powered by Vite
181
+ - [Jest](https://jestjs.io/) - JavaScript Testing Framework
182
+ - [React Testing Library](https://testing-library.com/) - to test UI components in a user-centric way
183
+ - [ESLint](https://eslint.org/) for code linting
184
+ - [Prettier](https://prettier.io) for code formatting
185
+ - [Tsup](https://github.com/egoist/tsup) — TypeScript bundler powered by esbuild
186
+ - [Yarn](https://yarnpkg.com/) from managing packages
187
+
188
+ ### Commands
189
+
190
+ - `yarn build` - Build the package
191
+ - `yarn dev` - Run the package locally
192
+ - `yarn lint` - Lint all files within this package
193
+ - `yarn test` - Run all unit tests
194
+ - `yarn test:report` - Open the test coverage report
195
+ - `yarn test:update:snapshot` - Run all unit tests and update the snapshot
196
+
197
+ ### Compilation
198
+
199
+ Running `yarn build` from the root of the package will use [tsup](https://tsup.egoist.dev/) to compile the raw TypeScript and React code to plain JavaScript.
200
+
201
+ The `/dist` folder contains the compiled output.
202
+
203
+ ```bash
204
+ toast
205
+ └── dist
206
+ ├── index.d.ts <-- Types
207
+ ├── index.js <-- CommonJS version
208
+ └── index.mjs <-- ES Modules version
209
+ ...
210
+ ```
211
+
212
+ ### Versioning
213
+
214
+ Follows [semantic versioning](https://semver.org/)
215
+
216
+ ## &copy; License
217
+
218
+ Licensed under [MIT License](LICENSE.md)
@@ -0,0 +1,50 @@
1
+ import { CustomTheme } from '@dt-dds/themes';
2
+ import * as react_jsx_runtime from 'react/jsx-runtime';
3
+ import { BaseProps } from '@dt-dds/react-core';
4
+ import { ReactNode } from 'react';
5
+ import { ToasterProps, ToastOptions } from 'react-hot-toast';
6
+
7
+ declare enum ToastPosition {
8
+ TopLeft = "top-left",
9
+ TopCenter = "top-center",
10
+ TopRight = "top-right",
11
+ BottomLeft = "bottom-left",
12
+ BottomCenter = "bottom-center",
13
+ BottomRight = "bottom-right"
14
+ }
15
+ declare enum ToastType {
16
+ Success = "success",
17
+ Error = "error",
18
+ Info = "informative",
19
+ Warning = "warning"
20
+ }
21
+
22
+ interface ToastProps extends Pick<BaseProps, 'dataTestId'> {
23
+ id: string;
24
+ type: ToastType;
25
+ title: string;
26
+ message: string;
27
+ onClose: () => void;
28
+ isVisible?: boolean;
29
+ dismissible?: boolean;
30
+ children?: ReactNode;
31
+ }
32
+ declare const Toast: ({ id, dataTestId, title, message, onClose, type, isVisible, dismissible, children, }: ToastProps) => react_jsx_runtime.JSX.Element;
33
+
34
+ interface EmitToastProps extends ToastOptions {
35
+ type: ToastType;
36
+ title: string;
37
+ message: string;
38
+ children?: ReactNode;
39
+ dismissible?: boolean;
40
+ }
41
+ declare const dismissToast: (id: string) => void;
42
+ declare const emitToast: ({ type, title, message, children, dismissible, ...props }: EmitToastProps) => void;
43
+ declare const Toaster: ({ gutter, ...props }: ToasterProps) => react_jsx_runtime.JSX.Element;
44
+
45
+ declare module '@emotion/react' {
46
+ interface Theme extends CustomTheme {
47
+ }
48
+ }
49
+
50
+ export { Toast, ToastPosition, type ToastProps, ToastType, Toaster, dismissToast, emitToast };
@@ -0,0 +1,50 @@
1
+ import { CustomTheme } from '@dt-dds/themes';
2
+ import * as react_jsx_runtime from 'react/jsx-runtime';
3
+ import { BaseProps } from '@dt-dds/react-core';
4
+ import { ReactNode } from 'react';
5
+ import { ToasterProps, ToastOptions } from 'react-hot-toast';
6
+
7
+ declare enum ToastPosition {
8
+ TopLeft = "top-left",
9
+ TopCenter = "top-center",
10
+ TopRight = "top-right",
11
+ BottomLeft = "bottom-left",
12
+ BottomCenter = "bottom-center",
13
+ BottomRight = "bottom-right"
14
+ }
15
+ declare enum ToastType {
16
+ Success = "success",
17
+ Error = "error",
18
+ Info = "informative",
19
+ Warning = "warning"
20
+ }
21
+
22
+ interface ToastProps extends Pick<BaseProps, 'dataTestId'> {
23
+ id: string;
24
+ type: ToastType;
25
+ title: string;
26
+ message: string;
27
+ onClose: () => void;
28
+ isVisible?: boolean;
29
+ dismissible?: boolean;
30
+ children?: ReactNode;
31
+ }
32
+ declare const Toast: ({ id, dataTestId, title, message, onClose, type, isVisible, dismissible, children, }: ToastProps) => react_jsx_runtime.JSX.Element;
33
+
34
+ interface EmitToastProps extends ToastOptions {
35
+ type: ToastType;
36
+ title: string;
37
+ message: string;
38
+ children?: ReactNode;
39
+ dismissible?: boolean;
40
+ }
41
+ declare const dismissToast: (id: string) => void;
42
+ declare const emitToast: ({ type, title, message, children, dismissible, ...props }: EmitToastProps) => void;
43
+ declare const Toaster: ({ gutter, ...props }: ToasterProps) => react_jsx_runtime.JSX.Element;
44
+
45
+ declare module '@emotion/react' {
46
+ interface Theme extends CustomTheme {
47
+ }
48
+ }
49
+
50
+ export { Toast, ToastPosition, type ToastProps, ToastType, Toaster, dismissToast, emitToast };