@dt-dds/react-toast 1.0.0-beta.56
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/CHANGELOG.md +532 -0
- package/LICENSE.md +21 -0
- package/README.md +212 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +575 -0
- package/dist/index.mjs +539 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
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
|
+
});
|
|
16
|
+
|
|
17
|
+
const App = () => {
|
|
18
|
+
return (
|
|
19
|
+
<div>
|
|
20
|
+
<button onClick={notify}>Click me</button>
|
|
21
|
+
<Toaster />
|
|
22
|
+
</div>
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Customizable
|
|
28
|
+
|
|
29
|
+
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.
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
interface CallToastArgs {
|
|
33
|
+
title: string;
|
|
34
|
+
message: string;
|
|
35
|
+
type: ToastType;
|
|
36
|
+
autoHide: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const ToasterContext = createContext({
|
|
40
|
+
callToast: (args: CallToastArgs) => null,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const Toaster = ({ children }: { children: React.ReactNode }) => {
|
|
44
|
+
const [toast, setToast] = useState<
|
|
45
|
+
Partial<ToastProps> & { autoHide: boolean }
|
|
46
|
+
>(null);
|
|
47
|
+
|
|
48
|
+
const callToast = ({ title, message, type, autoHide }: CallToastArgs) =>
|
|
49
|
+
setToast({
|
|
50
|
+
id: Date.now().toString(),
|
|
51
|
+
title,
|
|
52
|
+
message,
|
|
53
|
+
type,
|
|
54
|
+
autoHide,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const close = () =>
|
|
58
|
+
setToast((prevState) => ({ ...prevState, isVisible: false }));
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (toast && toast.autoHide) {
|
|
62
|
+
const timer = setTimeout(() => close(), 1000);
|
|
63
|
+
return () => clearTimeout(timer);
|
|
64
|
+
}
|
|
65
|
+
}, [toast]);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<ToasterContext.Provider value={{ callToast }}>
|
|
69
|
+
{children}
|
|
70
|
+
<div style={{ position: 'fixed', bottom: 10, left: 10 }}>
|
|
71
|
+
{toast && (
|
|
72
|
+
<Toast
|
|
73
|
+
key={toast.id}
|
|
74
|
+
id={toast.id}
|
|
75
|
+
type={toast.type}
|
|
76
|
+
title={toast.title}
|
|
77
|
+
message={toast.message}
|
|
78
|
+
isVisible={toast.isVisible}
|
|
79
|
+
onClose={close}
|
|
80
|
+
/>
|
|
81
|
+
)}
|
|
82
|
+
</div>
|
|
83
|
+
</ToasterContext.Provider>
|
|
84
|
+
);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const App = () => {
|
|
88
|
+
return (
|
|
89
|
+
<Toaster>
|
|
90
|
+
<MyComponent />
|
|
91
|
+
</Toaster>
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const MyComponent = () => {
|
|
96
|
+
const { callToast } = useContext(ToasterContext);
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<Button
|
|
100
|
+
onClick={() =>
|
|
101
|
+
callToast({
|
|
102
|
+
message: 'myMEssage' + count,
|
|
103
|
+
type: ToastType.Success,
|
|
104
|
+
title: 'success',
|
|
105
|
+
autoHide: false,
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
>
|
|
109
|
+
Clicked Me
|
|
110
|
+
</Button>
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## API
|
|
116
|
+
|
|
117
|
+
### Toaster
|
|
118
|
+
|
|
119
|
+
| Property | Type | Default | Description |
|
|
120
|
+
| -------------------- | :------------------------------ | :-------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
121
|
+
| `toastOptions` | `DefaultToastOptions` | - | These will act as default options for all toasts. See the documentation [here](https://react-hot-toast.com/docs/toast). |
|
|
122
|
+
| `reverseOrder` | `boolean` | - | Toasts spawn at top by default. Set to true if you want new toasts at the end. |
|
|
123
|
+
| `gutter` | `number` | 8 | Changes the gap between each toast. |
|
|
124
|
+
| `containerStyle` | `React.CSSProperties` | - | Customize the style of toaster div. This can be used to change the offset of all toasts |
|
|
125
|
+
| `containerClassName` | `string` | undefined | Add a custom CSS class name to toaster div. |
|
|
126
|
+
| `children` | `(toast: Toast) => JSX.Element` | - | You can provide your own render function to the Toaster by passing it as children. It will be called for each Toast allowing you to render any component based on the toast state. |
|
|
127
|
+
|
|
128
|
+
### emitToast
|
|
129
|
+
|
|
130
|
+
| Property | Type | Default | Description |
|
|
131
|
+
| ------------- | :---------------- | :------ | :------------------------------------------------------------------------ |
|
|
132
|
+
| `type` | `enum<ToastType>` | - | Sets the variant toast ("success", ..., "N") |
|
|
133
|
+
| `title` | `string` | - | The title to be displayed |
|
|
134
|
+
| `message` | `string` | - | The message to be displayed |
|
|
135
|
+
| `dismissable` | `boolean` | true | Sets the visibility of a close button that will fire the onClose callback |
|
|
136
|
+
| `children` | `ReactNode` | - | A child that will be rendered at the end of the content section |
|
|
137
|
+
|
|
138
|
+
The emitToast extends from ToastOptions. They will overwrite all options received from `<Toaster />`. For example:
|
|
139
|
+
|
|
140
|
+
| Property | Type | Default | Description |
|
|
141
|
+
| ---------- | :-------------- | :--------------------------------- | :---------------------------------- |
|
|
142
|
+
| `duration` | `number` | For success: 4000, error: Infinity | Show the toast for a duration in ms |
|
|
143
|
+
| `style` | `CSSProperties` | - | Add css directly to the toast. |
|
|
144
|
+
|
|
145
|
+
You can check all ToastOptions [here](https://react-hot-toast.com/docs/toast).
|
|
146
|
+
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.
|
|
147
|
+
The default duration for any type of toast is 4000ms. For the error type, the duration is infinite.
|
|
148
|
+
|
|
149
|
+
### dismissToast
|
|
150
|
+
|
|
151
|
+
| Property | Type | Default | Description |
|
|
152
|
+
| --------- | :------- | :------ | :------------------------- |
|
|
153
|
+
| `toastId` | `string` | - | ID of the toast to dismiss |
|
|
154
|
+
|
|
155
|
+
### Toast
|
|
156
|
+
|
|
157
|
+
| Property | Type | Default | Description |
|
|
158
|
+
| ------------- | :---------------- | :------ | :------------------------------------------------------------------------------------------------------------ |
|
|
159
|
+
| `id` | `string` | - | Toast identifier |
|
|
160
|
+
| `dataTest` | `string` | - | Defines a value for the data test |
|
|
161
|
+
| `title` | `string` | - | The title to be displayed |
|
|
162
|
+
| `message` | `string` | - | The message to be displayed |
|
|
163
|
+
| `onClose` | `string` | - | The callback when the close button is fired. |
|
|
164
|
+
| `type` | `enum<ToastType>` | - | Sets the variant toast ("success", ..., "N") |
|
|
165
|
+
| `isVisible` | `boolean` | true | Sets the visibility of the toast and triggers the fade out effect |
|
|
166
|
+
| `dismissable` | `boolean` | true | Sets the visibility of a close button that will fire the onClose callback |
|
|
167
|
+
| `children` | `ReactNode` | - | Child components to be rendered at the end of the content section, this can be used to receive action buttons |
|
|
168
|
+
|
|
169
|
+
### Stack
|
|
170
|
+
|
|
171
|
+
- [TypeScript](https://www.typescriptlang.org/) for static type checking
|
|
172
|
+
- [React](https://reactjs.org/) — JavaScript library for user interfaces
|
|
173
|
+
- [Emotion](https://emotion.sh/docs/introduction) — for writing css styles with JavaScript
|
|
174
|
+
- [Storybook](https://storybook.js.org/) — UI component environment powered by Vite
|
|
175
|
+
- [Jest](https://jestjs.io/) - JavaScript Testing Framework
|
|
176
|
+
- [React Testing Library](https://testing-library.com/) - to test UI components in a user-centric way
|
|
177
|
+
- [ESLint](https://eslint.org/) for code linting
|
|
178
|
+
- [Prettier](https://prettier.io) for code formatting
|
|
179
|
+
- [Tsup](https://github.com/egoist/tsup) — TypeScript bundler powered by esbuild
|
|
180
|
+
- [Yarn](https://yarnpkg.com/) from managing packages
|
|
181
|
+
|
|
182
|
+
### Commands
|
|
183
|
+
|
|
184
|
+
- `yarn build` - Build the package
|
|
185
|
+
- `yarn dev` - Run the package locally
|
|
186
|
+
- `yarn lint` - Lint all files within this package
|
|
187
|
+
- `yarn test` - Run all unit tests
|
|
188
|
+
- `yarn test:report` - Open the test coverage report
|
|
189
|
+
- `yarn test:update:snapshot` - Run all unit tests and update the snapshot
|
|
190
|
+
|
|
191
|
+
### Compilation
|
|
192
|
+
|
|
193
|
+
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.
|
|
194
|
+
|
|
195
|
+
The `/dist` folder contains the compiled output.
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
toast
|
|
199
|
+
└── dist
|
|
200
|
+
├── index.d.ts <-- Types
|
|
201
|
+
├── index.js <-- CommonJS version
|
|
202
|
+
└── index.mjs <-- ES Modules version
|
|
203
|
+
...
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Versioning
|
|
207
|
+
|
|
208
|
+
Follows [semantic versioning](https://semver.org/)
|
|
209
|
+
|
|
210
|
+
## © License
|
|
211
|
+
|
|
212
|
+
Licensed under [MIT License](LICENSE.md)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { BaseProps, Theme } from '@dt-dds/react-core';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import { ToasterProps, ToastOptions } from 'react-hot-toast';
|
|
5
|
+
|
|
6
|
+
declare enum ToastPosition {
|
|
7
|
+
TopLeft = "top-left",
|
|
8
|
+
TopCenter = "top-center",
|
|
9
|
+
TopRight = "top-right",
|
|
10
|
+
BottomLeft = "bottom-left",
|
|
11
|
+
BottomCenter = "bottom-center",
|
|
12
|
+
BottomRight = "bottom-right"
|
|
13
|
+
}
|
|
14
|
+
declare enum ToastType {
|
|
15
|
+
Success = "success",
|
|
16
|
+
Error = "error",
|
|
17
|
+
Info = "informative",
|
|
18
|
+
Warning = "warning"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ToastProps extends BaseProps {
|
|
22
|
+
id: string;
|
|
23
|
+
type: ToastType;
|
|
24
|
+
title: string;
|
|
25
|
+
message: string;
|
|
26
|
+
onClose: () => void;
|
|
27
|
+
isVisible?: boolean;
|
|
28
|
+
dismissible?: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare const Toast: ({ children, id, dataTestId, title, message, onClose, type, isVisible, dismissible, }: ToastProps) => react_jsx_runtime.JSX.Element;
|
|
31
|
+
|
|
32
|
+
interface EmitToastProps extends ToastOptions {
|
|
33
|
+
type: ToastType;
|
|
34
|
+
title: string;
|
|
35
|
+
message: string;
|
|
36
|
+
children?: ReactNode;
|
|
37
|
+
dismissible?: boolean;
|
|
38
|
+
}
|
|
39
|
+
declare const dismissToast: (id: string) => void;
|
|
40
|
+
declare const emitToast: ({ type, title, message, children, dismissible, ...props }: EmitToastProps) => void;
|
|
41
|
+
declare const Toaster: ({ gutter, ...props }: ToasterProps) => react_jsx_runtime.JSX.Element;
|
|
42
|
+
|
|
43
|
+
declare module '@emotion/react' {
|
|
44
|
+
interface Theme extends Theme {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { Toast, ToastPosition, ToastProps, ToastType, Toaster, dismissToast, emitToast };
|