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,
}: CategoryProps) {
return (
<div className={styles.category}>
<div className={styles.category} id={`category-${id}`}>
<div className={styles.iconContainer}>
<div className={styles.tail} />
<div className={styles.icon}>{icon}</div>

View file

@ -8,6 +8,7 @@ import { fade } from '@/lib/motion';
import styles from './favorite.module.css';
import { useKeyboardButton } from '@/hooks/use-keyboard-button';
import { waitUntil } from '@/helpers/wait';
interface FavoriteProps {
id: string;
@ -18,11 +19,25 @@ export function Favorite({ id, label }: FavoriteProps) {
const isFavorite = useSoundStore(state => state.sounds[id].isFavorite);
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 handleKeyDown = useKeyboardButton(() => {
toggleFavorite(id);
});
const handleKeyDown = useKeyboardButton(handleToggle);
return (
<AnimatePresence initial={false} mode="wait">
@ -36,7 +51,7 @@ export function Favorite({ id, label }: FavoriteProps) {
onKeyDown={handleKeyDown}
onClick={e => {
e.stopPropagation();
toggleFavorite(id);
handleToggle();
}}
>
<motion.span