1
1
# 🍪 react-cookify
2
2
3
- ![ NPM Downloads] ( https://img.shields.io/npm/dt/react-cookify )
4
- ![ GitHub package.json version] ( https://img.shields.io/github/package-json/v/jersyfi/react-cookify )
3
+ ![ npm] ( https://img.shields.io/npm/dm/react-cookify )
5
4
![ NPM] ( https://img.shields.io/npm/v/react-cookify )
6
5
![ Github Code Size] ( https://img.shields.io/github/languages/code-size/jersyfi/react-cookify )
7
6
![ npm bundle size] ( https://img.shields.io/bundlephobia/min/react-cookify )
@@ -16,207 +15,13 @@ How to handle GDPR correctly can be found on [Cookies, the GDPR, and the ePrivac
16
15
View and test how Cookify works in either [ Bootstrap] ( https://jersyfi.github.io/cookify/test/preview/bootstrap.html ) or [ Tailwind CSS] ( https://jersyfi.github.io/cookify/test/preview/tailwindcss.html ) .
17
16
18
17
## Documentation
19
-
20
- ### Get started
21
-
22
- #### Installation
23
- Install ` react-cookify ` via npm.
18
+ react-cookify is essentially a set of npm packages.
24
19
25
20
``` bash
26
21
npm install react-cookify
27
22
```
28
23
29
- #### Add Cookify to your App
30
- Start with adding the ` CookifyProvider ` to your App.
31
-
32
- ``` javascript
33
- import ' ../styles/globals.css'
34
- import { CookifyProvider } from ' react-cookify'
35
-
36
- export default function App ({ Component, pageProps }) {
37
- return (
38
- < CookifyProvider options= {{
39
- name: ' own-cookify-consent' ,
40
- types: {
41
- marketing: false ,
42
- performance : false ,
43
- },
44
- jscookie: {
45
- expires: 365 ,
46
- path: ' /' ,
47
- secure: true ,
48
- }
49
- }}>
50
- < Component {... pageProps} / >
51
- < / CookifyProvider>
52
- )
53
- }
54
- ```
55
-
56
- Now add the logic to your Home page.
57
-
58
- ``` javascript
59
- import { useEffect , useState } from ' react'
60
- import { useCookifyProvider , CookifyInput } from ' react-cookify'
61
-
62
- export default function Home () {
63
- const {consentObject , consentDisplayed , handleConsentDisplayedChange , consentTracking , actionAccept , actionNecessary , actionAll } = useCookifyProvider ()
64
- const [displayedClass , setDisplayedClass ] = useState (' ' )
65
-
66
- /* CSS not provided in example */
67
- const handleToggle = () => {
68
- if (consentDisplayed) {
69
- setDisplayedClass (' block' ) // block => display: block
70
- } else {
71
- setDisplayedClass (' hidden' ) // hidden => display: none
72
- }
73
- }
74
-
75
- /* Display the consent management */
76
- useEffect (() => {
77
- handleToggle ()
78
- }, [consentDisplayed])
79
-
80
- /* Track the data if needed */
81
- useEffect (() => {
82
- if (consentTracking !== 0 ) { // Only track after initialization
83
- console .log (' Track data here' , consentObject)
84
- }
85
- }, [consentTracking])
86
-
87
- return (
88
- <>
89
- < button onClick= {() => handleConsentDisplayedChange (true )}>
90
- Open Modal
91
- < / button>
92
-
93
- < div className= {displayedClass}>
94
- < div>
95
- < CookifyInput type= " checkbox" name= " necessary" id= " necessary" disabled / >
96
- < label htmlFor= " necessary" > Necessary< / label>
97
-
98
- < CookifyInput type= " checkbox" name= " marketing" id= " marketing" / >
99
- < label htmlFor= " marketing" > Marketing< / label>
100
-
101
- < CookifyInput type= " checkbox" name= " performance" id= " performance" / >
102
- < label htmlFor= " performance" > Performance < / label>
103
- < / div>
104
-
105
- < div>
106
- < button onClick= {actionAll}> All< / button>
107
- < button onClick= {actionAccept}> Accept< / button>
108
- < button onClick= {actionNecessary}> Necessary< / button>
109
- < / div>
110
- < / div>
111
- < / >
112
- )
113
- }
114
- ```
115
-
116
- #### Configure Cookify
117
- All configuration options with its default values.
118
-
119
- ``` typescript
120
- name : ' cookify-consent' ,
121
- storage : ' cookies' , // ['cookies' and 'storage']
122
- saveWithChange : false ,
123
- saveByDefault : false ,
124
- typeDefault : ' necessary' ,
125
- types : {
126
- // `necessary: true` will be set automatically
127
- // if `typeDefault` is empty, otherwise it will use the customized `typeDefault` instead of `necessary`
128
- },
129
- // js-cookie attributes
130
- // Only needed when using `storage: 'cookies'`
131
- // More information on https://github.com/js-cookie/js-cookie
132
- jscookie : {
133
- expires : 365 ,
134
- path : ' /' ,
135
- }
136
- ```
137
-
138
- ### Provider interaction
139
-
140
- #### States
141
-
142
- ##### consentObject
143
- ** Type:** consentObject: ConsentObjectType
144
- ** Syntax:** consentObject
145
- ``` bash
146
- console.log(consentObject)
147
-
148
- > Object: {
149
- viewed: false,
150
- data: {
151
- ' necessary' : true,
152
- ' marketing' : false,
153
- ' performance' : false,
154
- },
155
- }
156
- ```
157
-
158
- ##### consentDisplayed
159
- ** Type:** consentDisplayed: boolean
160
- ** Syntax:** consentDisplayed
161
- ``` bash
162
- console.log(consentDisplayed)
163
-
164
- > false
165
- ```
166
-
167
- ##### handleConsentDisplayedChange
168
- ** Type:** handleConsentDisplayedChange: (newConsentDisplayed: boolean) => void
169
- ** Syntax:** handleConsentDisplayedChange(false)
170
- ``` javascript
171
- < button onClick= {() => handleConsentDisplayedChange (true )}>
172
- Open Modal
173
- < / button>
174
- ```
175
-
176
- ##### consentTracking
177
- ** Type:** consentTracking: number
178
- ** Syntax:** consentTracking
179
- ``` bash
180
- console.log(consentTracking)
181
-
182
- > 0
183
- ```
184
-
185
- #### Actions
186
-
187
- ##### actionCheckbox
188
- ** Type:** actionCheckbox: (type: string) => void
189
- ** Syntax:** actionCheckbox('type')
190
- ``` javascript
191
- < input
192
- type= " checkbox"
193
- name= " marketing"
194
- id= " marketing"
195
- checked= {consentObject .data [' marketing' ]}
196
- onChange= {() => actionCheckbox (' marketing' )}
197
- / >
198
- ```
199
-
200
- ##### actionAccept
201
- ** Type:** actionAccept: () => void
202
- ** Syntax:** actionAccept
203
- ``` javascript
204
- < button onClick= {actionAccept}> Accept< / button>
205
- ```
206
-
207
- ##### actionNecessary
208
- ** Type:** actionNecessary: () => void
209
- ** Syntax:** actionNecessary
210
- ``` javascript
211
- < button onClick= {actionNecessary}> Necessary< / button>
212
- ```
213
-
214
- ##### actionAll
215
- ** Type:** actionAll: () => void
216
- ** Syntax:** actionAll
217
- ``` javascript
218
- < button onClick= {actionAll}> All< / button>
219
- ```
24
+ To view the complete documentation, please follow the link to [ Cookify Docs] ( https://cookify.jersyfi.dev/ ) .
220
25
221
26
## Framework Support & Features
222
27
You can view all library languages and supported features [ here] ( https://github.com/Jersyfi/cookify#framework-support--features ) .
0 commit comments