@jcsoftdev/ui 0.6.0 → 0.7.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/CHANGELOG.md +24 -0
- package/README.md +6 -2
- package/dist/atoms/avatar/avatar.d.ts +6 -1
- package/dist/atoms/avatar/avatar.js +3 -2
- package/dist/containers/kanban/kanban.js +2 -0
- package/dist/containers/list-detail/list-detail.js +2 -0
- package/dist/containers/responsive-list/responsive-list.d.ts +1 -1
- package/dist/containers/responsive-list/responsive-list.js +127 -116
- package/dist/forms/number-stepper/index.d.ts +1 -1
- package/dist/forms/number-stepper/number-stepper.d.ts +7 -0
- package/dist/forms/number-stepper/number-stepper.js +37 -6
- package/dist/i18n/strings.d.ts +8 -0
- package/dist/i18n/strings.js +4 -0
- package/dist/overlays/dialog/dialog.d.ts +1 -0
- package/dist/overlays/dialog/dialog.js +1 -1
- package/dist/overlays/toaster/toast-store.d.ts +2 -0
- package/dist/overlays/toaster/toast-store.js +6 -1
- package/dist/overlays/toaster/toaster.js +13 -0
- package/dist/states/skeleton/skeleton.d.ts +16 -1
- package/dist/states/skeleton/skeleton.js +12 -4
- package/dist/utils/focus-chain.d.ts +11 -0
- package/dist/utils/focus-chain.js +39 -0
- package/dist/utils/focus-rescue.d.ts +13 -0
- package/dist/utils/focus-rescue.js +48 -0
- package/package.json +2 -2
- package/src/atoms/avatar/avatar.tsx +8 -2
- package/src/containers/kanban/kanban.tsx +2 -0
- package/src/containers/list-detail/list-detail.tsx +2 -0
- package/src/containers/responsive-list/responsive-list.tsx +8 -2
- package/src/forms/number-stepper/index.ts +1 -0
- package/src/forms/number-stepper/number-stepper.tsx +51 -6
- package/src/i18n/strings.ts +8 -0
- package/src/overlays/dialog/dialog.tsx +2 -1
- package/src/overlays/toaster/toast-store.ts +8 -0
- package/src/overlays/toaster/toaster.tsx +14 -0
- package/src/states/skeleton/skeleton.tsx +32 -4
- package/src/utils/focus-chain.ts +53 -0
- package/src/utils/focus-rescue.ts +68 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import type
|
|
3
|
+
import { createContext, type ReactNode, useContext } from "react";
|
|
4
4
|
import { useUiStrings } from "../../i18n/index.js";
|
|
5
5
|
import { cn } from "../../utils/cn.js";
|
|
6
6
|
|
|
@@ -8,6 +8,25 @@ export type SkeletonVariant = "lines" | "table" | "card" | "list" | "kpis" | "ke
|
|
|
8
8
|
|
|
9
9
|
const PLACEHOLDER_IDS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"] as const;
|
|
10
10
|
|
|
11
|
+
const SkeletonGroupContext = createContext(false);
|
|
12
|
+
|
|
13
|
+
export interface SkeletonGroupProps {
|
|
14
|
+
/** Text announced once for the whole region. Defaults to the localized "Loading". */
|
|
15
|
+
label?: string;
|
|
16
|
+
className?: string;
|
|
17
|
+
children: ReactNode;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** One status announcement for every `Skeleton` inside; they render silent. */
|
|
21
|
+
export function SkeletonGroup({ label, className, children }: SkeletonGroupProps) {
|
|
22
|
+
const strings = useUiStrings();
|
|
23
|
+
return (
|
|
24
|
+
<div role="status" aria-busy="true" aria-label={label ?? strings.loading} className={className}>
|
|
25
|
+
<SkeletonGroupContext.Provider value={true}>{children}</SkeletonGroupContext.Provider>
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
11
30
|
export interface SkeletonProps {
|
|
12
31
|
variant?: SkeletonVariant;
|
|
13
32
|
/** Number of lines for the `lines` variant. */
|
|
@@ -18,6 +37,12 @@ export interface SkeletonProps {
|
|
|
18
37
|
count?: number;
|
|
19
38
|
/** Text announced to screen readers while loading. */
|
|
20
39
|
label?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Whether this skeleton is its own status region. Defaults to `true`, except inside a
|
|
42
|
+
* `SkeletonGroup`, which announces once. Set `false` on repeated skeletons so a screen reader
|
|
43
|
+
* does not say "Loading" for each; wrap them in a `SkeletonGroup` or a status of your own.
|
|
44
|
+
*/
|
|
45
|
+
announce?: boolean;
|
|
21
46
|
/**
|
|
22
47
|
* Classes on the root. The `bar` variant is one block sized by them (`h-8 w-32`), 12px tall and
|
|
23
48
|
* full width otherwise.
|
|
@@ -42,14 +67,17 @@ export function Skeleton({
|
|
|
42
67
|
rows = 4,
|
|
43
68
|
count = 4,
|
|
44
69
|
label,
|
|
70
|
+
announce,
|
|
45
71
|
className,
|
|
46
72
|
}: SkeletonProps) {
|
|
47
73
|
const strings = useUiStrings();
|
|
74
|
+
const grouped = useContext(SkeletonGroupContext);
|
|
75
|
+
const silent = !(announce ?? !grouped);
|
|
48
76
|
return (
|
|
49
77
|
<div
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
78
|
+
{...(silent
|
|
79
|
+
? { "aria-hidden": true }
|
|
80
|
+
: { role: "status", "aria-busy": true, "aria-label": label ?? strings.loading })}
|
|
53
81
|
className={cn(
|
|
54
82
|
"animate-pulse",
|
|
55
83
|
variant === "bar" && "block h-3 w-full rounded-xs bg-surface-2",
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const FOCUSABLE =
|
|
2
|
+
'button:not([disabled]), a[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
3
|
+
|
|
4
|
+
const CONTAINER_TAGS = new Set(["UL", "OL", "TBODY", "TABLE", "MAIN", "SECTION", "ARTICLE"]);
|
|
5
|
+
|
|
6
|
+
export function isVisible(element: Element): boolean {
|
|
7
|
+
return element.getClientRects().length > 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function focusLosing(): boolean {
|
|
11
|
+
const active = document.activeElement;
|
|
12
|
+
return active === null || active === document.body || active === document.documentElement;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Focuses the element, or a control inside it; returns whether focus moved there. */
|
|
16
|
+
export function focusWithin(element: HTMLElement): boolean {
|
|
17
|
+
const target = element.hasAttribute("tabindex")
|
|
18
|
+
? element
|
|
19
|
+
: (element.querySelector<HTMLElement>(FOCUSABLE) ?? element);
|
|
20
|
+
if (!target.hasAttribute("tabindex") && !target.matches(FOCUSABLE)) {
|
|
21
|
+
target.setAttribute("tabindex", "-1");
|
|
22
|
+
}
|
|
23
|
+
target.focus({ preventScroll: false });
|
|
24
|
+
return document.activeElement === target;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The element and its ancestors up to the body, nearest first. */
|
|
28
|
+
export function focusChain(element: Element | null): HTMLElement[] {
|
|
29
|
+
const chain: HTMLElement[] = [];
|
|
30
|
+
for (let node = element; node && node !== document.body; node = node.parentElement) {
|
|
31
|
+
if (node instanceof HTMLElement) chain.push(node);
|
|
32
|
+
}
|
|
33
|
+
return chain;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Focuses the first element of a chain captured with `focusChain` that still lives in the
|
|
38
|
+
* document: the element itself, otherwise the nearest surviving interactive or list-like ancestor.
|
|
39
|
+
*/
|
|
40
|
+
export function restoreFocusFromChain(chain: readonly HTMLElement[]): boolean {
|
|
41
|
+
for (const [index, element] of chain.entries()) {
|
|
42
|
+
if (!element.isConnected || !isVisible(element)) continue;
|
|
43
|
+
const usable =
|
|
44
|
+
index === 0 ||
|
|
45
|
+
element.hasAttribute("tabindex") ||
|
|
46
|
+
element.matches(FOCUSABLE) ||
|
|
47
|
+
CONTAINER_TAGS.has(element.tagName) ||
|
|
48
|
+
element.getAttribute("role") === "listbox" ||
|
|
49
|
+
element.getAttribute("role") === "list";
|
|
50
|
+
if (usable && focusWithin(element)) return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type RefObject, useEffect } from "react";
|
|
2
|
+
import { focusLosing, focusWithin, isVisible } from "./focus-chain.js";
|
|
3
|
+
|
|
4
|
+
export interface FocusRescueOptions {
|
|
5
|
+
/** Selector of the items inside the root. */
|
|
6
|
+
item: string;
|
|
7
|
+
/** Selector of the nearest ancestor that groups sibling items; the next item is looked up inside it. */
|
|
8
|
+
group: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* When the focused item leaves the DOM (approved, started, moved away) the browser drops focus on
|
|
13
|
+
* the body. This hands it to the item that took its place, the last one when it was the tail, or
|
|
14
|
+
* the group when nothing is left. Focus that moved on purpose is never touched.
|
|
15
|
+
*/
|
|
16
|
+
export function useFocusRescue(
|
|
17
|
+
rootRef: RefObject<HTMLElement | null>,
|
|
18
|
+
{ item, group }: FocusRescueOptions,
|
|
19
|
+
) {
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const root = rootRef.current;
|
|
22
|
+
if (!root) return;
|
|
23
|
+
let tracked: { element: HTMLElement; group: HTMLElement; index: number } | null = null;
|
|
24
|
+
|
|
25
|
+
const itemsOf = (scope: HTMLElement) =>
|
|
26
|
+
Array.from(scope.querySelectorAll<HTMLElement>(item)).filter(
|
|
27
|
+
(candidate) => candidate.closest(group) === scope && isVisible(candidate),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const track = (element: HTMLElement | null) => {
|
|
31
|
+
const found = element?.closest<HTMLElement>(item);
|
|
32
|
+
const scope = found?.closest<HTMLElement>(group);
|
|
33
|
+
if (!found || !scope) return;
|
|
34
|
+
tracked = { element: found, group: scope, index: itemsOf(scope).indexOf(found) };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const onFocusIn = (event: FocusEvent) =>
|
|
38
|
+
track(event.target instanceof HTMLElement ? event.target : null);
|
|
39
|
+
|
|
40
|
+
const rescue = () => {
|
|
41
|
+
if (!tracked) return;
|
|
42
|
+
if (tracked.element.isConnected) {
|
|
43
|
+
track(tracked.element);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const lost = tracked;
|
|
47
|
+
if (!focusLosing()) {
|
|
48
|
+
tracked = null;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
tracked = null;
|
|
52
|
+
const scope = lost.group.isConnected ? lost.group : null;
|
|
53
|
+
if (!scope) return;
|
|
54
|
+
const siblings = itemsOf(scope);
|
|
55
|
+
const next = siblings[Math.min(Math.max(lost.index, 0), siblings.length - 1)];
|
|
56
|
+
if (next) focusWithin(next);
|
|
57
|
+
else focusWithin(scope);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
root.addEventListener("focusin", onFocusIn);
|
|
61
|
+
const observer = new MutationObserver(rescue);
|
|
62
|
+
observer.observe(root, { childList: true, subtree: true });
|
|
63
|
+
return () => {
|
|
64
|
+
root.removeEventListener("focusin", onFocusIn);
|
|
65
|
+
observer.disconnect();
|
|
66
|
+
};
|
|
67
|
+
}, [rootRef, item, group]);
|
|
68
|
+
}
|