npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-achievements

v1.3.3

Published

This package allows users to transpose a React achievements engine over their React apps

Downloads

729

Readme

A flexible and customizable achievement system for React applications, perfect for adding gamification elements to your projects.

Install react-achievements using npm or yarn:

npm install react-achievements

or

yarn add react-achievements

Let's walk through setting up a simple RPG-style game with achievements using React-Achievements.

First, wrap your app or a part of your app with the AchievementProvider:

import React from 'react';
import { AchievementProvider } from 'react-achievements';
import achievementConfig from './achievementConfig';
import Game from './Game';

const initialState = {
  level: 1,
  experience: 0,
  monstersDefeated: 0,
  questsCompleted: 0
};

function App() {
  return (
    <AchievementProvider 
      config={achievementConfig} 
      initialState={initialState}
      badgesButtonPosition="top-right"
    >
      <Game />
    </AchievementProvider>
  );
}

export default App;

Create a file (e.g., achievementConfig.js) to define your achievements:

import levelUpIcon from './icons/level-up.png';
import monsterSlayerIcon from './icons/monster-slayer.png';
import questMasterIcon from './icons/quest-master.png';

const achievementConfig = {
  level: [
    {
      check: (value) => value >= 10,
      data: {
        id: 'level_10',
        title: 'Novice Adventurer',
        description: 'Reached level 10',
        icon: levelUpIcon
      }
    },
    {
      check: (value) => value >= 50,
      data: {
        id: 'level_50',
        title: 'Seasoned Warrior',
        description: 'Reached level 50',
        icon: levelUpIcon
      }
    }
  ],
  monstersDefeated: [
    {
      check: (value) => value >= 100,
      data: {
        id: 'monster_slayer',
        title: 'Monster Slayer',
        description: 'Defeated 100 monsters',
        icon: monsterSlayerIcon
      }
    }
  ],
  questsCompleted: [
    {
      check: (value) => value >= 50,
      data: {
        id: 'quest_master',
        title: 'Quest Master',
        description: 'Completed 50 quests',
        icon: questMasterIcon
      }
    }
  ]
};

export default achievementConfig;

In your game components, use the useAchievement hook to update metrics and trigger achievement checks:

import React, { useState } from 'react';
import { useAchievement } from 'react-achievements';

function Game() {
  const { setMetrics, metrics } = useAchievement();
  const [currentQuest, setCurrentQuest] = useState(null);

  const defeatMonster = () => {
    setMetrics(prevMetrics => {
      const newExperience = prevMetrics.experience + 10;
      const newLevel = Math.floor(newExperience / 100) + 1;
      return {
        ...prevMetrics,
        monstersDefeated: prevMetrics.monstersDefeated + 1,
        experience: newExperience,
        level: newLevel > prevMetrics.level ? newLevel : prevMetrics.level
      };
    });
  };

  const completeQuest = () => {
    setMetrics(prevMetrics => {
      const newExperience = prevMetrics.experience + 50;
      const newLevel = Math.floor(newExperience / 100) + 1;
      return {
        ...prevMetrics,
        questsCompleted: prevMetrics.questsCompleted + 1,
        experience: newExperience,
        level: newLevel > prevMetrics.level ? newLevel : prevMetrics.level
      };
    });
    setCurrentQuest(null);
  };

  const startQuest = () => {
    setCurrentQuest("Defeat the Dragon");
  };

  return (
    <div>
      <h1>My RPG Game</h1>
      <p>Level: {metrics.level}</p>
      <p>Experience: {metrics.experience}</p>
      <p>Monsters Defeated: {metrics.monstersDefeated}</p>
      <p>Quests Completed: {metrics.questsCompleted}</p>
      
      <div>
        <h2>Battle Arena</h2>
        <button onClick={defeatMonster}>Fight a Monster</button>
      </div>

      <div>
        <h2>Quest Board</h2>
        {currentQuest ? (
          <>
            <p>Current Quest: {currentQuest}</p>
            <button onClick={completeQuest}>Complete Quest</button>
          </>
        ) : (
          <button onClick={startQuest}>Start a New Quest</button>
        )}
      </div>
    </div>
  );
}

export default Game;
  • Flexible Achievement System: Define custom metrics and achievement conditions for your game or app.
  • Automatic Achievement Tracking: Achievements are automatically checked and unlocked when metrics change.
  • Achievement Notifications: A modal pops up when an achievement is unlocked, perfect for rewarding players.
  • Persistent Achievements: Unlocked achievements and metrics are stored in local storage, allowing players to keep their progress.
  • Achievement Gallery: Players can view all their unlocked achievements, encouraging completionism.
  • Confetti Effect: A celebratory confetti effect is displayed when an achievement is unlocked, adding to the excitement.

Props:

  • config: An object defining your metrics and achievements.
  • initialState: The initial state of your metrics.
  • storageKey (optional): A string to use as the key for localStorage. Default: 'react-achievements'
  • badgesButtonPosition (optional): Position of the badges button. Default: 'top-right'
  • styles (optional): Custom styles for the achievement components.

Returns an object with:

  • setMetrics: Function to update the metrics.
  • metrics: Current metrics object.
  • unlockedAchievements: Array of unlocked achievement IDs.
  • showBadgesModal: Function to manually show the badges modal.

React-Achievements allows for extensive customization of its appearance. You can override the default styles by passing a styles prop to the AchievementProvider:

const customStyles = {
  achievementModal: {
    overlay: {
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
    },
    content: {
      backgroundColor: '#2a2a2a',
      color: '#ffffff',
    },
    title: {
      color: '#ffd700',
    },
    button: {
      backgroundColor: '#4CAF50',
    },
  },
  badgesModal: {
    // Custom styles for the badges modal
  },
  badgesButton: {
    // Custom styles for the badges button
  },
};

function App() {
  return (
    <AchievementProvider 
      config={achievementConfig} 
      initialState={initialState}
      styles={customStyles}
    >
      <Game />
    </AchievementProvider>
  );
}

achievementModal

Customizes the modal that appears when an achievement is unlocked.

achievementModal: {
  overlay: {
    // Styles for the modal overlay (background)
    backgroundColor: 'rgba(0, 0, 0, 0.8)',
    // You can also customize other overlay properties like zIndex, transition, etc.
  },
  content: {
    // Styles for the modal content container
    backgroundColor: '#2a2a2a',
    color: '#ffffff',
    borderRadius: '10px',
    padding: '20px',
    // Add any other CSS properties for the content container
  },
  title: {
    // Styles for the achievement title
    fontSize: '24px',
    fontWeight: 'bold',
    color: '#ffd700',
  },
  icon: {
    // Styles for the achievement icon
    width: '64px',
    height: '64px',
    marginBottom: '10px',
  },
  description: {
    // Styles for the achievement description
    fontSize: '16px',
    marginTop: '10px',
  },
  button: {
    // Styles for the close button
    backgroundColor: '#4CAF50',
    color: 'white',
    padding: '10px 20px',
    border: 'none',
    borderRadius: '5px',
    cursor: 'pointer',
  },
}

badgesModal

badgesModal: {
  overlay: {
    // Similar to achievementModal overlay
  },
  content: {
    // Similar to achievementModal content
  },
  title: {
    // Styles for the modal title
  },
  badgeContainer: {
    // Styles for the container holding all badges
    display: 'flex',
    flexWrap: 'wrap',
    justifyContent: 'center',
  },
  badge: {
    // Styles for individual badge containers
    margin: '10px',
    textAlign: 'center',
  },
  badgeIcon: {
    // Styles for badge icons
    width: '50px',
    height: '50px',
  },
  badgeTitle: {
    // Styles for badge titles
    fontSize: '14px',
    marginTop: '5px',
  },
  button: {
    // Styles for the close button (similar to achievementModal button)
  },
}

badgesButton

badgesButton: {
  // Styles for the floating badges button
  position: 'fixed',
  padding: '10px 20px',
  backgroundColor: '#007bff',
  color: '#ffffff',
  border: 'none',
  borderRadius: '5px',
  cursor: 'pointer',
  zIndex: 1000,
  // You can add more CSS properties as needed. These are just regular CSS
}

This allows you to match the achievement system's look and feel to your game or application's theme.

React-Achievements provides a comprehensive achievement system for React applications, perfect for adding gamification elements to your projects. Whether you're building a game, an educational app, or any interactive experience, this package offers an easy way to implement and manage achievements, enhancing user engagement and retention.