Overview
TheFavoriteButton component provides a toggle button for adding and removing Pokemon from a user’s favorites list. It integrates with Zustand for state management and persists favorites to local storage.
Import
Props
Pokemon data object to be added/removed from favorites
Usage Example
Features
State Management
Uses Zustand store for favorites management:Dynamic Button State
The button changes appearance and text based on favorite status: When NOT favorited:- Default button styling
- Heart icon
- “Add to favorites” text
- White background with black text
- Close/X icon
- “Remove from favorites” text
- Bold font weight
Responsive Width
Adapts to screen size:- Full width on mobile (
w-full) - Auto width on desktop (
md:w-fit)
Component Structure
Zustand Store Integration
useFavoriteActions
Provides action methods:useIsFavorite
Returns the favorite status of a specific Pokemon:Persistence
Favorites are automatically persisted to local storage through the Zustand store middleware. Changes are immediately synchronized across all instances of the component.Icons
Uses React Icons library:IoHeart- Solid heart icon (from react-icons/io5)CgClose- Close/X icon (from react-icons/cg)
Styling Details
Base Styles
Favorite State Styles
Favorite State Styles
The
! suffix in class names (e.g., bg-white!) is UnoCSS syntax for !important, ensuring these styles override any conflicting styles.Icon Sizes
- Heart icon:
text-2xl - Close icon:
text-xl
Accessibility
The button provides clear visual and textual feedback:- Descriptive text labels (“Add to favorites” / “Remove from favorites”)
- Icon reinforces the action
- High contrast when favorited (white background, black text)
Related Components
- FavoriteCardButton - Compact version for use within PokemonCard
- PokemonCard - Uses FavoriteCardButton variant
Store Hooks
useFavoriteActions
Location:@/stores/favorite.store
Provides methods:
toggleFavorite(pokemon: PokemonSummary)- Add or remove from favoritesaddFavorite(pokemon: PokemonSummary)- Add to favoritesremoveFavorite(id: number)- Remove from favoritesclearFavorites()- Remove all favorites
useIsFavorite
Location:@/stores/favorite.store
Signature:
true if Pokemon with given ID is in favorites, false otherwise.
Example: Building a Favorites Page
Source Location
/src/components/pokemon/FavoriteButton.tsx:1-38