️(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
committed by Ovgodd
parent cec2eb5a10
commit 139aeda89b
4 changed files with 31 additions and 1 deletions
@@ -49,7 +49,7 @@ export const ChatTextArea = () => {
const isDisabled = !textAreaValue.trim() || isSending
const onKeyDown = async (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
e.stopPropagation()
if (e.key !== 'Escape') e.stopPropagation()
if (e.key !== 'Enter' || (e.key === 'Enter' && e.shiftKey) || isDisabled)
return
e.preventDefault()
@@ -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
@@ -194,6 +195,8 @@ export const SidePanel = () => {
activeKey: activePanelId,
})
useEscapeToClose(isSidePanelOpen, asideRef, closeSidePanel)
return (
<StyledSidePanel
ref={asideRef}
@@ -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])
}