-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cookie consent provider (#555)
* refactor: cookie consent manager * chore: swizzled layout/index * chore: added GlobalLayout * fix: build
- Loading branch information
1 parent
feb7233
commit 41c9bd5
Showing
6 changed files
with
139 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React, {useEffect} from "react" | ||
import {pageLinks} from "@site/src/constants/routes" | ||
import CookieConsentModal from "./CookieConsentModal/CookieConsentModal" | ||
import GlobalHead from "./GlobalHead" | ||
import {useCookieConsentManager} from "./CookieConsentProvider" | ||
|
||
const GlobalLayout: React.FC = () => { | ||
const { | ||
isCookieConsentModalVisible, | ||
openCookieConsentModal, | ||
closeCookieConsentModal, | ||
onAccept, | ||
onDeny, | ||
onPartialAccept, | ||
cookieConsent, | ||
} = useCookieConsentManager() | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined" && window.location.pathname.includes(pageLinks.privacyPolicy)) return | ||
|
||
if (!cookieConsent) { | ||
openCookieConsentModal() | ||
} | ||
}, [cookieConsent]) | ||
|
||
return ( | ||
<> | ||
<CookieConsentModal | ||
open={isCookieConsentModalVisible} | ||
onClose={closeCookieConsentModal} | ||
onAccept={onAccept} | ||
onDeny={onDeny} | ||
onPartialAccept={onPartialAccept} | ||
/> | ||
<GlobalHead isCookieConsentAccepted={Boolean(cookieConsent?.accepted)} preferences={cookieConsent?.preferences} /> | ||
</> | ||
) | ||
} | ||
|
||
export default GlobalLayout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,50 @@ | ||
import React, {useEffect} from "react" | ||
import Layout from "@theme-original/Layout" | ||
import type LayoutType from "@theme/Layout" | ||
import type {WrapperProps} from "@docusaurus/types" | ||
import GlobalHead from "@site/src/components/shared/GlobalHead" | ||
import CookieConsentModal from "@site/src/components/shared/CookieConsentModal/CookieConsentModal" | ||
import {useCookieConsentManager} from "@site/src/utils/hooks/useCookieConsentManager" | ||
import {pageLinks} from "@site/src/constants/routes" | ||
|
||
type Props = WrapperProps<typeof LayoutType> | ||
|
||
export default function LayoutWrapper(props: Props): JSX.Element { | ||
import React from "react" | ||
import clsx from "clsx" | ||
import ErrorBoundary from "@docusaurus/ErrorBoundary" | ||
import {PageMetadata, SkipToContentFallbackId, ThemeClassNames} from "@docusaurus/theme-common" | ||
import {useKeyboardNavigation} from "@docusaurus/theme-common/internal" | ||
import SkipToContent from "@theme/SkipToContent" | ||
import AnnouncementBar from "@theme/AnnouncementBar" | ||
import Navbar from "@theme/Navbar" | ||
import Footer from "@theme/Footer" | ||
import LayoutProvider from "@theme/Layout/Provider" | ||
import ErrorPageContent from "@theme/ErrorPageContent" | ||
import type {Props} from "@theme/Layout" | ||
import styles from "./styles.module.css" | ||
import GlobalLayout from "@site/src/components/shared/GlobalLayout" | ||
|
||
export default function Layout(props: Props): JSX.Element { | ||
const { | ||
isCookieConsentModalVisible, | ||
openCookieConsentModal, | ||
closeCookieConsentModal, | ||
onAccept, | ||
onDeny, | ||
onPartialAccept, | ||
cookieConsent, | ||
} = useCookieConsentManager() | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined" && window.location.pathname.includes(pageLinks.privacyPolicy)) return | ||
|
||
if (!cookieConsent) { | ||
openCookieConsentModal() | ||
} | ||
}, [cookieConsent]) | ||
children, | ||
noFooter, | ||
wrapperClassName, | ||
// Not really layout-related, but kept for convenience/retro-compatibility | ||
title, | ||
description, | ||
} = props | ||
|
||
useKeyboardNavigation() | ||
|
||
return ( | ||
<> | ||
<CookieConsentModal | ||
open={isCookieConsentModalVisible} | ||
onClose={closeCookieConsentModal} | ||
onAccept={onAccept} | ||
onDeny={onDeny} | ||
onPartialAccept={onPartialAccept} | ||
/> | ||
<GlobalHead isCookieConsentAccepted={Boolean(cookieConsent?.accepted)} preferences={cookieConsent?.preferences} /> | ||
<Layout {...props} /> | ||
</> | ||
<LayoutProvider> | ||
<GlobalLayout /> | ||
|
||
<PageMetadata title={title} description={description} /> | ||
|
||
<SkipToContent /> | ||
|
||
<AnnouncementBar /> | ||
|
||
<Navbar /> | ||
|
||
<div | ||
id={SkipToContentFallbackId} | ||
className={clsx(ThemeClassNames.wrapper.main, styles.mainWrapper, wrapperClassName)} | ||
> | ||
<ErrorBoundary fallback={(params) => <ErrorPageContent {...params} />}>{children}</ErrorBoundary> | ||
</div> | ||
|
||
{!noFooter && <Footer />} | ||
</LayoutProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
html, | ||
body { | ||
height: 100%; | ||
} | ||
|
||
.mainWrapper { | ||
flex: 1 0 auto; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
/* Docusaurus-specific utility class */ | ||
:global(.docusaurus-mt-lg) { | ||
margin-top: 3rem; | ||
} | ||
|
||
:global(#__docusaurus) { | ||
min-height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters