fix(ui): adapt window headers and restore media expansion geometry
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nodedc/ui-react",
|
||||
"version": "0.7.8",
|
||||
"version": "0.7.9",
|
||||
"type": "module",
|
||||
"files": [
|
||||
"dist"
|
||||
@@ -21,7 +21,7 @@
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@nodedc/ui-core": "0.7.3",
|
||||
"@nodedc/ui-core": "0.7.4",
|
||||
"lucide-react": "^0.468.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -40,6 +40,10 @@ export interface WindowProps extends Omit<HTMLAttributes<HTMLDivElement>, "title
|
||||
footer?: ReactNode;
|
||||
/** Context actions beside close, using canonical IconButton controls. */
|
||||
headerActions?: ReactNode;
|
||||
/** Keep actions on one line; place title below them when space is limited. */
|
||||
headerLayout?: "inline" | "adaptive";
|
||||
/** Temporarily enlarge within the viewport; restoring retains prior geometry and presentation. */
|
||||
expanded?: boolean;
|
||||
/** Keep contextual actions immediately before close when requested. */
|
||||
headerActionsPlacement?: "before-move" | "before-close";
|
||||
size?: WindowSize;
|
||||
@@ -72,6 +76,8 @@ export function Window({
|
||||
children,
|
||||
footer,
|
||||
headerActions,
|
||||
headerLayout = "inline",
|
||||
expanded = false,
|
||||
headerActionsPlacement = "before-move",
|
||||
size = "md",
|
||||
placement = "center",
|
||||
@@ -99,6 +105,9 @@ export function Window({
|
||||
const titleId = useId();
|
||||
const descriptionId = useId();
|
||||
const dialogRef = useRef<HTMLDivElement>(null);
|
||||
const minimumRef = useRef({width:320,height:200});
|
||||
const [headerMinimum,setHeaderMinimum]=useState(320);
|
||||
const [stackedTitle,setStackedTitle]=useState(false);
|
||||
const portalHostRef = useRef<Element | null>(null);
|
||||
const onCloseRef = useRef(onClose);
|
||||
const dragRef = useRef<{ pointerId: number; offsetX: number; offsetY: number } | null>(null);
|
||||
@@ -107,13 +116,15 @@ export function Window({
|
||||
const [floatingRect, setFloatingRect] = useState<WindowRect | null>(null);
|
||||
const rectRef = useRef<WindowRect | null>(null);
|
||||
const initialRectRef = useRef<WindowRect | null>(null);
|
||||
const expansionRestoreRef = useRef<WindowRect | null>(null);
|
||||
const commitRef = useRef(onRectCommit);
|
||||
commitRef.current = onRectCommit;
|
||||
// Temporary media expansion never replaces the application-owned window layout.
|
||||
commitRef.current = expanded ? undefined : onRectCommit;
|
||||
const changeRect = (next: WindowRect | null) => { rectRef.current = next; setFloatingRect(next); };
|
||||
const gestureChanged = useRef(false);
|
||||
const resizeRef = useRef<{ pointerId: number; x: number; y: number; rect: WindowRect; edge: ResizeEdge } | null>(null);
|
||||
const isFloating = (controlledFloating ?? floating) && allowFloating && placement === "center";
|
||||
const freeGeometry = isFloating || (resizable && placement === "end");
|
||||
const freeGeometry = expanded || isFloating || (resizable && placement === "end");
|
||||
const canDrag = draggable || freeGeometry;
|
||||
const shouldLockBodyScroll = !suspended && !isFloating && (lockBodyScroll ?? placement === "center");
|
||||
const shouldTrapFocus = !suspended && !isFloating && (trapFocus ?? placement === "center");
|
||||
@@ -121,6 +132,31 @@ export function Window({
|
||||
modeRef.current = { shouldTrapFocus, isFloating, suspended };
|
||||
onCloseRef.current = onClose;
|
||||
|
||||
useLayoutEffect(()=>{
|
||||
if(!open || headerLayout!=="adaptive")return;
|
||||
const dialog=dialogRef.current,head=dialog?.querySelector<HTMLElement>(".nodedc-window__head"),actions=head?.querySelector<HTMLElement>(".nodedc-window__actions"),heading=head?.querySelector<HTMLElement>(".nodedc-window__title");
|
||||
if(!dialog||!head||!actions||!heading)return;
|
||||
const measure=()=>{
|
||||
const headStyle=window.getComputedStyle(head),input=heading.querySelector<HTMLInputElement>("input"),copyStyle=window.getComputedStyle(input??heading);
|
||||
const sample=document.createElement("span");
|
||||
Object.assign(sample.style,{position:"fixed",visibility:"hidden",pointerEvents:"none",whiteSpace:"pre",font:copyStyle.font,letterSpacing:copyStyle.letterSpacing});
|
||||
sample.textContent=input?.value??heading.textContent??"";head.appendChild(sample);
|
||||
const titleWidth=sample.getBoundingClientRect().width+(parseFloat(copyStyle.paddingLeft)||0)+(parseFloat(copyStyle.paddingRight)||0);sample.remove();
|
||||
const padding=(parseFloat(headStyle.paddingLeft)||0)+(parseFloat(headStyle.paddingRight)||0),actionWidth=actions.scrollWidth;
|
||||
const minimum=Math.max(320,Math.ceil(actionWidth+padding+2));
|
||||
minimumRef.current={width:minimum,height:200};setHeaderMinimum(minimum);
|
||||
setStackedTitle(head.clientWidth < padding+actionWidth+titleWidth+(parseFloat(headStyle.columnGap)||0)+2);
|
||||
if(freeGeometry&&rectRef.current){
|
||||
const fitted=fitWindow(rectRef.current,{width:window.innerWidth,height:window.innerHeight},minimumRef.current);
|
||||
if(fitted.left!==rectRef.current.left||fitted.top!==rectRef.current.top||fitted.width!==rectRef.current.width||fitted.height!==rectRef.current.height){changeRect(fitted);if(!resizeRef.current&&!dragRef.current)commitRef.current?.(fitted);}
|
||||
}
|
||||
};
|
||||
measure();
|
||||
const observer=typeof ResizeObserver!=="undefined"?new ResizeObserver(measure):null;
|
||||
observer?.observe(head);observer?.observe(actions);window.addEventListener("resize",measure);
|
||||
return()=>{observer?.disconnect();window.removeEventListener("resize",measure);};
|
||||
},[open,headerLayout,title,headerActions,freeGeometry]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || typeof document === "undefined") return;
|
||||
const previousActiveElement = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
@@ -183,20 +219,37 @@ export function Window({
|
||||
}, [open, shouldLockBodyScroll]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (open && freeGeometry && !dragRef.current && !resizeRef.current) {
|
||||
if (!open) return;
|
||||
const viewport={width:window.innerWidth,height:window.innerHeight};
|
||||
if(expanded){
|
||||
if(!expansionRestoreRef.current){
|
||||
const bounds=dialogRef.current?.getBoundingClientRect();
|
||||
if(bounds){
|
||||
expansionRestoreRef.current=rectRef.current??{left:bounds.left,top:bounds.top,width:bounds.width,height:bounds.height};
|
||||
changeRect(fitWindow({left:16,top:16,width:viewport.width-32,height:viewport.height-32},viewport,minimumRef.current));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(expansionRestoreRef.current){
|
||||
changeRect(fitWindow(expansionRestoreRef.current,viewport,minimumRef.current));
|
||||
expansionRestoreRef.current=null;
|
||||
return;
|
||||
}
|
||||
if (freeGeometry && !dragRef.current && !resizeRef.current) {
|
||||
// The modal move toggle already restores its local rectangle. An
|
||||
// uncontrolled floating window must retain it when toggled twice.
|
||||
if (placement === "center" && !onRectCommit && !rect && rectRef.current) return;
|
||||
const bounds=dialogRef.current?.getBoundingClientRect();
|
||||
if(bounds){
|
||||
initialRectRef.current ??= initialRect??{left:bounds.left,top:bounds.top,width:bounds.width,height:bounds.height};
|
||||
changeRect(fitWindow(rect??initialRectRef.current,{width:window.innerWidth,height:window.innerHeight}));
|
||||
changeRect(fitWindow(rect??initialRectRef.current,{width:window.innerWidth,height:window.innerHeight},minimumRef.current));
|
||||
}
|
||||
}
|
||||
},[open,freeGeometry,rect?.left,rect?.top,rect?.width,rect?.height]);
|
||||
},[open,expanded,freeGeometry,rect?.left,rect?.top,rect?.width,rect?.height]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) { setDragPosition(null); setFloating(false); changeRect(null); initialRectRef.current = null; dragRef.current = null; resizeRef.current = null; }
|
||||
if (!open) { setDragPosition(null); setFloating(false); changeRect(null); initialRectRef.current = null; expansionRestoreRef.current = null; dragRef.current = null; resizeRef.current = null; }
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -205,7 +258,7 @@ export function Window({
|
||||
const resizing = resizeRef.current;
|
||||
if (resizing && event.pointerId === resizing.pointerId) {
|
||||
gestureChanged.current = true;
|
||||
changeRect(resizeWindow(resizing.rect, resizing.edge, event.clientX - resizing.x, event.clientY - resizing.y, { width: window.innerWidth, height: window.innerHeight }));
|
||||
changeRect(resizeWindow(resizing.rect, resizing.edge, event.clientX - resizing.x, event.clientY - resizing.y, { width: window.innerWidth, height: window.innerHeight },minimumRef.current));
|
||||
return;
|
||||
}
|
||||
const drag = dragRef.current;
|
||||
@@ -214,7 +267,7 @@ export function Window({
|
||||
const bounds = dialog.getBoundingClientRect();
|
||||
if (freeGeometry) {
|
||||
gestureChanged.current = true;
|
||||
changeRect(fitWindow({ left: event.clientX - drag.offsetX, top: event.clientY - drag.offsetY, width: bounds.width, height: bounds.height }, { width: window.innerWidth, height: window.innerHeight }));
|
||||
changeRect(fitWindow({ left: event.clientX - drag.offsetX, top: event.clientY - drag.offsetY, width: bounds.width, height: bounds.height }, { width: window.innerWidth, height: window.innerHeight },minimumRef.current));
|
||||
return;
|
||||
}
|
||||
const left = Math.max(8, Math.min(window.innerWidth - bounds.width - 8, event.clientX - drag.offsetX));
|
||||
@@ -239,10 +292,10 @@ export function Window({
|
||||
|
||||
useEffect(() => {
|
||||
if (!freeGeometry || !open) return;
|
||||
const fit = () => { if(rectRef.current) changeRect(fitWindow(rect ?? rectRef.current, { width: window.innerWidth, height: window.innerHeight })); };
|
||||
const fit = () => { if(rectRef.current) changeRect(fitWindow((expanded ? null : rect) ?? rectRef.current, { width: window.innerWidth, height: window.innerHeight },minimumRef.current)); };
|
||||
window.addEventListener("resize", fit);
|
||||
return () => window.removeEventListener("resize", fit);
|
||||
}, [freeGeometry, open, rect]);
|
||||
}, [freeGeometry, open, rect, expanded]);
|
||||
|
||||
if (!open || typeof document === "undefined") return null;
|
||||
|
||||
@@ -258,12 +311,16 @@ export function Window({
|
||||
if (!portalHostRef.current || !fullscreen || !dialogRef.current?.contains(fullscreen)) {
|
||||
portalHostRef.current = fullscreen ?? document.body;
|
||||
}
|
||||
const baseStyle={...style,...(headerLayout==="adaptive"?{minWidth:Math.min(headerMinimum,Math.max(1,window.innerWidth-16))}:{})};
|
||||
return createPortal(
|
||||
<LoadingContext.Provider value={null}><InspectorSelectPolicyContext.Provider value={false}>
|
||||
<WindowSuspensionContext.Provider value={suspend}><div className="nodedc-overlay nodedc-ui-root" style={suspended ? {display:"none"} : undefined} data-placement={placement} data-floating={isFloating || undefined} onPointerDown={handleBackdropPointerDown}>
|
||||
<div
|
||||
ref={dialogRef}
|
||||
className={cn("nodedc-window nodedc-glass-material nodedc-material-rim", className)}
|
||||
data-expanded={expanded||undefined}
|
||||
data-header-layout={headerLayout}
|
||||
data-header-stacked={stackedTitle||undefined}
|
||||
data-material="glass-v4"
|
||||
data-glass-context={isFloating ? "scene" : undefined}
|
||||
data-size={size === "md" ? undefined : size}
|
||||
@@ -276,7 +333,7 @@ export function Window({
|
||||
aria-labelledby={titleId}
|
||||
aria-describedby={subtitle ? descriptionId : undefined}
|
||||
tabIndex={-1}
|
||||
style={freeGeometry && floatingRect ? { ...style, position: "fixed", ...floatingRect, right: "auto", bottom: "auto", maxWidth: "none", maxHeight: "none" } : dragPosition ? { ...style, position: "fixed", left: dragPosition.left, top: dragPosition.top, right: "auto", bottom: "auto" } : style}
|
||||
style={freeGeometry && floatingRect ? { ...baseStyle, position: "fixed", ...floatingRect, right: "auto", bottom: "auto", maxWidth: "none", maxHeight: "none" } : dragPosition ? { ...baseStyle, position: "fixed", left: dragPosition.left, top: dragPosition.top, right: "auto", bottom: "auto" } : baseStyle}
|
||||
{...props}
|
||||
>
|
||||
<header
|
||||
@@ -290,7 +347,7 @@ export function Window({
|
||||
const dx = event.key === "ArrowLeft" ? -10 : event.key === "ArrowRight" ? 10 : 0;
|
||||
const dy = event.key === "ArrowUp" ? -10 : event.key === "ArrowDown" ? 10 : 0;
|
||||
const bounds = { width: window.innerWidth, height: window.innerHeight };
|
||||
const next = event.shiftKey ? resizeWindow(floatingRect, "se", dx, dy, bounds) : fitWindow({ ...floatingRect, left: floatingRect.left + dx, top: floatingRect.top + dy }, bounds);
|
||||
const next = event.shiftKey ? resizeWindow(floatingRect, "se", dx, dy, bounds,minimumRef.current) : fitWindow({ ...floatingRect, left: floatingRect.left + dx, top: floatingRect.top + dy }, bounds,minimumRef.current);
|
||||
changeRect(next); commitRef.current?.(next);
|
||||
}}
|
||||
onPointerDown={(event) => {
|
||||
@@ -313,7 +370,7 @@ export function Window({
|
||||
dragRef.current = null; resizeRef.current = null;
|
||||
if (!isFloating) {
|
||||
const rect = dialogRef.current?.getBoundingClientRect();
|
||||
if (rect) changeRect(fitWindow(rectRef.current ?? { left: rect.left, top: rect.top, width: rect.width, height: rect.height }, { width: window.innerWidth, height: window.innerHeight }));
|
||||
if (rect) changeRect(fitWindow(rectRef.current ?? { left: rect.left, top: rect.top, width: rect.width, height: rect.height }, { width: window.innerWidth, height: window.innerHeight },minimumRef.current));
|
||||
}
|
||||
if (rectRef.current) commitRef.current?.({...rectRef.current});
|
||||
setFloating(!isFloating);
|
||||
|
||||
Reference in New Issue
Block a user