File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ export const App: React.FC = observer(() => {
2121 } ) ;
2222 const [ isGameContainerVisible , setIsGameContainerVisible ] = React . useState ( true ) ;
2323 const [ viewportHeight , setViewportHeight ] = React . useState ( window . innerHeight ) ;
24+ const [ shouldFocusInput , setShouldFocusInput ] = React . useState ( false ) ;
2425
2526 React . useEffect ( ( ) => {
2627 const handleResize = ( ) => {
@@ -50,6 +51,9 @@ export const App: React.FC = observer(() => {
5051
5152 const handleGameStart = ( ) => {
5253 setShowStartScreen ( false ) ;
54+ if ( settingsStore . gameMode === 'type' ) {
55+ setShouldFocusInput ( true ) ;
56+ }
5357 } ;
5458
5559 const handleRestart = ( ) => {
@@ -98,7 +102,12 @@ export const App: React.FC = observer(() => {
98102 < main className = "game-container" >
99103 { settingsStore . gameMode !== 'picker' && < FlagDisplay /> }
100104 { settingsStore . gameMode === 'quiz' && < QuizMode /> }
101- { settingsStore . gameMode === 'type' && < TypeMode /> }
105+ { settingsStore . gameMode === 'type' && (
106+ < TypeMode
107+ key = { gameStore . isGameOver ? 'game-over' : 'playing' }
108+ shouldFocus = { shouldFocusInput }
109+ />
110+ ) }
102111 { settingsStore . gameMode === 'picker' && < PickerMode /> }
103112 </ main >
104113 { gameStore . isGameOver && (
Original file line number Diff line number Diff line change @@ -7,7 +7,9 @@ import './TypeMode.css';
77import { useTranslation } from '../../hooks/useTranslation' ;
88import { CORRECT_ANSWER_DELAY , INCORRECT_ANSWER_DELAY } from '../../constants/timing' ;
99
10- export const TypeMode : React . FC = observer ( ( ) => {
10+ export const TypeMode : React . FC < {
11+ shouldFocus ?: boolean ;
12+ } > = observer ( ( { shouldFocus } ) => {
1113 const { gameStore, settingsStore } = useStores ( ) ;
1214 const [ answer , setAnswer ] = React . useState ( '' ) ;
1315 const [ suggestion , setSuggestion ] = React . useState ( '' ) ;
@@ -29,6 +31,18 @@ export const TypeMode: React.FC = observer(() => {
2931 }
3032 } , [ gameStore . currentFlag ] ) ;
3133
34+ React . useEffect ( ( ) => {
35+ if ( gameStore . isGameOver && inputRef . current ) {
36+ inputRef . current . blur ( ) ;
37+ }
38+ } , [ gameStore . isGameOver ] ) ;
39+
40+ React . useEffect ( ( ) => {
41+ if ( shouldFocus && inputRef . current ) {
42+ inputRef . current . focus ( ) ;
43+ }
44+ } , [ shouldFocus ] ) ;
45+
3246 const handleKeyDown = ( e : React . KeyboardEvent < HTMLInputElement > | React . TouchEvent < HTMLInputElement > ) => {
3347 if ( e . type === 'keydown' ) {
3448 const keyEvent = e as React . KeyboardEvent ;
You can’t perform that action at this time.
0 commit comments