@gopherium/godmin 0.15.0 → 0.16.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/dist/toaster.js CHANGED
@@ -19,6 +19,28 @@ const DISMISS_GLYPH = '✕';
19
19
  * How many toasts stay on screen at once.
20
20
  */
21
21
  const LIMIT = 3;
22
+ /**
23
+ * How many characters of a name a toast shows before the ellipsis.
24
+ */
25
+ const NAME_LENGTH = 45;
26
+ /**
27
+ * The mark that ends a name a toast cut short.
28
+ */
29
+ const ELLIPSIS = '…';
30
+ /**
31
+ * Returns a title cut to the given number of characters, its trailing spaces trimmed and an ellipsis added.
32
+ * @param title - The title to name.
33
+ * @param length - How many characters to keep.
34
+ * @returns The title whole when it fits, the cut title otherwise.
35
+ */
36
+ function cutName(title, length) {
37
+ const characters = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
38
+ const parts = Array.from(characters.segment(title), (part) => part.segment);
39
+ if (parts.length <= length) {
40
+ return title;
41
+ }
42
+ return `${parts.slice(0, length).join('').trimEnd()}${ELLIPSIS}`;
43
+ }
22
44
  const ToasterContext = createContext(null);
23
45
  /**
24
46
  * Returns the handle raising messages in the toast region.
@@ -129,10 +151,10 @@ function ActionToast({ toast, action, dismissAfter, dismissLabel, region, onLeav
129
151
  }
130
152
  /**
131
153
  * Renders the region holding raised messages around the given tree.
132
- * @param props - The tree the region wraps, how long a toast stays, the dismiss control name and how many stay.
154
+ * @param props - The tree the region wraps, the toast time, the close button name, how many stay and the name length.
133
155
  * @returns The wrapped tree with its region.
134
156
  */
135
- export function Toaster({ children, dismissAfter = DISMISS_AFTER, dismissLabel = DISMISS_LABEL, limit = LIMIT, }) {
157
+ export function Toaster({ children, dismissAfter = DISMISS_AFTER, dismissLabel = DISMISS_LABEL, limit = LIMIT, nameLength = NAME_LENGTH, }) {
136
158
  const [toasts, setToasts] = useState([]);
137
159
  const nextId = useRef(0);
138
160
  const region = useRef(null);
@@ -158,7 +180,8 @@ export function Toaster({ children, dismissAfter = DISMISS_AFTER, dismissLabel =
158
180
  speak(message, 'polite');
159
181
  setToasts((held) => admit(held, { id, message, action, leaving: false }, limit));
160
182
  }, [limit]);
161
- const handle = useMemo(() => ({ show }), [show]);
183
+ const name = useCallback((title) => cutName(title, nameLength), [nameLength]);
184
+ const handle = useMemo(() => ({ show, name }), [show, name]);
162
185
  const hint = useId();
163
186
  return (_jsxs(ToasterContext.Provider, { value: handle, children: [children, _jsx("div", { ref: region, className: "godmin-toasts", tabIndex: -1, children: toasts.map((toast) => toast.action === undefined ? (_jsx(PlainToast, { toast: toast, dismissAfter: dismissAfter, hint: hint, region: region, onLeave: leave, onGone: gone }, toast.id)) : (_jsx(ActionToast, { toast: toast, action: toast.action, dismissAfter: dismissAfter, dismissLabel: dismissLabel, region: region, onLeave: leave, onGone: gone }, toast.id))) }), _jsx("span", { id: hint, hidden: true, children: dismissLabel })] }));
164
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gopherium/godmin",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "The admin kit for Gopherium projects: host layer for React admin applications on the WordPress Design System.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -84,6 +84,7 @@
84
84
  "devDependencies": {
85
85
  "@tanstack/react-router": "^1.170.18",
86
86
  "@testing-library/react": "^16.3.2",
87
+ "@testing-library/user-event": "^14.6.7",
87
88
  "@types/node": "^26.1.2",
88
89
  "@types/react": "^19.2.17",
89
90
  "@vitejs/plugin-react": "^6.0.3",