Skip to content

dave-b-b/react-achievements-zustand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

24 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ† React-Achievements-Zustand

A flexible achievement system for React applications using Zustand for state management.

React Achievements Demo

Try it out: StackBlitz Demo

Quick Start

npm install react react-dom zustand react-achievements-zustand
import { 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>;
}

Core Features

Metric Updates

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);

Achievement Notifications

  • Powered by react-toastify
  • Shows multiple achievements simultaneously
  • Customizable appearance
/* Custom notification styling */
.Toastify__toast {
  background-color: #2c3e50;
  color: #ecf0f1;
  border-radius: 10px;
}

State Management

// Save progress
const { metrics, previouslyAwardedAchievements } = useAchievementState();

// Load progress
<AchievementProvider
  config={achievementConfig}
  initialState={{
    metrics: savedMetrics,
    previouslyAwardedAchievements: savedAchievements
  }}
/>

Reset Progress

const { resetStorage } = useAchievement();
resetStorage(); // Clears all progress

Available Icons

Achievement Types

  • ๐Ÿ† 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

Progress & Milestones

  • ๐Ÿ“ˆ 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

Social & Engagement

  • ๐Ÿ”— 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

Time & Activity

  • โ˜€๏ธ 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

Creativity & Expertise

  • ๐ŸŽจ artist - Artistic achievement
  • โœ๏ธ writer - Writing achievement
  • ๐Ÿ”ฌ innovator - Innovation
  • ๐Ÿ› ๏ธ creator - Creation achievement
  • ๐ŸŽ“ expert - Expertise achievement
  • ๐ŸŽญ performer - Performance achievement
  • ๐Ÿง  thinker - Thinking achievement
  • ๐Ÿ—บ๏ธ explorer - Exploration achievement

Action & Interaction

  • ๐Ÿ–ฑ๏ธ 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

Numbers & Counters

  • 1๏ธโƒฃ one - First achievement
  • ๐Ÿ”Ÿ ten - Tenth achievement
  • ๐Ÿ’ฏ hundred - Hundredth achievement
  • ๐Ÿ”ข thousand - Thousandth achievement

System Icons

  • โญ default - Default fallback icon
  • โณ loading - Loading state
  • โš ๏ธ error - Error state
  • โœ… success - Success state
  • โŒ failure - Failure state

Additional Decorative Icons

  • ๐Ÿšฉ flag - Flag marker
  • ๐Ÿ’Ž gem - Gem reward
  • ๐ŸŽ—๏ธ ribbon - Ribbon award
  • ๐ŸŽ–๏ธ badge - Badge award
  • โš”๏ธ monsterDefeated - Combat achievement
  • ๐Ÿ“ฆ itemCollected - Collection achievement

Usage Example

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
    }
  }]
};

Advanced Features

Custom Styling

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>

Persistence

<AchievementProvider
  config={achievementConfig}
  storageKey="my-game-achievements" // localStorage key
  initialState={loadedState}
/>

Complete Documentation

About

React achievements package with Zustand state management

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors