diff --git a/project2/src/App.tsx b/project2/src/App.tsx index e520cf6..ce65daf 100644 --- a/project2/src/App.tsx +++ b/project2/src/App.tsx @@ -150,6 +150,24 @@ function App() { ); } }, []); + + const closePokemonDetail = () => { + document.body.style.overflow = ''; + setSelectedId(null); + }; + useEffect(() => { + if(selectedId === null) return; + + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + closePokemonDetail(); + } + }; + document.addEventListener('keydown', handleKeyDown); + return () => { + document.removeEventListener('keydown', handleKeyDown); + }; + }, [selectedId]); const handleRate = async (pokemonId: number, rating: number) => { if (!ratingsReady || !voterId) return; @@ -334,10 +352,7 @@ function App() { ratingDisabled={!ratingsReady || savingRating} ratingError={ratingError} onRate={(rating) => void handleRate(selectedPokemon.id, rating)} - onBack={() => { - document.body.style.overflow = ''; - setSelectedId(null); - }} + onBack={closePokemonDetail} /> )} diff --git a/project2/src/components/PokemonCard.tsx b/project2/src/components/PokemonCard.tsx index d52dcc6..c30c609 100644 --- a/project2/src/components/PokemonCard.tsx +++ b/project2/src/components/PokemonCard.tsx @@ -24,7 +24,7 @@ function getTitleFontSize(name: string): string { export function PokemonCard({ pokemon, onSelect }: PokemonCardProps) { return ( -