react-achievements 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,164 +1,371 @@
1
- # Welcome to React-Achievements!
1
+ Certainly! I'll provide you with a revised version of the README that removes the artifacts-related content. Here's the updated version without the artifacts:
2
+ <h1 align="center">🏆 React-Achievements 🏆</h1>
2
3
 
3
- A flexible and customizable achievement system for React applications.
4
4
 
5
- ## Installation
5
+ A flexible and customizable achievement system for React applications, perfect for adding gamification elements to your projects.
6
6
 
7
- The first thing you need to do is install react-achievements:
7
+ <h2 align="center">🚀 Installation</h2>
8
8
 
9
- `npm install react-achievements`
9
+ Install `react-achievements` using npm or yarn:
10
10
 
11
- or if you're using yarn:
12
- `yarn add react-achievements`
11
+ ```bash
12
+ npm install react-achievements
13
+ ```
14
+
15
+ or
13
16
 
14
- ## Usage
17
+ ```bash
18
+ yarn add react-achievements
19
+ ```
15
20
 
16
- ### Set up the AchievementProvider
21
+ <h2 align="center">🎮 Usage</h2>
17
22
 
18
- Wrap your app or a part of your app with the AchievementProvider:
23
+ Let's walk through setting up a simple RPG-style game with achievements using React-Achievements.
19
24
 
20
- ```
25
+ <h3 align="center">🛠 Set up the AchievementProvider</h3>
26
+
27
+ First, wrap your app or a part of your app with the AchievementProvider:
28
+
29
+ ```jsx
30
+ import React from 'react';
21
31
  import { AchievementProvider } from 'react-achievements';
22
32
  import achievementConfig from './achievementConfig';
33
+ import Game from './Game';
34
+
35
+ const initialState = {
36
+ level: 1,
37
+ experience: 0,
38
+ monstersDefeated: 0,
39
+ questsCompleted: 0
40
+ };
23
41
 
24
42
  function App() {
25
43
  return (
26
- <AchievementProvider config={achievementConfig} initialState={<your_object>}>
27
- {/* Your app components */}
44
+ <AchievementProvider
45
+ config={achievementConfig}
46
+ initialState={initialState}
47
+ badgesButtonPosition="top-right"
48
+ >
49
+ <Game />
28
50
  </AchievementProvider>
29
51
  );
30
52
  }
31
- ```
32
53
 
33
- The object that you pass to the initialState prop should have keys that match the metrics defined in your achievement configuration. For example, if your achievement configuration defines a 'transactions' metric, your initialState object should look like this:
34
- ```typescript
35
- const user = {
36
- transactions: [
37
- {
38
- id: 1,
39
- amount: 100
40
- },
41
- {
42
- id: 2,
43
- amount: 200
44
- }
45
- ]
46
- }
54
+ export default App;
47
55
  ```
48
56
 
49
- ### Create an achievement configuration
57
+ <h3 align="center">📝 Create an achievement configuration</h3>
58
+
50
59
  Create a file (e.g., achievementConfig.js) to define your achievements:
51
- (It is important that your icons be located in the public folder of your project)
60
+
52
61
  ```javascript
53
- import image1 from './public/path/to/image1.png';
54
- import image2 from './public/path/to/image2.png';
62
+ import levelUpIcon from './icons/level-up.png';
63
+ import monsterSlayerIcon from './icons/monster-slayer.png';
64
+ import questMasterIcon from './icons/quest-master.png';
55
65
 
56
66
  const achievementConfig = {
57
- transactions: [
67
+ level: [
58
68
  {
59
- check: (value) => value.length >= 1,
69
+ check: (value) => value >= 10,
60
70
  data: {
61
- id: 'first_transaction',
62
- title: 'First Transaction',
63
- description: 'Completed your first transaction',
64
- icon: image1
71
+ id: 'level_10',
72
+ title: 'Novice Adventurer',
73
+ description: 'Reached level 10',
74
+ icon: levelUpIcon
65
75
  }
66
76
  },
67
77
  {
68
- check: (value) => values.reduce((sum, val) => sum + (typeof val === 'number' ? val : 0), 0) >= 100,
78
+ check: (value) => value >= 50,
69
79
  data: {
70
- id: 'thousand_dollars',
71
- title: 'Big Spender',
72
- description: 'Spent a total of $1000',
73
- icon: image2
80
+ id: 'level_50',
81
+ title: 'Seasoned Warrior',
82
+ description: 'Reached level 50',
83
+ icon: levelUpIcon
74
84
  }
75
- },
85
+ }
86
+ ],
87
+ monstersDefeated: [
88
+ {
89
+ check: (value) => value >= 100,
90
+ data: {
91
+ id: 'monster_slayer',
92
+ title: 'Monster Slayer',
93
+ description: 'Defeated 100 monsters',
94
+ icon: monsterSlayerIcon
95
+ }
96
+ }
76
97
  ],
77
- // Add more metrics and achievements as needed
98
+ questsCompleted: [
99
+ {
100
+ check: (value) => value >= 50,
101
+ data: {
102
+ id: 'quest_master',
103
+ title: 'Quest Master',
104
+ description: 'Completed 50 quests',
105
+ icon: questMasterIcon
106
+ }
107
+ }
108
+ ]
78
109
  };
79
110
 
80
111
  export default achievementConfig;
81
112
  ```
82
113
 
83
- ### Update the location of the badges button
84
- You can specify the position of the badges button by passing
85
- badgesButtonPosition to the AchievementProvider:
86
- ```javascript
87
- <AchievementProvider config={achievementConfig} badgesButtonPosition="bottom-right">
88
- ```
114
+ <h3 align="center">🎣 Use the useAchievement hook</h3>
89
115
 
90
- The possible values for badgesButtonPosition are
91
- - `'top-left'`
92
- - `'top-right'`
93
- - `'bottom-left'`
94
- - `'bottom-right'`
95
-
96
- ### Use the useAchievement hook
97
- In your components, use the useAchievement hook to update metrics and trigger achievement checks:
98
- ```javascript
116
+ In your game components, use the useAchievement hook to update metrics and trigger achievement checks:
117
+ ```jsx
118
+ import React, { useState } from 'react';
99
119
  import { useAchievement } from 'react-achievements';
100
120
 
101
- function TransactionComponent() {
102
- const { setMetrics } = useAchievement();
121
+ function Game() {
122
+ const { setMetrics, metrics } = useAchievement();
123
+ const [currentQuest, setCurrentQuest] = useState(null);
124
+
125
+ const defeatMonster = () => {
126
+ setMetrics(prevMetrics => {
127
+ const newExperience = prevMetrics.experience + 10;
128
+ const newLevel = Math.floor(newExperience / 100) + 1;
129
+ return {
130
+ ...prevMetrics,
131
+ monstersDefeated: prevMetrics.monstersDefeated + 1,
132
+ experience: newExperience,
133
+ level: newLevel > prevMetrics.level ? newLevel : prevMetrics.level
134
+ };
135
+ });
136
+ };
103
137
 
104
- const handleNewTransaction = () => {
105
- // Your transaction logic here
106
- setMetrics(prevMetrics => ({
107
- ...prevMetrics,
108
- transactions: (prevMetrics.transactions || 0) + 1
109
- }));
138
+ const completeQuest = () => {
139
+ setMetrics(prevMetrics => {
140
+ const newExperience = prevMetrics.experience + 50;
141
+ const newLevel = Math.floor(newExperience / 100) + 1;
142
+ return {
143
+ ...prevMetrics,
144
+ questsCompleted: prevMetrics.questsCompleted + 1,
145
+ experience: newExperience,
146
+ level: newLevel > prevMetrics.level ? newLevel : prevMetrics.level
147
+ };
148
+ });
149
+ setCurrentQuest(null);
150
+ };
151
+
152
+ const startQuest = () => {
153
+ setCurrentQuest("Defeat the Dragon");
110
154
  };
111
155
 
112
156
  return (
113
- <button onClick={handleNewTransaction}>New Transaction</button>
157
+ <div>
158
+ <h1>My RPG Game</h1>
159
+ <p>Level: {metrics.level}</p>
160
+ <p>Experience: {metrics.experience}</p>
161
+ <p>Monsters Defeated: {metrics.monstersDefeated}</p>
162
+ <p>Quests Completed: {metrics.questsCompleted}</p>
163
+
164
+ <div>
165
+ <h2>Battle Arena</h2>
166
+ <button onClick={defeatMonster}>Fight a Monster</button>
167
+ </div>
168
+
169
+ <div>
170
+ <h2>Quest Board</h2>
171
+ {currentQuest ? (
172
+ <>
173
+ <p>Current Quest: {currentQuest}</p>
174
+ <button onClick={completeQuest}>Complete Quest</button>
175
+ </>
176
+ ) : (
177
+ <button onClick={startQuest}>Start a New Quest</button>
178
+ )}
179
+ </div>
180
+ </div>
114
181
  );
115
182
  }
183
+
184
+ export default Game;
116
185
  ```
117
186
 
118
- ## Features
187
+ <h2 align="center">✨ Features</h2>
188
+
189
+ - Flexible Achievement System: Define custom metrics and achievement conditions for your game or app.
190
+ - Automatic Achievement Tracking: Achievements are automatically checked and unlocked when metrics change.
191
+ - Achievement Notifications: A modal pops up when an achievement is unlocked, perfect for rewarding players.
192
+ - Persistent Achievements: Unlocked achievements and metrics are stored in local storage, allowing players to keep their progress.
193
+ - Achievement Gallery: Players can view all their unlocked achievements, encouraging completionism.
194
+ - Confetti Effect: A celebratory confetti effect is displayed when an achievement is unlocked, adding to the excitement.
119
195
 
120
- ### Flexible Achievement System:
121
- Define custom metrics and achievement conditions.
196
+ <h2 align="center">🔧 API</h2>
122
197
 
123
- ### Automatic Achievement Tracking:
124
- Achievements are automatically checked and unlocked when metrics change.
198
+ <h3 align="center">🏗 AchievementProvider</h3>
125
199
 
126
- ### Achievement Notifications:
127
- A modal pops up when an achievement is unlocked.
200
+ #### Props:
128
201
 
129
- ### Persistent Achievements:
130
- Achieved achievements are stored in local storage. We have not implemented a feature for this to be permanently stored in a database. But we will.
131
- Achievements are stored in local storage as:
132
- 1. Key: 'react-achievements-achievements' (This is an array of all the achievements that have been unlocked)
133
- 2. Key: 'react-achievements-metrics' (This is an object of all the metrics that have been updated)
202
+ - `config`: An object defining your metrics and achievements.
203
+ - `initialState`: The initial state of your metrics.
204
+ - `storageKey` (optional): A string to use as the key for localStorage. Default: 'react-achievements'
205
+ - `badgesButtonPosition` (optional): Position of the badges button. Default: 'top-right'
206
+ - `styles` (optional): Custom styles for the achievement components.
134
207
 
135
- ### Achievement Gallery:
136
- Users can view all their unlocked achievements.
208
+ <h3 align="center">🪝 useAchievement Hook</h3>
137
209
 
138
- ### Customizable UI:
139
- The achievement modal and badges button can be styled to fit your app's design.
140
- Confetti Effect: A celebratory confetti effect is displayed when an achievement is unlocked.
210
+ #### Returns an object with:
141
211
 
142
- ## API
143
- ### AchievementProvider
144
- #### Props:
212
+ - `setMetrics`: Function to update the metrics.
213
+ - `metrics`: Current metrics object.
214
+ - `unlockedAchievements`: Array of unlocked achievement IDs.
215
+ - `showBadgesModal`: Function to manually show the badges modal.
216
+
217
+ <h2 align="center">🎨 Customization</h2>
218
+
219
+ React-Achievements allows for extensive customization of its appearance. You can override the default styles by passing a `styles` prop to the `AchievementProvider`:
220
+
221
+ ```jsx
222
+ const customStyles = {
223
+ achievementModal: {
224
+ overlay: {
225
+ backgroundColor: 'rgba(0, 0, 0, 0.8)',
226
+ },
227
+ content: {
228
+ backgroundColor: '#2a2a2a',
229
+ color: '#ffffff',
230
+ },
231
+ title: {
232
+ color: '#ffd700',
233
+ },
234
+ button: {
235
+ backgroundColor: '#4CAF50',
236
+ },
237
+ },
238
+ badgesModal: {
239
+ // Custom styles for the badges modal
240
+ },
241
+ badgesButton: {
242
+ // Custom styles for the badges button
243
+ },
244
+ };
245
+
246
+ function App() {
247
+ return (
248
+ <AchievementProvider
249
+ config={achievementConfig}
250
+ initialState={initialState}
251
+ styles={customStyles}
252
+ >
253
+ <Game />
254
+ </AchievementProvider>
255
+ );
256
+ }
257
+ ```
145
258
 
146
- 1. config: An object defining your metrics and achievements.
147
- storageKey (optional): A string to use as the key for localStorage. Default: 'react-achievements'
148
- badgesButtonPosition (optional): Position of the badges button. Options: 'top-left', 'top-right', 'bottom-left', 'bottom-right'. Default: 'top-right'
259
+ ### achievementModal
149
260
 
150
- 2. useAchievement Hook Returns an object with:
261
+ Customizes the modal that appears when an achievement is unlocked.
151
262
 
152
- 3. setMetrics: Function to update the metrics.
263
+ ```
264
+ achievementModal: {
265
+ overlay: {
266
+ // Styles for the modal overlay (background)
267
+ backgroundColor: 'rgba(0, 0, 0, 0.8)',
268
+ // You can also customize other overlay properties like zIndex, transition, etc.
269
+ },
270
+ content: {
271
+ // Styles for the modal content container
272
+ backgroundColor: '#2a2a2a',
273
+ color: '#ffffff',
274
+ borderRadius: '10px',
275
+ padding: '20px',
276
+ // Add any other CSS properties for the content container
277
+ },
278
+ title: {
279
+ // Styles for the achievement title
280
+ fontSize: '24px',
281
+ fontWeight: 'bold',
282
+ color: '#ffd700',
283
+ },
284
+ icon: {
285
+ // Styles for the achievement icon
286
+ width: '64px',
287
+ height: '64px',
288
+ marginBottom: '10px',
289
+ },
290
+ description: {
291
+ // Styles for the achievement description
292
+ fontSize: '16px',
293
+ marginTop: '10px',
294
+ },
295
+ button: {
296
+ // Styles for the close button
297
+ backgroundColor: '#4CAF50',
298
+ color: 'white',
299
+ padding: '10px 20px',
300
+ border: 'none',
301
+ borderRadius: '5px',
302
+ cursor: 'pointer',
303
+ },
304
+ }
305
+ ```
153
306
 
154
- 4. metrics: Current metrics object.
155
- 5. achievedAchievements: Array of achieved achievement IDs.
156
- 6. showBadgesModal: Function to manually show the badges modal.
307
+ ### badgesModal
157
308
 
158
- Customization
309
+ ```
310
+ badgesModal: {
311
+ overlay: {
312
+ // Similar to achievementModal overlay
313
+ },
314
+ content: {
315
+ // Similar to achievementModal content
316
+ },
317
+ title: {
318
+ // Styles for the modal title
319
+ },
320
+ badgeContainer: {
321
+ // Styles for the container holding all badges
322
+ display: 'flex',
323
+ flexWrap: 'wrap',
324
+ justifyContent: 'center',
325
+ },
326
+ badge: {
327
+ // Styles for individual badge containers
328
+ margin: '10px',
329
+ textAlign: 'center',
330
+ },
331
+ badgeIcon: {
332
+ // Styles for badge icons
333
+ width: '50px',
334
+ height: '50px',
335
+ },
336
+ badgeTitle: {
337
+ // Styles for badge titles
338
+ fontSize: '14px',
339
+ marginTop: '5px',
340
+ },
341
+ button: {
342
+ // Styles for the close button (similar to achievementModal button)
343
+ },
344
+ }
345
+ ```
346
+
347
+
348
+ ### badgesButton
349
+
350
+ ```
351
+ badgesButton: {
352
+ // Styles for the floating badges button
353
+ position: 'fixed',
354
+ padding: '10px 20px',
355
+ backgroundColor: '#007bff',
356
+ color: '#ffffff',
357
+ border: 'none',
358
+ borderRadius: '5px',
359
+ cursor: 'pointer',
360
+ zIndex: 1000,
361
+ // You can add more CSS properties as needed. These are just regular CSS
362
+ }
363
+
364
+ ```
159
365
 
160
- You can customize the appearance of the achievement modal, badges modal, and badges button by modifying their respective components in the package.
366
+ This allows you to match the achievement system's look and feel to your game or application's theme.
161
367
 
162
- License MIT
368
+ <h2 align="center">📄 License</h2>
369
+ MIT
163
370
 
164
- This README provides a comprehensive overview of how to use the react-achievements package, including setup, usage examples, features, and API details. You may want to adjust or expand certain sections based on the specific implementation details or additional features of your package.
371
+ 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.
@@ -1,9 +1,11 @@
1
1
  import React from 'react';
2
2
  import { AchievementData } from '../types';
3
+ import { Styles } from '../defaultStyles';
3
4
  interface AchievementModalProps {
4
- show: boolean;
5
+ isOpen: boolean;
5
6
  achievement: AchievementData | null;
6
7
  onClose: () => void;
8
+ styles: Styles['achievementModal'];
7
9
  }
8
10
  declare const _default: React.NamedExoticComponent<AchievementModalProps>;
9
11
  export default _default;
@@ -1,7 +1,9 @@
1
1
  import React from 'react';
2
+ import { Styles } from '../defaultStyles';
2
3
  interface BadgesButtonProps {
3
4
  onClick: () => void;
4
5
  position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
6
+ styles: Styles['badgesButton'];
5
7
  }
6
8
  declare const _default: React.NamedExoticComponent<BadgesButtonProps>;
7
9
  export default _default;
@@ -1,9 +1,11 @@
1
1
  import React from 'react';
2
2
  import { AchievementData } from '../types';
3
+ import { Styles } from '../defaultStyles';
3
4
  interface BadgesModalProps {
4
- show: boolean;
5
+ isOpen: boolean;
5
6
  achievements: AchievementData[];
6
7
  onClose: () => void;
8
+ styles: Styles['badgesModal'];
7
9
  }
8
10
  declare const _default: React.NamedExoticComponent<BadgesModalProps>;
9
11
  export default _default;
@@ -1,8 +1,6 @@
1
1
  import React from 'react';
2
- import { ConfettiProps } from 'react-confetti';
3
2
  interface ConfettiWrapperProps {
4
3
  show: boolean;
5
- confettiProps?: Partial<ConfettiProps>;
6
4
  }
7
5
  declare const ConfettiWrapper: React.FC<ConfettiWrapperProps>;
8
6
  export default ConfettiWrapper;
@@ -1,5 +1,6 @@
1
1
  import React, { ReactNode } from 'react';
2
2
  import { Metrics, AchievementConfig, MetricValue } from '../types';
3
+ import { Styles } from '../defaultStyles';
3
4
  interface AchievementContextProps {
4
5
  metrics: Metrics;
5
6
  setMetrics: (metrics: Metrics | ((prevMetrics: Metrics) => Metrics)) => void;
@@ -13,6 +14,7 @@ interface AchievementProviderProps {
13
14
  initialState?: Record<string, MetricValue>;
14
15
  storageKey?: string;
15
16
  badgesButtonPosition?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
17
+ styles?: Partial<Styles>;
16
18
  }
17
19
  export declare const AchievementProvider: React.FC<AchievementProviderProps>;
18
20
  export declare const useAchievement: () => AchievementContextProps;
@@ -0,0 +1,28 @@
1
+ type StyleObject = {
2
+ [key: string]: string | number;
3
+ };
4
+ interface ModalStyles {
5
+ overlay: StyleObject;
6
+ content: StyleObject;
7
+ title: StyleObject;
8
+ icon: StyleObject;
9
+ description: StyleObject;
10
+ button: StyleObject;
11
+ }
12
+ interface BadgesModalStyles {
13
+ overlay: StyleObject;
14
+ content: StyleObject;
15
+ title: StyleObject;
16
+ badgeContainer: StyleObject;
17
+ badge: StyleObject;
18
+ badgeIcon: StyleObject;
19
+ badgeTitle: StyleObject;
20
+ button: StyleObject;
21
+ }
22
+ export interface Styles {
23
+ achievementModal: ModalStyles;
24
+ badgesModal: BadgesModalStyles;
25
+ badgesButton: StyleObject;
26
+ }
27
+ export declare const defaultStyles: Styles;
28
+ export {};