@@ -15,6 +15,7 @@ import {
1515} from '../algorithms/python' ;
1616import { getPseudocodeForLocale } from '../algorithms/pseudocode' ;
1717import { highlightPseudocodeToHtml } from '../utils/pseudocodeHighlight' ;
18+ import { getFocusableElements } from '../utils/focusableElements' ;
1819import Editor from '@monaco-editor/react' ;
1920import { useTheme } from '../hooks/useTheme' ;
2021import { usePythonExecution } from '../hooks/usePythonExecution' ;
@@ -78,20 +79,17 @@ function PythonCodePanel({ isOpen, onClose, algorithm }) {
7879 document . addEventListener ( 'keydown' , handleEscape ) ;
7980 return ( ) => document . removeEventListener ( 'keydown' , handleEscape ) ;
8081 } , [ isOpen , onClose ] ) ;
81-
8282 // Focus trap
8383 useEffect ( ( ) => {
8484 if ( isOpen && panelRef . current ) {
85- const focusableElements = panelRef . current . querySelectorAll (
86- 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
87- ) ;
88- const firstElement = focusableElements [ 0 ] ;
89- const lastElement = focusableElements [ focusableElements . length - 1 ] ;
90-
9185 const handleKeyDown = event => {
9286 if ( event . key === 'Escape' ) {
9387 onClose ( ) ;
9488 } else if ( event . key === 'Tab' ) {
89+ const focusableElements = getFocusableElements ( panelRef . current ) ;
90+ const firstElement = focusableElements [ 0 ] ;
91+ const lastElement = focusableElements [ focusableElements . length - 1 ] ;
92+
9593 if ( event . shiftKey ) {
9694 if ( document . activeElement === firstElement ) {
9795 event . preventDefault ( ) ;
@@ -107,12 +105,11 @@ function PythonCodePanel({ isOpen, onClose, algorithm }) {
107105 } ;
108106
109107 document . addEventListener ( 'keydown' , handleKeyDown ) ;
110- firstElement ?. focus ( ) ;
108+ getFocusableElements ( panelRef . current ) [ 0 ] ?. focus ( ) ;
111109
112110 return ( ) => document . removeEventListener ( 'keydown' , handleKeyDown ) ;
113111 }
114112 } , [ isOpen , onClose ] ) ;
115-
116113 const pythonCode = getPythonCode ( algorithm ) ;
117114 const displayName = getAlgorithmDisplayName ( algorithm ) ;
118115 const pseudocodeText = useMemo (
0 commit comments