File tree Expand file tree Collapse file tree 2 files changed +28
-4
lines changed
src/frontend/apps/impress/src/features/docs/doc-tree/components Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -100,14 +100,24 @@ export const DocSubPageItem = (props: TreeViewNodeProps<Doc>) => {
100100
101101 const handleKeyDown = ( e : React . KeyboardEvent ) => {
102102 // F2: focus first action button
103- const shoulOpenActions = ! menuOpen && node . isFocused ;
104- if ( e . key === 'F2' && shoulOpenActions ) {
103+ const shouldOpenActions = ! menuOpen && node . isFocused ;
104+ if ( e . key === 'F2' && shouldOpenActions ) {
105105 buttonOptionRef . current ?. focus ( ) ;
106106 e . stopPropagation ( ) ;
107107 return ;
108108 }
109109 } ;
110110
111+ const handleActionsOpenChange = ( isOpen : boolean ) => {
112+ setMenuOpen ( isOpen ) ;
113+
114+ // When the menu closes (via Escape or activating an option),
115+ // return focus to the tree item so focus is not lost.
116+ if ( ! isOpen ) {
117+ node . focus ( ) ;
118+ }
119+ } ;
120+
111121 return (
112122 < Box
113123 className = "--docs-sub-page-item"
@@ -181,7 +191,7 @@ export const DocSubPageItem = (props: TreeViewNodeProps<Doc>) => {
181191 < DocTreeItemActions
182192 doc = { doc }
183193 isOpen = { menuOpen }
184- onOpenChange = { setMenuOpen }
194+ onOpenChange = { handleActionsOpenChange }
185195 parentId = { node . data . parentKey }
186196 onCreateSuccess = { afterCreate }
187197 actionsRef = { actionsRef }
Original file line number Diff line number Diff line change @@ -116,6 +116,20 @@ export const DocTree = ({ currentDoc }: DocTreeProps) => {
116116 [ selectRoot , navigateToRoot , rootActionsOpen ] ,
117117 ) ;
118118
119+ // Handle menu open/close for root item - mirrors DocSubPageItem behavior
120+ const handleRootActionsOpenChange = useCallback ( ( isOpen : boolean ) => {
121+ setRootActionsOpen ( isOpen ) ;
122+
123+ // When the menu closes, return focus to the root tree item
124+ // (same behavior as DocSubPageItem for consistency)
125+ // Use requestAnimationFrame for smoother focus transition without flickering
126+ if ( ! isOpen ) {
127+ requestAnimationFrame ( ( ) => {
128+ rootItemRef . current ?. focus ( ) ;
129+ } ) ;
130+ }
131+ } , [ ] ) ;
132+
119133 /**
120134 * This effect is used to reset the tree when a new document
121135 * that is not part of the current tree is loaded.
@@ -310,7 +324,7 @@ export const DocTree = ({ currentDoc }: DocTreeProps) => {
310324 } }
311325 isOpen = { rootActionsOpen }
312326 isRoot = { true }
313- onOpenChange = { setRootActionsOpen }
327+ onOpenChange = { handleRootActionsOpenChange }
314328 actionsRef = { rootActionsRef }
315329 buttonOptionRef = { rootButtonOptionRef }
316330 />
You can’t perform that action at this time.
0 commit comments