Skip to content

Commit dd9e34a

Browse files
Merge pull request #2 from use-cookie-consent/initial-code-setup
Initial code setup
2 parents 033e465 + 5bba0ee commit dd9e34a

File tree

13 files changed

+12285
-47511
lines changed

13 files changed

+12285
-47511
lines changed

.eslintignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
build
3+
.vscode
4+
.storybook
5+
.github
6+
jest.config.js
7+
jest.setup.js
8+
.prettierrc.js

.eslintrc.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
3+
parserOptions: {
4+
ecmaVersion: 2020,
5+
sourceType: 'module',
6+
ecmaFeatures: {
7+
jsx: true,
8+
},
9+
},
10+
settings: {
11+
react: {
12+
version: 'detect',
13+
},
14+
},
15+
extends: [
16+
'plugin:react/recommended',
17+
'plugin:@typescript-eslint/recommended',
18+
'plugin:prettier/recommended',
19+
],
20+
rules: {},
21+
};

.github/workflows/build-and-test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/setup-node@v2
2626
with:
2727
node-version: ${{ matrix.node-version }}
28-
cache: 'npm'
29-
- run: npm ci
30-
- run: npm run build --if-present
31-
- run: npm test
28+
- run: yarn
29+
- run: yarn build
30+
- run: yarn lint
31+
- run: yarn test
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Publish to NPM and GPM
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
build-and-test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v2
13+
with:
14+
node-version: 14
15+
- run: yarn
16+
- run: yarn lint
17+
- run: yarn build
18+
- run: yarn test
19+
20+
publish-npm:
21+
needs: build-and-test
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v2
25+
- uses: actions/setup-node@v2
26+
with:
27+
node-version: 14
28+
registry-url: https://registry.npmjs.org/
29+
- run: yarn
30+
- run: yarn publish --access public
31+
env:
32+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
33+
34+
publish-gpr:
35+
needs: build-and-test
36+
runs-on: ubuntu-latest
37+
permissions:
38+
contents: read
39+
packages: write
40+
steps:
41+
- uses: actions/checkout@v2
42+
- uses: actions/setup-node@v2
43+
with:
44+
node-version: 14
45+
registry-url: https://npm.pkg.github.com/
46+
- run: yarn
47+
- run: npm publish --access public
48+
env:
49+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

.prettierrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
semi: true,
3+
trailingComma: 'all',
4+
singleQuote: true,
5+
printWidth: 80,
6+
tabWidth: 2,
7+
};

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.fixAll.eslint": true
4+
}
5+
}

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# useCookieConsent hook for React.js
2+
3+
[![Build](https://img.shields.io/github/checks-status/use-cookie-consent/use-cookie-consent-react/main)](https://github.com/use-cookie-consent/use-cookie-consent-react/actions)
4+
[![NPM Version](https://img.shields.io/npm/v/@use-cookie-consent/react)](https://www.npmjs.com/package/@use-cookie-consent/react)
5+
[![NPM Downloads](https://img.shields.io/npm/dm/@use-cookie-consent/react)](https://www.npmjs.com/package/@use-cookie-consent/react)
6+
[![Codecov](https://img.shields.io/codecov/c/github/use-cookie-consent/use-cookie-consent-react)](https://github.com/use-cookie-consent/use-cookie-consent-react/actions/workflows/codecov.yml)
7+
![Lines of code](https://img.shields.io/tokei/lines/github/use-cookie-consent/use-cookie-consent-react)
8+
[![License](https://img.shields.io/npm/l/@use-cookie-consent/react)](https://github.com/use-cookie-consent/use-cookie-consent-react/blob/main/LICENSE)
9+
10+
## Description
11+
12+
This package provides a wrapper around [`@use-cookie-consent/core`](https://github.com/use-cookie-consent/use-cookie-consent-core) package to provide best experience for React applications. Namely, it provides you React context, which provides all the functionality of [core](https://github.com/use-cookie-consent/use-cookie-consent-core) package but with reactive updates.
13+
14+
## Installation
15+
16+
```bash
17+
npm install @use-cookie-consent/react
18+
```
19+
20+
or
21+
22+
```bash
23+
yarn add @use-cookie-consent/react
24+
```
25+
26+
## Usage
27+
28+
### Basic usage
29+
30+
The first step to this setup is to wrap your app in the `CookieConsentProvider`:
31+
32+
```jsx
33+
import { CookieConsentProvider } from '@use-cookie-consent/react'
34+
35+
export default function App() {
36+
return {
37+
<CookieConsentProvider>
38+
...
39+
</CookieConsentProvider>
40+
}
41+
}
42+
```
43+
44+
Then you can read and update the cookie consent data from any component which is inside of the `<CookieConsentProvider>`. Both components you see below are not connected to each other in any way except the `useCookieConsentContext` function. You would put both of them separately somewhere in your app WITHOUT connecting them by any callbacks or passing state through props.
45+
46+
```jsx
47+
// This component is an example of a component that you use to
48+
// accept/decline cookie consent, without reading any data.
49+
const CookieBanner = () => {
50+
const { acceptAllCookies, declineAllCookies, acceptCookies } =
51+
useCookieConsentContext();
52+
53+
return (
54+
<div>
55+
<button onClick={acceptAllCookies}>Accept all</button>
56+
<button onClick={() => acceptCookies({ thirdParty: true })}>
57+
Accept third-party
58+
</button>
59+
<button onClick={() => acceptCookies({ firstParty: true })}>
60+
Accept first-party
61+
</button>
62+
<button onClick={declineAllCookies}>Reject all</button>
63+
</div>
64+
);
65+
};
66+
```
67+
68+
```jsx
69+
// This component is an example of a component which would show
70+
// the state of the cookie consent, without updating anything.
71+
const CookiesPreview = () => {
72+
const { consent } = useCookieConsentContext();
73+
74+
return (
75+
<div>
76+
<div>
77+
{`Third-party cookies ${consent.thirdParty ? 'approved' : 'rejected'}`}
78+
</div>
79+
<div>
80+
{`First-party cookies ${consent.firstParty ? 'approved' : 'rejected'}`}
81+
</div>
82+
</div>
83+
);
84+
};
85+
```
86+
87+
Thanks to @pixelass for prividing the examples you see above.
88+
89+
## Contributors
90+
91+
- [Antoni Silvestrovic](https://github.com/bring-shrubbery)
92+
- [Gregor Adams](https://github.com/pixelass)
93+
94+
## License
95+
96+
[MIT](https://github.com/use-cookie-consent/use-cookie-consent-react/blob/main/LICENSE)

0 commit comments

Comments
 (0)