react-achievements 1.2.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 +91 -94
- package/dist/components/AchievementModal.d.ts +3 -1
- package/dist/components/BadgesButton.d.ts +2 -0
- package/dist/components/BadgesModal.d.ts +3 -1
- package/dist/components/ConfettiWrapper.d.ts +0 -2
- package/dist/context/AchievementContext.d.ts +3 -1
- package/dist/defaultStyles.d.ts +28 -0
- package/dist/index.cjs.js +191 -109
- package/dist/index.esm.js +191 -109
- package/dist/types.d.ts +6 -7
- package/dist/utils/EventEmitter.d.ts +6 -0
- package/package.json +1 -1
- package/src/components/AchievementModal.tsx +13 -34
- package/src/components/BadgesButton.tsx +5 -11
- package/src/components/BadgesModal.tsx +15 -39
- package/src/components/ConfettiWrapper.tsx +2 -10
- package/src/context/AchievementContext.tsx +72 -35
- package/src/defaultStyles.ts +138 -0
- package/src/types.ts +8 -10
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
|
-
|
|
7
|
+
Install react-achievements using npm or yarn:
|
|
8
8
|
|
|
9
9
|
`npm install react-achievements`
|
|
10
10
|
|
|
11
|
-
|
|
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
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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
|
-
|
|
51
|
-
|
|
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
|
-
|
|
56
|
+
transactions: [
|
|
58
57
|
{
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
124
|
+
return (
|
|
125
|
+
<button onClick={() => handleNewTransaction(100)}>New Transaction</button>
|
|
126
|
+
);
|
|
115
127
|
}
|
|
116
128
|
```
|
|
117
|
-
|
|
118
129
|
## Features
|
|
119
130
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
140
|
+
### AchievementProvider
|
|
151
141
|
|
|
152
|
-
|
|
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
|
-
|
|
155
|
-
5. achievedAchievements: Array of achieved achievement IDs.
|
|
156
|
-
6. showBadgesModal: Function to manually show the badges modal.
|
|
149
|
+
### useAchievement Hook
|
|
157
150
|
|
|
158
|
-
|
|
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
|
-
|
|
157
|
+
## License
|
|
161
158
|
|
|
162
|
-
|
|
159
|
+
MIT
|
|
163
160
|
|
|
164
|
-
This
|
|
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
|
-
|
|
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
|
-
|
|
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,9 +1,10 @@
|
|
|
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;
|
|
6
|
-
|
|
7
|
+
unlockedAchievements: string[];
|
|
7
8
|
checkAchievements: () => void;
|
|
8
9
|
showBadgesModal: () => void;
|
|
9
10
|
}
|
|
@@ -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 {};
|