File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11'use client' ;
22
3- import { useCookies } from 'react-cookie' ;
43import classNames from 'classnames' ;
54import React from 'react' ;
65import Button from './Button' ;
76import Link from './Link' ;
87import ReactPortal from './ReactPortal' ;
8+ import { useCookie } from '../hooks' ;
99import type { ThemeToken } from '../types' ;
1010
1111const COOKIE_NAME = 'cookieconsent_status' ;
@@ -28,8 +28,8 @@ export default function CookieConsent({
2828 rootElement = 'body' ,
2929 theme = 'high-contrast' ,
3030} : CookieConsentProps ) {
31- const [ cookies , setCookie ] = useCookies ( [ COOKIE_NAME ] ) ;
32- if ( cookies [ COOKIE_NAME ] === COOKIE_VALUE ) return null ;
31+ const [ cookie , setCookie ] = useCookie ( COOKIE_NAME ) ;
32+ if ( cookie === COOKIE_VALUE ) return null ;
3333 return (
3434 < ReactPortal rootElement = { rootElement } >
3535 < div
@@ -57,7 +57,7 @@ export default function CookieConsent({
5757 aria-label = { buttonText }
5858 className = "cookie-consent__button"
5959 onClick = { ( ) => {
60- setCookie ( COOKIE_NAME , COOKIE_VALUE , { path : '/' } ) ;
60+ setCookie ( COOKIE_VALUE ) ;
6161 onConsent ( true ) ;
6262 } }
6363 shape = "square"
Original file line number Diff line number Diff line change 11export * from './useBodyOverflow' ;
22export * from './useBreakpoint' ;
3+ export * from './useCookie' ;
34export * from './useElementSize' ;
4- export * from './useIsLoaded' ;
55export * from './useIsMounted' ;
66export * from './useIsomorphicLayoutEffect' ;
77export * from './useKeyboardEvent' ;
Original file line number Diff line number Diff line change 1+ import { useCallback , useState } from 'react' ;
2+ import Cookies from 'universal-cookie' ;
3+ import type { Cookie , CookieSetOptions } from 'universal-cookie' ;
4+
5+ export function useCookie (
6+ key : string ,
7+ ) : [
8+ Cookie ,
9+ ( value : Cookie , options ?: CookieSetOptions ) => void ,
10+ ( options ?: CookieSetOptions ) => void ,
11+ ] {
12+ const [ cookies ] = useState ( ( ) => new Cookies ( ) ) ;
13+ const [ cookie , setCookieValue ] = useState < Cookie > ( ( ) => {
14+ const value = cookies . get ( key ) ;
15+ if ( value ) return value ;
16+ return null ;
17+ } ) ;
18+
19+ const setCookie = useCallback (
20+ ( value : Cookie , options ?: CookieSetOptions ) => {
21+ cookies . set ( key , value , options ) ;
22+ setCookieValue ( value ) ;
23+ } ,
24+ [ cookies , key ] ,
25+ ) ;
26+
27+ const removeCookie = useCallback (
28+ ( options ?: CookieSetOptions ) => {
29+ cookies . remove ( key , options ) ;
30+ setCookieValue ( null ) ;
31+ } ,
32+ [ key ] ,
33+ ) ;
34+
35+ return [ cookie , setCookie , removeCookie ] ;
36+ }
You can’t perform that action at this time.
0 commit comments