️(frontend) close side panel with Escape key

useEscapeToClose: close panel on Escape, restore focus, let chat input bubble
This commit is contained in:
Cyril
2026-07-13 13:59:36 +02:00
parent 53cc8642eb
commit 72af826402
4 changed files with 42 additions and 8 deletions
+1
View File
@@ -24,6 +24,7 @@ and this project adheres to
- ♻️(frontend) inline model weights to avoid loading them from remote
- ♻️(frontend) inline MediaPipe WASM modules to avoid loading from remote
- ⬆️(frontend) upgrade posthog-js from 1.387.0 to 1.391.2
- ♿️(frontend) close side panel with Escape key #1507
### Fixed
@@ -16,6 +16,7 @@ import { Info } from './Info'
import { HStack } from '@/styled-system/jsx'
import { useReactionsToolbar } from '@/features/reactions/hooks/useReactionsToolbar'
import { useRestoreFocus } from '@/hooks/useRestoreFocus'
import { useEscapeToClose } from '@/hooks/useEscapeToClose'
type StyledSidePanelProps = {
title: string
@@ -188,21 +189,25 @@ export const SidePanel = () => {
focusAside()
}, [activePanelId, focusAside])
const closePanel = useCallback(() => {
layoutStore.activePanelId = null
layoutStore.activeSubPanelId = null
}, [])
useRestoreFocus(isSidePanelOpen, {
onOpened: handlePanelOpened,
preventScroll: true,
activeKey: activePanelId,
})
useEscapeToClose(isSidePanelOpen, asideRef, closePanel)
return (
<StyledSidePanel
ref={asideRef}
title={title}
ariaLabel={t('ariaLabel', { title })}
onClose={() => {
layoutStore.activePanelId = null
layoutStore.activeSubPanelId = null
}}
onClose={closePanel}
closeButtonTooltip={t('closeButton', {
content: t(`content.${activeSubPanelId || activePanelId}`),
})}
@@ -35,6 +35,11 @@ export const ChatInput = ({
if (!isDisabled) handleSubmit()
}
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key !== 'Escape') e.stopPropagation()
submitOnEnter(e)
}
useEffect(() => {
const resize = () => {
if (!inputRef.current) return
@@ -79,10 +84,7 @@ export const ChatInput = ({
>
<TextArea
ref={inputRef}
onKeyDown={(e) => {
e.stopPropagation()
submitOnEnter(e)
}}
onKeyDown={handleKeyDown}
onKeyUp={(e) => e.stopPropagation()}
placeholder={t('textArea.placeholder')}
value={text}
@@ -0,0 +1,26 @@
import { useEffect, useRef, type RefObject } from 'react'
export const useEscapeToClose = (
isActive: boolean,
containerRef: RefObject<HTMLElement | null>,
onClose: () => void
) => {
const onCloseRef = useRef(onClose)
useEffect(() => {
onCloseRef.current = onClose
})
useEffect(() => {
if (!isActive) return
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key !== 'Escape') return
if (!containerRef.current?.contains(document.activeElement)) return
e.stopPropagation()
onCloseRef.current()
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [isActive, containerRef])
}