Skip to content

Commit

Permalink
Add custom hash property (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskupczyk authored Oct 10, 2023
1 parent bfca683 commit ed259b7
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { ConsentProvider } from 'react-hook-consent';
mandatory: true,
},
],
// customHash: 'my-custom-hash', // optional, e.g. when changing the options based on language
theme: 'light',
}}
>
Expand All @@ -75,10 +76,11 @@ import { ConsentProvider } from 'react-hook-consent';

#### Options Prop

| Name | Type | Required | Description | Default |
| -------- | ----------------------- | -------- | ----------------------------------------------- | ------- |
| services | ConsentOptionsService[] | yes | The configuration of the services to be covered | |
| theme | Theme | | Configuration of light or dark theme | dark |
| Name | Type | Required | Description | Default |
| ---------- | ----------------------- | -------- | ---------------------------------------------------- | ------- |
| services | ConsentOptionsService[] | yes | The configuration of the services to be covered | |
| customHash | string | | A custom hash to detect if options have been updated | |
| theme | Theme | | Configuration of light or dark theme | dark |

The `services` array can be configured as follows:

Expand Down
1 change: 1 addition & 0 deletions examples/next-v13/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const consentOptions: ConsentOptions = {
mandatory: true,
},
],
// customHash: 'my-custom-hash',
};

export default function App({ Component, pageProps }: AppProps) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-hook-consent",
"version": "3.3.0",
"version": "3.4.0",
"description": "React consent management solution and banner for cookies and (external) scripts.",
"scripts": {
"build": "node esbuild.config.mjs production",
Expand Down
1 change: 1 addition & 0 deletions src/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type ConsentOptionsService = {

export type ConsentOptions = {
services: ConsentOptionsService[];
customHash?: string;
theme?: Theme;
};

Expand Down
3 changes: 1 addition & 2 deletions src/core/local-storage/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export function getFromLocalStorage(hash: string) {
const { consent, hash: storedHash } = JSON.parse(item) as StoredState;

let isBannerVisible = false;
let isDetailsVisible = false;
if (storedHash !== hash) {
isBannerVisible = true;
}

return { consent: consent && consent.length > 0 ? consent : [], isBannerVisible, isDetailsVisible };
return { consent: consent && consent.length > 0 ? consent : [], isBannerVisible, isDetailsVisible: false };
}
2 changes: 1 addition & 1 deletion src/useConsentState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function useConsentState(options: ConsentOptions) {
consent: [],
isBannerVisible: false,
isDetailsVisible: false,
hash: hash(options),
hash: options.customHash ?? hash(options),
});

useEffect(() => {
Expand Down

0 comments on commit ed259b7

Please sign in to comment.