fix: refocus on show more button

This commit is contained in:
MAZE 2024-04-25 19:59:41 +03:30
parent 54c777276d
commit b955fc93f4

View file

@ -21,6 +21,9 @@ export function Sounds({ functional, id, sounds }: SoundsProps) {
const firstNewSound = useRef<HTMLDivElement>(null); const firstNewSound = useRef<HTMLDivElement>(null);
const showMoreButton = useRef<HTMLButtonElement>(null);
const [exitComplete, setExitComplete] = useState(false);
useEffect(() => { useEffect(() => {
if (showAll) { if (showAll) {
firstNewSound.current?.focus(); firstNewSound.current?.focus();
@ -75,12 +78,17 @@ export function Sounds({ functional, id, sounds }: SoundsProps) {
</div> </div>
{sounds.length > 6 && ( {sounds.length > 6 && (
<AnimatePresence initial={false} mode="wait"> <AnimatePresence
initial={false}
mode="wait"
onExitComplete={() => setExitComplete(true)}
>
<motion.button <motion.button
animate="show" animate="show"
exit="hidden" exit="hidden"
initial="hidden" initial="hidden"
key={showAll ? `${id}-show-less` : `${id}-show-more`} key={showAll ? `${id}-show-less` : `${id}-show-more`}
ref={showMoreButton}
transition={{ duration: 0.2 }} transition={{ duration: 0.2 }}
variants={variants} variants={variants}
className={cn( className={cn(
@ -88,6 +96,12 @@ export function Sounds({ functional, id, sounds }: SoundsProps) {
hasHiddenSelection && !showAll && styles.active, hasHiddenSelection && !showAll && styles.active,
)} )}
onClick={() => setShowAll(prev => !prev)} onClick={() => setShowAll(prev => !prev)}
onAnimationComplete={() => {
if (!showAll && exitComplete) {
setExitComplete(false);
showMoreButton.current?.focus();
}
}}
> >
{showAll ? 'Show Less' : 'Show More'} {showAll ? 'Show Less' : 'Show More'}
</motion.button> </motion.button>