A flexible achievement system for React applications using Zustand for state management.
Try it out: StackBlitz Demo
npm install react react-dom zustand react-achievements-zustandimport { AchievementProvider, useAchievement } from 'react-achievements-zustand';
// 1. Define achievements
const achievementConfig = {
score: [{
isConditionMet: (value) => value >= 100,
achievementDetails: {
achievementId: 'high-score',
achievementTitle: 'High Score!',
achievementDescription: 'Score 100 points',
achievementIconKey: 'trophy'
}
}]
};
// 2. Use the provider
function App() {
return (
<AchievementProvider config={achievementConfig}>
<Game />
</AchievementProvider>
);
}
// 3. Update metrics
function Game() {
const { updateMetrics } = useAchievement();
const handleScore = () => {
updateMetrics({
score: [100] // Values are always passed in arrays
});
};
return <button onClick={handleScore}>Score Points</button>;
}const { updateMetrics } = useAchievement();
// Single update
updateMetrics({ score: [100] });
// Multiple metrics
updateMetrics({
score: [100],
combo: [5],
time: [60]
});
// Sequential updates
updateMetrics({ score: [50] });
setTimeout(() => updateMetrics({ score: [100] }), 1000);- Powered by react-toastify
- Shows multiple achievements simultaneously
- Customizable appearance
/* Custom notification styling */
.Toastify__toast {
background-color: #2c3e50;
color: #ecf0f1;
border-radius: 10px;
}// Save progress
const { metrics, previouslyAwardedAchievements } = useAchievementState();
// Load progress
<AchievementProvider
config={achievementConfig}
initialState={{
metrics: savedMetrics,
previouslyAwardedAchievements: savedAchievements
}}
/>const { resetStorage } = useAchievement();
resetStorage(); // Clears all progress- ๐
trophy- Classic achievement symbol - โญ
star- General achievement - ๐ฅ
bronze- Bronze tier achievement - ๐ฅ
silver- Silver tier achievement - ๐ฅ
gold- Gold tier achievement - ๐
diamond- Diamond tier achievement - โจ
legendary- Legendary achievement - ๐ฅ
epic- Epic achievement - ๐ฎ
rare- Rare achievement - ๐
common- Common achievement - ๐
special- Special achievement
- ๐
growth- Progress indicator - ๐ฅ
streak- Streak achievements - ๐
master- Mastery achievements - ๐
pioneer- Pioneer or early adopter - ๐ก
breakthrough- Discovery or breakthrough - ๐ฑ
newBeginnings- First-time achievements - ๐ฃ
firstStep- First achievement - ๐
milestoneReached- Milestone completion - ๐
challengeCompleted- Challenge completion
- ๐
shared- Sharing achievement - โค๏ธ
liked- Appreciation - ๐ฌ
commented- Communication - ๐ฅ
followed- Following achievement - ๐ค
invited- Invitation achievement - ๐๏ธ
communityMember- Community participation - ๐
supporter- Support achievement - ๐
connected- Connection achievement - ๐
participant- Participation - ๐ฃ
influencer- Influence achievement
- โ๏ธ
activeDay- Daily activity - ๐
activeWeek- Weekly activity - ๐๏ธ
activeMonth- Monthly activity - โฐ
earlyBird- Early participation - ๐
nightOwl- Late participation - โณ
dedicated- Time dedication - โฑ๏ธ
punctual- Timeliness - ๐
consistent- Consistency - ๐
marathon- Long-term achievement
- ๐จ
artist- Artistic achievement - โ๏ธ
writer- Writing achievement - ๐ฌ
innovator- Innovation - ๐ ๏ธ
creator- Creation achievement - ๐
expert- Expertise achievement - ๐ญ
performer- Performance achievement - ๐ง
thinker- Thinking achievement - ๐บ๏ธ
explorer- Exploration achievement
- ๐ฑ๏ธ
clicked- Click interaction - ๐
used- Usage achievement - ๐
found- Discovery achievement - ๐งฑ
built- Building achievement - ๐งฉ
solved- Problem solving - ๐ญ
discovered- Discovery - ๐
unlocked- Unlocking achievement - โฌ๏ธ
upgraded- Upgrade achievement - ๐ง
repaired- Fix achievement - ๐ก๏ธ
defended- Defense achievement
- 1๏ธโฃ
one- First achievement - ๐
ten- Tenth achievement - ๐ฏ
hundred- Hundredth achievement - ๐ข
thousand- Thousandth achievement
- โญ
default- Default fallback icon - โณ
loading- Loading state โ ๏ธ error- Error state- โ
success- Success state - โ
failure- Failure state
- ๐ฉ
flag- Flag marker - ๐
gem- Gem reward - ๐๏ธ
ribbon- Ribbon award - ๐๏ธ
badge- Badge award - โ๏ธ
monsterDefeated- Combat achievement - ๐ฆ
itemCollected- Collection achievement
const achievementConfig = {
score: [{
isConditionMet: (value) => value >= 100,
achievementDetails: {
achievementId: 'high-score',
achievementTitle: 'High Score!',
achievementDescription: 'Score 100 points',
achievementIconKey: 'legendary' // Use any icon key from above
}
}]
};const styles = {
badgesButton: {
position: 'fixed',
top: '20px',
right: '20px',
// ...more styles
},
badgesModal: {
overlay: { backgroundColor: 'rgba(0,0,0,0.8)' },
// ...more styles
}
};
<AchievementProvider styles={styles}>
<App />
</AchievementProvider><AchievementProvider
config={achievementConfig}
storageKey="my-game-achievements" // localStorage key
initialState={loadedState}
/>