react-achievements 1.3.0 → 1.3.1

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
@@ -4,11 +4,11 @@ A flexible and customizable achievement system for React applications.
4
4
 
5
5
  ## Installation
6
6
 
7
- The first thing you need to do is install react-achievements:
7
+ Install react-achievements using npm or yarn:
8
8
 
9
9
  `npm install react-achievements`
10
10
 
11
- or if you're using yarn:
11
+
12
12
  `yarn add react-achievements`
13
13
 
14
14
  ## Usage
@@ -17,7 +17,7 @@ or if you're using yarn:
17
17
 
18
18
  Wrap your app or a part of your app with the AchievementProvider:
19
19
 
20
- ```
20
+ ```javscript
21
21
  import { AchievementProvider } from 'react-achievements';
22
22
  import achievementConfig from './achievementConfig';
23
23
 
@@ -30,135 +30,132 @@ function App() {
30
30
  }
31
31
  ```
32
32
 
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 = {
33
+ The `initialState` prop should contain the current state of your metrics. For example:
34
+
35
+ ```javascript
36
+ const initialState = {
36
37
  transactions: [
37
- {
38
- id: 1,
39
- amount: 100
40
- },
41
- {
42
- id: 2,
43
- amount: 200
44
- }
38
+ { id: 1, amount: 100 },
39
+ { id: 2, amount: 200 }
45
40
  ]
46
- }
41
+ };
47
42
  ```
48
43
 
49
44
  ### Create an achievement configuration
50
- 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)
45
+
46
+ Create a file (e.g., `achievementConfig.js`) to define your achievements:
47
+
48
+
52
49
  ```javascript
50
+
51
+
53
52
  import image1 from './public/path/to/image1.png';
54
53
  import image2 from './public/path/to/image2.png';
55
54
 
56
55
  const achievementConfig = {
57
- transactions: [
56
+ transactions: [
58
57
  {
59
- check: (value) => value.length >= 1,
60
- data: {
61
- id: 'first_transaction',
62
- title: 'First Transaction',
63
- description: 'Completed your first transaction',
64
- icon: image1
65
- }
58
+ check: (value) => value.length >= 1,
59
+ data: {
60
+ id: 'first_transaction',
61
+ title: 'First Transaction',
62
+ description: 'Completed your first transaction',
63
+ icon: image1
64
+ }
66
65
  },
67
66
  {
68
- check: (value) => values.reduce((sum, val) => sum + (typeof val === 'number' ? val : 0), 0) >= 100,
69
- data: {
70
- id: 'thousand_dollars',
71
- title: 'Big Spender',
72
- description: 'Spent a total of $1000',
73
- icon: image2
74
- }
67
+ check: (value) => value.reduce((sum, transaction) => sum + transaction.amount, 0) >= 1000,
68
+ data: {
69
+ id: 'thousand_dollars',
70
+ title: 'Big Spender',
71
+ description: 'Spent a total of $1000',
72
+ icon: image2
73
+ }
75
74
  },
76
- ],
77
- // Add more metrics and achievements as needed
75
+ ],
76
+ // Add more metrics and achievements as needed, but keys (in this case transactions) must match with those in initialState variable
78
77
  };
78
+ ```
79
79
 
80
80
  export default achievementConfig;
81
- ```
82
81
 
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:
82
+ Note: Ensure your icons are located in the public folder of your project.
83
+
84
+ ### Customize the badges button location
85
+
86
86
  ```javascript
87
- <AchievementProvider config={achievementConfig} badgesButtonPosition="bottom-right">
87
+ <AchievementProvider
88
+ config={achievementConfig}
89
+ badgesButtonPosition="bottom-right"
90
+ >
91
+ {/* Your app components */}
92
+ </AchievementProvider>
88
93
  ```
94
+ Specify the position of the badges button:
95
+ Possible values for `badgesButtonPosition` are:
96
+ - 'top-left'
97
+ - 'top-right'
98
+ - 'bottom-left'
99
+ - 'bottom-right'
100
+
101
+
89
102
 
90
- The possible values for badgesButtonPosition are
91
- - `'top-left'`
92
- - `'top-right'`
93
- - `'bottom-left'`
94
- - `'bottom-right'`
103
+ ### Use the useAchievement hook
104
+
105
+ In your components, use the `useAchievement` hook to update metrics and trigger achievement checks:
95
106
 
96
- ### Use the useAchievement hook
97
- In your components, use the useAchievement hook to update metrics and trigger achievement checks:
98
107
  ```javascript
99
108
  import { useAchievement } from 'react-achievements';
100
109
 
101
110
  function TransactionComponent() {
102
- const { setMetrics } = useAchievement();
103
-
104
- const handleNewTransaction = () => {
105
- // Your transaction logic here
106
- setMetrics(prevMetrics => ({
107
- ...prevMetrics,
108
- transactions: (prevMetrics.transactions || 0) + 1
111
+ const { setMetrics } = useAchievement();
112
+
113
+ const handleNewTransaction = (amount) => {
114
+ setMetrics(prevMetrics => (
115
+ {
116
+ ...prevMetrics,
117
+ transactions: [
118
+ ...prevMetrics.transactions,
119
+ { id: Date.now(), amount }
120
+ ]
109
121
  }));
110
- };
122
+ };
111
123
 
112
- return (
113
- <button onClick={handleNewTransaction}>New Transaction</button>
114
- );
124
+ return (
125
+ <button onClick={() => handleNewTransaction(100)}>New Transaction</button>
126
+ );
115
127
  }
116
128
  ```
117
-
118
129
  ## Features
119
130
 
120
- ### Flexible Achievement System:
121
- Define custom metrics and achievement conditions.
122
-
123
- ### Automatic Achievement Tracking:
124
- Achievements are automatically checked and unlocked when metrics change.
125
-
126
- ### Achievement Notifications:
127
- A modal pops up when an achievement is unlocked.
128
-
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)
134
-
135
- ### Achievement Gallery:
136
- Users can view all their unlocked achievements.
137
-
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.
131
+ - Flexible Achievement System: Define custom metrics and achievement conditions.
132
+ - Automatic Achievement Tracking: Achievements are automatically checked and unlocked when metrics change.
133
+ - Achievement Notifications: A modal pops up when an achievement is unlocked.
134
+ - Persistent Achievements: Unlocked achievements and metrics are stored in local storage.
135
+ - Achievement Gallery: Users can view all their unlocked achievements.
136
+ - Confetti Effect: A celebratory confetti effect is displayed when an achievement is unlocked.
141
137
 
142
138
  ## API
143
- ### AchievementProvider
144
- #### Props:
145
-
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'
149
139
 
150
- 2. useAchievement Hook Returns an object with:
140
+ ### AchievementProvider
151
141
 
152
- 3. setMetrics: Function to update the metrics.
142
+ #### Props:
143
+ - `config`: An object defining your metrics and achievements.
144
+ - `initialState`: The initial state of your metrics.
145
+ - `storageKey` (optional): A string to use as the key for localStorage. Default: 'react-achievements'
146
+ - `badgesButtonPosition` (optional): Position of the badges button. Default: 'top-right'
147
+ - `styles` (optional): Custom styles for the achievement components.
153
148
 
154
- 4. metrics: Current metrics object.
155
- 5. achievedAchievements: Array of achieved achievement IDs.
156
- 6. showBadgesModal: Function to manually show the badges modal.
149
+ ### useAchievement Hook
157
150
 
158
- Customization
151
+ Returns an object with:
152
+ - `setMetrics`: Function to update the metrics.
153
+ - `metrics`: Current metrics object.
154
+ - `unlockedAchievements`: Array of unlocked achievement IDs.
155
+ - `showBadgesModal`: Function to manually show the badges modal.
159
156
 
160
- You can customize the appearance of the achievement modal, badges modal, and badges button by modifying their respective components in the package.
157
+ ## License
161
158
 
162
- License MIT
159
+ MIT
163
160
 
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.
161
+ This package provides a comprehensive achievement system for React applications. It's designed to be flexible, customizable, and easy to integrate into existing projects.
@@ -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 {};