1- import { useCallback , useEffect , useId , useRef , useState } from "react" ;
1+ import { useCallback , useEffect , useId , useLayoutEffect , useRef , useState } from "react" ;
22import { createPortal } from "react-dom" ;
33import { formatSize , t } from "../i18n" ;
44import { api } from "../lib/api" ;
@@ -15,6 +15,18 @@ import { BackButton } from "./ui/BackButton";
1515import "./DirectoryWorkspace.css" ;
1616
1717export type FolderInspection = { path : string ; measurement ?: DirSizeRow ; returnToRadar ?: boolean } ;
18+ type ReturnPosition = { opener : HTMLButtonElement ; scroll : { element : HTMLElement ; top : number } [ ] } ;
19+
20+ function capturePosition ( opener : HTMLButtonElement , dialog : HTMLDivElement | null ) : ReturnPosition {
21+ const scroll : ReturnPosition [ "scroll" ] = [ ] ;
22+ for ( let element = opener . parentElement ; element ; element = element . parentElement ) {
23+ if ( element === dialog || / ^ ( a u t o | s c r o l l ) $ / . test ( getComputedStyle ( element ) . overflowY ) ) {
24+ scroll . push ( { element, top : element . scrollTop } ) ;
25+ }
26+ if ( element === dialog ) break ;
27+ }
28+ return { opener, scroll } ;
29+ }
1830
1931export function DirectoryWorkspace ( {
2032 radar, inspection, apps, onInspect, onBack, onClose, onOpenSoftware,
@@ -31,27 +43,34 @@ export function DirectoryWorkspace({
3143 const title = useId ( ) ;
3244 const dialog = useRef < HTMLDivElement > ( null ) ;
3345 const backButton = useRef < HTMLButtonElement > ( null ) ;
34- const restoreEvidenceFocus = useRef < HTMLButtonElement | null > ( null ) ;
46+ const radarPosition = useRef < ReturnPosition | null > ( null ) ;
47+ const referencePosition = useRef < ReturnPosition | null > ( null ) ;
3548 const [ error , setError ] = useState < string | null > ( null ) ;
36- const [ opened , setOpened ] = useState < { inspection : FolderInspection ; key : string ; opener : HTMLButtonElement } | null > ( null ) ;
49+ const [ opened , setOpened ] = useState < ( { inspection : FolderInspection ; key : string } & ReturnPosition ) | null > ( null ) ;
3750 const app = opened && opened . inspection === inspection
3851 ? apps . find ( item => appKey ( item ) === opened . key )
3952 : undefined ;
4053 const back = ( ) => {
4154 if ( app && opened ) {
42- restoreEvidenceFocus . current = opened . opener ;
55+ referencePosition . current = opened ;
4356 setOpened ( null ) ;
4457 }
4558 else if ( inspection && radar ) onBack ( ) ;
4659 else onClose ( ) ;
4760 } ;
4861 useDialogFocus ( true , dialog , dialogLayer . overview , back ) ;
49- useEffect ( ( ) => {
50- const opener = restoreEvidenceFocus . current ;
51- restoreEvidenceFocus . current = null ;
52- if ( opener ?. isConnected ) opener . focus ( { preventScroll : true } ) ;
53- else backButton . current ?. focus ( ) ;
54- } , [ inspection , app ] ) ;
62+ useLayoutEffect ( ( ) => {
63+ const position = referencePosition . current ?? ( ! inspection && radar ? radarPosition . current : null ) ;
64+ referencePosition . current = null ;
65+ if ( ! inspection ) radarPosition . current = null ;
66+ if ( position ?. opener . isConnected ) {
67+ for ( const { element, top } of position . scroll ) element . scrollTo ( { top, behavior : "instant" } ) ;
68+ position . opener . focus ( { preventScroll : true } ) ;
69+ } else {
70+ if ( dialog . current ) dialog . current . scrollTop = 0 ;
71+ backButton . current ?. focus ( ) ;
72+ }
73+ } , [ inspection , app , radar ] ) ;
5574
5675 const openPath = ( path : string ) => {
5776 setError ( null ) ;
@@ -67,10 +86,13 @@ export function DirectoryWorkspace({
6786 </ header >
6887 { error && < p role = "alert" className = "folder-reference-incomplete" > { error } </ p > }
6988 { radar && (
70- < div hidden = { Boolean ( inspection ) } >
89+ < div className = "directory-radar-view" hidden = { Boolean ( inspection ) } >
7190 < DiskRadarPanel
7291 onError = { setError }
73- onInspect = { row => onInspect ( { path : row ?. path ?? "" , measurement : row } ) }
92+ onInspect = { ( row , opener ) => {
93+ radarPosition . current = capturePosition ( opener , dialog . current ) ;
94+ onInspect ( { path : row ?. path ?? "" , measurement : row } ) ;
95+ } }
7496 />
7597 </ div >
7698 ) }
@@ -79,7 +101,7 @@ export function DirectoryWorkspace({
79101 < FolderReferencePanel key = { inspection . path } inspection = { inspection } apps = { apps }
80102 onInspect = { path => onInspect ( { path, returnToRadar : inspection . returnToRadar } ) }
81103 onOpenPath = { openPath }
82- onApp = { ( item , opener ) => setOpened ( { inspection, key : appKey ( item ) , opener } ) }
104+ onApp = { ( item , opener ) => setOpened ( { inspection, key : appKey ( item ) , ... capturePosition ( opener , dialog . current ) } ) }
83105 />
84106 </ div >
85107 ) }
0 commit comments