mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(ui): app-wide keyboard shortcuts with TUI keybind import
This commit is contained in:
1 parent
b18e4f13b3
commit
b06de60f6b
38 files changed
+2467
-440
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { stubMatchMedia } from '@/test/test-utils'
|
||||
import { BottomSheet, BottomSheetHeader, BottomSheetContent } from './bottom-sheet'
|
||||
import { useSwipeDismiss } from '@/hooks/useMobile'
|
||||
|
||||
@@ -11,6 +12,21 @@ vi.mock('@/hooks/useMobile', () => ({
|
||||
})),
|
||||
}))
|
||||
|
||||
function renderSheet(isOpen: boolean) {
|
||||
return render(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<BottomSheet isOpen={isOpen} onClose={() => {}} ariaLabel="Test sheet">
|
||||
<button type="button">inside</button>
|
||||
</BottomSheet>
|
||||
</>,
|
||||
)
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
Reflect.deleteProperty(window, 'matchMedia')
|
||||
})
|
||||
|
||||
describe('BottomSheet', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
@@ -132,6 +148,58 @@ describe('BottomSheet', () => {
|
||||
|
||||
expect(mockBind).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('moves focus to the chat prompt on desktop when it closes', () => {
|
||||
stubMatchMedia(true)
|
||||
const { rerender } = renderSheet(false)
|
||||
|
||||
rerender(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<BottomSheet isOpen onClose={() => {}} ariaLabel="Test sheet">
|
||||
<button type="button">inside</button>
|
||||
</BottomSheet>
|
||||
</>,
|
||||
)
|
||||
screen.getByRole('button', { name: 'inside' }).focus()
|
||||
|
||||
rerender(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<BottomSheet isOpen={false} onClose={() => {}} ariaLabel="Test sheet">
|
||||
<button type="button">inside</button>
|
||||
</BottomSheet>
|
||||
</>,
|
||||
)
|
||||
|
||||
expect(screen.getByLabelText('prompt')).toHaveFocus()
|
||||
})
|
||||
|
||||
it('does not refocus the chat prompt on touch when it closes', () => {
|
||||
stubMatchMedia(false)
|
||||
const { rerender } = renderSheet(false)
|
||||
|
||||
rerender(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<BottomSheet isOpen onClose={() => {}} ariaLabel="Test sheet">
|
||||
<button type="button">inside</button>
|
||||
</BottomSheet>
|
||||
</>,
|
||||
)
|
||||
screen.getByRole('button', { name: 'inside' }).focus()
|
||||
|
||||
rerender(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<BottomSheet isOpen={false} onClose={() => {}} ariaLabel="Test sheet">
|
||||
<button type="button">inside</button>
|
||||
</BottomSheet>
|
||||
</>,
|
||||
)
|
||||
|
||||
expect(screen.getByLabelText('prompt')).not.toHaveFocus()
|
||||
})
|
||||
})
|
||||
|
||||
describe('BottomSheetHeader', () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { createPortal } from 'react-dom'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { MODAL_TRANSITION_MS } from '@/lib/utils'
|
||||
import { useSwipeDismiss } from '@/hooks/useMobile'
|
||||
import { getFocusedElement, restoreOverlayFocus } from '@/lib/overlayFocus'
|
||||
|
||||
export interface BottomSheetProps {
|
||||
isOpen: boolean
|
||||
@@ -23,6 +24,8 @@ export function BottomSheet({
|
||||
}: BottomSheetProps) {
|
||||
const [shouldRender, setShouldRender] = useState(false)
|
||||
const panelRef = useRef<HTMLDivElement>(null)
|
||||
const returnFocusRef = useRef<HTMLElement | null>(null)
|
||||
const wasOpenRef = useRef(isOpen)
|
||||
|
||||
const { bind, swipeStyles } = useSwipeDismiss(onClose, {
|
||||
enabled: isOpen,
|
||||
@@ -42,6 +45,15 @@ export function BottomSheet({
|
||||
}
|
||||
}, [isOpen])
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
returnFocusRef.current = getFocusedElement()
|
||||
} else if (wasOpenRef.current) {
|
||||
restoreOverlayFocus(returnFocusRef.current, panelRef.current)
|
||||
}
|
||||
wasOpenRef.current = isOpen
|
||||
}, [isOpen])
|
||||
|
||||
useEffect(() => {
|
||||
const handleEscape = (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape' && isOpen) {
|
||||
@@ -86,6 +98,7 @@ export function BottomSheet({
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={ariaLabel}
|
||||
data-state={isOpen ? 'open' : 'closed'}
|
||||
style={swipeStyles}
|
||||
>
|
||||
<div className="w-full flex justify-center pt-3 pb-2">
|
||||
|
||||
@@ -441,6 +441,22 @@ function ReturnFocusHost({ onCloseAutoFocus }: { onCloseAutoFocus?: (event: Even
|
||||
)
|
||||
}
|
||||
|
||||
function PromptReturnFocusHost() {
|
||||
const [open, setOpen] = useState(false)
|
||||
return (
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="chat prompt" />
|
||||
<button type="button" onClick={() => setOpen(true)}>open prompt dialog</button>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogContent>
|
||||
<DialogTitle>Prompt Focus Dialog</DialogTitle>
|
||||
<input aria-label="prompt dialog input" autoFocus />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
async function openAndEscape(returnTarget: HTMLElement) {
|
||||
returnTarget.focus()
|
||||
fireEvent.click(screen.getByText('open'))
|
||||
@@ -495,6 +511,22 @@ describe('Dialog return focus', () => {
|
||||
|
||||
expect(prompt).not.toHaveFocus()
|
||||
})
|
||||
|
||||
it('moves focus to the chat prompt when the dialog closes on desktop', async () => {
|
||||
stubMatchMedia(true)
|
||||
render(<PromptReturnFocusHost />)
|
||||
const opener = screen.getByText('open prompt dialog')
|
||||
opener.focus()
|
||||
|
||||
fireEvent.click(opener)
|
||||
const input = await screen.findByLabelText('prompt dialog input')
|
||||
await waitFor(() => expect(input).toHaveFocus())
|
||||
fireEvent.keyDown(input, { key: 'Escape' })
|
||||
await waitFor(() => expect(screen.queryByLabelText('prompt dialog input')).not.toBeInTheDocument())
|
||||
await act(() => new Promise((resolve) => setTimeout(resolve, 0)))
|
||||
|
||||
expect(screen.getByLabelText('chat prompt')).toHaveFocus()
|
||||
})
|
||||
})
|
||||
|
||||
function stubVisualViewport(height: number) {
|
||||
|
||||
@@ -5,23 +5,11 @@ import { X } from "lucide-react"
|
||||
import { cn } from "@/lib/utils"
|
||||
import { useSwipeBack } from '@/hooks/useMobile'
|
||||
import { useVisualViewport } from '@/hooks/useVisualViewport'
|
||||
import { FINE_POINTER_MEDIA_QUERY } from '@/hooks/useMediaQuery'
|
||||
import { getFocusedElement, restoreOverlayFocus } from '@/lib/overlayFocus'
|
||||
|
||||
const DialogOpenContext = React.createContext<boolean>(true)
|
||||
const DialogReturnFocusContext = React.createContext<HTMLElement | null>(null)
|
||||
|
||||
function getFocusedElement(): HTMLElement | null {
|
||||
return typeof document !== 'undefined' && document.activeElement instanceof HTMLElement
|
||||
? document.activeElement
|
||||
: null
|
||||
}
|
||||
|
||||
function canRestoreFocus(element: HTMLElement): boolean {
|
||||
if (!element.isConnected || element === document.body) return false
|
||||
if (!element.matches('input, textarea, [contenteditable="true"]')) return true
|
||||
return typeof window.matchMedia === 'function' && window.matchMedia(FINE_POINTER_MEDIA_QUERY).matches
|
||||
}
|
||||
|
||||
function Dialog({ open, ...props }: React.ComponentProps<typeof DialogPrimitive.Root>) {
|
||||
const [openState, setOpenState] = React.useState(() => ({
|
||||
open,
|
||||
@@ -83,9 +71,10 @@ const DialogContent = React.forwardRef<
|
||||
const returnFocus = React.useContext(DialogReturnFocusContext)
|
||||
const handleCloseAutoFocus = (event: Event) => {
|
||||
onCloseAutoFocus?.(event)
|
||||
if (event.defaultPrevented || !returnFocus || !canRestoreFocus(returnFocus)) return
|
||||
event.preventDefault()
|
||||
returnFocus.focus()
|
||||
if (event.defaultPrevented) return
|
||||
if (restoreOverlayFocus(returnFocus, event.currentTarget as Element | null)) {
|
||||
event.preventDefault()
|
||||
}
|
||||
}
|
||||
const handleEscapeKeyDown = (event: KeyboardEvent) => {
|
||||
onEscapeKeyDown?.(event)
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { stubMatchMedia } from '@/test/test-utils'
|
||||
import { SideDrawer, SideDrawerHeader, SideDrawerContent } from './side-drawer'
|
||||
|
||||
function renderDrawer(isOpen: boolean) {
|
||||
return render(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<SideDrawer isOpen={isOpen} onClose={() => {}} ariaLabel="Test drawer">
|
||||
<button type="button">inside</button>
|
||||
</SideDrawer>
|
||||
</>,
|
||||
)
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
Reflect.deleteProperty(window, 'matchMedia')
|
||||
})
|
||||
|
||||
describe('SideDrawer', () => {
|
||||
it('renders when isOpen is true', () => {
|
||||
render(
|
||||
@@ -79,6 +95,32 @@ describe('SideDrawer', () => {
|
||||
expect(closeInner).toHaveBeenCalledTimes(1)
|
||||
expect(closeOuter).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('moves focus to the chat prompt on desktop when it closes', () => {
|
||||
stubMatchMedia(true)
|
||||
const { rerender } = renderDrawer(false)
|
||||
|
||||
rerender(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<SideDrawer isOpen onClose={() => {}} ariaLabel="Test drawer">
|
||||
<button type="button">inside</button>
|
||||
</SideDrawer>
|
||||
</>,
|
||||
)
|
||||
screen.getByRole('button', { name: 'inside' }).focus()
|
||||
|
||||
rerender(
|
||||
<>
|
||||
<textarea data-prompt-input aria-label="prompt" />
|
||||
<SideDrawer isOpen={false} onClose={() => {}} ariaLabel="Test drawer">
|
||||
<button type="button">inside</button>
|
||||
</SideDrawer>
|
||||
</>,
|
||||
)
|
||||
|
||||
expect(screen.getByLabelText('prompt')).toHaveFocus()
|
||||
})
|
||||
})
|
||||
|
||||
describe('SideDrawerHeader', () => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from 'react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { MODAL_TRANSITION_MS } from '@/lib/utils'
|
||||
import { X } from 'lucide-react'
|
||||
import { getFocusedElement, restoreOverlayFocus } from '@/lib/overlayFocus'
|
||||
|
||||
const openDrawerStack: symbol[] = []
|
||||
|
||||
@@ -25,6 +26,9 @@ export function SideDrawer({
|
||||
ariaLabel,
|
||||
}: SideDrawerProps) {
|
||||
const [shouldRender, setShouldRender] = useState(false)
|
||||
const panelRef = useRef<HTMLDivElement>(null)
|
||||
const returnFocusRef = useRef<HTMLElement | null>(null)
|
||||
const wasOpenRef = useRef(isOpen)
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
@@ -35,6 +39,15 @@ export function SideDrawer({
|
||||
}
|
||||
}, [isOpen])
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
returnFocusRef.current = getFocusedElement()
|
||||
} else if (wasOpenRef.current) {
|
||||
restoreOverlayFocus(returnFocusRef.current, panelRef.current)
|
||||
}
|
||||
wasOpenRef.current = isOpen
|
||||
}, [isOpen])
|
||||
|
||||
const onCloseRef = useRef(onClose)
|
||||
useEffect(() => {
|
||||
onCloseRef.current = onClose
|
||||
@@ -81,6 +94,7 @@ export function SideDrawer({
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<div
|
||||
ref={panelRef}
|
||||
className={cn(
|
||||
'fixed top-0 bottom-0 bg-background border-l border-border pt-safe flex flex-col z-50',
|
||||
side === 'right' ? 'right-0' : 'left-0',
|
||||
@@ -90,6 +104,7 @@ export function SideDrawer({
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={ariaLabel}
|
||||
data-state={isOpen ? 'open' : 'closed'}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user