fix(ui): separate tree visibility from disclosure and preserve scroll
This commit is contained in:
@@ -11,6 +11,8 @@ export function Tree({nodes,selected,selectedIds,onSelect,onVisibilityChange,lab
|
||||
const [collapsed,setCollapsed]=useState<Map<string,boolean>>(()=>new Map());
|
||||
const [focus,setFocus]=useState<string|null>(null);
|
||||
const anchor=useRef<string|null>(null);
|
||||
const lastReveal=useRef<{selected:string;key:number|undefined}|null>(null),pendingReveal=useRef<string|null>(null);
|
||||
const disclosureScroll=useRef<number|null>(null);
|
||||
const host=useRef<HTMLDivElement>(null),elements=useRef(new Map<string,HTMLDivElement>());
|
||||
const parents=new Map<string,string>(),index=new Map<string,TreeNode>();
|
||||
const visit=(items:TreeNode[],parent?:string)=>{for(const item of items){index.set(item.id,item);if(parent)parents.set(item.id,parent);visit(item.children??[],item.id);}};visit(nodes);
|
||||
@@ -19,15 +21,29 @@ export function Tree({nodes,selected,selectedIds,onSelect,onVisibilityChange,lab
|
||||
const rows:{node:TreeNode;depth:number;open:boolean}[]=[];
|
||||
const flatten=(items:TreeNode[],depth=0)=>{for(const node of items){if(!matches(node))continue;const open=!!query||!(collapsed.get(node.id)??depth>=defaultExpandedDepth);rows.push({node,depth,open});if(open)flatten(node.children??[],depth+1);}};flatten(nodes);
|
||||
const reveal=(id:string)=>{const target=elements.current.get(id),container=host.current;if(!target||!container)return;const row=target.getBoundingClientRect(),box=container.getBoundingClientRect();if(row.top<box.top)container.scrollTop-=box.top-row.top;else if(row.bottom>box.bottom)container.scrollTop+=row.bottom-box.bottom;};
|
||||
useLayoutEffect(()=>{if(selected){setFocus(selected);setCollapsed(old=>{if([...ancestors].every(id=>old.get(id)===false))return old;const next=new Map(old);for(const id of ancestors)next.set(id,false);return next;});}},[selected,revealKey,nodes]);
|
||||
useLayoutEffect(()=>{if(selected)reveal(selected);},[selected,revealKey,query,collapsed]);
|
||||
const toggle=(id:string)=>setCollapsed(old=>{const next=new Map(old);const depth=rows.find(row=>row.node.id===id)?.depth??0;next.set(id,!(old.get(id)??depth>=defaultExpandedDepth));return next;});
|
||||
// Data/visibility refreshes are not a new selection. An unresolved external
|
||||
// selection may wait for async nodes, but a manual disclosure always wins.
|
||||
useLayoutEffect(()=>{
|
||||
if(!selected){lastReveal.current=null;pendingReveal.current=null;return;}
|
||||
if(lastReveal.current?.selected===selected&&lastReveal.current.key===revealKey)return;
|
||||
if(!index.has(selected))return;
|
||||
lastReveal.current={selected,key:revealKey};pendingReveal.current=selected;setFocus(selected);
|
||||
setCollapsed(old=>{if([...ancestors].every(id=>old.get(id)===false))return old;const next=new Map(old);for(const id of ancestors)next.set(id,false);return next;});
|
||||
},[selected,revealKey,nodes]);
|
||||
useLayoutEffect(()=>{
|
||||
if(disclosureScroll.current!==null){if(host.current)host.current.scrollTop=disclosureScroll.current;disclosureScroll.current=null;return;}
|
||||
const id=pendingReveal.current;if(id&&elements.current.has(id)){reveal(id);pendingReveal.current=null;}
|
||||
},[selected,revealKey,nodes,query,collapsed]);
|
||||
const toggle=(id:string)=>{
|
||||
pendingReveal.current=null;disclosureScroll.current=host.current?.scrollTop??null;
|
||||
setCollapsed(old=>{const next=new Map(old);const depth=rows.find(row=>row.node.id===id)?.depth??0;next.set(id,!(old.get(id)??depth>=defaultExpandedDepth));return next;});
|
||||
};
|
||||
const select=(id:string,event:{ctrlKey:boolean;metaKey:boolean;shiftKey:boolean})=>{setFocus(id);
|
||||
const rangeIds=event.shiftKey&&selectedIds?treeSelectionRange(rows.map(row=>row.node.id),anchor.current,id):[id];
|
||||
if(!event.shiftKey)anchor.current=id;
|
||||
onSelect(id,{ctrlKey:event.ctrlKey,metaKey:event.metaKey,shiftKey:event.shiftKey,rangeIds});elements.current.get(id)?.focus({preventScroll:true});reveal(id);};
|
||||
return <div ref={host} className="nodedc-tree" role="tree" aria-label={label} aria-multiselectable={selectedIds?true:undefined}>
|
||||
{rows.map(({node,depth,open},position)=><div key={node.id} ref={element=>{if(element)elements.current.set(node.id,element);else elements.current.delete(node.id);}} role="treeitem" aria-level={depth+1} aria-selected={selectedIds?selectedIds.includes(node.id):selected===node.id} aria-expanded={node.children?.length?open:undefined} tabIndex={(focus??selected??rows[0]?.node.id)===node.id?0:-1} className="nodedc-tree__row" style={{'--tree-depth':depth} as CSSProperties} title={node.label} onFocus={()=>setFocus(node.id)} onClick={event=>select(node.id,event)} onDoubleClick={()=>node.children?.length&&toggle(node.id)} onKeyDown={event=>{
|
||||
{rows.map(({node,depth,open},position)=><div key={node.id} ref={element=>{if(element)elements.current.set(node.id,element);else elements.current.delete(node.id);}} role="treeitem" aria-level={depth+1} aria-selected={selectedIds?selectedIds.includes(node.id):selected===node.id} aria-expanded={node.children?.length?open:undefined} tabIndex={(focus??selected??rows[0]?.node.id)===node.id?0:-1} className="nodedc-tree__row" style={{'--tree-depth':depth} as CSSProperties} title={node.label} onFocus={()=>setFocus(node.id)} onClick={event=>select(node.id,event)} onKeyDown={event=>{
|
||||
if(event.target!==event.currentTarget)return;
|
||||
let next:string|undefined;
|
||||
if(event.key==='ArrowDown')next=rows[Math.min(rows.length-1,position+1)]?.node.id;
|
||||
|
||||
Reference in New Issue
Block a user