ag-common 0.0.666 → 0.0.668

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,13 @@ 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
 
65
+ border: solid 1px;
58
66
  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
67
  `;
66
68
  const CloseStyle = styled_1.default.span `
67
69
  position: absolute;
@@ -77,15 +79,9 @@ const CloseStyle = styled_1.default.span `
77
79
  justify-content: center;
78
80
  align-items: center;
79
81
  color: white;
82
+ z-index: 1;
80
83
  &:hover {
81
- background-color: #333;
82
- }
83
-
84
- &[data-dark='false'] {
85
- color: #000;
86
- &:hover {
87
- background-color: #eee;
88
- }
84
+ background-color: var(--bg);
89
85
  }
90
86
  `;
91
87
  const Icon = styled_1.default.div `
@@ -97,15 +93,14 @@ const ProgressBarStyled = (0, styled_1.default)(ProgressBar_1.ProgressBar) `
97
93
  height: 0.75rem;
98
94
  margin-top: 0.5rem;
99
95
  `;
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;
96
+ const Toast = ({ toast, close, style, }) => {
97
+ var _a, _b, _c, _d, _e;
103
98
  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;
99
+ if ((_a = toast.options) === null || _a === void 0 ? void 0 : _a.autoClose) {
100
+ closeMs = (_b = toast.options) === null || _b === void 0 ? void 0 : _b.autoClose;
106
101
  }
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') {
102
+ else if (((_c = toast.options) === null || _c === void 0 ? void 0 : _c.autoClose) === undefined) {
103
+ if (((_d = toast === null || toast === void 0 ? void 0 : toast.options) === null || _d === void 0 ? void 0 : _d.appearance) === 'success') {
109
104
  closeMs = 5000;
110
105
  }
111
106
  else {
@@ -113,7 +108,7 @@ const Toast = ({ toast, close, providerOptions, }) => {
113
108
  }
114
109
  }
115
110
  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) {
111
+ switch ((_e = toast === null || toast === void 0 ? void 0 : toast.options) === null || _e === void 0 ? void 0 : _e.appearance) {
117
112
  case 'error': {
118
113
  icon = react_1.default.createElement(Cross_1.Cross, null);
119
114
  break;
@@ -131,24 +126,43 @@ const Toast = ({ toast, close, providerOptions, }) => {
131
126
  return () => clearTimeout(timeout);
132
127
  // eslint-disable-next-line react-hooks/exhaustive-deps
133
128
  }, []);
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,
129
+ if (toast.type === 'standard') {
130
+ const st = { color: style.color, '--bg': style.borderColor };
131
+ return (react_1.default.createElement(ToastStyle, { style: style },
132
+ react_1.default.createElement(CloseStyle, { onClick: () => close(toast.id), style: st }, "\u00D7"),
133
+ react_1.default.createElement(Icon, { style: { fill: style.color } }, icon),
134
+ toast.message,
135
+ closeMs !== undefined && (react_1.default.createElement(ProgressBarStyled, { max: 100, min: 0, dotPercentages: null, transitionToMs: closeMs }))));
136
+ }
137
+ const st = { color: style.color, '--bg': style.borderColor };
138
+ return (react_1.default.createElement(ToastStyle, { style: style },
139
+ react_1.default.createElement(CloseStyle, { onClick: () => close(toast.id), style: st }, "\u00D7"),
140
+ react_1.default.createElement(FlexRow_1.FlexRow, { noWrap: true, center: true },
141
+ toast.icon === undefined && react_1.default.createElement(Icon, null, icon),
142
+ toast.icon,
143
+ react_1.default.createElement(FlexColumn_1.FlexColumn, { style: { marginLeft: toast.icon === null ? '0' : '0.5rem' } },
144
+ react_1.default.createElement("b", null, toast.title),
145
+ toast.content)),
138
146
  closeMs !== undefined && (react_1.default.createElement(ProgressBarStyled, { max: 100, min: 0, dotPercentages: null, transitionToMs: closeMs }))));
139
147
  };
140
148
  exports.Toast = Toast;
141
149
  const ToastProvider = ({ children, providerOptions, }) => {
142
150
  const [toasts, setToasts] = (0, react_1.useState)([]);
143
151
  const addToast = (message, options) => setToasts((currentToasts) => [
144
- ...currentToasts.filter((ct) => ct.message !== message ||
152
+ ...currentToasts.filter((ct) => ct.type === 'detailed' ||
153
+ ct.message !== message ||
145
154
  JSON.stringify(ct.options) !== JSON.stringify(options)),
146
- { id: (0, random_1.random)(10000).toString(), message, options },
155
+ { id: (0, random_1.random)(10000).toString(), message, options, type: 'standard' },
156
+ ]);
157
+ const addToastDetailed = (p, options) => setToasts((currentToasts) => [
158
+ ...currentToasts,
159
+ Object.assign(Object.assign({ id: (0, random_1.random)(10000).toString() }, p), { options, type: 'detailed' }),
147
160
  ]);
148
161
  const close = (id) => setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
149
- const contextValue = (0, react_1.useMemo)(() => ({ addToast }), []);
162
+ const contextValue = (0, react_1.useMemo)(() => ({ addToast, addToastDetailed }), []);
163
+ const style = (0, common_1.getVarStyles)(providerOptions === null || providerOptions === void 0 ? void 0 : providerOptions.style);
150
164
  return (react_1.default.createElement(exports.ToastContext.Provider, { value: contextValue },
151
165
  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 : {} }))))));
166
+ 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
167
  };
154
168
  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.668",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",