ag-common 0.0.666 → 0.0.667

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.
@@ -1,11 +1,13 @@
1
1
  import React from 'react';
2
- import type { IToastInt, IToastProviderOptions, TAddToast } from './types';
2
+ import type { IVarStyles } from '../../styles/common';
3
+ import type { IToastInt, IToastProviderOptions, TAddToast, TAddToastDetailed } from './types';
3
4
  export declare const ToastContext: React.Context<{
4
5
  addToast: TAddToast;
6
+ addToastDetailed: TAddToastDetailed;
5
7
  }>;
6
- export declare const Toast: ({ toast, close, providerOptions, }: {
8
+ export declare const Toast: ({ toast, close, style, }: {
7
9
  toast: IToastInt;
8
- providerOptions: IToastProviderOptions;
10
+ style: IVarStyles;
9
11
  close: (s: string) => void;
10
12
  }) => React.JSX.Element;
11
13
  export declare const ToastProvider: ({ children, providerOptions, }: {
@@ -32,6 +32,9 @@ const styled_1 = __importDefault(require("@emotion/styled"));
32
32
  const react_1 = __importStar(require("react"));
33
33
  const random_1 = require("../../../common/helpers/random");
34
34
  const Warning_1 = require("../../icons/Warning");
35
+ const common_1 = require("../../styles/common");
36
+ const FlexColumn_1 = require("../FlexColumn");
37
+ const FlexRow_1 = require("../FlexRow");
35
38
  const ProgressBar_1 = require("../ProgressBar");
36
39
  const Cross_1 = require("./Cross");
37
40
  const Tick_1 = require("./Tick");
@@ -42,6 +45,11 @@ const ToastContainerStyle = styled_1.default.div `
42
45
  right: 0;
43
46
  margin: 0.5rem;
44
47
  z-index: 10000;
48
+
49
+ display: flex;
50
+ flex-flow: column;
51
+ align-items: flex-end;
52
+ max-width: 50vw;
45
53
  `;
46
54
  const ToastStyle = styled_1.default.div `
47
55
  display: flex;
@@ -49,19 +57,12 @@ const ToastStyle = styled_1.default.div `
49
57
  align-items: center;
50
58
  padding: 0.5rem;
51
59
  position: relative;
52
- color: white;
53
60
  font-size: 1.2rem;
54
61
  &:not(:last-of-type) {
55
62
  margin-bottom: 8px;
56
63
  }
57
64
 
58
65
  border-radius: 6px;
59
- background-color: rgba(0, 0, 0, 0.8);
60
- color: white;
61
- &[data-dark='false'] {
62
- background-color: rgba(255, 255, 255, 0.8);
63
- color: black;
64
- }
65
66
  `;
66
67
  const CloseStyle = styled_1.default.span `
67
68
  position: absolute;
@@ -77,15 +78,9 @@ const CloseStyle = styled_1.default.span `
77
78
  justify-content: center;
78
79
  align-items: center;
79
80
  color: white;
81
+ z-index: 1;
80
82
  &:hover {
81
- background-color: #333;
82
- }
83
-
84
- &[data-dark='false'] {
85
- color: #000;
86
- &:hover {
87
- background-color: #eee;
88
- }
83
+ background-color: var(--bg);
89
84
  }
90
85
  `;
91
86
  const Icon = styled_1.default.div `
@@ -97,15 +92,14 @@ const ProgressBarStyled = (0, styled_1.default)(ProgressBar_1.ProgressBar) `
97
92
  height: 0.75rem;
98
93
  margin-top: 0.5rem;
99
94
  `;
100
- const Toast = ({ toast, close, providerOptions, }) => {
101
- var _a, _b, _c, _d, _e, _f;
102
- const darkMode = (_a = providerOptions === null || providerOptions === void 0 ? void 0 : providerOptions.darkMode) !== null && _a !== void 0 ? _a : false;
95
+ const Toast = ({ toast, close, style, }) => {
96
+ var _a, _b, _c, _d, _e;
103
97
  let closeMs;
104
- if ((_b = toast.options) === null || _b === void 0 ? void 0 : _b.autoClose) {
105
- closeMs = (_c = toast.options) === null || _c === void 0 ? void 0 : _c.autoClose;
98
+ if ((_a = toast.options) === null || _a === void 0 ? void 0 : _a.autoClose) {
99
+ closeMs = (_b = toast.options) === null || _b === void 0 ? void 0 : _b.autoClose;
106
100
  }
107
- else if (((_d = toast.options) === null || _d === void 0 ? void 0 : _d.autoClose) === undefined) {
108
- if (((_e = toast === null || toast === void 0 ? void 0 : toast.options) === null || _e === void 0 ? void 0 : _e.appearance) === 'success') {
101
+ else if (((_c = toast.options) === null || _c === void 0 ? void 0 : _c.autoClose) === undefined) {
102
+ if (((_d = toast === null || toast === void 0 ? void 0 : toast.options) === null || _d === void 0 ? void 0 : _d.appearance) === 'success') {
109
103
  closeMs = 5000;
110
104
  }
111
105
  else {
@@ -113,7 +107,7 @@ const Toast = ({ toast, close, providerOptions, }) => {
113
107
  }
114
108
  }
115
109
  let icon = react_1.default.createElement(Tick_1.Tick, null);
116
- switch ((_f = toast === null || toast === void 0 ? void 0 : toast.options) === null || _f === void 0 ? void 0 : _f.appearance) {
110
+ switch ((_e = toast === null || toast === void 0 ? void 0 : toast.options) === null || _e === void 0 ? void 0 : _e.appearance) {
117
111
  case 'error': {
118
112
  icon = react_1.default.createElement(Cross_1.Cross, null);
119
113
  break;
@@ -131,24 +125,43 @@ const Toast = ({ toast, close, providerOptions, }) => {
131
125
  return () => clearTimeout(timeout);
132
126
  // eslint-disable-next-line react-hooks/exhaustive-deps
133
127
  }, []);
134
- return (react_1.default.createElement(ToastStyle, { "data-dark": darkMode },
135
- react_1.default.createElement(CloseStyle, { "data-dark": darkMode, onClick: () => close(toast.id) }, "\u00D7"),
136
- react_1.default.createElement(Icon, null, icon),
137
- toast.message,
128
+ if (toast.type === 'standard') {
129
+ const st = { color: style.color, '--bg': style.borderColor };
130
+ return (react_1.default.createElement(ToastStyle, { style: style },
131
+ react_1.default.createElement(CloseStyle, { onClick: () => close(toast.id), style: st }, "\u00D7"),
132
+ react_1.default.createElement(Icon, { style: { fill: style.color } }, icon),
133
+ toast.message,
134
+ closeMs !== undefined && (react_1.default.createElement(ProgressBarStyled, { max: 100, min: 0, dotPercentages: null, transitionToMs: closeMs }))));
135
+ }
136
+ const st = { color: style.color, '--bg': style.borderColor };
137
+ return (react_1.default.createElement(ToastStyle, { style: style },
138
+ react_1.default.createElement(CloseStyle, { onClick: () => close(toast.id), style: st }, "\u00D7"),
139
+ react_1.default.createElement(FlexRow_1.FlexRow, { noWrap: true, center: true },
140
+ toast.icon === undefined && react_1.default.createElement(Icon, null, icon),
141
+ toast.icon,
142
+ react_1.default.createElement(FlexColumn_1.FlexColumn, { style: { marginLeft: toast.icon === null ? '0' : '0.5rem' } },
143
+ react_1.default.createElement("b", null, toast.title),
144
+ toast.content)),
138
145
  closeMs !== undefined && (react_1.default.createElement(ProgressBarStyled, { max: 100, min: 0, dotPercentages: null, transitionToMs: closeMs }))));
139
146
  };
140
147
  exports.Toast = Toast;
141
148
  const ToastProvider = ({ children, providerOptions, }) => {
142
149
  const [toasts, setToasts] = (0, react_1.useState)([]);
143
150
  const addToast = (message, options) => setToasts((currentToasts) => [
144
- ...currentToasts.filter((ct) => ct.message !== message ||
151
+ ...currentToasts.filter((ct) => ct.type === 'detailed' ||
152
+ ct.message !== message ||
145
153
  JSON.stringify(ct.options) !== JSON.stringify(options)),
146
- { id: (0, random_1.random)(10000).toString(), message, options },
154
+ { id: (0, random_1.random)(10000).toString(), message, options, type: 'standard' },
155
+ ]);
156
+ const addToastDetailed = (p, options) => setToasts((currentToasts) => [
157
+ ...currentToasts,
158
+ Object.assign(Object.assign({ id: (0, random_1.random)(10000).toString() }, p), { options, type: 'detailed' }),
147
159
  ]);
148
160
  const close = (id) => setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
149
- const contextValue = (0, react_1.useMemo)(() => ({ addToast }), []);
161
+ const contextValue = (0, react_1.useMemo)(() => ({ addToast, addToastDetailed }), []);
162
+ const style = (0, common_1.getVarStyles)(providerOptions === null || providerOptions === void 0 ? void 0 : providerOptions.style);
150
163
  return (react_1.default.createElement(exports.ToastContext.Provider, { value: contextValue },
151
164
  children,
152
- react_1.default.createElement(ToastContainerStyle, null, toasts.map((toast) => (react_1.default.createElement(exports.Toast, { key: toast.id, toast: toast, close: close, providerOptions: providerOptions !== null && providerOptions !== void 0 ? providerOptions : {} }))))));
165
+ react_1.default.createElement(ToastContainerStyle, null, toasts.map((toast) => (react_1.default.createElement(exports.Toast, { key: toast.id, toast: toast, close: close, style: style }))))));
153
166
  };
154
167
  exports.ToastProvider = ToastProvider;
@@ -1,3 +1,5 @@
1
+ /// <reference types="react" />
2
+ import type { IVarStyles } from '../../styles';
1
3
  export type TToastAppearance = 'error' | 'warning' | 'success';
2
4
  export type TToastOptions = {
3
5
  appearance: TToastAppearance;
@@ -5,14 +7,24 @@ export type TToastOptions = {
5
7
  autoClose?: number | null;
6
8
  } | undefined;
7
9
  export type TAddToast = (m: string, options?: TToastOptions) => void;
10
+ export type TAddToastDetailed = (p: Omit<IToastDetailed, 'id' | 'type' | 'options'>, options?: TToastOptions) => void;
8
11
  export interface IToastProviderOptions {
9
- /** default false */
10
- darkMode?: boolean;
12
+ style?: Partial<IVarStyles>;
11
13
  }
12
- export interface IToast {
14
+ interface IToastStandard {
15
+ type: 'standard';
13
16
  message: string;
14
17
  options?: TToastOptions;
18
+ id: string;
15
19
  }
16
- export interface IToastInt extends IToast {
20
+ interface IToastDetailed {
21
+ type: 'detailed';
22
+ title?: string;
23
+ content: JSX.Element;
17
24
  id: string;
25
+ options?: TToastOptions;
26
+ /** if provided will show. undef will show default icon. null wont show */
27
+ icon?: JSX.Element | null;
18
28
  }
29
+ export type IToastInt = IToastStandard | IToastDetailed;
30
+ export {};
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.666",
2
+ "version": "0.0.667",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",