@yamada-ui/notice 0.1.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/chunk-JM3M4FPB.mjs +209 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +233 -0
- package/dist/index.mjs +8 -0
- package/dist/notice.d.ts +49 -0
- package/dist/notice.js +231 -0
- package/dist/notice.mjs +8 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Hirotomo Yamada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @yamada-ui/notice
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
$ pnpm add @yamada-ui/notice
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
$ yarn add @yamada-ui/notice
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
or
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
$ npm install @yamada-ui/notice
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Contribution
|
|
22
|
+
|
|
23
|
+
Wouldn't you like to contribute? That's amazing! We have prepared a [contribution guide](https://github.com/hirotomoyamada/yamada-ui/blob/main/CONTRIBUTING.md) to assist you.
|
|
24
|
+
|
|
25
|
+
## Licence
|
|
26
|
+
|
|
27
|
+
This package is licensed under the terms of the
|
|
28
|
+
[MIT license](https://github.com/hirotomoyamada/yamada-ui/blob/main/LICENSE).
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// src/notice.tsx
|
|
2
|
+
import { Alert, AlertDescription, AlertIcon, AlertTitle } from "@yamada-ui/alert";
|
|
3
|
+
import { CloseButton } from "@yamada-ui/close-button";
|
|
4
|
+
import {
|
|
5
|
+
ui,
|
|
6
|
+
useTheme
|
|
7
|
+
} from "@yamada-ui/core";
|
|
8
|
+
import { merge } from "@yamada-ui/utils";
|
|
9
|
+
import { useMemo } from "react";
|
|
10
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
11
|
+
var findId = (options, id) => options.find((notice) => notice.id === id);
|
|
12
|
+
var findNotice = (state, id) => {
|
|
13
|
+
const placement = getNoticePlacement(state, id);
|
|
14
|
+
const index = placement ? state[placement].findIndex((notice) => notice.id === id) : -1;
|
|
15
|
+
return { placement, index };
|
|
16
|
+
};
|
|
17
|
+
var getNoticePlacement = (state, id) => {
|
|
18
|
+
for (const [placement, values] of Object.entries(state)) {
|
|
19
|
+
if (findId(values, id))
|
|
20
|
+
return placement;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var counter = 0;
|
|
24
|
+
var createNotice = (message, { id, placement = "top", duration, onCloseComplete, status, style }) => {
|
|
25
|
+
counter += 1;
|
|
26
|
+
id = id != null ? id : counter;
|
|
27
|
+
return {
|
|
28
|
+
id,
|
|
29
|
+
placement,
|
|
30
|
+
status,
|
|
31
|
+
duration,
|
|
32
|
+
message,
|
|
33
|
+
onDelete: () => noticeStore.remove(String(id), placement),
|
|
34
|
+
isDelete: false,
|
|
35
|
+
onCloseComplete,
|
|
36
|
+
style
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
var createRender = (options) => {
|
|
40
|
+
const { component } = options;
|
|
41
|
+
const Render = (props) => {
|
|
42
|
+
if (typeof component === "function") {
|
|
43
|
+
return component({ ...props, ...options });
|
|
44
|
+
} else {
|
|
45
|
+
return /* @__PURE__ */ jsx(Notice, { ...props, ...options });
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return Render;
|
|
49
|
+
};
|
|
50
|
+
var createNoticeFunc = (defaultOptions, theme) => {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
const themeOptions = (_b = (_a = theme.__config.notice) == null ? void 0 : _a.options) != null ? _b : {};
|
|
53
|
+
const computedOptions = (options) => merge(themeOptions, merge(defaultOptions, options));
|
|
54
|
+
const notice = (options = {}) => {
|
|
55
|
+
options = computedOptions(options);
|
|
56
|
+
const message = createRender(options);
|
|
57
|
+
return noticeStore.create(message, options);
|
|
58
|
+
};
|
|
59
|
+
notice.update = (id, options) => {
|
|
60
|
+
options = computedOptions(options);
|
|
61
|
+
noticeStore.update(id, options);
|
|
62
|
+
};
|
|
63
|
+
notice.closeAll = noticeStore.closeAll;
|
|
64
|
+
notice.close = noticeStore.close;
|
|
65
|
+
notice.isActive = noticeStore.isActive;
|
|
66
|
+
return notice;
|
|
67
|
+
};
|
|
68
|
+
var useNotice = (defaultOptions) => {
|
|
69
|
+
const { theme } = useTheme();
|
|
70
|
+
return useMemo(() => createNoticeFunc(defaultOptions != null ? defaultOptions : {}, theme), [defaultOptions, theme]);
|
|
71
|
+
};
|
|
72
|
+
var initialState = {
|
|
73
|
+
top: [],
|
|
74
|
+
"top-left": [],
|
|
75
|
+
"top-right": [],
|
|
76
|
+
bottom: [],
|
|
77
|
+
"bottom-left": [],
|
|
78
|
+
"bottom-right": []
|
|
79
|
+
};
|
|
80
|
+
var createNoticeStore = (initialState2) => {
|
|
81
|
+
let state = initialState2;
|
|
82
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
83
|
+
const setState = (setStateFn) => {
|
|
84
|
+
state = setStateFn(state);
|
|
85
|
+
listeners.forEach((l) => l());
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
getSnapshot: () => state,
|
|
89
|
+
subscribe: (listener) => {
|
|
90
|
+
listeners.add(listener);
|
|
91
|
+
return () => {
|
|
92
|
+
setState(() => initialState2);
|
|
93
|
+
listeners.delete(listener);
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
remove: (id, placement) => {
|
|
97
|
+
setState((prevState) => ({
|
|
98
|
+
...prevState,
|
|
99
|
+
[placement]: prevState[placement].filter((notice) => notice.id != id)
|
|
100
|
+
}));
|
|
101
|
+
},
|
|
102
|
+
create: (message, options) => {
|
|
103
|
+
var _a;
|
|
104
|
+
const limit = ((_a = options.limit) != null ? _a : 0) - 1;
|
|
105
|
+
const notice = createNotice(message, options);
|
|
106
|
+
const { placement, id } = notice;
|
|
107
|
+
setState((prev) => {
|
|
108
|
+
var _a2;
|
|
109
|
+
let prevNotices = (_a2 = prev[placement]) != null ? _a2 : [];
|
|
110
|
+
if (limit > 0 && prevNotices.length > limit) {
|
|
111
|
+
const n = prevNotices.length - limit;
|
|
112
|
+
const notices2 = placement.includes("top") ? prevNotices.slice(n * -1) : prevNotices.slice(0, n);
|
|
113
|
+
const ids = notices2.map(({ id: id2 }) => id2);
|
|
114
|
+
prevNotices = prevNotices.map(
|
|
115
|
+
(notice2) => ids.includes(notice2.id) ? { ...notice2, isDelete: true } : notice2
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
const notices = placement.includes("top") ? [notice, ...prevNotices] : [...prevNotices, notice];
|
|
119
|
+
return { ...prev, [placement]: notices };
|
|
120
|
+
});
|
|
121
|
+
return id;
|
|
122
|
+
},
|
|
123
|
+
update: (id, options) => {
|
|
124
|
+
setState((prev) => {
|
|
125
|
+
const next = { ...prev };
|
|
126
|
+
const { placement, index } = findNotice(next, id);
|
|
127
|
+
if (placement && index !== -1) {
|
|
128
|
+
next[placement][index] = {
|
|
129
|
+
...next[placement][index],
|
|
130
|
+
...options,
|
|
131
|
+
message: createRender(options)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return next;
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
closeAll: ({ placement } = {}) => {
|
|
138
|
+
setState((prev) => {
|
|
139
|
+
let placements = [
|
|
140
|
+
"bottom",
|
|
141
|
+
"bottom-right",
|
|
142
|
+
"bottom-left",
|
|
143
|
+
"top",
|
|
144
|
+
"top-left",
|
|
145
|
+
"top-right"
|
|
146
|
+
];
|
|
147
|
+
if (placement)
|
|
148
|
+
placements = placement;
|
|
149
|
+
return placements.reduce(
|
|
150
|
+
(acc, placement2) => {
|
|
151
|
+
acc[placement2] = prev[placement2].map((notice) => ({ ...notice, isDelete: true }));
|
|
152
|
+
return acc;
|
|
153
|
+
},
|
|
154
|
+
{ ...prev }
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
close: (id) => {
|
|
159
|
+
setState((prev) => {
|
|
160
|
+
const placement = getNoticePlacement(prev, id);
|
|
161
|
+
if (!placement)
|
|
162
|
+
return prev;
|
|
163
|
+
return {
|
|
164
|
+
...prev,
|
|
165
|
+
[placement]: prev[placement].map(
|
|
166
|
+
(notice) => notice.id == id ? { ...notice, isDelete: true } : notice
|
|
167
|
+
)
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
isActive: (id) => Boolean(findNotice(noticeStore.getSnapshot(), id).placement)
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
var noticeStore = createNoticeStore(initialState);
|
|
175
|
+
var Notice = ({
|
|
176
|
+
variant = "subtle",
|
|
177
|
+
colorScheme,
|
|
178
|
+
status,
|
|
179
|
+
icon,
|
|
180
|
+
title,
|
|
181
|
+
description,
|
|
182
|
+
isClosable,
|
|
183
|
+
onClose
|
|
184
|
+
}) => {
|
|
185
|
+
return /* @__PURE__ */ jsxs(
|
|
186
|
+
Alert,
|
|
187
|
+
{
|
|
188
|
+
status,
|
|
189
|
+
variant,
|
|
190
|
+
colorScheme,
|
|
191
|
+
alignItems: "start",
|
|
192
|
+
boxShadow: "lg",
|
|
193
|
+
pe: isClosable ? 8 : void 0,
|
|
194
|
+
children: [
|
|
195
|
+
/* @__PURE__ */ jsx(AlertIcon, { variant: icon == null ? void 0 : icon.variant, ...(icon == null ? void 0 : icon.color) ? { color: icon.color } : {}, children: icon == null ? void 0 : icon.children }),
|
|
196
|
+
/* @__PURE__ */ jsxs(ui.div, { flex: "1", children: [
|
|
197
|
+
title ? /* @__PURE__ */ jsx(AlertTitle, { noOfLines: 1, children: title }) : null,
|
|
198
|
+
description ? /* @__PURE__ */ jsx(AlertDescription, { noOfLines: 3, children: description }) : null
|
|
199
|
+
] }),
|
|
200
|
+
isClosable ? /* @__PURE__ */ jsx(CloseButton, { size: "sm", onClick: onClose, position: "absolute", top: 2, right: 2 }) : null
|
|
201
|
+
]
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export {
|
|
207
|
+
useNotice,
|
|
208
|
+
noticeStore
|
|
209
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
noticeStore: () => noticeStore,
|
|
24
|
+
useNotice: () => useNotice
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/notice.tsx
|
|
29
|
+
var import_alert = require("@yamada-ui/alert");
|
|
30
|
+
var import_close_button = require("@yamada-ui/close-button");
|
|
31
|
+
var import_core = require("@yamada-ui/core");
|
|
32
|
+
var import_utils = require("@yamada-ui/utils");
|
|
33
|
+
var import_react = require("react");
|
|
34
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
35
|
+
var findId = (options, id) => options.find((notice) => notice.id === id);
|
|
36
|
+
var findNotice = (state, id) => {
|
|
37
|
+
const placement = getNoticePlacement(state, id);
|
|
38
|
+
const index = placement ? state[placement].findIndex((notice) => notice.id === id) : -1;
|
|
39
|
+
return { placement, index };
|
|
40
|
+
};
|
|
41
|
+
var getNoticePlacement = (state, id) => {
|
|
42
|
+
for (const [placement, values] of Object.entries(state)) {
|
|
43
|
+
if (findId(values, id))
|
|
44
|
+
return placement;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var counter = 0;
|
|
48
|
+
var createNotice = (message, { id, placement = "top", duration, onCloseComplete, status, style }) => {
|
|
49
|
+
counter += 1;
|
|
50
|
+
id = id != null ? id : counter;
|
|
51
|
+
return {
|
|
52
|
+
id,
|
|
53
|
+
placement,
|
|
54
|
+
status,
|
|
55
|
+
duration,
|
|
56
|
+
message,
|
|
57
|
+
onDelete: () => noticeStore.remove(String(id), placement),
|
|
58
|
+
isDelete: false,
|
|
59
|
+
onCloseComplete,
|
|
60
|
+
style
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
var createRender = (options) => {
|
|
64
|
+
const { component } = options;
|
|
65
|
+
const Render = (props) => {
|
|
66
|
+
if (typeof component === "function") {
|
|
67
|
+
return component({ ...props, ...options });
|
|
68
|
+
} else {
|
|
69
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Notice, { ...props, ...options });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
return Render;
|
|
73
|
+
};
|
|
74
|
+
var createNoticeFunc = (defaultOptions, theme) => {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
const themeOptions = (_b = (_a = theme.__config.notice) == null ? void 0 : _a.options) != null ? _b : {};
|
|
77
|
+
const computedOptions = (options) => (0, import_utils.merge)(themeOptions, (0, import_utils.merge)(defaultOptions, options));
|
|
78
|
+
const notice = (options = {}) => {
|
|
79
|
+
options = computedOptions(options);
|
|
80
|
+
const message = createRender(options);
|
|
81
|
+
return noticeStore.create(message, options);
|
|
82
|
+
};
|
|
83
|
+
notice.update = (id, options) => {
|
|
84
|
+
options = computedOptions(options);
|
|
85
|
+
noticeStore.update(id, options);
|
|
86
|
+
};
|
|
87
|
+
notice.closeAll = noticeStore.closeAll;
|
|
88
|
+
notice.close = noticeStore.close;
|
|
89
|
+
notice.isActive = noticeStore.isActive;
|
|
90
|
+
return notice;
|
|
91
|
+
};
|
|
92
|
+
var useNotice = (defaultOptions) => {
|
|
93
|
+
const { theme } = (0, import_core.useTheme)();
|
|
94
|
+
return (0, import_react.useMemo)(() => createNoticeFunc(defaultOptions != null ? defaultOptions : {}, theme), [defaultOptions, theme]);
|
|
95
|
+
};
|
|
96
|
+
var initialState = {
|
|
97
|
+
top: [],
|
|
98
|
+
"top-left": [],
|
|
99
|
+
"top-right": [],
|
|
100
|
+
bottom: [],
|
|
101
|
+
"bottom-left": [],
|
|
102
|
+
"bottom-right": []
|
|
103
|
+
};
|
|
104
|
+
var createNoticeStore = (initialState2) => {
|
|
105
|
+
let state = initialState2;
|
|
106
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
107
|
+
const setState = (setStateFn) => {
|
|
108
|
+
state = setStateFn(state);
|
|
109
|
+
listeners.forEach((l) => l());
|
|
110
|
+
};
|
|
111
|
+
return {
|
|
112
|
+
getSnapshot: () => state,
|
|
113
|
+
subscribe: (listener) => {
|
|
114
|
+
listeners.add(listener);
|
|
115
|
+
return () => {
|
|
116
|
+
setState(() => initialState2);
|
|
117
|
+
listeners.delete(listener);
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
remove: (id, placement) => {
|
|
121
|
+
setState((prevState) => ({
|
|
122
|
+
...prevState,
|
|
123
|
+
[placement]: prevState[placement].filter((notice) => notice.id != id)
|
|
124
|
+
}));
|
|
125
|
+
},
|
|
126
|
+
create: (message, options) => {
|
|
127
|
+
var _a;
|
|
128
|
+
const limit = ((_a = options.limit) != null ? _a : 0) - 1;
|
|
129
|
+
const notice = createNotice(message, options);
|
|
130
|
+
const { placement, id } = notice;
|
|
131
|
+
setState((prev) => {
|
|
132
|
+
var _a2;
|
|
133
|
+
let prevNotices = (_a2 = prev[placement]) != null ? _a2 : [];
|
|
134
|
+
if (limit > 0 && prevNotices.length > limit) {
|
|
135
|
+
const n = prevNotices.length - limit;
|
|
136
|
+
const notices2 = placement.includes("top") ? prevNotices.slice(n * -1) : prevNotices.slice(0, n);
|
|
137
|
+
const ids = notices2.map(({ id: id2 }) => id2);
|
|
138
|
+
prevNotices = prevNotices.map(
|
|
139
|
+
(notice2) => ids.includes(notice2.id) ? { ...notice2, isDelete: true } : notice2
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
const notices = placement.includes("top") ? [notice, ...prevNotices] : [...prevNotices, notice];
|
|
143
|
+
return { ...prev, [placement]: notices };
|
|
144
|
+
});
|
|
145
|
+
return id;
|
|
146
|
+
},
|
|
147
|
+
update: (id, options) => {
|
|
148
|
+
setState((prev) => {
|
|
149
|
+
const next = { ...prev };
|
|
150
|
+
const { placement, index } = findNotice(next, id);
|
|
151
|
+
if (placement && index !== -1) {
|
|
152
|
+
next[placement][index] = {
|
|
153
|
+
...next[placement][index],
|
|
154
|
+
...options,
|
|
155
|
+
message: createRender(options)
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return next;
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
closeAll: ({ placement } = {}) => {
|
|
162
|
+
setState((prev) => {
|
|
163
|
+
let placements = [
|
|
164
|
+
"bottom",
|
|
165
|
+
"bottom-right",
|
|
166
|
+
"bottom-left",
|
|
167
|
+
"top",
|
|
168
|
+
"top-left",
|
|
169
|
+
"top-right"
|
|
170
|
+
];
|
|
171
|
+
if (placement)
|
|
172
|
+
placements = placement;
|
|
173
|
+
return placements.reduce(
|
|
174
|
+
(acc, placement2) => {
|
|
175
|
+
acc[placement2] = prev[placement2].map((notice) => ({ ...notice, isDelete: true }));
|
|
176
|
+
return acc;
|
|
177
|
+
},
|
|
178
|
+
{ ...prev }
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
close: (id) => {
|
|
183
|
+
setState((prev) => {
|
|
184
|
+
const placement = getNoticePlacement(prev, id);
|
|
185
|
+
if (!placement)
|
|
186
|
+
return prev;
|
|
187
|
+
return {
|
|
188
|
+
...prev,
|
|
189
|
+
[placement]: prev[placement].map(
|
|
190
|
+
(notice) => notice.id == id ? { ...notice, isDelete: true } : notice
|
|
191
|
+
)
|
|
192
|
+
};
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
isActive: (id) => Boolean(findNotice(noticeStore.getSnapshot(), id).placement)
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
var noticeStore = createNoticeStore(initialState);
|
|
199
|
+
var Notice = ({
|
|
200
|
+
variant = "subtle",
|
|
201
|
+
colorScheme,
|
|
202
|
+
status,
|
|
203
|
+
icon,
|
|
204
|
+
title,
|
|
205
|
+
description,
|
|
206
|
+
isClosable,
|
|
207
|
+
onClose
|
|
208
|
+
}) => {
|
|
209
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
210
|
+
import_alert.Alert,
|
|
211
|
+
{
|
|
212
|
+
status,
|
|
213
|
+
variant,
|
|
214
|
+
colorScheme,
|
|
215
|
+
alignItems: "start",
|
|
216
|
+
boxShadow: "lg",
|
|
217
|
+
pe: isClosable ? 8 : void 0,
|
|
218
|
+
children: [
|
|
219
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_alert.AlertIcon, { variant: icon == null ? void 0 : icon.variant, ...(icon == null ? void 0 : icon.color) ? { color: icon.color } : {}, children: icon == null ? void 0 : icon.children }),
|
|
220
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_core.ui.div, { flex: "1", children: [
|
|
221
|
+
title ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_alert.AlertTitle, { noOfLines: 1, children: title }) : null,
|
|
222
|
+
description ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_alert.AlertDescription, { noOfLines: 3, children: description }) : null
|
|
223
|
+
] }),
|
|
224
|
+
isClosable ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_close_button.CloseButton, { size: "sm", onClick: onClose, position: "absolute", top: 2, right: 2 }) : null
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
};
|
|
229
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
230
|
+
0 && (module.exports = {
|
|
231
|
+
noticeStore,
|
|
232
|
+
useNotice
|
|
233
|
+
});
|
package/dist/index.mjs
ADDED
package/dist/notice.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AlertProps } from '@yamada-ui/alert';
|
|
2
|
+
import { NoticeConfigOptions, NoticePlacement, NoticeComponentProps, CSSUIObject, StyledTheme } from '@yamada-ui/core';
|
|
3
|
+
import { Dict } from '@yamada-ui/utils';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
5
|
+
|
|
6
|
+
type UseNoticeOptions = NoticeConfigOptions;
|
|
7
|
+
type NoticeOptions = {
|
|
8
|
+
id: string | number;
|
|
9
|
+
placement: NoticePlacement;
|
|
10
|
+
duration: UseNoticeOptions['duration'];
|
|
11
|
+
status: UseNoticeOptions['status'];
|
|
12
|
+
message: (props: NoticeComponentProps) => ReactNode;
|
|
13
|
+
isDelete?: boolean;
|
|
14
|
+
onDelete: () => void;
|
|
15
|
+
onCloseComplete?: () => void;
|
|
16
|
+
style?: CSSUIObject;
|
|
17
|
+
};
|
|
18
|
+
declare const createNoticeFunc: (defaultOptions: UseNoticeOptions, theme: StyledTheme<Dict<any>>) => {
|
|
19
|
+
(options?: UseNoticeOptions): string | number;
|
|
20
|
+
update(id: string | number, options: Omit<UseNoticeOptions, 'id'>): void;
|
|
21
|
+
closeAll: (options?: {
|
|
22
|
+
placement?: NoticePlacement[];
|
|
23
|
+
}) => void;
|
|
24
|
+
close: (id: string | number) => void;
|
|
25
|
+
isActive: (id: string | number) => boolean;
|
|
26
|
+
};
|
|
27
|
+
type CreateNoticeReturn = ReturnType<typeof createNoticeFunc>;
|
|
28
|
+
declare const useNotice: (defaultOptions?: UseNoticeOptions) => CreateNoticeReturn;
|
|
29
|
+
type State = {
|
|
30
|
+
[K in NoticePlacement]: NoticeOptions[];
|
|
31
|
+
};
|
|
32
|
+
type Store = {
|
|
33
|
+
subscribe: (onStoreChange: () => void) => () => void;
|
|
34
|
+
getSnapshot: () => State;
|
|
35
|
+
create: (message: (props: NoticeComponentProps) => ReactNode, options: UseNoticeOptions) => string | number;
|
|
36
|
+
close: (id: string | number) => void;
|
|
37
|
+
closeAll: (options?: {
|
|
38
|
+
placement?: NoticePlacement[];
|
|
39
|
+
}) => void;
|
|
40
|
+
update: (id: string | number, options: Omit<UseNoticeOptions, 'id'>) => void;
|
|
41
|
+
remove: (id: string | number, placement: NoticePlacement) => void;
|
|
42
|
+
isActive: (id: string | number) => boolean;
|
|
43
|
+
};
|
|
44
|
+
declare const noticeStore: Store;
|
|
45
|
+
type NoticeProps = Omit<AlertProps, keyof UseNoticeOptions> & UseNoticeOptions & {
|
|
46
|
+
onClose?: () => void;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export { NoticeOptions, NoticeProps, UseNoticeOptions, noticeStore, useNotice };
|
package/dist/notice.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/notice.tsx
|
|
21
|
+
var notice_exports = {};
|
|
22
|
+
__export(notice_exports, {
|
|
23
|
+
noticeStore: () => noticeStore,
|
|
24
|
+
useNotice: () => useNotice
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(notice_exports);
|
|
27
|
+
var import_alert = require("@yamada-ui/alert");
|
|
28
|
+
var import_close_button = require("@yamada-ui/close-button");
|
|
29
|
+
var import_core = require("@yamada-ui/core");
|
|
30
|
+
var import_utils = require("@yamada-ui/utils");
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
33
|
+
var findId = (options, id) => options.find((notice) => notice.id === id);
|
|
34
|
+
var findNotice = (state, id) => {
|
|
35
|
+
const placement = getNoticePlacement(state, id);
|
|
36
|
+
const index = placement ? state[placement].findIndex((notice) => notice.id === id) : -1;
|
|
37
|
+
return { placement, index };
|
|
38
|
+
};
|
|
39
|
+
var getNoticePlacement = (state, id) => {
|
|
40
|
+
for (const [placement, values] of Object.entries(state)) {
|
|
41
|
+
if (findId(values, id))
|
|
42
|
+
return placement;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var counter = 0;
|
|
46
|
+
var createNotice = (message, { id, placement = "top", duration, onCloseComplete, status, style }) => {
|
|
47
|
+
counter += 1;
|
|
48
|
+
id = id != null ? id : counter;
|
|
49
|
+
return {
|
|
50
|
+
id,
|
|
51
|
+
placement,
|
|
52
|
+
status,
|
|
53
|
+
duration,
|
|
54
|
+
message,
|
|
55
|
+
onDelete: () => noticeStore.remove(String(id), placement),
|
|
56
|
+
isDelete: false,
|
|
57
|
+
onCloseComplete,
|
|
58
|
+
style
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
var createRender = (options) => {
|
|
62
|
+
const { component } = options;
|
|
63
|
+
const Render = (props) => {
|
|
64
|
+
if (typeof component === "function") {
|
|
65
|
+
return component({ ...props, ...options });
|
|
66
|
+
} else {
|
|
67
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Notice, { ...props, ...options });
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
return Render;
|
|
71
|
+
};
|
|
72
|
+
var createNoticeFunc = (defaultOptions, theme) => {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
const themeOptions = (_b = (_a = theme.__config.notice) == null ? void 0 : _a.options) != null ? _b : {};
|
|
75
|
+
const computedOptions = (options) => (0, import_utils.merge)(themeOptions, (0, import_utils.merge)(defaultOptions, options));
|
|
76
|
+
const notice = (options = {}) => {
|
|
77
|
+
options = computedOptions(options);
|
|
78
|
+
const message = createRender(options);
|
|
79
|
+
return noticeStore.create(message, options);
|
|
80
|
+
};
|
|
81
|
+
notice.update = (id, options) => {
|
|
82
|
+
options = computedOptions(options);
|
|
83
|
+
noticeStore.update(id, options);
|
|
84
|
+
};
|
|
85
|
+
notice.closeAll = noticeStore.closeAll;
|
|
86
|
+
notice.close = noticeStore.close;
|
|
87
|
+
notice.isActive = noticeStore.isActive;
|
|
88
|
+
return notice;
|
|
89
|
+
};
|
|
90
|
+
var useNotice = (defaultOptions) => {
|
|
91
|
+
const { theme } = (0, import_core.useTheme)();
|
|
92
|
+
return (0, import_react.useMemo)(() => createNoticeFunc(defaultOptions != null ? defaultOptions : {}, theme), [defaultOptions, theme]);
|
|
93
|
+
};
|
|
94
|
+
var initialState = {
|
|
95
|
+
top: [],
|
|
96
|
+
"top-left": [],
|
|
97
|
+
"top-right": [],
|
|
98
|
+
bottom: [],
|
|
99
|
+
"bottom-left": [],
|
|
100
|
+
"bottom-right": []
|
|
101
|
+
};
|
|
102
|
+
var createNoticeStore = (initialState2) => {
|
|
103
|
+
let state = initialState2;
|
|
104
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
105
|
+
const setState = (setStateFn) => {
|
|
106
|
+
state = setStateFn(state);
|
|
107
|
+
listeners.forEach((l) => l());
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
getSnapshot: () => state,
|
|
111
|
+
subscribe: (listener) => {
|
|
112
|
+
listeners.add(listener);
|
|
113
|
+
return () => {
|
|
114
|
+
setState(() => initialState2);
|
|
115
|
+
listeners.delete(listener);
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
remove: (id, placement) => {
|
|
119
|
+
setState((prevState) => ({
|
|
120
|
+
...prevState,
|
|
121
|
+
[placement]: prevState[placement].filter((notice) => notice.id != id)
|
|
122
|
+
}));
|
|
123
|
+
},
|
|
124
|
+
create: (message, options) => {
|
|
125
|
+
var _a;
|
|
126
|
+
const limit = ((_a = options.limit) != null ? _a : 0) - 1;
|
|
127
|
+
const notice = createNotice(message, options);
|
|
128
|
+
const { placement, id } = notice;
|
|
129
|
+
setState((prev) => {
|
|
130
|
+
var _a2;
|
|
131
|
+
let prevNotices = (_a2 = prev[placement]) != null ? _a2 : [];
|
|
132
|
+
if (limit > 0 && prevNotices.length > limit) {
|
|
133
|
+
const n = prevNotices.length - limit;
|
|
134
|
+
const notices2 = placement.includes("top") ? prevNotices.slice(n * -1) : prevNotices.slice(0, n);
|
|
135
|
+
const ids = notices2.map(({ id: id2 }) => id2);
|
|
136
|
+
prevNotices = prevNotices.map(
|
|
137
|
+
(notice2) => ids.includes(notice2.id) ? { ...notice2, isDelete: true } : notice2
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
const notices = placement.includes("top") ? [notice, ...prevNotices] : [...prevNotices, notice];
|
|
141
|
+
return { ...prev, [placement]: notices };
|
|
142
|
+
});
|
|
143
|
+
return id;
|
|
144
|
+
},
|
|
145
|
+
update: (id, options) => {
|
|
146
|
+
setState((prev) => {
|
|
147
|
+
const next = { ...prev };
|
|
148
|
+
const { placement, index } = findNotice(next, id);
|
|
149
|
+
if (placement && index !== -1) {
|
|
150
|
+
next[placement][index] = {
|
|
151
|
+
...next[placement][index],
|
|
152
|
+
...options,
|
|
153
|
+
message: createRender(options)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return next;
|
|
157
|
+
});
|
|
158
|
+
},
|
|
159
|
+
closeAll: ({ placement } = {}) => {
|
|
160
|
+
setState((prev) => {
|
|
161
|
+
let placements = [
|
|
162
|
+
"bottom",
|
|
163
|
+
"bottom-right",
|
|
164
|
+
"bottom-left",
|
|
165
|
+
"top",
|
|
166
|
+
"top-left",
|
|
167
|
+
"top-right"
|
|
168
|
+
];
|
|
169
|
+
if (placement)
|
|
170
|
+
placements = placement;
|
|
171
|
+
return placements.reduce(
|
|
172
|
+
(acc, placement2) => {
|
|
173
|
+
acc[placement2] = prev[placement2].map((notice) => ({ ...notice, isDelete: true }));
|
|
174
|
+
return acc;
|
|
175
|
+
},
|
|
176
|
+
{ ...prev }
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
close: (id) => {
|
|
181
|
+
setState((prev) => {
|
|
182
|
+
const placement = getNoticePlacement(prev, id);
|
|
183
|
+
if (!placement)
|
|
184
|
+
return prev;
|
|
185
|
+
return {
|
|
186
|
+
...prev,
|
|
187
|
+
[placement]: prev[placement].map(
|
|
188
|
+
(notice) => notice.id == id ? { ...notice, isDelete: true } : notice
|
|
189
|
+
)
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
isActive: (id) => Boolean(findNotice(noticeStore.getSnapshot(), id).placement)
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
var noticeStore = createNoticeStore(initialState);
|
|
197
|
+
var Notice = ({
|
|
198
|
+
variant = "subtle",
|
|
199
|
+
colorScheme,
|
|
200
|
+
status,
|
|
201
|
+
icon,
|
|
202
|
+
title,
|
|
203
|
+
description,
|
|
204
|
+
isClosable,
|
|
205
|
+
onClose
|
|
206
|
+
}) => {
|
|
207
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
208
|
+
import_alert.Alert,
|
|
209
|
+
{
|
|
210
|
+
status,
|
|
211
|
+
variant,
|
|
212
|
+
colorScheme,
|
|
213
|
+
alignItems: "start",
|
|
214
|
+
boxShadow: "lg",
|
|
215
|
+
pe: isClosable ? 8 : void 0,
|
|
216
|
+
children: [
|
|
217
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_alert.AlertIcon, { variant: icon == null ? void 0 : icon.variant, ...(icon == null ? void 0 : icon.color) ? { color: icon.color } : {}, children: icon == null ? void 0 : icon.children }),
|
|
218
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_core.ui.div, { flex: "1", children: [
|
|
219
|
+
title ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_alert.AlertTitle, { noOfLines: 1, children: title }) : null,
|
|
220
|
+
description ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_alert.AlertDescription, { noOfLines: 3, children: description }) : null
|
|
221
|
+
] }),
|
|
222
|
+
isClosable ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_close_button.CloseButton, { size: "sm", onClick: onClose, position: "absolute", top: 2, right: 2 }) : null
|
|
223
|
+
]
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
};
|
|
227
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
228
|
+
0 && (module.exports = {
|
|
229
|
+
noticeStore,
|
|
230
|
+
useNotice
|
|
231
|
+
});
|
package/dist/notice.mjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yamada-ui/notice",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Yamada UI notice component",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"yamada",
|
|
7
|
+
"yamada ui",
|
|
8
|
+
"react",
|
|
9
|
+
"emotion",
|
|
10
|
+
"component",
|
|
11
|
+
"notice",
|
|
12
|
+
"ui",
|
|
13
|
+
"uikit",
|
|
14
|
+
"styled",
|
|
15
|
+
"style-props",
|
|
16
|
+
"styled-component",
|
|
17
|
+
"css-in-js"
|
|
18
|
+
],
|
|
19
|
+
"author": "Hirotomo Yamada <hirotomo.yamada@avap.co.jp>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"main": "dist/index.js",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/hirotomoyamada/yamada-ui",
|
|
32
|
+
"directory": "packages/components/notice"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/hirotomoyamada/yamada-ui/issues"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"react-loader-spinner": "^5.3.4",
|
|
39
|
+
"@yamada-ui/core": "0.1.0",
|
|
40
|
+
"@yamada-ui/alert": "0.1.0",
|
|
41
|
+
"@yamada-ui/utils": "0.1.0",
|
|
42
|
+
"@yamada-ui/close-button": "0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"react": "^18.0.0",
|
|
46
|
+
"clean-package": "2.2.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react": ">=18"
|
|
50
|
+
},
|
|
51
|
+
"clean-package": "../../../clean-package.config.json",
|
|
52
|
+
"tsup": {
|
|
53
|
+
"clean": true,
|
|
54
|
+
"target": "es2019",
|
|
55
|
+
"format": [
|
|
56
|
+
"cjs",
|
|
57
|
+
"esm"
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
"module": "dist/index.mjs",
|
|
61
|
+
"types": "dist/index.d.ts",
|
|
62
|
+
"exports": {
|
|
63
|
+
".": {
|
|
64
|
+
"types": "./dist/index.d.ts",
|
|
65
|
+
"import": "./dist/index.mjs",
|
|
66
|
+
"require": "./dist/index.js"
|
|
67
|
+
},
|
|
68
|
+
"./package.json": "./package.json"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"dev": "pnpm build:fast -- --watch",
|
|
72
|
+
"build": "tsup src --dts",
|
|
73
|
+
"build:fast": "tsup src",
|
|
74
|
+
"clean": "rimraf dist .turbo",
|
|
75
|
+
"typecheck": "tsc --noEmit",
|
|
76
|
+
"gen:docs": "tsx ../../../scripts/generate-docs"
|
|
77
|
+
}
|
|
78
|
+
}
|