MenuItem component represents a single country in the menu list. It renders a checkbox that allows users to mark countries as visited or unvisited.
Overview
The MenuItem component:- Displays a country checkbox with label
- Handles selection/deselection of countries
- Updates global state through Jotai atoms
- Provides hover effects for better UX
- Uses memo for performance optimization
Props
Country object containing name, ISO code, and selection state
Country Type
TheCountry interface is defined as:
Display name of the country
ISO 3166-1 alpha-2 country code (e.g., “US”, “GB”, “JP”)
Geographic region the country belongs to
Bounding box coordinates for map focus [west, south, east, north]
Whether the country is currently marked as visited
Component Signature
Features
State Management
The component uses Jotai write-only atoms to update the global state:Checkbox Behavior
The checkbox reflects the country’s selection state and triggers updates on change:Usage Example
Rendering
The component renders as a list item containing a label with checkbox and text:Styling
The component uses Tailwind CSS classes for styling:- Hover effect: Background changes on hover (
hover:bg-zinc-50) - Dark mode: Supports dark theme with
dark:variants - Checkbox: Custom styled with primary color when checked
- Focus ring: Visible focus indicator for accessibility
Checkbox Styles
Accessibility
- Uses semantic
<label>element wrapping the checkbox - Checkbox
idmatches the label’s implicit association - Keyboard accessible (Space/Enter to toggle)
- Focus visible with ring indicator
- Screen reader friendly structure
Performance
The component is wrapped withmemo() to prevent unnecessary re-renders:
toggleCountry callback is memoized with useCallback to maintain referential equality:
State Updates
When a checkbox is toggled, it dispatches actions to Jotai atoms:- Add:
addCountryAtomadds the country’s ISO code to the selection - Remove:
removeCountryAtomremoves the country’s ISO code from the selection
- Re-render of the MenuItem with updated
selectedstate - Update of the Globe component to highlight/unhighlight the country
- Recalculation of Progress indicators in parent regions
Dependencies
jotai- State managementreact- Core React functionalityCountrytype frommodels/country
Source Code
Location:src/components/menu-item.tsx