Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable cookie banner when the docs site is embedded #255

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const config = {
},
],

clientModules: [require.resolve("./src/clientModules/embed.ts")],

plugins: [
"docusaurus-plugin-sass",
[
Expand Down
28 changes: 28 additions & 0 deletions src/clientModules/embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";

export interface DocsConfigWindow extends Window {
displayConfig?: {
cookieBanner: {
forceHide: boolean;
};
};
}

if (ExecutionEnvironment.canUseDOM) {
document.addEventListener("DOMContentLoaded", function () {
const urlParams = new URLSearchParams(window.location.search);
const isEmbedded = urlParams.get("embedded");
if (!isEmbedded) {
return;
}

console.log("embedded mode detected");

(window as DocsConfigWindow).displayConfig = {
...(window as DocsConfigWindow).displayConfig,
cookieBanner: {
forceHide: true,
},
};
});
}
7 changes: 6 additions & 1 deletion src/components/CookieBanner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import React, { FC } from "react";
import { Link } from "react-router-dom";
import CookieBanner, { Cookies } from "react-cookie-banner";
import styles from "./index.module.scss";
import { DocsConfigWindow } from "@site/src/clientModules/embed";

export const CookiesMessageBanner: FC = () => {
const cookies = new Cookies();

if (!cookies || cookies.get("accepts-cookies")) {
if (
!cookies ||
cookies.get("accepts-cookies") ||
(window as DocsConfigWindow).displayConfig?.cookieBanner.forceHide
) {
return null;
}

Expand Down
Loading