feat(ui): add measured level buttons and suspend modal hosts during scene editing

This commit is contained in:
Codex
2026-11-04 17:13:43 +03:00
parent bbbfa8912f
commit 8ff33a6a17
11 changed files with 130 additions and 14 deletions
+5 -2
View File
@@ -1,12 +1,15 @@
import {useEffect,useId,useLayoutEffect,useRef,useState,type ReactNode} from 'react';
import {useContext,useEffect,useId,useLayoutEffect,useRef,useState,type ReactNode} from 'react';
import {createPortal} from 'react-dom';
import {IconButton} from './Button.js';
import {Icon} from './Icon.js';
import {WindowSuspensionContext} from './WindowSuspensionContext.js';
import {cn} from './cn.js';
export interface EditorWorkspaceProps {editing:boolean;title:ReactNode;subtitle?:ReactNode;onBack:()=>void;children:ReactNode;className?:string}
/** One mounted renderer moves between its original slot and a modeless editor window. */
export function EditorWorkspace({editing,title,subtitle,onBack,children,className}:EditorWorkspaceProps){
const suspend=useContext(WindowSuspensionContext);
useLayoutEffect(()=>{if(editing)return suspend?.();},[editing,suspend]);
const inline=useRef<HTMLDivElement>(null),[host]=useState(()=>document.createElement('div'));
const [expanded,setExpanded]=useState(false),opener=useRef<HTMLElement|null>(null),titleId=useId();
useLayoutEffect(()=>{host.className='nodedc-editor-workspace__host';inline.current!.append(host);return()=>host.remove();},[host]);
@@ -16,7 +19,7 @@ export function EditorWorkspace({editing,title,subtitle,onBack,children,classNam
if(host.parentElement!==target){if(target.moveBefore&&host.isConnected)target.moveBefore(host,null);else target.append(host);}
host.dataset.editing=String(editing);
if(editing)host.querySelector<HTMLElement>('[data-editor-back]')?.focus({preventScroll:true});
else {setExpanded(false);if(opener.current?.isConnected)opener.current.focus({preventScroll:true});}
else {setExpanded(false);const frame=requestAnimationFrame(()=>{if(opener.current?.isConnected)opener.current.focus({preventScroll:true});});return()=>cancelAnimationFrame(frame);}
},[editing,host]);
useEffect(()=>{host.dataset.expanded=String(expanded);},[expanded,host]);
return <div ref={inline} className={cn('nodedc-editor-workspace__slot',className)}>{createPortal(
+26
View File
@@ -0,0 +1,26 @@
import type {CSSProperties} from 'react';
import {Button,type ButtonProps} from './Button.js';
import {cn} from './cn.js';
export interface LevelButtonProps extends Omit<ButtonProps,'children'|'shape'|'value'> {
label:string;
/** Measured fraction. Missing is unknown, never zero or animated progress. */
value?:number|null;
valueText?:string;
freshness?:'live'|'stale'|'unknown';
levelTone?:'neutral'|'success'|'warning'|'danger';
/** Explicit operator-selected fill; labels retain contrast over both parts. */
fillColor?:string;
}
/** Compact measured resource level which opens details, with native button semantics. */
export function LevelButton({label,value,valueText,freshness='live',levelTone='neutral',fillColor,className,style,...props}:LevelButtonProps){
const fraction=typeof value==='number'&&Number.isFinite(value)?Math.max(0,Math.min(1,value)):null;
const custom=typeof fillColor==='string'&&/^#[0-9a-f]{6}$/i.test(fillColor)?fillColor:null;
const channels=custom?.slice(1).match(/../g)?.map(v=>{const c=parseInt(v,16)/255;return c<=.04045?c/12.92:((c+.055)/1.055)**2.4;});
const ink=channels&&(.2126*channels[0]+.7152*channels[1]+.0722*channels[2])>.179?'#000000':'#ffffff';
const text=valueText??(fraction===null?'—':`${Math.round(fraction*100)}%`);
return <Button {...props} shape="pill" className={cn('nodedc-level-button',className)}
aria-label={`${label}: ${text}${freshness==='stale'?' · последние показания':''}`}
data-level-tone={levelTone} data-freshness={freshness} data-custom-fill={custom?true:undefined}
style={{...style,'--nodedc-level-percent':`${(fraction??0)*100}%`,...(custom?{'--nodedc-level-custom':custom,'--nodedc-level-ink':ink}:{})} as CSSProperties}><span aria-hidden="true">{text}</span>{custom&&fraction!==null&&<span aria-hidden="true" className="nodedc-level-button__fill">{text}</span>}</Button>;
}
+20 -7
View File
@@ -1,4 +1,5 @@
import {
useCallback,
useEffect,
useId,
useLayoutEffect,
@@ -11,6 +12,7 @@ import {
import { createPortal } from "react-dom";
import { InspectorSelectPolicyContext } from "./InspectorContext.js";
import { LoadingContext } from "./LoadingContext.js";
import { WindowSuspensionContext } from "./WindowSuspensionContext.js";
import { cn } from "./cn.js";
import { Icon } from "./Icon.js";
import { IconButton } from "./Button.js";
@@ -82,6 +84,9 @@ export function Window({
style,
...props
}: WindowProps) {
const [suspensions, setSuspensions] = useState(0);
const suspend = useCallback(() => {setSuspensions(value => value + 1); return () => setSuspensions(value => Math.max(0, value - 1));}, []);
const suspended = suspensions > 0;
const titleId = useId();
const descriptionId = useId();
const dialogRef = useRef<HTMLDivElement>(null);
@@ -100,10 +105,10 @@ export function Window({
const isFloating = floating && allowFloating && placement === "center";
const freeGeometry = isFloating || (resizable && placement === "end");
const canDrag = draggable || freeGeometry;
const shouldLockBodyScroll = !isFloating && (lockBodyScroll ?? placement === "center");
const shouldTrapFocus = !isFloating && (trapFocus ?? placement === "center");
const modeRef = useRef({ shouldTrapFocus, isFloating });
modeRef.current = { shouldTrapFocus, isFloating };
const shouldLockBodyScroll = !suspended && !isFloating && (lockBodyScroll ?? placement === "center");
const shouldTrapFocus = !suspended && !isFloating && (trapFocus ?? placement === "center");
const modeRef = useRef({ shouldTrapFocus, isFloating, suspended });
modeRef.current = { shouldTrapFocus, isFloating, suspended };
onCloseRef.current = onClose;
useEffect(() => {
@@ -116,7 +121,15 @@ export function Window({
});
const handleKeyDown = (event: KeyboardEvent) => {
if (event.defaultPrevented) return;
if (event.defaultPrevented || modeRef.current.suspended) return;
// Portalled child dialogs own their Escape/Tab keys. A parent listener
// can run first, so event.defaultPrevented alone cannot guard nesting.
const focusedDialog = document.activeElement?.closest('[role="dialog"]');
if (focusedDialog && focusedDialog !== dialogRef.current) return;
if (!focusedDialog) {
const topModal = Array.from(document.querySelectorAll<HTMLElement>('[role="dialog"][aria-modal="true"]')).filter(dialog => dialog.getClientRects().length > 0).at(-1);
if (topModal && topModal !== dialogRef.current) return;
}
if (event.key === "Escape" && closeOnEscape && (!modeRef.current.isFloating || dialogRef.current?.contains(document.activeElement))) {
event.preventDefault();
onCloseRef.current();
@@ -227,7 +240,7 @@ export function Window({
return createPortal(
<LoadingContext.Provider value={null}><InspectorSelectPolicyContext.Provider value={false}>
<div className="nodedc-overlay nodedc-ui-root" data-placement={placement} data-floating={isFloating || undefined} onPointerDown={handleBackdropPointerDown}>
<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)}
@@ -298,7 +311,7 @@ export function Window({
{footer ? <footer className="nodedc-window__footer">{footer}</footer> : null}
</div>
</div>
</InspectorSelectPolicyContext.Provider></LoadingContext.Provider>,
</WindowSuspensionContext.Provider></InspectorSelectPolicyContext.Provider></LoadingContext.Provider>,
document.fullscreenElement ?? document.body,
);
}
@@ -0,0 +1,3 @@
import {createContext} from 'react';
/** A detached editor hides its owning window without unmounting its contents. */
export const WindowSuspensionContext=createContext<null|(()=>()=>void)>(null);
+2
View File
@@ -54,3 +54,5 @@ export * from "./ResizableViewport.js";
export {useApplicationPanelFullscreen} from "./ApplicationPanelFullscreen.js";
export {SceneAnnotation} from "./SceneAnnotation.js";
export {LevelButton,type LevelButtonProps} from "./LevelButton.js";