@ttoss/react-notifications 2.2.1 → 2.3.0
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 +78 -5
- package/dist/esm/index.js +5 -1
- package/dist/index.d.ts +1 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -18,8 +18,8 @@ Add a provider on top of your application and set [Modal app element](https://re
|
|
|
18
18
|
|
|
19
19
|
```tsx
|
|
20
20
|
import { NotificationsProvider } from '@ttoss/react-notifications';
|
|
21
|
-
import { ThemeProvider } from
|
|
22
|
-
import { Modal } from
|
|
21
|
+
import { ThemeProvider } from '@ttoss/ui';
|
|
22
|
+
import { Modal } from '@ttoss/components';
|
|
23
23
|
|
|
24
24
|
ReactDOM.render(
|
|
25
25
|
<React.StrictMode>
|
|
@@ -30,6 +30,7 @@ ReactDOM.render(
|
|
|
30
30
|
</ThemeProvider>
|
|
31
31
|
</React.StrictMode>,
|
|
32
32
|
document.getElementById('root')
|
|
33
|
+
);
|
|
33
34
|
|
|
34
35
|
Modal.setAppElement('#root');
|
|
35
36
|
```
|
|
@@ -43,7 +44,7 @@ import { useNotifications } from '@ttoss/react-notifications';
|
|
|
43
44
|
import { Box, Button } from '@ttoss/ui';
|
|
44
45
|
|
|
45
46
|
const Component = () => {
|
|
46
|
-
const {
|
|
47
|
+
const { isLoading, setLoading } = useNotifications();
|
|
47
48
|
|
|
48
49
|
return (
|
|
49
50
|
<Box>
|
|
@@ -148,7 +149,7 @@ import { useNotifications, NotificationsBox } from '@ttoss/react-notifications';
|
|
|
148
149
|
import { Box, Button } from '@ttoss/ui';
|
|
149
150
|
|
|
150
151
|
const Component = () => {
|
|
151
|
-
const { addNotification
|
|
152
|
+
const { addNotification } = useNotifications();
|
|
152
153
|
|
|
153
154
|
return (
|
|
154
155
|
<Box>
|
|
@@ -157,7 +158,7 @@ const Component = () => {
|
|
|
157
158
|
onClick={() =>
|
|
158
159
|
addNotification({
|
|
159
160
|
message: "I'm a notification",
|
|
160
|
-
type: 'info'
|
|
161
|
+
type: 'info',
|
|
161
162
|
boxId: 'my-box',
|
|
162
163
|
})
|
|
163
164
|
}
|
|
@@ -202,10 +203,82 @@ const Header = () => {
|
|
|
202
203
|
};
|
|
203
204
|
```
|
|
204
205
|
|
|
206
|
+
### Persistent Notifications
|
|
207
|
+
|
|
208
|
+
You can create persistent notifications that will not be removed when `clearNotifications()` is called by setting the `persist` property to `true`. This is useful for important notifications that should remain visible until manually dismissed.
|
|
209
|
+
|
|
210
|
+
```tsx
|
|
211
|
+
import { useNotifications } from '@ttoss/react-notifications';
|
|
212
|
+
import { Box, Button } from '@ttoss/ui';
|
|
213
|
+
|
|
214
|
+
const Component = () => {
|
|
215
|
+
const { addNotification, clearNotifications } = useNotifications();
|
|
216
|
+
|
|
217
|
+
return (
|
|
218
|
+
<Box>
|
|
219
|
+
<Button
|
|
220
|
+
onClick={() =>
|
|
221
|
+
addNotification({
|
|
222
|
+
message: "I'm a persistent notification",
|
|
223
|
+
type: 'warning',
|
|
224
|
+
persist: true, // This notification will not be cleared by clearNotifications()
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
>
|
|
228
|
+
Add Persistent Notification
|
|
229
|
+
</Button>
|
|
230
|
+
<Button
|
|
231
|
+
onClick={() =>
|
|
232
|
+
addNotification({
|
|
233
|
+
message: "I'm a regular notification",
|
|
234
|
+
type: 'info',
|
|
235
|
+
persist: false, // This notification will be cleared by clearNotifications()
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
>
|
|
239
|
+
Add Regular Notification
|
|
240
|
+
</Button>
|
|
241
|
+
<Button onClick={clearNotifications}>
|
|
242
|
+
Clear All Non-Persistent Notifications
|
|
243
|
+
</Button>
|
|
244
|
+
</Box>
|
|
245
|
+
);
|
|
246
|
+
};
|
|
247
|
+
```
|
|
248
|
+
|
|
205
249
|
#### Recommendation
|
|
206
250
|
|
|
207
251
|
"You can place the `NotificationsBox` component at the root of your application to handle notifications rendering automatically, eliminating the need to manage it manually elsewhere. If you need a specific `NotificationsBox`, simply render the `NotificationsBox` in the desired location and use the `boxId` property to differentiate it."
|
|
208
252
|
|
|
253
|
+
## API
|
|
254
|
+
|
|
255
|
+
### Notification Properties
|
|
256
|
+
|
|
257
|
+
| Property | Type | Default | Description |
|
|
258
|
+
| ---------- | --------------------------------------------- | -------------- | ----------------------------------------------------------------------------- |
|
|
259
|
+
| `id` | `string \| number` | Auto-generated | Unique identifier for the notification |
|
|
260
|
+
| `title` | `string` | - | Optional title for the notification |
|
|
261
|
+
| `message` | `string` | **Required** | The notification message content |
|
|
262
|
+
| `type` | `'warning' \| 'error' \| 'info' \| 'success'` | **Required** | The type of notification |
|
|
263
|
+
| `viewType` | `'toast' \| 'modal' \| 'box' \| 'header'` | `'box'` | Where the notification should be displayed |
|
|
264
|
+
| `toast` | `ToastOptions` | - | Additional options for toast notifications |
|
|
265
|
+
| `boxId` | `string \| number` | - | ID of the specific NotificationsBox to display the notification |
|
|
266
|
+
| `persist` | `boolean` | `false` | Whether the notification should persist when `clearNotifications()` is called |
|
|
267
|
+
|
|
268
|
+
### useNotifications Hook
|
|
269
|
+
|
|
270
|
+
The `useNotifications` hook returns an object with the following properties:
|
|
271
|
+
|
|
272
|
+
| Property | Type | Description |
|
|
273
|
+
| -------------------- | -------------------------------------------------------- | -------------------------------------------------- |
|
|
274
|
+
| `notifications` | `Notification[]` | Array of current notifications |
|
|
275
|
+
| `addNotification` | `(notification: Notification \| Notification[]) => void` | Function to add one or more notifications |
|
|
276
|
+
| `removeNotification` | `(id: string \| number) => void` | Function to remove a specific notification by ID |
|
|
277
|
+
| `clearNotifications` | `() => void` | Function to clear all non-persistent notifications |
|
|
278
|
+
| `isLoading` | `boolean` | Current loading state |
|
|
279
|
+
| `setLoading` | `(loading: boolean) => void` | Function to set the loading state |
|
|
280
|
+
| `defaultViewType` | `'toast' \| 'modal' \| 'box' \| 'header'` | The default view type for notifications |
|
|
281
|
+
|
|
209
282
|
## License
|
|
210
283
|
|
|
211
284
|
[MIT](https://github.com/ttoss/ttoss/blob/main/LICENSE)
|
package/dist/esm/index.js
CHANGED
|
@@ -144,7 +144,11 @@ var NotificationsProvider = props => {
|
|
|
144
144
|
});
|
|
145
145
|
}, [prefix, props.defaultViewType, removeNotification]);
|
|
146
146
|
const clearNotifications = React.useCallback(() => {
|
|
147
|
-
setNotifications(
|
|
147
|
+
setNotifications(prevNotifications => {
|
|
148
|
+
return prevNotifications?.filter(notification => {
|
|
149
|
+
return notification.persist === true;
|
|
150
|
+
});
|
|
151
|
+
});
|
|
148
152
|
}, []);
|
|
149
153
|
return /* @__PURE__ */jsxs2(NotificationsContext.Provider, {
|
|
150
154
|
value: {
|
package/dist/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/react-notifications",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "ttoss notifications module for React apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ttoss",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"react": ">=16.8.0",
|
|
28
|
+
"@ttoss/react-icons": "^0.4.12",
|
|
28
29
|
"@ttoss/components": "^2.2.22",
|
|
29
|
-
"@ttoss/ui": "^5.8.2"
|
|
30
|
-
"@ttoss/react-icons": "^0.4.12"
|
|
30
|
+
"@ttoss/ui": "^5.8.2"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/react": "^19.1.8",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"tsup": "^8.5.0",
|
|
37
37
|
"@ttoss/components": "^2.2.22",
|
|
38
38
|
"@ttoss/react-icons": "^0.4.12",
|
|
39
|
-
"@ttoss/test-utils": "^2.1.24",
|
|
40
39
|
"@ttoss/config": "^1.35.4",
|
|
40
|
+
"@ttoss/test-utils": "^2.1.24",
|
|
41
41
|
"@ttoss/ui": "^5.8.2"
|
|
42
42
|
},
|
|
43
43
|
"keywords": [
|