@ignite-ember/ui-react 0.0.0-stage → 0.2.1

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.
Files changed (134) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +260 -2
  4. package/dist/CopySource.d.ts +4 -0
  5. package/dist/CopySource.d.ts.map +1 -0
  6. package/dist/CopySource.js +50 -0
  7. package/dist/CopySource.js.map +1 -0
  8. package/dist/DataTable.d.ts +77 -0
  9. package/dist/DataTable.d.ts.map +1 -0
  10. package/dist/DataTable.js +45 -0
  11. package/dist/DataTable.js.map +1 -0
  12. package/dist/Dialog.d.ts +41 -0
  13. package/dist/Dialog.d.ts.map +1 -0
  14. package/dist/Dialog.js +94 -0
  15. package/dist/Dialog.js.map +1 -0
  16. package/dist/ErrorBoundary.d.ts +97 -0
  17. package/dist/ErrorBoundary.d.ts.map +1 -0
  18. package/dist/ErrorBoundary.js +97 -0
  19. package/dist/ErrorBoundary.js.map +1 -0
  20. package/dist/LiveRegion.d.ts +41 -0
  21. package/dist/LiveRegion.d.ts.map +1 -0
  22. package/dist/LiveRegion.js +76 -0
  23. package/dist/LiveRegion.js.map +1 -0
  24. package/dist/Markdown.d.ts +22 -0
  25. package/dist/Markdown.d.ts.map +1 -0
  26. package/dist/Markdown.js +56 -0
  27. package/dist/Markdown.js.map +1 -0
  28. package/dist/MarkdownInline.d.ts +53 -0
  29. package/dist/MarkdownInline.d.ts.map +1 -0
  30. package/dist/MarkdownInline.js +159 -0
  31. package/dist/MarkdownInline.js.map +1 -0
  32. package/dist/MenuIcon.d.ts +60 -0
  33. package/dist/MenuIcon.d.ts.map +1 -0
  34. package/dist/MenuIcon.js +102 -0
  35. package/dist/MenuIcon.js.map +1 -0
  36. package/dist/Mermaid.d.ts +40 -0
  37. package/dist/Mermaid.d.ts.map +1 -0
  38. package/dist/Mermaid.js +121 -0
  39. package/dist/Mermaid.js.map +1 -0
  40. package/dist/Monogram.d.ts +46 -0
  41. package/dist/Monogram.d.ts.map +1 -0
  42. package/dist/Monogram.js +96 -0
  43. package/dist/Monogram.js.map +1 -0
  44. package/dist/OverflowMenu.d.ts +60 -0
  45. package/dist/OverflowMenu.d.ts.map +1 -0
  46. package/dist/OverflowMenu.js +149 -0
  47. package/dist/OverflowMenu.js.map +1 -0
  48. package/dist/Shell.d.ts +67 -0
  49. package/dist/Shell.d.ts.map +1 -0
  50. package/dist/Shell.js +44 -0
  51. package/dist/Shell.js.map +1 -0
  52. package/dist/Skeleton.d.ts +62 -0
  53. package/dist/Skeleton.d.ts.map +1 -0
  54. package/dist/Skeleton.js +56 -0
  55. package/dist/Skeleton.js.map +1 -0
  56. package/dist/Split.d.ts +85 -0
  57. package/dist/Split.d.ts.map +1 -0
  58. package/dist/Split.js +156 -0
  59. package/dist/Split.js.map +1 -0
  60. package/dist/Tabs.d.ts +77 -0
  61. package/dist/Tabs.d.ts.map +1 -0
  62. package/dist/Tabs.js +91 -0
  63. package/dist/Tabs.js.map +1 -0
  64. package/dist/Toast.d.ts +98 -0
  65. package/dist/Toast.d.ts.map +1 -0
  66. package/dist/Toast.js +129 -0
  67. package/dist/Toast.js.map +1 -0
  68. package/dist/Tooltip.d.ts +24 -0
  69. package/dist/Tooltip.d.ts.map +1 -0
  70. package/dist/Tooltip.js +166 -0
  71. package/dist/Tooltip.js.map +1 -0
  72. package/dist/controls.d.ts +117 -0
  73. package/dist/controls.d.ts.map +1 -0
  74. package/dist/controls.js +59 -0
  75. package/dist/controls.js.map +1 -0
  76. package/dist/cx.d.ts +3 -0
  77. package/dist/cx.d.ts.map +1 -0
  78. package/dist/cx.js +5 -0
  79. package/dist/cx.js.map +1 -0
  80. package/dist/fenceInfo.d.ts +26 -0
  81. package/dist/fenceInfo.d.ts.map +1 -0
  82. package/dist/fenceInfo.js +22 -0
  83. package/dist/fenceInfo.js.map +1 -0
  84. package/dist/formControls.d.ts +143 -0
  85. package/dist/formControls.d.ts.map +1 -0
  86. package/dist/formControls.js +108 -0
  87. package/dist/formControls.js.map +1 -0
  88. package/dist/index.d.ts +78 -0
  89. package/dist/index.d.ts.map +1 -0
  90. package/dist/index.js +78 -0
  91. package/dist/index.js.map +1 -0
  92. package/dist/layout.d.ts +95 -0
  93. package/dist/layout.d.ts.map +1 -0
  94. package/dist/layout.js +93 -0
  95. package/dist/layout.js.map +1 -0
  96. package/dist/primitives.d.ts +33 -0
  97. package/dist/primitives.d.ts.map +1 -0
  98. package/dist/primitives.js +33 -0
  99. package/dist/primitives.js.map +1 -0
  100. package/dist/progress.d.ts +64 -0
  101. package/dist/progress.d.ts.map +1 -0
  102. package/dist/progress.js +51 -0
  103. package/dist/progress.js.map +1 -0
  104. package/dist/text.d.ts +63 -0
  105. package/dist/text.d.ts.map +1 -0
  106. package/dist/text.js +61 -0
  107. package/dist/text.js.map +1 -0
  108. package/package.json +56 -4
  109. package/src/CopySource.tsx +67 -0
  110. package/src/DataTable.tsx +120 -0
  111. package/src/Dialog.tsx +158 -0
  112. package/src/ErrorBoundary.tsx +137 -0
  113. package/src/LiveRegion.tsx +127 -0
  114. package/src/Markdown.tsx +75 -0
  115. package/src/MarkdownInline.tsx +177 -0
  116. package/src/MenuIcon.tsx +209 -0
  117. package/src/Mermaid.tsx +144 -0
  118. package/src/Monogram.tsx +112 -0
  119. package/src/OverflowMenu.tsx +359 -0
  120. package/src/Shell.tsx +99 -0
  121. package/src/Skeleton.tsx +98 -0
  122. package/src/Split.tsx +267 -0
  123. package/src/Tabs.tsx +194 -0
  124. package/src/Toast.tsx +269 -0
  125. package/src/Tooltip.tsx +213 -0
  126. package/src/controls.tsx +159 -0
  127. package/src/cx.ts +4 -0
  128. package/src/fenceInfo.ts +36 -0
  129. package/src/formControls.tsx +249 -0
  130. package/src/index.ts +149 -0
  131. package/src/layout.tsx +141 -0
  132. package/src/primitives.tsx +33 -0
  133. package/src/progress.tsx +129 -0
  134. package/src/text.tsx +99 -0
@@ -0,0 +1,359 @@
1
+ import { useCallback, useEffect, useId, useRef, useState } from "react";
2
+ import { MenuIcon, type MenuIconName } from "./MenuIcon.js";
3
+
4
+ /**
5
+ * A row of actions collapsed behind one button.
6
+ *
7
+ * For the case where the actions are real but secondary: four buttons
8
+ * across a header give each of them the same weight as the page
9
+ * title, and the one that deletes the thing ends up the same size as
10
+ * the one that exports it.
11
+ *
12
+ * Hiding them costs discoverability, so everything that made them
13
+ * usable as buttons has to survive the move — focus, the keyboard,
14
+ * and the names screen readers announce. That is most of what is
15
+ * below.
16
+ */
17
+
18
+ /** One action in an {@link OverflowMenu}. */
19
+ export interface OverflowAction {
20
+ /** Stable across renders; the label changes on toggles. */
21
+ key: string;
22
+ /** What it is called. */
23
+ label: string;
24
+ /** What picking it does. Omitted when `items` makes it a submenu. */
25
+ onSelect?: () => void;
26
+ /** Variants of this action, opened as a submenu rather than run.
27
+ * Use it when one verb has several objects — export as HTML, as
28
+ * Markdown, as PDF — so the menu stays a list of things you can
29
+ * do rather than a list of every combination of them. */
30
+ items?: OverflowAction[];
31
+ /** Destructive — rendered apart from the rest and in the danger colour. */
32
+ danger?: boolean;
33
+ /** A toggle that is currently on. Announced as such, not just coloured. */
34
+ active?: boolean;
35
+ /** Whether it can be picked. Shown as it is, and kept out of the tab order. */
36
+ disabled?: boolean;
37
+ /** Why you would pick this. Shown on hover, same as the buttons had. */
38
+ title?: string;
39
+ /** The glyph beside the label.
40
+ *
41
+ * Optional. A menu of five verbs reads as five equal options, and
42
+ * picking the wrong one of Archive and Remove costs quite
43
+ * differently — the shape is recognised before the word is read. */
44
+ icon?: MenuIconName;
45
+ }
46
+
47
+ /** Direct children only — a submenu's own items belong to its list. */
48
+ const OWN_ITEMS =
49
+ ":scope > [role='menuitem'], :scope > .overflow-sub-wrap > [role='menuitem']";
50
+
51
+ function enabledItems(root: HTMLElement | null): HTMLButtonElement[] {
52
+ return Array.from(
53
+ root?.querySelectorAll<HTMLButtonElement>(OWN_ITEMS) ?? [],
54
+ ).filter((el) => !el.disabled);
55
+ }
56
+
57
+ /** Up/down within one list, wrapping. Returns whether it handled the key. */
58
+ function moveFocus(root: HTMLElement | null, key: string): boolean {
59
+ if (key !== "ArrowDown" && key !== "ArrowUp") return false;
60
+ const all = enabledItems(root);
61
+ if (!all.length) return false;
62
+ const at = all.indexOf(document.activeElement as HTMLButtonElement);
63
+ const step = key === "ArrowDown" ? 1 : -1;
64
+ // Wrapping, because a menu this short has no scroll to tell you
65
+ // that you have reached the end of it.
66
+ all[(at + step + all.length) % all.length]?.focus();
67
+ return true;
68
+ }
69
+
70
+ /**
71
+ * A row of actions collapsed behind one button.
72
+ *
73
+ * For actions that are real but secondary. Everything that made the
74
+ * buttons usable as buttons survives the move: focus, the keyboard,
75
+ * and the names screen readers announce.
76
+ *
77
+ * @param actions — the actions to offer; the destructive ones are set
78
+ * apart from the rest.
79
+ * @param label — what the trigger is called, for anyone who cannot see
80
+ * three dots.
81
+ */
82
+ export function OverflowMenu({
83
+ actions,
84
+ label = "More actions",
85
+ }: {
86
+ actions: OverflowAction[];
87
+ /** What the trigger is called, for anyone who cannot see three dots. */
88
+ label?: string;
89
+ }) {
90
+ const [open, setOpen] = useState(false);
91
+ const trigger = useRef<HTMLButtonElement>(null);
92
+ const list = useRef<HTMLDivElement>(null);
93
+ const menuId = useId();
94
+
95
+ // Closing returns focus to the trigger. Without it a keyboard user
96
+ // who opens the menu and changes their mind is dropped at the top
97
+ // of the document with no way back to where they were.
98
+ const close = useCallback((restoreFocus = true) => {
99
+ setOpen(false);
100
+ if (restoreFocus) trigger.current?.focus();
101
+ }, []);
102
+
103
+ // Opening moves focus into the menu, which is what makes it a menu
104
+ // rather than a panel that happens to be visible: the next Tab or
105
+ // arrow key lands on an action instead of on whatever follows the
106
+ // trigger in the document.
107
+ useEffect(() => {
108
+ if (open) enabledItems(list.current)[0]?.focus();
109
+ }, [open]);
110
+
111
+ const onKeyDown = (e: React.KeyboardEvent) => {
112
+ if (e.key === "Escape") {
113
+ // Only if no submenu is open — that one owns Escape first, and
114
+ // closes back to here rather than out of the menu entirely.
115
+ if (list.current?.querySelector(".overflow-sub")) return;
116
+ e.stopPropagation();
117
+ close();
118
+ return;
119
+ }
120
+ if (moveFocus(list.current, e.key)) e.preventDefault();
121
+ };
122
+
123
+ if (!actions.length) return null;
124
+
125
+ const safe = actions.filter((a) => !a.danger);
126
+ const destructive = actions.filter((a) => a.danger);
127
+ const dismiss = () => close(false);
128
+
129
+ return (
130
+ <div className="overflow-menu">
131
+ <button
132
+ ref={trigger}
133
+ type="button"
134
+ className="kb-icon-btn"
135
+ aria-haspopup="menu"
136
+ aria-expanded={open}
137
+ aria-controls={open ? menuId : undefined}
138
+ aria-label={label}
139
+ title={label}
140
+ onClick={() => (open ? close() : setOpen(true))}
141
+ >
142
+ <svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true">
143
+ <circle cx="3" cy="8" r="1.4" fill="currentColor" />
144
+ <circle cx="8" cy="8" r="1.4" fill="currentColor" />
145
+ <circle cx="13" cy="8" r="1.4" fill="currentColor" />
146
+ </svg>
147
+ </button>
148
+
149
+ {open && (
150
+ <>
151
+ {/* Outside click closes it — the same backdrop the composer's
152
+ mode picker uses. No focus restore here: the click has
153
+ already put focus wherever the user aimed it, and pulling
154
+ it back to the trigger would undo that. */}
155
+ <div className="overflow-backdrop" onClick={dismiss} />
156
+ <div
157
+ ref={list}
158
+ id={menuId}
159
+ className="overflow-list"
160
+ role="menu"
161
+ aria-label={label}
162
+ onKeyDown={onKeyDown}
163
+ >
164
+ {safe.map((a) => (
165
+ <Entry key={a.key} action={a} onDone={dismiss} />
166
+ ))}
167
+ {/* A separator, so the item that removes the thing is not
168
+ one slip of the wrist below the one that shares it. */}
169
+ {destructive.length > 0 && safe.length > 0 && (
170
+ <div className="overflow-sep" role="separator" />
171
+ )}
172
+ {destructive.map((a) => (
173
+ <Entry key={a.key} action={a} onDone={dismiss} />
174
+ ))}
175
+ </div>
176
+ </>
177
+ )}
178
+ </div>
179
+ );
180
+ }
181
+
182
+ function Entry({
183
+ action,
184
+ onDone,
185
+ }: {
186
+ action: OverflowAction;
187
+ onDone: () => void;
188
+ }) {
189
+ return action.items?.length ? (
190
+ <Submenu action={action} onDone={onDone} />
191
+ ) : (
192
+ <Item action={action} onDone={onDone} />
193
+ );
194
+ }
195
+
196
+ /**
197
+ * An item that opens its own list beside it.
198
+ *
199
+ * Opens to the *left*. The parent menu is already pinned to the right
200
+ * edge of what it hangs off, so a submenu flying out to the right —
201
+ * the desktop default — would open past the window.
202
+ */
203
+ function Submenu({
204
+ action,
205
+ onDone,
206
+ }: {
207
+ action: OverflowAction;
208
+ onDone: () => void;
209
+ }) {
210
+ const [open, setOpen] = useState(false);
211
+ const item = useRef<HTMLButtonElement>(null);
212
+ const list = useRef<HTMLDivElement>(null);
213
+ const menuId = useId();
214
+
215
+ useEffect(() => {
216
+ if (open) enabledItems(list.current)[0]?.focus();
217
+ }, [open]);
218
+
219
+ const back = () => {
220
+ setOpen(false);
221
+ item.current?.focus();
222
+ };
223
+
224
+ return (
225
+ <div
226
+ className="overflow-sub-wrap"
227
+ // Hover opens it, the way a desktop menu does. `onMouseLeave`
228
+ // rather than a timer: a submenu that lingers after the pointer
229
+ // has gone covers the items below the one that opened it.
230
+ onMouseEnter={() => {
231
+ if (!action.disabled) setOpen(true);
232
+ }}
233
+ onMouseLeave={() => setOpen(false)}
234
+ >
235
+ <button
236
+ ref={item}
237
+ type="button"
238
+ role="menuitem"
239
+ className="popup-item overflow-item overflow-item-parent"
240
+ aria-haspopup="menu"
241
+ aria-expanded={open}
242
+ aria-controls={open ? menuId : undefined}
243
+ disabled={action.disabled}
244
+ title={action.title}
245
+ onClick={() => setOpen((on) => !on)}
246
+ onKeyDown={(e) => {
247
+ // Right opens and steps in, matching every other menu on
248
+ // the desktop. Enter and Space come free from `button`.
249
+ if (e.key === "ArrowRight" || e.key === "Enter" || e.key === " ") {
250
+ e.preventDefault();
251
+ e.stopPropagation();
252
+ setOpen(true);
253
+ }
254
+ }}
255
+ >
256
+ {action.icon && <MenuIcon name={action.icon} />}
257
+ {action.label}
258
+ <svg
259
+ className="overflow-chevron"
260
+ viewBox="0 0 16 16"
261
+ width="11"
262
+ height="11"
263
+ aria-hidden="true"
264
+ >
265
+ <path
266
+ d="M6 3.5 10.5 8 6 12.5"
267
+ fill="none"
268
+ stroke="currentColor"
269
+ strokeWidth="1.6"
270
+ strokeLinecap="round"
271
+ strokeLinejoin="round"
272
+ />
273
+ </svg>
274
+ </button>
275
+
276
+ {open && (
277
+ <div
278
+ ref={list}
279
+ id={menuId}
280
+ className="overflow-list overflow-sub"
281
+ role="menu"
282
+ aria-label={action.label}
283
+ onKeyDown={(e) => {
284
+ // Escape and Left close *this* list and go back to the
285
+ // item that opened it, rather than dropping the whole
286
+ // menu — one level per keypress, like the trail does.
287
+ if (e.key === "Escape" || e.key === "ArrowLeft") {
288
+ e.preventDefault();
289
+ e.stopPropagation();
290
+ back();
291
+ return;
292
+ }
293
+ if (moveFocus(list.current, e.key)) {
294
+ e.preventDefault();
295
+ e.stopPropagation();
296
+ }
297
+ }}
298
+ >
299
+ {action.items?.map((child) => (
300
+ <Item
301
+ key={child.key}
302
+ action={child}
303
+ onDone={onDone}
304
+ parent={action.label}
305
+ />
306
+ ))}
307
+ </div>
308
+ )}
309
+ </div>
310
+ );
311
+ }
312
+
313
+ function Item({
314
+ action,
315
+ onDone,
316
+ parent,
317
+ }: {
318
+ action: OverflowAction;
319
+ onDone: () => void;
320
+ /** The submenu this sits under, if any. */
321
+ parent?: string;
322
+ }) {
323
+ return (
324
+ <button
325
+ type="button"
326
+ role="menuitem"
327
+ // A toggle is `menuitemradio`-ish, but `aria-pressed` on a
328
+ // menuitem is not read. `aria-current` is: it says "this is the
329
+ // state you are in" rather than leaving the state to the colour.
330
+ aria-current={action.active ? true : undefined}
331
+ // "PDF" alone says nothing once the submenu that gives it
332
+ // meaning has been navigated past. The full name travels with
333
+ // the item.
334
+ aria-label={parent ? `${parent} ${action.label}` : undefined}
335
+ className={[
336
+ "popup-item",
337
+ "overflow-item",
338
+ action.active ? "active" : "",
339
+ action.danger ? "overflow-item-danger" : "",
340
+ ]
341
+ .filter(Boolean)
342
+ .join(" ")}
343
+ disabled={action.disabled}
344
+ title={action.title}
345
+ // Closed without pulling focus back to the trigger, because most
346
+ // of these actions replace the header the trigger lives in —
347
+ // restoring focus to an element that is about to unmount loses
348
+ // it on `document.body`. Where focus goes next belongs to the
349
+ // action. Dismissing without choosing still restores it.
350
+ onClick={() => {
351
+ onDone();
352
+ action.onSelect?.();
353
+ }}
354
+ >
355
+ {action.icon && <MenuIcon name={action.icon} />}
356
+ {action.label}
357
+ </button>
358
+ );
359
+ }
package/src/Shell.tsx ADDED
@@ -0,0 +1,99 @@
1
+ /**
2
+ * The page an extension fills.
3
+ *
4
+ * `.igni-root` set colour, type and `margin: 0`, and nothing about
5
+ * layout — no height, no `box-sizing`, nothing on `html`. So every
6
+ * full-frame extension opened by writing the same six lines of CSS,
7
+ * and wrote them differently. Two panels in one product then scroll
8
+ * differently, which is the kind of difference nobody can name and
9
+ * everybody notices.
10
+ *
11
+ * `min-height: 0` is the reason this is a published component rather
12
+ * than a recipe in a README. A flex child defaults to
13
+ * `min-height: auto`, which refuses to shrink below its content, so
14
+ * a scroll container nested in a flex column grows the page instead
15
+ * of scrolling. It is invisible until the content is long, it is the
16
+ * most-reported layout bug in flexbox, and the fix is one line that
17
+ * reads like it does nothing.
18
+ */
19
+ import type { HTMLAttributes, ReactNode } from "react";
20
+
21
+ function cx(...parts: (string | false | null | undefined)[]): string {
22
+ return parts.filter(Boolean).join(" ");
23
+ }
24
+
25
+ /** What an {@link AppShell} takes. */
26
+ export interface AppShellProps extends HTMLAttributes<HTMLDivElement> {
27
+ /** Fixed at the top. A `Toolbar`, usually. */
28
+ header?: ReactNode;
29
+ /** Fixed at the bottom. Status, or a form's actions. */
30
+ footer?: ReactNode;
31
+ }
32
+
33
+ /**
34
+ * Header, body, footer — with exactly one part allowed to grow.
35
+ *
36
+ * The body is a flex column itself, so a `Scroll` placed directly
37
+ * inside it fills what is left over rather than the whole frame.
38
+ *
39
+ * @param header — fixed at the top; a `Toolbar`, usually.
40
+ * @param footer — fixed at the bottom: status, or a form's actions.
41
+ * @param children — the body, the one part allowed to grow.
42
+ * @param className — added to the element's classes, beside the one the
43
+ * theme gives it.
44
+ * @param rest — the remaining props, spread onto the shell.
45
+ */
46
+ export function AppShell({
47
+ header,
48
+ footer,
49
+ children,
50
+ className,
51
+ ...rest
52
+ }: AppShellProps) {
53
+ return (
54
+ <div {...rest} className={cx("ui-shell", className)}>
55
+ {header === undefined ? null : (
56
+ <div className="ui-shell-header">{header}</div>
57
+ )}
58
+ <div className="ui-shell-body">{children}</div>
59
+ {footer === undefined ? null : (
60
+ <div className="ui-shell-footer">{footer}</div>
61
+ )}
62
+ </div>
63
+ );
64
+ }
65
+
66
+ /** What a {@link Scroll} takes. */
67
+ export interface ScrollProps extends HTMLAttributes<HTMLDivElement> {
68
+ /** Scroll sideways instead. A wide table, not a long list. */
69
+ axis?: "y" | "x";
70
+ }
71
+
72
+ /**
73
+ * The part that scrolls.
74
+ *
75
+ * `overscroll-behavior: contain`, so reaching the end does not chain
76
+ * the scroll to the host page behind the frame — an extension that
77
+ * scrolls igni when its own list bottoms out feels like a bug in
78
+ * igni.
79
+ *
80
+ * Not focusable by default. A scrollable region that holds no
81
+ * focusable children is unreachable by keyboard, so pass
82
+ * `tabIndex={0}` when that is the case; doing it unconditionally
83
+ * would put a stop in the tab order of every list that already has
84
+ * buttons in it.
85
+ *
86
+ * @param axis — scroll sideways instead; for a wide table, not a long
87
+ * list.
88
+ * @param className — added to the element's classes, beside the one the
89
+ * theme gives it.
90
+ * @param rest — the remaining props, spread onto the element.
91
+ */
92
+ export function Scroll({ axis = "y", className, ...rest }: ScrollProps) {
93
+ return (
94
+ <div
95
+ {...rest}
96
+ className={cx(axis === "x" ? "ui-scroll-x" : "ui-scroll", className)}
97
+ />
98
+ );
99
+ }
@@ -0,0 +1,98 @@
1
+ import type { CSSProperties } from "react";
2
+
3
+ /**
4
+ * Animated placeholder block. Render at the same approximate size
5
+ * and rhythm as the eventual content so the layout doesn't jump
6
+ * when async data lands.
7
+ *
8
+ * Usage:
9
+ * <Skeleton.Line width="60%" />
10
+ * <Skeleton.Block height={64} />
11
+ * <Skeleton.Circle size={48} />
12
+ */
13
+ export const Skeleton = {
14
+ /**
15
+ * A line of placeholder text, the size of the line it stands in for.
16
+ *
17
+ * @param width — how wide the line is, in pixels or as a CSS length.
18
+ * @param height — how tall the line is, in pixels or as a CSS length.
19
+ * @param style — the element's inline style, merged over the
20
+ * defaults.
21
+ */
22
+ Line({
23
+ width = "100%",
24
+ height = 12,
25
+ style,
26
+ }: {
27
+ width?: number | string;
28
+ height?: number | string;
29
+ style?: CSSProperties;
30
+ }) {
31
+ return (
32
+ <span
33
+ className="skeleton skeleton-text"
34
+ style={{ display: "block", width, height, ...style }}
35
+ />
36
+ );
37
+ },
38
+ /**
39
+ * A block of placeholder content, the size of the content it stands in for.
40
+ *
41
+ * @param height — how tall the block is, in pixels or as a CSS
42
+ * length.
43
+ * @param width — how wide the block is, in pixels or as a CSS length.
44
+ * @param style — the element's inline style, merged over the
45
+ * defaults.
46
+ */
47
+ Block({
48
+ height = 64,
49
+ width = "100%",
50
+ style,
51
+ }: {
52
+ height?: number | string;
53
+ width?: number | string;
54
+ style?: CSSProperties;
55
+ }) {
56
+ return (
57
+ <span
58
+ className="skeleton skeleton-block"
59
+ style={{ display: "block", height, width, ...style }}
60
+ />
61
+ );
62
+ },
63
+ /**
64
+ * A circle, the size of the thing it stands in for.
65
+ *
66
+ * @param size — the diameter of the circle, in pixels.
67
+ * @param style — the element's inline style, merged over the
68
+ * defaults.
69
+ */
70
+ Circle({ size = 24, style }: { size?: number; style?: CSSProperties }) {
71
+ return (
72
+ <span
73
+ className="skeleton skeleton-circle"
74
+ style={{ display: "inline-block", width: size, height: size, ...style }}
75
+ />
76
+ );
77
+ },
78
+ /**
79
+ * Several lines of placeholder text, the shape of the list they stand in for.
80
+ *
81
+ * @param count — how many rows of text to draw.
82
+ * @param style — the stack's inline style, merged over the defaults.
83
+ */
84
+ Rows({ count = 4, style }: { count?: number; style?: CSSProperties }) {
85
+ return (
86
+ <div
87
+ style={{ display: "flex", flexDirection: "column", gap: 12, ...style }}
88
+ >
89
+ {Array.from({ length: count }).map((_, i) => (
90
+ <div key={i}>
91
+ <Skeleton.Line width="40%" height={11} />
92
+ <Skeleton.Line width="100%" />
93
+ </div>
94
+ ))}
95
+ </div>
96
+ );
97
+ },
98
+ };