@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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +260 -2
- package/dist/CopySource.d.ts +4 -0
- package/dist/CopySource.d.ts.map +1 -0
- package/dist/CopySource.js +50 -0
- package/dist/CopySource.js.map +1 -0
- package/dist/DataTable.d.ts +77 -0
- package/dist/DataTable.d.ts.map +1 -0
- package/dist/DataTable.js +45 -0
- package/dist/DataTable.js.map +1 -0
- package/dist/Dialog.d.ts +41 -0
- package/dist/Dialog.d.ts.map +1 -0
- package/dist/Dialog.js +94 -0
- package/dist/Dialog.js.map +1 -0
- package/dist/ErrorBoundary.d.ts +97 -0
- package/dist/ErrorBoundary.d.ts.map +1 -0
- package/dist/ErrorBoundary.js +97 -0
- package/dist/ErrorBoundary.js.map +1 -0
- package/dist/LiveRegion.d.ts +41 -0
- package/dist/LiveRegion.d.ts.map +1 -0
- package/dist/LiveRegion.js +76 -0
- package/dist/LiveRegion.js.map +1 -0
- package/dist/Markdown.d.ts +22 -0
- package/dist/Markdown.d.ts.map +1 -0
- package/dist/Markdown.js +56 -0
- package/dist/Markdown.js.map +1 -0
- package/dist/MarkdownInline.d.ts +53 -0
- package/dist/MarkdownInline.d.ts.map +1 -0
- package/dist/MarkdownInline.js +159 -0
- package/dist/MarkdownInline.js.map +1 -0
- package/dist/MenuIcon.d.ts +60 -0
- package/dist/MenuIcon.d.ts.map +1 -0
- package/dist/MenuIcon.js +102 -0
- package/dist/MenuIcon.js.map +1 -0
- package/dist/Mermaid.d.ts +40 -0
- package/dist/Mermaid.d.ts.map +1 -0
- package/dist/Mermaid.js +121 -0
- package/dist/Mermaid.js.map +1 -0
- package/dist/Monogram.d.ts +46 -0
- package/dist/Monogram.d.ts.map +1 -0
- package/dist/Monogram.js +96 -0
- package/dist/Monogram.js.map +1 -0
- package/dist/OverflowMenu.d.ts +60 -0
- package/dist/OverflowMenu.d.ts.map +1 -0
- package/dist/OverflowMenu.js +149 -0
- package/dist/OverflowMenu.js.map +1 -0
- package/dist/Shell.d.ts +67 -0
- package/dist/Shell.d.ts.map +1 -0
- package/dist/Shell.js +44 -0
- package/dist/Shell.js.map +1 -0
- package/dist/Skeleton.d.ts +62 -0
- package/dist/Skeleton.d.ts.map +1 -0
- package/dist/Skeleton.js +56 -0
- package/dist/Skeleton.js.map +1 -0
- package/dist/Split.d.ts +85 -0
- package/dist/Split.d.ts.map +1 -0
- package/dist/Split.js +156 -0
- package/dist/Split.js.map +1 -0
- package/dist/Tabs.d.ts +77 -0
- package/dist/Tabs.d.ts.map +1 -0
- package/dist/Tabs.js +91 -0
- package/dist/Tabs.js.map +1 -0
- package/dist/Toast.d.ts +98 -0
- package/dist/Toast.d.ts.map +1 -0
- package/dist/Toast.js +129 -0
- package/dist/Toast.js.map +1 -0
- package/dist/Tooltip.d.ts +24 -0
- package/dist/Tooltip.d.ts.map +1 -0
- package/dist/Tooltip.js +166 -0
- package/dist/Tooltip.js.map +1 -0
- package/dist/controls.d.ts +117 -0
- package/dist/controls.d.ts.map +1 -0
- package/dist/controls.js +59 -0
- package/dist/controls.js.map +1 -0
- package/dist/cx.d.ts +3 -0
- package/dist/cx.d.ts.map +1 -0
- package/dist/cx.js +5 -0
- package/dist/cx.js.map +1 -0
- package/dist/fenceInfo.d.ts +26 -0
- package/dist/fenceInfo.d.ts.map +1 -0
- package/dist/fenceInfo.js +22 -0
- package/dist/fenceInfo.js.map +1 -0
- package/dist/formControls.d.ts +143 -0
- package/dist/formControls.d.ts.map +1 -0
- package/dist/formControls.js +108 -0
- package/dist/formControls.js.map +1 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +95 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +93 -0
- package/dist/layout.js.map +1 -0
- package/dist/primitives.d.ts +33 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +33 -0
- package/dist/primitives.js.map +1 -0
- package/dist/progress.d.ts +64 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +51 -0
- package/dist/progress.js.map +1 -0
- package/dist/text.d.ts +63 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +61 -0
- package/dist/text.js.map +1 -0
- package/package.json +56 -4
- package/src/CopySource.tsx +67 -0
- package/src/DataTable.tsx +120 -0
- package/src/Dialog.tsx +158 -0
- package/src/ErrorBoundary.tsx +137 -0
- package/src/LiveRegion.tsx +127 -0
- package/src/Markdown.tsx +75 -0
- package/src/MarkdownInline.tsx +177 -0
- package/src/MenuIcon.tsx +209 -0
- package/src/Mermaid.tsx +144 -0
- package/src/Monogram.tsx +112 -0
- package/src/OverflowMenu.tsx +359 -0
- package/src/Shell.tsx +99 -0
- package/src/Skeleton.tsx +98 -0
- package/src/Split.tsx +267 -0
- package/src/Tabs.tsx +194 -0
- package/src/Toast.tsx +269 -0
- package/src/Tooltip.tsx +213 -0
- package/src/controls.tsx +159 -0
- package/src/cx.ts +4 -0
- package/src/fenceInfo.ts +36 -0
- package/src/formControls.tsx +249 -0
- package/src/index.ts +149 -0
- package/src/layout.tsx +141 -0
- package/src/primitives.tsx +33 -0
- package/src/progress.tsx +129 -0
- package/src/text.tsx +99 -0
package/src/Split.tsx
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two panes and a rule you can move.
|
|
3
|
+
*
|
|
4
|
+
* Both extensions built against this floor wanted one and neither
|
|
5
|
+
* could have it: `ColumnHandle` is 180 lines in the app, does exactly
|
|
6
|
+
* this, and was not published. `examples/release-notes` hand-rolls
|
|
7
|
+
* the grid, the pointer capture, the clamping and the separator, and
|
|
8
|
+
* says so in its README. Every two-pane extension would write that
|
|
9
|
+
* again, differently — which is how two panels in one product end up
|
|
10
|
+
* resizing differently.
|
|
11
|
+
*
|
|
12
|
+
* **It drives a CSS variable, not React state.** A `pointermove`
|
|
13
|
+
* handler that re-rendered would re-render the whole document on
|
|
14
|
+
* every frame of the drag, and would stutter on exactly the documents
|
|
15
|
+
* busy enough to be worth resizing. Writing the variable lets the
|
|
16
|
+
* browser reflow and leaves the tree alone.
|
|
17
|
+
*
|
|
18
|
+
* **It does not remember the width**, and that is the deliberate
|
|
19
|
+
* difference from the app's version. An extension is served from an
|
|
20
|
+
* ephemeral port, a port is part of an origin, so its `localStorage`
|
|
21
|
+
* is wiped whenever the backend restarts — persisting there would be
|
|
22
|
+
* a promise this cannot keep. `onResize` hands the width back
|
|
23
|
+
* instead, and an extension that wants it remembered puts it
|
|
24
|
+
* somewhere that survives: `bridge.setLocation`, or its own backend.
|
|
25
|
+
*
|
|
26
|
+
* The other difference is `aria-valuenow`. A focusable `separator`
|
|
27
|
+
* that never says where it is tells a screen-reader user that
|
|
28
|
+
* something is adjustable and not what it is adjusted to.
|
|
29
|
+
*/
|
|
30
|
+
import {
|
|
31
|
+
useCallback,
|
|
32
|
+
useId,
|
|
33
|
+
useRef,
|
|
34
|
+
useState,
|
|
35
|
+
type CSSProperties,
|
|
36
|
+
type HTMLAttributes,
|
|
37
|
+
type PointerEvent as ReactPointerEvent,
|
|
38
|
+
type KeyboardEvent as ReactKeyboardEvent,
|
|
39
|
+
type ReactNode,
|
|
40
|
+
} from "react";
|
|
41
|
+
|
|
42
|
+
import { cx } from "./cx.js";
|
|
43
|
+
|
|
44
|
+
/** Keyboard step, and the larger one with Shift held. */
|
|
45
|
+
const STEP = 12;
|
|
46
|
+
const BIG_STEP = 48;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* How much of the window one pane may take, whatever `max` says.
|
|
50
|
+
*
|
|
51
|
+
* A pane dragged wide on a narrow laptop otherwise leaves the other
|
|
52
|
+
* a few characters across — a state the reader has to drag their way
|
|
53
|
+
* back out of.
|
|
54
|
+
*/
|
|
55
|
+
const MOST_OF_WINDOW = 0.6;
|
|
56
|
+
|
|
57
|
+
/** What a {@link Split} takes. */
|
|
58
|
+
export interface SplitProps extends Omit<
|
|
59
|
+
HTMLAttributes<HTMLDivElement>,
|
|
60
|
+
"onResize"
|
|
61
|
+
> {
|
|
62
|
+
/** The fixed-width pane. */
|
|
63
|
+
aside: ReactNode;
|
|
64
|
+
/** The pane that takes what is left. */
|
|
65
|
+
children: ReactNode;
|
|
66
|
+
/** Which side `aside` is on. Defaults to the left. */
|
|
67
|
+
side?: "start" | "end";
|
|
68
|
+
/** Starting width of `aside`, in pixels. */
|
|
69
|
+
defaultWidth?: number;
|
|
70
|
+
/** The narrowest `aside` may be, in pixels. */
|
|
71
|
+
min?: number;
|
|
72
|
+
/** The widest `aside` may be, in pixels — and never more than 60% of the window. */
|
|
73
|
+
max?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Said to a screen reader, and shown on hover.
|
|
76
|
+
*
|
|
77
|
+
* Required: "separator" is what the role already announces, and a
|
|
78
|
+
* page with two of them needs to say which is which.
|
|
79
|
+
*/
|
|
80
|
+
label: string;
|
|
81
|
+
/**
|
|
82
|
+
* The width changed. Called on release, on each keypress and on a
|
|
83
|
+
* double-click back to `defaultWidth`, not on every pointer move.
|
|
84
|
+
*
|
|
85
|
+
* A handler that fires sixty times a second is a handler somebody
|
|
86
|
+
* writes a round trip into.
|
|
87
|
+
*/
|
|
88
|
+
onResize?: (width: number) => void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Two panes and a rule you can move.
|
|
93
|
+
*
|
|
94
|
+
* It drives a CSS variable rather than React state, and it does not
|
|
95
|
+
* remember the width: `onResize` hands the width back, and an
|
|
96
|
+
* extension that wants it kept stores it somewhere that survives a
|
|
97
|
+
* restart (`bridge.setLocation`, or its own backend).
|
|
98
|
+
*
|
|
99
|
+
* @param aside — the fixed-width pane.
|
|
100
|
+
* @param children — the pane that takes what is left.
|
|
101
|
+
* @param side — which side `aside` is on; the left by default.
|
|
102
|
+
* @param defaultWidth — the starting width of `aside`, in pixels.
|
|
103
|
+
* @param min — the narrowest `aside` may be, in pixels.
|
|
104
|
+
* @param max — the widest `aside` may be, in pixels, and never more
|
|
105
|
+
* than 60% of the window.
|
|
106
|
+
* @param label — said to a screen reader, and shown on hover.
|
|
107
|
+
* @param onResize — the width changed; called on release, on each
|
|
108
|
+
* keypress and on a double-click back to `defaultWidth`, not on every
|
|
109
|
+
* pointer move.
|
|
110
|
+
* @param className — added to the element's classes, beside the one the
|
|
111
|
+
* theme gives it.
|
|
112
|
+
* @param rest — the remaining props, spread onto the element.
|
|
113
|
+
*/
|
|
114
|
+
export function Split({
|
|
115
|
+
aside,
|
|
116
|
+
children,
|
|
117
|
+
side = "start",
|
|
118
|
+
defaultWidth = 280,
|
|
119
|
+
min = 160,
|
|
120
|
+
max = 640,
|
|
121
|
+
label,
|
|
122
|
+
onResize,
|
|
123
|
+
className,
|
|
124
|
+
...rest
|
|
125
|
+
}: SplitProps) {
|
|
126
|
+
// Scoped to this instance, so two splits on one page do not fight
|
|
127
|
+
// over one custom property.
|
|
128
|
+
const variable = `--ui-split-${useId().replace(/[^a-zA-Z0-9]/g, "")}`;
|
|
129
|
+
const root = useRef<HTMLDivElement | null>(null);
|
|
130
|
+
// The aside itself, not "the first child": with `side="end"` the
|
|
131
|
+
// first child is the main pane.
|
|
132
|
+
const asidePane = useRef<HTMLDivElement | null>(null);
|
|
133
|
+
const drag = useRef<{ startX: number; startWidth: number } | null>(null);
|
|
134
|
+
// Mirrored into state only so `aria-valuenow` is right. The layout
|
|
135
|
+
// itself never reads this.
|
|
136
|
+
const [announced, setAnnounced] = useState(defaultWidth);
|
|
137
|
+
|
|
138
|
+
const clamp = useCallback(
|
|
139
|
+
(px: number) => {
|
|
140
|
+
const ceiling = Math.min(
|
|
141
|
+
max,
|
|
142
|
+
Math.round(
|
|
143
|
+
(typeof window === "undefined" ? max : window.innerWidth) *
|
|
144
|
+
MOST_OF_WINDOW,
|
|
145
|
+
),
|
|
146
|
+
);
|
|
147
|
+
// `min` wins over the ceiling on a very narrow window: a pane
|
|
148
|
+
// below its minimum is unreadable, and unreadable is worse
|
|
149
|
+
// than wide.
|
|
150
|
+
return Math.round(Math.max(min, Math.min(px, Math.max(min, ceiling))));
|
|
151
|
+
},
|
|
152
|
+
[min, max],
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const apply = useCallback(
|
|
156
|
+
(px: number) => {
|
|
157
|
+
const width = clamp(px);
|
|
158
|
+
root.current?.style.setProperty(variable, `${width}px`);
|
|
159
|
+
setAnnounced(width);
|
|
160
|
+
return width;
|
|
161
|
+
},
|
|
162
|
+
[clamp, variable],
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const currentWidth = () =>
|
|
166
|
+
asidePane.current?.getBoundingClientRect().width ?? defaultWidth;
|
|
167
|
+
|
|
168
|
+
const onPointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
171
|
+
drag.current = { startX: event.clientX, startWidth: currentWidth() };
|
|
172
|
+
// On the root rather than the strip, so it survives the pointer
|
|
173
|
+
// leaving — which it does on any drag faster than the reflow.
|
|
174
|
+
root.current?.classList.add("ui-split-resizing");
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const onPointerMove = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
178
|
+
const from = drag.current;
|
|
179
|
+
if (from === null) return;
|
|
180
|
+
const travelled = event.clientX - from.startX;
|
|
181
|
+
apply(from.startWidth + (side === "start" ? travelled : -travelled));
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const finish = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
185
|
+
if (drag.current === null) return;
|
|
186
|
+
drag.current = null;
|
|
187
|
+
root.current?.classList.remove("ui-split-resizing");
|
|
188
|
+
event.currentTarget.releasePointerCapture?.(event.pointerId);
|
|
189
|
+
onResize?.(clamp(currentWidth()));
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const onKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {
|
|
193
|
+
// A 5px strip is a hard target with a trackpad and an impossible
|
|
194
|
+
// one without a pointer, and this is the only way to move a pane
|
|
195
|
+
// that is otherwise fixed.
|
|
196
|
+
const step = event.shiftKey ? BIG_STEP : STEP;
|
|
197
|
+
const wider = side === "start" ? "ArrowRight" : "ArrowLeft";
|
|
198
|
+
const narrower = side === "start" ? "ArrowLeft" : "ArrowRight";
|
|
199
|
+
let next: number | null = null;
|
|
200
|
+
if (event.key === wider) next = currentWidth() + step;
|
|
201
|
+
else if (event.key === narrower) next = currentWidth() - step;
|
|
202
|
+
else if (event.key === "Home") next = min;
|
|
203
|
+
else if (event.key === "End") next = max;
|
|
204
|
+
if (next === null) return;
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
// Applied first: `onResize?.(apply(next))` skips the argument
|
|
207
|
+
// when there is no `onResize`, and the pane never moved.
|
|
208
|
+
const width = apply(next);
|
|
209
|
+
onResize?.(width);
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const handle = (
|
|
213
|
+
<div
|
|
214
|
+
key="handle"
|
|
215
|
+
className="ui-split-handle"
|
|
216
|
+
role="separator"
|
|
217
|
+
aria-orientation="vertical"
|
|
218
|
+
aria-label={label}
|
|
219
|
+
title={label}
|
|
220
|
+
aria-valuenow={announced}
|
|
221
|
+
aria-valuemin={min}
|
|
222
|
+
aria-valuemax={max}
|
|
223
|
+
tabIndex={0}
|
|
224
|
+
onPointerDown={onPointerDown}
|
|
225
|
+
onPointerMove={onPointerMove}
|
|
226
|
+
onPointerUp={finish}
|
|
227
|
+
onPointerCancel={finish}
|
|
228
|
+
onKeyDown={onKeyDown}
|
|
229
|
+
// Back to where it started. A pane dragged somewhere silly is
|
|
230
|
+
// otherwise a slow drag back to a width nobody remembers.
|
|
231
|
+
onDoubleClick={() => {
|
|
232
|
+
const width = apply(defaultWidth);
|
|
233
|
+
onResize?.(width);
|
|
234
|
+
}}
|
|
235
|
+
/>
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const asideElement = (
|
|
239
|
+
<div key="aside" ref={asidePane} className="ui-split-aside">
|
|
240
|
+
{aside}
|
|
241
|
+
</div>
|
|
242
|
+
);
|
|
243
|
+
const mainElement = (
|
|
244
|
+
<div key="main" className="ui-split-main">
|
|
245
|
+
{children}
|
|
246
|
+
</div>
|
|
247
|
+
);
|
|
248
|
+
// Keyed, so flipping `side` reorders the panes instead of remounting them.
|
|
249
|
+
const panes =
|
|
250
|
+
side === "start"
|
|
251
|
+
? [asideElement, handle, mainElement]
|
|
252
|
+
: [mainElement, handle, asideElement];
|
|
253
|
+
|
|
254
|
+
return (
|
|
255
|
+
<div
|
|
256
|
+
{...rest}
|
|
257
|
+
ref={root}
|
|
258
|
+
className={cx("ui-split", side === "end" && "ui-split-end", className)}
|
|
259
|
+
style={
|
|
260
|
+
{ [variable]: `${defaultWidth}px`, ...rest.style } as CSSProperties
|
|
261
|
+
}
|
|
262
|
+
data-split-var={variable}
|
|
263
|
+
>
|
|
264
|
+
{panes}
|
|
265
|
+
</div>
|
|
266
|
+
);
|
|
267
|
+
}
|
package/src/Tabs.tsx
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tab strip and the panels it controls.
|
|
3
|
+
*
|
|
4
|
+
* Part of the primitives; see `primitives.tsx`. **Under the
|
|
5
|
+
* compatibility floor.**
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { HTMLAttributes, KeyboardEvent, ReactNode } from "react";
|
|
9
|
+
import { useEffect, useId, useRef } from "react";
|
|
10
|
+
|
|
11
|
+
import { cx } from "./cx.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The index a navigation key moves the selection to, from tab `i` of
|
|
15
|
+
* `count`, or `null` for a key the strip does not handle. The arrows
|
|
16
|
+
* wrap at either end.
|
|
17
|
+
*/
|
|
18
|
+
function tabAfterKey(key: string, i: number, count: number): number | null {
|
|
19
|
+
switch (key) {
|
|
20
|
+
case "ArrowRight":
|
|
21
|
+
return (i + 1) % count;
|
|
22
|
+
case "ArrowLeft":
|
|
23
|
+
return (i - 1 + count) % count;
|
|
24
|
+
case "Home":
|
|
25
|
+
return 0;
|
|
26
|
+
case "End":
|
|
27
|
+
return count - 1;
|
|
28
|
+
default:
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What a {@link Tabs} takes. */
|
|
34
|
+
export interface TabsProps extends Omit<
|
|
35
|
+
HTMLAttributes<HTMLDivElement>,
|
|
36
|
+
"onChange"
|
|
37
|
+
> {
|
|
38
|
+
/** The tabs. The `id` is what {@link TabPanel}s match on. */
|
|
39
|
+
tabs: { id: string; label: ReactNode }[];
|
|
40
|
+
/** Which tab is showing. */
|
|
41
|
+
active: string;
|
|
42
|
+
/** Called with the id of the tab the reader chose. */
|
|
43
|
+
onChange: (id: string) => void;
|
|
44
|
+
/** The strip's name, for a screen reader. */
|
|
45
|
+
label: string;
|
|
46
|
+
/**
|
|
47
|
+
* The id prefix shared with this strip's {@link TabPanel}s.
|
|
48
|
+
*
|
|
49
|
+
* Optional, and generated when it is absent — but a `TabPanel`
|
|
50
|
+
* *requires* one, so a caller that renders panels passes the same
|
|
51
|
+
* `base` to both: a generated one is never visible to the panels.
|
|
52
|
+
*/
|
|
53
|
+
base?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Tabs with the arrow keys wired, because a tab strip that only
|
|
58
|
+
* responds to clicks is a tab strip half the people cannot use.
|
|
59
|
+
*
|
|
60
|
+
* @param tabs — the tabs; the `id` is what the panels match on.
|
|
61
|
+
* @param active — which tab is showing.
|
|
62
|
+
* @param onChange — called with the id of the tab the reader chose.
|
|
63
|
+
* @param label — the strip's name, for a screen reader.
|
|
64
|
+
* @param base — the id prefix shared with this strip's panels; one is
|
|
65
|
+
* generated when it is left out, and the panels need the same one.
|
|
66
|
+
* @param className — added to the element's classes, beside the one the
|
|
67
|
+
* theme gives it.
|
|
68
|
+
* @param rest — the remaining props, spread onto the element.
|
|
69
|
+
*/
|
|
70
|
+
export function Tabs({
|
|
71
|
+
tabs,
|
|
72
|
+
active,
|
|
73
|
+
onChange,
|
|
74
|
+
label,
|
|
75
|
+
base: givenBase,
|
|
76
|
+
className,
|
|
77
|
+
...rest
|
|
78
|
+
}: TabsProps) {
|
|
79
|
+
// Hooks run unconditionally; the generated id is simply unused
|
|
80
|
+
// when a caller supplied one.
|
|
81
|
+
const generated = useId();
|
|
82
|
+
const base = givenBase ?? generated;
|
|
83
|
+
const strip = useRef<HTMLDivElement | null>(null);
|
|
84
|
+
|
|
85
|
+
// Focus follows selection, as the ARIA practices require: otherwise
|
|
86
|
+
// the focus ring sits on a tab that is now tabIndex={-1} while a
|
|
87
|
+
// different one is selected, and the next Tab leaves the strip from
|
|
88
|
+
// somewhere the reader cannot see.
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
const el = strip.current?.querySelector<HTMLElement>(
|
|
91
|
+
'[aria-selected="true"]',
|
|
92
|
+
);
|
|
93
|
+
if (el && strip.current?.contains(document.activeElement)) el.focus();
|
|
94
|
+
}, [active]);
|
|
95
|
+
|
|
96
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
97
|
+
if (tabs.length === 0) return;
|
|
98
|
+
const i = Math.max(
|
|
99
|
+
tabs.findIndex((t) => t.id === active),
|
|
100
|
+
0,
|
|
101
|
+
);
|
|
102
|
+
const next = tabAfterKey(event.key, i, tabs.length);
|
|
103
|
+
if (next === null) return;
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
onChange(tabs[next].id);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<div
|
|
110
|
+
{...rest}
|
|
111
|
+
ref={strip}
|
|
112
|
+
role="tablist"
|
|
113
|
+
aria-label={label}
|
|
114
|
+
aria-orientation="horizontal"
|
|
115
|
+
className={cx("ui-tabs", className)}
|
|
116
|
+
onKeyDown={onKeyDown}
|
|
117
|
+
>
|
|
118
|
+
{tabs.map((tab) => (
|
|
119
|
+
<button
|
|
120
|
+
key={tab.id}
|
|
121
|
+
// Real DOM ids, so a panel can point back with
|
|
122
|
+
// aria-labelledby and this one can point at it. Without
|
|
123
|
+
// them a screen reader announces "tab, 1 of 2" controlling
|
|
124
|
+
// nothing.
|
|
125
|
+
id={`${base}-${tab.id}`}
|
|
126
|
+
aria-controls={`${base}-${tab.id}-panel`}
|
|
127
|
+
type="button"
|
|
128
|
+
role="tab"
|
|
129
|
+
aria-selected={tab.id === active}
|
|
130
|
+
tabIndex={tab.id === active ? 0 : -1}
|
|
131
|
+
className="ui-tab"
|
|
132
|
+
onClick={() => onChange(tab.id)}
|
|
133
|
+
>
|
|
134
|
+
{tab.label}
|
|
135
|
+
</button>
|
|
136
|
+
))}
|
|
137
|
+
</div>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The panel a tab controls. Needs the same `base` the strip used.
|
|
143
|
+
*
|
|
144
|
+
* `Tabs` without it is half the ARIA pattern: a strip that announces
|
|
145
|
+
* it controls something needs something for it to control.
|
|
146
|
+
*/
|
|
147
|
+
export interface TabPanelProps extends HTMLAttributes<HTMLDivElement> {
|
|
148
|
+
/** The id prefix shared with its {@link Tabs}, so the two agree. */
|
|
149
|
+
base: string;
|
|
150
|
+
/** Which tab this panel belongs to. */
|
|
151
|
+
id: string;
|
|
152
|
+
/** Which tab is showing. The panel renders only when they match. */
|
|
153
|
+
active: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The panel a tab controls.
|
|
158
|
+
*
|
|
159
|
+
* See {@link TabPanelProps}. It renders nothing at all when its tab is
|
|
160
|
+
* not the active one, so the content of a panel that is not showing
|
|
161
|
+
* is not in the page.
|
|
162
|
+
*
|
|
163
|
+
* @param base — the id prefix shared with its tabs, so the two agree.
|
|
164
|
+
* @param id — which tab this panel belongs to.
|
|
165
|
+
* @param active — which tab is showing; the panel renders only when
|
|
166
|
+
* they match.
|
|
167
|
+
* @param children — the content of the panel, shown while its tab is
|
|
168
|
+
* the active one.
|
|
169
|
+
* @param className — the panel's own class; the theme gives it
|
|
170
|
+
* none.
|
|
171
|
+
* @param rest — the remaining props, spread onto the element.
|
|
172
|
+
*/
|
|
173
|
+
export function TabPanel({
|
|
174
|
+
base,
|
|
175
|
+
id,
|
|
176
|
+
active,
|
|
177
|
+
children,
|
|
178
|
+
className,
|
|
179
|
+
...rest
|
|
180
|
+
}: TabPanelProps) {
|
|
181
|
+
if (id !== active) return null;
|
|
182
|
+
return (
|
|
183
|
+
<div
|
|
184
|
+
{...rest}
|
|
185
|
+
id={`${base}-${id}-panel`}
|
|
186
|
+
role="tabpanel"
|
|
187
|
+
aria-labelledby={`${base}-${id}`}
|
|
188
|
+
tabIndex={0}
|
|
189
|
+
className={className}
|
|
190
|
+
>
|
|
191
|
+
{children}
|
|
192
|
+
</div>
|
|
193
|
+
);
|
|
194
|
+
}
|