Skip to content

Commit

Permalink
refactor: seperate favorite button
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed Oct 11, 2023
1 parent d8c9806 commit 4124beb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/components/sound/like/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Like } from './like';
21 changes: 21 additions & 0 deletions src/components/sound/like/like.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.favoriteButton {
position: absolute;
top: 10px;
right: 10px;
display: flex;
width: 30px;
height: 30px;
align-items: center;
justify-content: center;
border: 1px solid var(--color-neutral-200);
border-radius: 50%;
background-color: black;
background-color: var(--color-neutral-100);
color: var(--color-foreground-subtle);
cursor: pointer;
outline: none;

&.isFavorite {
color: #f43f5e;
}
}
28 changes: 28 additions & 0 deletions src/components/sound/like/like.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BiHeart, BiSolidHeart } from 'react-icons/bi/index';

import { useFavoriteStore } from '@/store/favorite';
import { cn } from '@/helpers/styles';

import styles from './like.module.css';

interface LikeProps {
id: string;
}

export function Like({ id }: LikeProps) {
const isFavorite = useFavoriteStore(state => state.favorites.includes(id));
const toggleFavorite = useFavoriteStore(state => state.toggleFavorite);

return (
<button
aria-label="Add Sound to Favorites"
className={cn(styles.favoriteButton, isFavorite && styles.isFavorite)}
onClick={e => {
e.stopPropagation();
toggleFavorite(id);
}}
>
{isFavorite ? <BiSolidHeart /> : <BiHeart />}
</button>
);
}
22 changes: 0 additions & 22 deletions src/components/sound/sound.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,6 @@
content: '';
}

& .favoriteButton {
position: absolute;
top: 10px;
right: 10px;
display: flex;
width: 30px;
height: 30px;
align-items: center;
justify-content: center;
border: 1px solid var(--color-neutral-200);
border-radius: 50%;
background-color: black;
background-color: var(--color-neutral-100);
color: var(--color-foreground-subtle);
cursor: pointer;
outline: none;

&.isFavorite {
color: #f43f5e;
}
}

& .icon {
position: relative;
z-index: 2;
Expand Down
16 changes: 2 additions & 14 deletions src/components/sound/sound.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useCallback, useEffect } from 'react';
import { BiHeart, BiSolidHeart } from 'react-icons/bi/index';

import { Range } from './range';
import { Like } from './like';

import { useSound } from '@/hooks/use-sound';
import { useSoundStore } from '@/store';
import { useFavoriteStore } from '@/store/favorite';
import { usePlay } from '@/contexts/play';
import { cn } from '@/helpers/styles';

Expand Down Expand Up @@ -40,9 +39,6 @@ export function Sound({
const volume = useSoundStore(state => state.sounds[id].volume);
const isSelected = useSoundStore(state => state.sounds[id].isSelected);

const isFavorite = useFavoriteStore(state => state.favorites.includes(id));
const toggleFavorite = useFavoriteStore(state => state.toggleFavorite);

const sound = useSound(src, { loop: true, volume });

useEffect(() => {
Expand Down Expand Up @@ -84,15 +80,7 @@ export function Sound({
onClick={toggle}
onKeyDown={toggle}
>
<button
className={cn(styles.favoriteButton, isFavorite && styles.isFavorite)}
onClick={e => {
e.stopPropagation();
toggleFavorite(id);
}}
>
{isFavorite ? <BiSolidHeart /> : <BiHeart />}
</button>
<Like id={id} />
<div className={styles.icon}>{icon}</div>
<h3 id={label}>{label}</h3>
<Range id={id} label={label} />
Expand Down

0 comments on commit 4124beb

Please sign in to comment.