Skip to content

Commit 2783977

Browse files
committed
chore: Keyboard navigation on Othello page
1 parent ebf5c06 commit 2783977

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/web/react/components/othello/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ReactElement, memo, useCallback, useMemo, useState } from 'react';
1+
import { type ReactElement, memo, useCallback, useMemo, useState, useEffect } from 'react';
22

33
import { Table } from '@/web/react/components/board';
44

@@ -41,6 +41,15 @@ const BoardWrapper = memo(({ states, game }: { states: GameState[]; game: GameMo
4141
const end = useCallback(() => setCurrentTurn(states.length - 1), [states.length]);
4242
const canEnd = currentTurn !== states.length - 1;
4343

44+
useEffect(() => {
45+
const keyboardControls = (e: KeyboardEvent) => {
46+
if (e.key === 'ArrowLeft' && canPrevious) previous();
47+
if (e.key === 'ArrowRight' && canNext) next();
48+
};
49+
window.addEventListener('keydown', keyboardControls);
50+
return () => window.removeEventListener('keydown', keyboardControls);
51+
}, [previous, canPrevious, next, canNext]);
52+
4453
const state = states[currentTurn];
4554

4655
const winCtx = game.winCtx;

0 commit comments

Comments
 (0)