Skip to content

Commit

Permalink
Press spacebar to generate (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilgourgouillon committed Dec 19, 2023
1 parent 2c5feed commit 709040d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useNoteSettingsContext';
export * from './useChordSettingsContext';
export * from './useSpeedContext';
export * from './useSpaceBarEffect';
18 changes: 18 additions & 0 deletions src/hooks/useSpaceBarEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect } from "react";

export const useSpaceBarEffect = (maFonction: () => void) => {
useEffect(() => {
const handleKeyPress = (event: KeyboardEvent) => {
event.preventDefault();
if (event.code === 'Space') {
maFonction();
}
};

window.addEventListener('keydown', handleKeyPress);

return () => {
window.removeEventListener('keydown', handleKeyPress);
};
}, [maFonction]);
};
7 changes: 5 additions & 2 deletions src/pages/ChordsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { ChordsList, ChordsSettings, TimerCue } from "../components";

import { useChordSettingsContext, useSpeedContext } from "../hooks";
import { useChordSettingsContext, useSpaceBarEffect, useSpeedContext } from "../hooks";

export const ChordsPage = () => {
const { chords, isShapeVisible, cagedPosition, getRandomChordsOnClick } =
useChordSettingsContext();
const { resetSecondsElapsed } = useSpeedContext();

const handleChordsListOnClick = () => {
getRandomChordsOnClick();
resetSecondsElapsed();
}

useSpaceBarEffect(handleChordsListOnClick);

const ShapeDecorator: React.ReactNode = (
<div className={`${isShapeVisible ? "" : "invisible"}`}>
{cagedPosition}
</div>
);


return (
<div className="h-full flex flex-col items-center justify-center">
<div className="w-screen flex items-center justify-center">
Expand Down
4 changes: 3 additions & 1 deletion src/pages/NotesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NotesList, NotesSettings, TimerCue } from '../components';
import { useNoteSettingsContext, useSpeedContext } from '../hooks';
import { useNoteSettingsContext, useSpaceBarEffect, useSpeedContext } from '../hooks';

export const NotesPage = () => {
const { notes, isStringVisible, guitarString, getRandomNotesOnClick } = useNoteSettingsContext();
Expand All @@ -10,6 +10,8 @@ export const NotesPage = () => {
resetSecondsElapsed();
}

useSpaceBarEffect(handleNotesListOnClick);

const GuitarStringDecorator: React.ReactNode = (
<div className={`${isStringVisible ? '' : 'invisible'}`}>{guitarString}</div>
);
Expand Down

0 comments on commit 709040d

Please sign in to comment.