Files
NODEDC_DESIGN_GUIDELINE/packages/ui-react/src/LoadingRegion.tsx
T
Codex cb534dbe39 feat(ui): add docking, viewport controls and composed settings
Add shared dock layouts, navigation cube, range bands, progress actions and comparison surfaces. Extend toolbar menus and loading composition, and align tokens, registry entries, documentation and the living catalog.
2026-10-29 09:38:25 +03:00

39 lines
1.7 KiB
TypeScript

import { useCallback, useContext, useId, useLayoutEffect, useState, type HTMLAttributes } from "react";
import { ActivityIndicator } from "./ActivityIndicator.js";
import { LoadingContext } from "./LoadingContext.js";
import { cn } from "./cn.js";
export interface LoadingRegionProps extends HTMLAttributes<HTMLDivElement> {
loading: boolean;
label: string;
/** Collect nested initial-content loads into this surface's single indicator. */
aggregate?: boolean;
}
/** Keeps content mounted. Failures/unmounts release each nested pending lease. */
export function LoadingRegion({ loading, label, aggregate = false, children, className, ...props }: LoadingRegionProps) {
const parent = useContext(LoadingContext);
const id = useId();
const [pending, setPending] = useState<ReadonlySet<string>>(() => new Set());
const report = useCallback((key: string, busy: boolean) => {
setPending(previous => {
if (previous.has(key) === busy) return previous;
const next = new Set(previous);
if (busy) next.add(key); else next.delete(key);
return next;
});
}, []);
useLayoutEffect(() => {
parent?.(id, loading);
return () => parent?.(id, false);
}, [parent, id, loading]);
const busy = loading || (aggregate && !parent && pending.size > 0);
const content = <div {...props} className={cn("nodedc-loading-region", className)} aria-busy={busy}>
{children}
{busy && !parent ? <div className="nodedc-loading-region__status" role="status" aria-label={label}>
<ActivityIndicator /><span aria-hidden="true">{label}</span>
</div> : null}
</div>;
return aggregate && !parent ? <LoadingContext.Provider value={report}>{content}</LoadingContext.Provider> : content;
}