diff --git a/components/ScriptsSidePanel.test.ts b/components/ScriptsSidePanel.test.ts index 6626a5dc6..c369eed43 100644 --- a/components/ScriptsSidePanel.test.ts +++ b/components/ScriptsSidePanel.test.ts @@ -55,3 +55,9 @@ test("scripts side panel add control is a split button with secondary create act assert.match(source, /snippets\.action\.newScript/); assert.match(source, /snippets\.action\.newSnippet/); }); + +test("scripts side panel package dialog traps focus and exposes dialog close contract", () => { + assert.match(source, /packageDialogRef/); + assert.match(source, /data-dialog-close="true"/); + assert.match(source, /isPackageDialogOpen/); +}); diff --git a/components/ScriptsSidePanel.tsx b/components/ScriptsSidePanel.tsx index 6ca551c0f..08ca3e22a 100644 --- a/components/ScriptsSidePanel.tsx +++ b/components/ScriptsSidePanel.tsx @@ -258,6 +258,8 @@ const ScriptsSidePanelInner: React.FC = ({ const [newPackageName, setNewPackageName] = useState(''); const [packageError, setPackageError] = useState(''); const [addMenuOpen, setAddMenuOpen] = useState(false); + const packageDialogRef = useRef(null); + const packageNameInputRef = useRef(null); useEffect(() => { const handler = (event: Event) => { @@ -601,6 +603,57 @@ const ScriptsSidePanelInner: React.FC = ({ setIsPackageDialogOpen(true); }, []); + // Keep Tab focus inside the package dialog while it is open. + useEffect(() => { + if (!isPackageDialogOpen) return; + const focusTimer = window.setTimeout(() => packageNameInputRef.current?.focus(), 30); + + const listFocusable = (): HTMLElement[] => { + const root = packageDialogRef.current; + if (!root) return []; + const nodes = root.querySelectorAll( + 'button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])', + ); + return Array.from(nodes).filter((el) => { + if (el.classList.contains('sr-only')) return false; + if (el.getAttribute('aria-hidden') === 'true') return false; + return el.tabIndex >= 0 || el.tagName === 'INPUT' || el.tagName === 'BUTTON'; + }); + }; + + const onKeyDown = (e: KeyboardEvent) => { + if (e.key !== 'Tab') return; + const focusable = listFocusable(); + if (focusable.length === 0) return; + const first = focusable[0]; + const last = focusable[focusable.length - 1]; + const active = document.activeElement as HTMLElement | null; + if (e.shiftKey) { + if (!active || active === first || !packageDialogRef.current?.contains(active)) { + e.preventDefault(); + last.focus(); + } + } else if (!active || active === last || !packageDialogRef.current?.contains(active)) { + e.preventDefault(); + first.focus(); + } + }; + + const onFocusIn = (e: FocusEvent) => { + const target = e.target as Node | null; + if (target && packageDialogRef.current?.contains(target)) return; + (listFocusable()[0] ?? packageNameInputRef.current)?.focus(); + }; + + document.addEventListener('keydown', onKeyDown, true); + document.addEventListener('focusin', onFocusIn); + return () => { + window.clearTimeout(focusTimer); + document.removeEventListener('keydown', onKeyDown, true); + document.removeEventListener('focusin', onFocusIn); + }; + }, [isPackageDialogOpen]); + const handleCreatePackage = useCallback(() => { if (!onPackagesChange) { setPackageError(t('snippets.renameDialog.error.empty')); @@ -920,6 +973,7 @@ const ScriptsSidePanelInner: React.FC = ({ {isPackageDialogOpen ? (
= ({
{ setNewPackageName(e.target.value); diff --git a/components/TerminalLayer.tsx b/components/TerminalLayer.tsx index e8d90410a..a7508c0c1 100644 --- a/components/TerminalLayer.tsx +++ b/components/TerminalLayer.tsx @@ -1676,6 +1676,10 @@ const TerminalLayerInner: React.FC = ({ toast.error(t('scripts.recording.noSession')); return; } + if (isTerminalSensitiveInputActive(sessionId)) { + toast.info(t('scripts.actions.skippedSensitiveSessions', { count: 1 })); + return; + } if (!isScriptSnippet(snippet)) { const command = await resolveSnippetCommand(snippet); if (command === null) return;