From 54c777276deccfda20bb7f027cef40d141a445b1 Mon Sep 17 00:00:00 2001 From: MAZE Date: Thu, 25 Apr 2024 19:48:09 +0330 Subject: [PATCH 1/2] fix: focus on the first new sound --- src/components/sound/sound.tsx | 23 +++++++++-------------- src/components/sounds/sounds.tsx | 11 ++++++++++- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/components/sound/sound.tsx b/src/components/sound/sound.tsx index 5d526eb..a7b6184 100644 --- a/src/components/sound/sound.tsx +++ b/src/components/sound/sound.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect } from 'react'; +import { useCallback, useEffect, forwardRef } from 'react'; import { ImSpinner9 } from 'react-icons/im/index'; import { Range } from './range'; @@ -10,27 +10,21 @@ import { cn } from '@/helpers/styles'; import styles from './sound.module.css'; -import type { Sound } from '@/data/types'; +import type { Sound as SoundType } from '@/data/types'; import { useKeyboardButton } from '@/hooks/use-keyboard-button'; -interface SoundProps extends Sound { +interface SoundProps extends SoundType { functional: boolean; hidden: boolean; selectHidden: (key: string) => void; unselectHidden: (key: string) => void; } -export function Sound({ - functional, - hidden, - icon, - id, - label, - selectHidden, - src, - unselectHidden, -}: SoundProps) { +export const Sound = forwardRef(function Sound( + { functional, hidden, icon, id, label, selectHidden, src, unselectHidden }, + ref, +) { const isPlaying = useSoundStore(state => state.isPlaying); const play = useSoundStore(state => state.play); const select = useSoundStore(state => state.select); @@ -82,6 +76,7 @@ export function Sound({ return (
); -} +}); diff --git a/src/components/sounds/sounds.tsx b/src/components/sounds/sounds.tsx index ed59517..2263211 100644 --- a/src/components/sounds/sounds.tsx +++ b/src/components/sounds/sounds.tsx @@ -1,4 +1,4 @@ -import { useState, useMemo, useCallback } from 'react'; +import { useState, useMemo, useCallback, useRef, useEffect } from 'react'; import { AnimatePresence, motion } from 'framer-motion'; import { Sound } from '@/components/sound'; @@ -19,6 +19,14 @@ interface SoundsProps { export function Sounds({ functional, id, sounds }: SoundsProps) { const [showAll, setShowAll] = useLocalStorage(`${id}-show-more`, false); + const firstNewSound = useRef(null); + + useEffect(() => { + if (showAll) { + firstNewSound.current?.focus(); + } + }, [showAll]); + const [hiddenSelections, setHiddenSelections] = useState<{ [key: string]: boolean; }>({}); @@ -54,6 +62,7 @@ export function Sounds({ functional, id, sounds }: SoundsProps) { {...sound} functional={functional} hidden={!showAll && index > 5} + ref={index === 6 ? firstNewSound : undefined} selectHidden={selectHidden} unselectHidden={unselectHidden} /> From b955fc93f42c1bd71d5fb5ff46f9e3a039c7fe83 Mon Sep 17 00:00:00 2001 From: MAZE Date: Thu, 25 Apr 2024 19:59:41 +0330 Subject: [PATCH 2/2] fix: refocus on show more button --- src/components/sounds/sounds.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/sounds/sounds.tsx b/src/components/sounds/sounds.tsx index 2263211..0325e6a 100644 --- a/src/components/sounds/sounds.tsx +++ b/src/components/sounds/sounds.tsx @@ -21,6 +21,9 @@ export function Sounds({ functional, id, sounds }: SoundsProps) { const firstNewSound = useRef(null); + const showMoreButton = useRef(null); + const [exitComplete, setExitComplete] = useState(false); + useEffect(() => { if (showAll) { firstNewSound.current?.focus(); @@ -75,12 +78,17 @@ export function Sounds({ functional, id, sounds }: SoundsProps) { {sounds.length > 6 && ( - + setExitComplete(true)} + > setShowAll(prev => !prev)} + onAnimationComplete={() => { + if (!showAll && exitComplete) { + setExitComplete(false); + showMoreButton.current?.focus(); + } + }} > {showAll ? 'Show Less' : 'Show More'}