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.
This commit is contained in:
Codex
2026-10-29 09:38:25 +03:00
parent a6e6c8f2d9
commit cb534dbe39
51 changed files with 2273 additions and 336 deletions
+51 -17
View File
@@ -1,4 +1,7 @@
import {
createContext,
useContext,
useMemo,
useCallback,
useEffect,
useId,
@@ -9,9 +12,12 @@ import {
type ReactNode,
} from "react";
import { createPortal } from "react-dom";
import { computeFloatingPosition, type FloatingPlacement } from "@nodedc/ui-core";
import { computeFloatingPosition, type FloatingPlacement, type RectLike } from "@nodedc/ui-core";
import { cn } from "./cn.js";
const DropdownLineage = createContext<readonly string[]>([]);
const openDropdowns = new Map<string, readonly string[]>();
export interface DropdownTriggerApi {
open: boolean;
show: () => void;
@@ -34,6 +40,11 @@ export interface DropdownProps {
surfaceClassName?: string;
surfaceRole?: "menu" | "listbox" | "dialog";
onOpenChange?: (open: boolean) => void;
/** Animated controls may reserve their maximum extent as a stable anchor. */
getAnchorRect?: () => RectLike | null;
flip?: boolean;
/** Capture-phase dismissal may consume a scene gesture before its tools run. */
onPointerDownOutside?: (event: PointerEvent) => void;
}
export function Dropdown({
@@ -48,8 +59,13 @@ export function Dropdown({
surfaceClassName,
surfaceRole = "menu",
onOpenChange,
getAnchorRect,
flip = true,
onPointerDownOutside,
}: DropdownProps) {
const instanceId = useId();
const ancestors = useContext(DropdownLineage);
const lineage = useMemo(() => [...ancestors, instanceId], [ancestors, instanceId]);
const surfaceId = `${instanceId.replaceAll(":", "")}-surface`;
const [isOpen, setIsOpen] = useState(false);
const [anchorElement, setAnchorElement] = useState<HTMLElement | null>(null);
@@ -72,7 +88,7 @@ export function Dropdown({
const updatePosition = useCallback(() => {
const anchorNode = anchorElement ?? triggerElement;
if (!anchorNode || !surfaceRef.current) return;
const anchor = anchorNode.getBoundingClientRect();
const anchor = getAnchorRect?.() ?? anchorNode.getBoundingClientRect();
const anchorVisible = (
anchor.bottom > 0
&& anchor.top < window.innerHeight
@@ -93,58 +109,73 @@ export function Dropdown({
surfaceWidth,
surfaceHeight,
placement,
offset,
offset, flip,
});
setSurfaceStyle({
const next:CSSProperties = {
top: position.top,
left: position.left,
width: surfaceWidth,
width: Math.min(surfaceWidth, position.maxWidth ?? surfaceWidth),
maxHeight: position.maxHeight,
visibility: "visible",
});
}, [anchorElement, close, minWidth, offset, placement, triggerElement, width]);
};
setSurfaceStyle(old => Object.keys(next).every(key => old[key as keyof CSSProperties] === next[key as keyof CSSProperties]) ? old : next);
}, [anchorElement, close, minWidth, offset, placement, triggerElement, width, getAnchorRect, flip]);
useLayoutEffect(() => {
if (!isOpen) return;
updatePosition();
}, [isOpen, updatePosition]);
useEffect(() => {
if (!isOpen || !getAnchorRect) return;
let frame:number;
const tick=()=>{updatePosition();frame=requestAnimationFrame(tick);};
frame=requestAnimationFrame(tick);
return()=>cancelAnimationFrame(frame);
},[isOpen,getAnchorRect,updatePosition]);
useEffect(() => {
if (!isOpen) return;
const handlePointerDown = (event: PointerEvent) => {
const target = event.target;
if (!(target instanceof Node)) return;
if (anchorElement?.contains(target) || triggerElement?.contains(target) || surfaceRef.current?.contains(target)) return;
const surface = (target instanceof Element ? target : target.parentElement)?.closest('[data-dropdown-lineage]');
if (surface?.getAttribute('data-dropdown-lineage')?.split(' ').includes(instanceId)) return;
close();
onPointerDownOutside?.(event);
};
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
if (event.key === "Escape" && !event.defaultPrevented) {
if ([...openDropdowns].some(([id, parents]) => id !== instanceId && parents.includes(instanceId))) return;
close();
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
triggerElement?.focus();
}
};
const handleOtherOpen = (event: Event) => {
const detail = (event as CustomEvent<{ id?: string }>).detail;
if (detail.id !== instanceId) close();
const detail = (event as CustomEvent<{ id?: string; lineage?: readonly string[] }>).detail;
if (detail.id !== instanceId && !detail.lineage?.includes(instanceId)) close();
};
const handleViewportChange = () => updatePosition();
window.dispatchEvent(new CustomEvent("nodedc-dropdown-open", { detail: { id: instanceId } }));
document.addEventListener("pointerdown", handlePointerDown);
openDropdowns.set(instanceId, lineage);
window.dispatchEvent(new CustomEvent("nodedc-dropdown-open", { detail: { id: instanceId, lineage } }));
document.addEventListener("pointerdown", handlePointerDown, true);
document.addEventListener("keydown", handleKeyDown, true);
window.addEventListener("nodedc-dropdown-open", handleOtherOpen as EventListener);
window.addEventListener("resize", handleViewportChange);
window.addEventListener("scroll", handleViewportChange, true);
return () => {
document.removeEventListener("pointerdown", handlePointerDown);
openDropdowns.delete(instanceId);
document.removeEventListener("pointerdown", handlePointerDown, true);
document.removeEventListener("keydown", handleKeyDown, true);
window.removeEventListener("nodedc-dropdown-open", handleOtherOpen as EventListener);
window.removeEventListener("resize", handleViewportChange);
window.removeEventListener("scroll", handleViewportChange, true);
};
}, [anchorElement, close, instanceId, isOpen, triggerElement, updatePosition]);
}, [anchorElement, close, instanceId, isOpen, triggerElement, updatePosition, lineage, onPointerDownOutside]);
return (
<span className={cn("nodedc-dropdown-anchor", className)}>
@@ -154,13 +185,16 @@ export function Dropdown({
<div
ref={surfaceRef}
id={surfaceId}
data-dropdown-lineage={lineage.join(' ')}
className={cn("nodedc-dropdown-surface", surfaceClassName)}
role={surfaceRole}
style={surfaceStyle}
>
{typeof children === "function" ? children({ close }) : children}
<DropdownLineage.Provider value={lineage}>
{typeof children === "function" ? children({ close }) : children}
</DropdownLineage.Provider>
</div>,
document.body,
document.fullscreenElement ?? document.body,
)
: null}
</span>