@ttoss/react-notifications 1.22.6 → 1.22.8

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 CHANGED
@@ -52,3 +52,28 @@ const Component = () => {
52
52
  );
53
53
  };
54
54
  ```
55
+
56
+ ### Modal
57
+
58
+ This modules loads your notification in a Modal. Just put a container under a `NotificationsProvider` and you are ready to go.
59
+
60
+ ```tsx
61
+ import { useNotifications } from '@ttoss/react-notifications';
62
+
63
+ const Component = () => {
64
+ const { setNotifications } = useNotifications();
65
+
66
+ return (
67
+ <div>
68
+ <button
69
+ onClick={() =>
70
+ setNotifications({ message: "I'm a notification", type: 'info' })
71
+ }
72
+ >
73
+ Click Me!
74
+ </button>
75
+ <NotificationsModal />
76
+ </div>
77
+ );
78
+ };
79
+ ```
package/dist/esm/index.js CHANGED
@@ -52,18 +52,93 @@ var useNotifications = () => {
52
52
  };
53
53
  };
54
54
 
55
- // src/NotificationBox.tsx
56
- import { Box } from "@ttoss/ui";
55
+ // src/NotificationsBox.tsx
56
+ import * as React2 from "react";
57
+ import { Button, Flex as Flex2, Stack } from "@ttoss/ui";
57
58
  import { jsx as jsx2 } from "react/jsx-runtime";
58
- var NotificationBox = () => {
59
+ var NotificationBoxWrapper = ({
60
+ direction,
61
+ notifications,
62
+ children
63
+ }) => {
64
+ const sx = {
65
+ alignItems: "center",
66
+ justifyContent: "center",
67
+ gap: "md",
68
+ marginTop: !notifications || Array.isArray(notifications) && !notifications.length ? 0 : "2xl"
69
+ };
70
+ return direction === "flex" ? /* @__PURE__ */jsx2(Flex2, {
71
+ sx,
72
+ children
73
+ }) : /* @__PURE__ */jsx2(Stack, {
74
+ sx,
75
+ children
76
+ });
77
+ };
78
+ var NotificationsBox = ({
79
+ direction = "flex"
80
+ }) => {
59
81
  const {
82
+ setNotifications,
60
83
  notifications
61
84
  } = useNotifications();
62
85
  if (!notifications) {
63
86
  return null;
64
87
  }
65
- return /* @__PURE__ */jsx2(Box, {
66
- children: JSON.stringify(notifications, null, 2)
88
+ const ButtonMemoized = /*#__PURE__*/React2.memo(({
89
+ message,
90
+ type
91
+ }) => {
92
+ return /* @__PURE__ */jsx2(Button, {
93
+ sx: {
94
+ backgroundColor: type === "error" ? "danger" : "positive"
95
+ },
96
+ onClick: () => {
97
+ if (Array.isArray(notifications) && notifications.length > 1) {
98
+ return setNotifications(notifications.filter(notification => {
99
+ return notification.message !== message;
100
+ }));
101
+ }
102
+ return setNotifications(void 0);
103
+ },
104
+ rightIcon: "close",
105
+ leftIcon: type === "error" ? "warning" : void 0,
106
+ children: message
107
+ });
108
+ });
109
+ ButtonMemoized.displayName = "ButtonMemoized";
110
+ return /* @__PURE__ */jsx2(NotificationBoxWrapper, {
111
+ ...{
112
+ notifications,
113
+ direction
114
+ },
115
+ children: Array.isArray(notifications) ? notifications.map(notification => {
116
+ return /* @__PURE__ */jsx2(ButtonMemoized, {
117
+ ...notification
118
+ }, JSON.stringify(notification));
119
+ }) : /* @__PURE__ */jsx2(ButtonMemoized, {
120
+ ...notifications
121
+ })
122
+ });
123
+ };
124
+
125
+ // src/NotificationsModal.tsx
126
+ import { Modal } from "@ttoss/components";
127
+ import { jsx as jsx3 } from "react/jsx-runtime";
128
+ var NotificationsModal = () => {
129
+ const {
130
+ notifications
131
+ } = useNotifications();
132
+ return /* @__PURE__ */jsx3(Modal, {
133
+ isOpen: !!notifications,
134
+ style: {
135
+ content: {
136
+ minWidth: "30%"
137
+ }
138
+ },
139
+ children: /* @__PURE__ */jsx3(NotificationsBox, {
140
+ direction: "stack"
141
+ })
67
142
  });
68
143
  };
69
- export { NotificationBox, NotificationsProvider, useNotifications };
144
+ export { NotificationsBox, NotificationsModal, NotificationsProvider, useNotifications };
package/dist/index.d.ts CHANGED
@@ -13,9 +13,13 @@ declare const useNotifications: () => {
13
13
  isLoading: boolean;
14
14
  setLoading: (arg: boolean) => void;
15
15
  notifications: NotifyParams | NotifyParams[] | undefined;
16
- setNotifications: (args: NotifyParams | NotifyParams[]) => void;
16
+ setNotifications: (args: NotifyParams | NotifyParams[] | undefined) => void;
17
17
  };
18
18
 
19
- declare const NotificationBox: () => JSX.Element | null;
19
+ declare const NotificationsBox: ({ direction, }: {
20
+ direction?: "flex" | "stack" | undefined;
21
+ }) => JSX.Element | null;
20
22
 
21
- export { NotificationBox, NotificationsProvider, NotificationsProviderProps, useNotifications };
23
+ declare const NotificationsModal: () => JSX.Element;
24
+
25
+ export { NotificationsBox, NotificationsModal, NotificationsProvider, NotificationsProviderProps, NotifyParams, useNotifications };
package/dist/index.js CHANGED
@@ -38,7 +38,8 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
38
38
  // src/index.ts
39
39
  var src_exports = {};
40
40
  __export(src_exports, {
41
- NotificationBox: () => NotificationBox,
41
+ NotificationsBox: () => NotificationsBox,
42
+ NotificationsModal: () => NotificationsModal,
42
43
  NotificationsProvider: () => NotificationsProvider,
43
44
  useNotifications: () => useNotifications
44
45
  });
@@ -96,23 +97,99 @@ var useNotifications = () => {
96
97
  };
97
98
  };
98
99
 
99
- // src/NotificationBox.tsx
100
+ // src/NotificationsBox.tsx
101
+ var React2 = __toESM(require("react"));
100
102
  var import_ui2 = require("@ttoss/ui");
101
103
  var import_jsx_runtime2 = require("react/jsx-runtime");
102
- var NotificationBox = () => {
104
+ var NotificationBoxWrapper = ({
105
+ direction,
106
+ notifications,
107
+ children
108
+ }) => {
109
+ const sx = {
110
+ alignItems: "center",
111
+ justifyContent: "center",
112
+ gap: "md",
113
+ marginTop: !notifications || Array.isArray(notifications) && !notifications.length ? 0 : "2xl"
114
+ };
115
+ return direction === "flex" ? /* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_ui2.Flex, {
116
+ sx,
117
+ children
118
+ }) : /* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_ui2.Stack, {
119
+ sx,
120
+ children
121
+ });
122
+ };
123
+ var NotificationsBox = ({
124
+ direction = "flex"
125
+ }) => {
103
126
  const {
127
+ setNotifications,
104
128
  notifications
105
129
  } = useNotifications();
106
130
  if (!notifications) {
107
131
  return null;
108
132
  }
109
- return /* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_ui2.Box, {
110
- children: JSON.stringify(notifications, null, 2)
133
+ const ButtonMemoized = React2.memo(({
134
+ message,
135
+ type
136
+ }) => {
137
+ return /* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_ui2.Button, {
138
+ sx: {
139
+ backgroundColor: type === "error" ? "danger" : "positive"
140
+ },
141
+ onClick: () => {
142
+ if (Array.isArray(notifications) && notifications.length > 1) {
143
+ return setNotifications(notifications.filter(notification => {
144
+ return notification.message !== message;
145
+ }));
146
+ }
147
+ return setNotifications(void 0);
148
+ },
149
+ rightIcon: "close",
150
+ leftIcon: type === "error" ? "warning" : void 0,
151
+ children: message
152
+ });
153
+ });
154
+ ButtonMemoized.displayName = "ButtonMemoized";
155
+ return /* @__PURE__ */(0, import_jsx_runtime2.jsx)(NotificationBoxWrapper, {
156
+ ...{
157
+ notifications,
158
+ direction
159
+ },
160
+ children: Array.isArray(notifications) ? notifications.map(notification => {
161
+ return /* @__PURE__ */(0, import_jsx_runtime2.jsx)(ButtonMemoized, {
162
+ ...notification
163
+ }, JSON.stringify(notification));
164
+ }) : /* @__PURE__ */(0, import_jsx_runtime2.jsx)(ButtonMemoized, {
165
+ ...notifications
166
+ })
167
+ });
168
+ };
169
+
170
+ // src/NotificationsModal.tsx
171
+ var import_components = require("@ttoss/components");
172
+ var import_jsx_runtime3 = require("react/jsx-runtime");
173
+ var NotificationsModal = () => {
174
+ const {
175
+ notifications
176
+ } = useNotifications();
177
+ return /* @__PURE__ */(0, import_jsx_runtime3.jsx)(import_components.Modal, {
178
+ isOpen: !!notifications,
179
+ style: {
180
+ content: {
181
+ minWidth: "30%"
182
+ }
183
+ },
184
+ children: /* @__PURE__ */(0, import_jsx_runtime3.jsx)(NotificationsBox, {
185
+ direction: "stack"
186
+ })
111
187
  });
112
188
  };
113
189
  // Annotate the CommonJS export names for ESM import in node:
114
190
  0 && (module.exports = {
115
- NotificationBox,
191
+ NotificationsBox,
192
+ NotificationsModal,
116
193
  NotificationsProvider,
117
194
  useNotifications
118
195
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/react-notifications",
3
- "version": "1.22.6",
3
+ "version": "1.22.8",
4
4
  "description": "ttoss notifications module for React apps.",
5
5
  "license": "UNLICENSED",
6
6
  "author": "ttoss",
@@ -0,0 +1,83 @@
1
+ import * as React from 'react';
2
+ import { Button, Flex, Stack } from '@ttoss/ui';
3
+ import { type NotifyParams, useNotifications } from './Provider';
4
+
5
+ const NotificationBoxWrapper = ({
6
+ direction,
7
+ notifications,
8
+ children,
9
+ }: React.PropsWithChildren<{
10
+ direction: 'flex' | 'stack';
11
+ notifications: NotifyParams | NotifyParams[] | undefined;
12
+ }>) => {
13
+ const sx = {
14
+ alignItems: 'center',
15
+ justifyContent: 'center',
16
+ gap: 'md',
17
+ marginTop:
18
+ !notifications || (Array.isArray(notifications) && !notifications.length)
19
+ ? 0
20
+ : '2xl',
21
+ };
22
+
23
+ return direction === 'flex' ? (
24
+ <Flex sx={sx}>{children}</Flex>
25
+ ) : (
26
+ <Stack sx={sx}>{children}</Stack>
27
+ );
28
+ };
29
+
30
+ export const NotificationsBox = ({
31
+ direction = 'flex',
32
+ }: {
33
+ direction?: 'flex' | 'stack';
34
+ }) => {
35
+ const { setNotifications, notifications } = useNotifications();
36
+
37
+ if (!notifications) {
38
+ return null;
39
+ }
40
+
41
+ const ButtonMemoized = React.memo(({ message, type }: NotifyParams) => {
42
+ return (
43
+ <Button
44
+ sx={{
45
+ backgroundColor: type === 'error' ? 'danger' : 'positive',
46
+ }}
47
+ onClick={() => {
48
+ if (Array.isArray(notifications) && notifications.length > 1) {
49
+ return setNotifications(
50
+ notifications.filter((notification) => {
51
+ return notification.message !== message;
52
+ })
53
+ );
54
+ }
55
+ return setNotifications(undefined);
56
+ }}
57
+ rightIcon="close"
58
+ leftIcon={type === 'error' ? 'warning' : undefined}
59
+ >
60
+ {message}
61
+ </Button>
62
+ );
63
+ });
64
+
65
+ ButtonMemoized.displayName = 'ButtonMemoized';
66
+
67
+ return (
68
+ <NotificationBoxWrapper {...{ notifications, direction }}>
69
+ {Array.isArray(notifications) ? (
70
+ notifications.map((notification) => {
71
+ return (
72
+ <ButtonMemoized
73
+ key={JSON.stringify(notification)}
74
+ {...notification}
75
+ />
76
+ );
77
+ })
78
+ ) : (
79
+ <ButtonMemoized {...notifications} />
80
+ )}
81
+ </NotificationBoxWrapper>
82
+ );
83
+ };
@@ -0,0 +1,14 @@
1
+ import * as React from 'react';
2
+ import { Modal } from '@ttoss/components';
3
+ import { NotificationsBox } from './NotificationsBox';
4
+ import { useNotifications } from './Provider';
5
+
6
+ export const NotificationsModal = () => {
7
+ const { notifications } = useNotifications();
8
+
9
+ return (
10
+ <Modal isOpen={!!notifications} style={{ content: { minWidth: '30%' } }}>
11
+ <NotificationsBox direction="stack" />
12
+ </Modal>
13
+ );
14
+ };
package/src/Provider.tsx CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { Flex, InfiniteLinearProgress } from '@ttoss/ui';
3
3
 
4
- type NotifyParams = {
4
+ export type NotifyParams = {
5
5
  message: 'string' | React.ReactNode;
6
6
  type: 'success' | 'error' | 'warning' | 'info';
7
7
  };
@@ -9,7 +9,7 @@ type NotifyParams = {
9
9
  const NotificationsContext = React.createContext<{
10
10
  isLoading: boolean;
11
11
  setLoading: (arg: boolean) => void;
12
- setNotifications: (args: NotifyParams | NotifyParams[]) => void;
12
+ setNotifications: (args: NotifyParams | NotifyParams[] | undefined) => void;
13
13
  notifications?: NotifyParams | NotifyParams[];
14
14
  enableNotificationBox?: boolean;
15
15
  }>({
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export {
2
2
  NotificationsProvider,
3
3
  useNotifications,
4
- NotificationsProviderProps,
4
+ type NotificationsProviderProps,
5
+ type NotifyParams,
5
6
  } from './Provider';
6
- export { NotificationBox } from './NotificationBox';
7
+ export { NotificationsBox } from './NotificationsBox';
8
+ export { NotificationsModal } from './NotificationsModal';
@@ -1,12 +0,0 @@
1
- import { Box } from '@ttoss/ui';
2
- import { useNotifications } from './Provider';
3
-
4
- export const NotificationBox = () => {
5
- const { notifications } = useNotifications();
6
-
7
- if (!notifications) {
8
- return null;
9
- }
10
-
11
- return <Box>{JSON.stringify(notifications, null, 2)}</Box>;
12
- };