mirror of
https://github.com/remvze/moodist.git
synced 2025-12-18 09:24:14 +00:00
feat: scroll into view after marking favorite
This commit is contained in:
parent
f4c66e3092
commit
74f6b5851d
2 changed files with 20 additions and 5 deletions
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue