feat: scroll into view after marking favorite

This commit is contained in:
MAZE 2024-06-17 21:01:53 +04:30
parent f4c66e3092
commit 74f6b5851d
2 changed files with 20 additions and 5 deletions

View file

@ -16,7 +16,7 @@ export function Category({
title, title,
}: CategoryProps) { }: CategoryProps) {
return ( return (
<div className={styles.category}> <div className={styles.category} id={`category-${id}`}>
<div className={styles.iconContainer}> <div className={styles.iconContainer}>
<div className={styles.tail} /> <div className={styles.tail} />
<div className={styles.icon}>{icon}</div> <div className={styles.icon}>{icon}</div>

View file

@ -8,6 +8,7 @@ import { fade } from '@/lib/motion';
import styles from './favorite.module.css'; import styles from './favorite.module.css';
import { useKeyboardButton } from '@/hooks/use-keyboard-button'; import { useKeyboardButton } from '@/hooks/use-keyboard-button';
import { waitUntil } from '@/helpers/wait';
interface FavoriteProps { interface FavoriteProps {
id: string; id: string;
@ -18,11 +19,25 @@ export function Favorite({ id, label }: FavoriteProps) {
const isFavorite = useSoundStore(state => state.sounds[id].isFavorite); const isFavorite = useSoundStore(state => state.sounds[id].isFavorite);
const toggleFavorite = useSoundStore(state => state.toggleFavorite); const toggleFavorite = useSoundStore(state => state.toggleFavorite);
const handleToggle = async () => {
toggleFavorite(id);
// Check if false -> true
if (!isFavorite) {
await waitUntil(
() => !!document.getElementById('category-favorites'),
50,
);
document
.getElementById('category-favorites')
?.scrollIntoView({ behavior: 'smooth' });
}
};
const variants = fade(); const variants = fade();
const handleKeyDown = useKeyboardButton(() => { const handleKeyDown = useKeyboardButton(handleToggle);
toggleFavorite(id);
});
return ( return (
<AnimatePresence initial={false} mode="wait"> <AnimatePresence initial={false} mode="wait">
@ -36,7 +51,7 @@ export function Favorite({ id, label }: FavoriteProps) {
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
onClick={e => { onClick={e => {
e.stopPropagation(); e.stopPropagation();
toggleFavorite(id); handleToggle();
}} }}
> >
<motion.span <motion.span