Skip to content

Commit

Permalink
fix: focus on the first new sound
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed Apr 25, 2024
1 parent 601ba6d commit 54c7772
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
23 changes: 9 additions & 14 deletions src/components/sound/sound.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<HTMLDivElement, SoundProps>(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);
Expand Down Expand Up @@ -82,6 +76,7 @@ export function Sound({
return (
<div
aria-label={`${label} sound`}
ref={ref}
role="button"
tabIndex={0}
className={cn(
Expand All @@ -108,4 +103,4 @@ export function Sound({
<Range id={id} label={label} />
</div>
);
}
});
11 changes: 10 additions & 1 deletion src/components/sounds/sounds.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,6 +19,14 @@ interface SoundsProps {
export function Sounds({ functional, id, sounds }: SoundsProps) {
const [showAll, setShowAll] = useLocalStorage(`${id}-show-more`, false);

const firstNewSound = useRef<HTMLDivElement>(null);

useEffect(() => {
if (showAll) {
firstNewSound.current?.focus();
}
}, [showAll]);

const [hiddenSelections, setHiddenSelections] = useState<{
[key: string]: boolean;
}>({});
Expand Down Expand Up @@ -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}
/>
Expand Down

0 comments on commit 54c7772

Please sign in to comment.