This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
A React implementation of the Konami Code detector, featuring a custom hook (useKonami) for detecting keyboard input sequences. The project is built with React 19, TypeScript, Vite, and Vitest.
npm run devStarts Vite dev server (typically http://localhost:5173)
npm run buildRuns TypeScript compiler check (tsc -b) followed by Vite build. Output is in dist/.
npm test # Run all tests once
npm run test:watch # Watch mode for development
npm run test:ui # Interactive UI for tests
npm run test:coverage # Generate coverage reportTests use Vitest with jsdom environment. Test setup is in tests/setup.ts.
npm run lintRuns ESLint with flat config (eslint.config.js).
npm run previewPreview production build locally.
Located in src/hooks/useKonami.ts. This is the primary functionality of the library.
Key Implementation Details:
- Uses
useRefto maintain stable references for progress and callbacks, avoiding unnecessary effect re-runs - Global window keyboard listener attached when
enabled=true - Timeout mechanism resets progress if keys aren't pressed within the timeout window (default 2s)
- Ignores repeat keydown events from held keys (
e.repeatcheck) - Progress resets to 0 after successful activation, but
activatedstate remains true - Manual
reset()function clears both progress and activated state
State Management:
progressRefkeeps sync withprogressstate for closure stability in event handlercallbacksRefstores latest callback functions to prevent effect dependenciestimeoutReftracks the reset timer between keypresses
Exports:
DEFAULT_KONAMI_CODE: Array of arrow keys + 'b', 'a'KonamiOptions: Configuration interfaceKonamiState: Return value interfaceuseKonami: Main hook function
Located in src/components/KonamiDemo.tsx.
Responsibilities:
- Visual feedback for sequence progress
- Success/error/timeout state display with temporary feedback messages
- Progress bar visualization
- Manual reset button after activation
- Console logging in development mode only
State Management:
- Container state controls overall UI state (normal/success/error/timeout)
- Feedback messages with auto-hide after 2s
- Progress calculation as percentage for progress bar
src/
hooks/
useKonami.ts # Core hook implementation
components/
KonamiDemo.tsx # Demo UI component
KonamiDemo.css # Demo styles
ErrorBoundary.tsx # Error boundary wrapper
App.tsx # Root component
main.tsx # Entry point
tests/
setup.ts # Vitest setup
hooks/
useKonami.test.ts # Comprehensive hook tests
Tests for useKonami in tests/hooks/useKonami.test.ts cover:
- Default behavior and initialization
- Custom code sequences
- Wrong key reset behavior
- Timeout mechanism with fake timers
- All callback invocations (onActivated, onProgress, onReset)
- Repeat key filtering
- Manual reset functionality
- Dynamic enable/disable
- Proper cleanup on unmount
Testing Patterns:
- Use
vi.useFakeTimers()in beforeEach - Use
simulateKeyPress()helper to dispatch keyboard events - Use
act()from @testing-library/react for state updates - Use
renderHook()for testing hooks in isolation
Project uses TypeScript 5.9 with project references:
tsconfig.json: Base config with referencestsconfig.app.json: App source configurationtsconfig.node.json: Node/Vite configuration
- Vite 7: Dev server and bundler
- Vitest 4: Test runner with jsdom environment
- ESLint 9: Linting with flat config, React hooks and refresh plugins
- @vitejs/plugin-react: React Fast Refresh support