Skip to content

Commit

Permalink
fix: input onblur focus resolves #3 (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoffens authored Apr 30, 2024
1 parent 9e0bbe7 commit b4f722e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/renderer/src/runner/runner.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable prettier/prettier */
import { ChangeEvent, ReactElement, useEffect, useState } from 'react'
import { ChangeEvent, ReactElement, useEffect, useState, useRef } from 'react'
import { Command, ResultStreamEvent, Runtime } from './runtime'

export default function Runner(): ReactElement {
const [runtimeList, setRuntimes] = useState<Runtime[]>([])
const [historyIndex, setHistoryIndex] = useState<number>(-1)
const [commanderMode, setCommanderMode] = useState<boolean>(false)
const [rawMode, setRawMode] = useState<boolean>(false)
const inputRef = useRef<HTMLInputElement>(null)
const reloadRuntimesFromBackend = async (): Promise<void> => {
const isCommanderMode = await window.electron.ipcRenderer.invoke('runner.isCommanderMode')
const runtimesFetch: Runtime[] = await window.electron.ipcRenderer.invoke('runtimes')
Expand Down Expand Up @@ -125,6 +125,31 @@ export default function Runner(): ReactElement {
}
}

useEffect(() => {
inputRef.current?.focus()

// Conditionally handle keydown of letter or arrow to refocus input
const handleGlobalKeyDown = (event): void => {
if (
/^[a-zA-Z]$/.test(event.key) ||
event.key === 'ArrowUp' ||
event.key === 'ArrowDown' ||
(!event.shiftKey && !event.ctrlKey && !event.altKey)
) {
if (document.activeElement !== inputRef.current) {
inputRef.current?.focus()
}
handleKeyDown(event)
}
}

document.addEventListener('keydown', handleGlobalKeyDown)

return () => {
document.removeEventListener('keydown', handleGlobalKeyDown)
}
}, [])

useEffect(() => {
reloadRuntimesFromBackend().catch((error) => console.error(error))
}, [])
Expand Down Expand Up @@ -173,6 +198,7 @@ export default function Runner(): ReactElement {
<div className="runner-input-container">
<div className="runner-input">
<input
ref={inputRef}
autoFocus
className="runner-input-field"
placeholder=">"
Expand Down

0 comments on commit b4f722e

Please sign in to comment.