@ttoss/react-notifications 1.19.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 ADDED
@@ -0,0 +1,54 @@
1
+ # @ttoss/react-notifications
2
+
3
+ ## About
4
+
5
+ This module handles notifications in your applications and other ttoss modules.
6
+
7
+ ## Getting Started
8
+
9
+ ### Install
10
+
11
+ ```shell
12
+ yarn add @ttoss/{notifications,ui}
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Provider
18
+
19
+ Add a provider on top of your application.
20
+
21
+ ```tsx
22
+ import { NotificationsProvider } from '@ttoss/react-notifications';
23
+ import { ThemeProvider } from "@ttoss/ui";
24
+
25
+ ReactDOM.render(
26
+ <React.StrictMode>
27
+ <ThemeProvider>
28
+ <NotificationsProvider>
29
+ <App />
30
+ </NotificationsProvider>
31
+ </ThemeProvider>
32
+ </React.StrictMode>,
33
+ document.getElementById('root')
34
+ ```
35
+
36
+ ### Loading
37
+
38
+ This modules provides a global loading bar that you can use on every part of your App.
39
+
40
+ ```tsx
41
+ import { useNotifications } from '@ttoss/react-notifications';
42
+
43
+ const Component = () => {
44
+ const { loading, setLoading } = useNotifications();
45
+
46
+ return (
47
+ <div>
48
+ <button onClick={() => setLoading(true)} disabled={isLoading}>
49
+ Click Me!
50
+ </button>
51
+ </div>
52
+ );
53
+ };
54
+ ```
@@ -0,0 +1,63 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+
3
+ // src/Provider.tsx
4
+ import * as React from "react";
5
+ import { Flex, InfiniteLinearProgress } from "@ttoss/ui";
6
+ import { jsx, jsxs } from "react/jsx-runtime";
7
+ var NotificationsContext = React.createContext({
8
+ isLoading: false,
9
+ setLoading: () => {
10
+ return void 0;
11
+ },
12
+ setNotifications: () => {
13
+ return void 0;
14
+ }
15
+ });
16
+ var NotificationsProvider = ({
17
+ children,
18
+ enableNotificationBox
19
+ }) => {
20
+ const [isLoading, setLoading] = React.useState(false);
21
+ const [notifications, setNotifications] = React.useState();
22
+ return /* @__PURE__ */ jsxs(
23
+ NotificationsContext.Provider,
24
+ {
25
+ value: {
26
+ isLoading,
27
+ setLoading,
28
+ setNotifications,
29
+ notifications,
30
+ enableNotificationBox
31
+ },
32
+ children: [
33
+ isLoading && /* @__PURE__ */ jsx(Flex, { sx: { position: "absolute", width: "100%", top: 0 }, children: /* @__PURE__ */ jsx(InfiniteLinearProgress, {}) }),
34
+ children
35
+ ]
36
+ }
37
+ );
38
+ };
39
+ var useNotifications = () => {
40
+ const { isLoading, setLoading, notifications, setNotifications } = React.useContext(NotificationsContext);
41
+ return {
42
+ isLoading,
43
+ setLoading,
44
+ notifications,
45
+ setNotifications
46
+ };
47
+ };
48
+
49
+ // src/NotificationBox.tsx
50
+ import { Box } from "@ttoss/ui";
51
+ import { jsx as jsx2 } from "react/jsx-runtime";
52
+ var NotificationBox = () => {
53
+ const { notifications } = useNotifications();
54
+ if (!notifications) {
55
+ return null;
56
+ }
57
+ return /* @__PURE__ */ jsx2(Box, { children: JSON.stringify(notifications, null, 2) });
58
+ };
59
+ export {
60
+ NotificationBox,
61
+ NotificationsProvider,
62
+ useNotifications
63
+ };
@@ -0,0 +1,21 @@
1
+ import * as React from 'react';
2
+
3
+ type NotifyParams = {
4
+ message: 'string' | React.ReactNode;
5
+ type: 'success' | 'error' | 'warning' | 'info';
6
+ };
7
+ type NotificationsProviderProps = {
8
+ children: React.ReactNode;
9
+ enableNotificationBox?: boolean;
10
+ };
11
+ declare const NotificationsProvider: ({ children, enableNotificationBox, }: NotificationsProviderProps) => JSX.Element;
12
+ declare const useNotifications: () => {
13
+ isLoading: boolean;
14
+ setLoading: (arg: boolean) => void;
15
+ notifications: NotifyParams | NotifyParams[] | undefined;
16
+ setNotifications: (args: NotifyParams | NotifyParams[]) => void;
17
+ };
18
+
19
+ declare const NotificationBox: () => JSX.Element | null;
20
+
21
+ export { NotificationBox, NotificationsProvider, NotificationsProviderProps, useNotifications };
package/dist/index.js ADDED
@@ -0,0 +1,97 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
+ mod
24
+ ));
25
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
26
+
27
+ // src/index.ts
28
+ var src_exports = {};
29
+ __export(src_exports, {
30
+ NotificationBox: () => NotificationBox,
31
+ NotificationsProvider: () => NotificationsProvider,
32
+ useNotifications: () => useNotifications
33
+ });
34
+ module.exports = __toCommonJS(src_exports);
35
+
36
+ // src/Provider.tsx
37
+ var React = __toESM(require("react"));
38
+ var import_ui = require("@ttoss/ui");
39
+ var import_jsx_runtime = require("react/jsx-runtime");
40
+ var NotificationsContext = React.createContext({
41
+ isLoading: false,
42
+ setLoading: () => {
43
+ return void 0;
44
+ },
45
+ setNotifications: () => {
46
+ return void 0;
47
+ }
48
+ });
49
+ var NotificationsProvider = ({
50
+ children,
51
+ enableNotificationBox
52
+ }) => {
53
+ const [isLoading, setLoading] = React.useState(false);
54
+ const [notifications, setNotifications] = React.useState();
55
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
56
+ NotificationsContext.Provider,
57
+ {
58
+ value: {
59
+ isLoading,
60
+ setLoading,
61
+ setNotifications,
62
+ notifications,
63
+ enableNotificationBox
64
+ },
65
+ children: [
66
+ isLoading && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Flex, { sx: { position: "absolute", width: "100%", top: 0 }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.InfiniteLinearProgress, {}) }),
67
+ children
68
+ ]
69
+ }
70
+ );
71
+ };
72
+ var useNotifications = () => {
73
+ const { isLoading, setLoading, notifications, setNotifications } = React.useContext(NotificationsContext);
74
+ return {
75
+ isLoading,
76
+ setLoading,
77
+ notifications,
78
+ setNotifications
79
+ };
80
+ };
81
+
82
+ // src/NotificationBox.tsx
83
+ var import_ui2 = require("@ttoss/ui");
84
+ var import_jsx_runtime2 = require("react/jsx-runtime");
85
+ var NotificationBox = () => {
86
+ const { notifications } = useNotifications();
87
+ if (!notifications) {
88
+ return null;
89
+ }
90
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui2.Box, { children: JSON.stringify(notifications, null, 2) });
91
+ };
92
+ // Annotate the CommonJS export names for ESM import in node:
93
+ 0 && (module.exports = {
94
+ NotificationBox,
95
+ NotificationsProvider,
96
+ useNotifications
97
+ });
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@ttoss/react-notifications",
3
+ "version": "1.19.0",
4
+ "description": "ttoss notifications module for React apps.",
5
+ "license": "UNLICENSED",
6
+ "author": "ttoss",
7
+ "contributors": [
8
+ "Pedro Arantes <pedro@arantespp.com> (https://arantespp.com/contact)"
9
+ ],
10
+ "main": "dist/index.js",
11
+ "module": "dist/esm/index.js",
12
+ "files": [
13
+ "dist",
14
+ "src"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsup",
18
+ "test": "jest"
19
+ },
20
+ "typings": "dist/index.d.ts",
21
+ "dependencies": {
22
+ "@ttoss/components": "^1.24.4"
23
+ },
24
+ "peerDependencies": {
25
+ "@ttoss/ui": "^1.25.1",
26
+ "react": ">=16.8.0"
27
+ },
28
+ "devDependencies": {
29
+ "@ttoss/config": "^1.25.0",
30
+ "jest": "^29.3.1",
31
+ "react": "^18.2.0"
32
+ },
33
+ "keywords": [
34
+ "React",
35
+ "notifications"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "gitHead": "9159bd57009f4fee620218bc1a0e7bbac28acccd"
41
+ }
@@ -0,0 +1,12 @@
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
+ };
@@ -0,0 +1,70 @@
1
+ import * as React from 'react';
2
+ import { Flex, InfiniteLinearProgress } from '@ttoss/ui';
3
+
4
+ type NotifyParams = {
5
+ message: 'string' | React.ReactNode;
6
+ type: 'success' | 'error' | 'warning' | 'info';
7
+ };
8
+
9
+ const NotificationsContext = React.createContext<{
10
+ isLoading: boolean;
11
+ setLoading: (arg: boolean) => void;
12
+ setNotifications: (args: NotifyParams | NotifyParams[]) => void;
13
+ notifications?: NotifyParams | NotifyParams[];
14
+ enableNotificationBox?: boolean;
15
+ }>({
16
+ isLoading: false,
17
+ setLoading: () => {
18
+ return undefined;
19
+ },
20
+ setNotifications: () => {
21
+ return undefined;
22
+ },
23
+ });
24
+
25
+ export type NotificationsProviderProps = {
26
+ children: React.ReactNode;
27
+ enableNotificationBox?: boolean;
28
+ };
29
+
30
+ export const NotificationsProvider = ({
31
+ children,
32
+ enableNotificationBox,
33
+ }: NotificationsProviderProps) => {
34
+ const [isLoading, setLoading] = React.useState(false);
35
+
36
+ const [notifications, setNotifications] = React.useState<
37
+ NotifyParams | NotifyParams[] | undefined
38
+ >();
39
+
40
+ return (
41
+ <NotificationsContext.Provider
42
+ value={{
43
+ isLoading,
44
+ setLoading,
45
+ setNotifications,
46
+ notifications,
47
+ enableNotificationBox,
48
+ }}
49
+ >
50
+ {isLoading && (
51
+ <Flex sx={{ position: 'absolute', width: '100%', top: 0 }}>
52
+ <InfiniteLinearProgress />
53
+ </Flex>
54
+ )}
55
+ {children}
56
+ </NotificationsContext.Provider>
57
+ );
58
+ };
59
+
60
+ export const useNotifications = () => {
61
+ const { isLoading, setLoading, notifications, setNotifications } =
62
+ React.useContext(NotificationsContext);
63
+
64
+ return {
65
+ isLoading,
66
+ setLoading,
67
+ notifications,
68
+ setNotifications,
69
+ };
70
+ };
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export {
2
+ NotificationsProvider,
3
+ useNotifications,
4
+ NotificationsProviderProps,
5
+ } from './Provider';
6
+ export { NotificationBox } from './NotificationBox';