From 0f7552accb8f1986f48f788a310c1d4e308d7ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Fri, 3 Mar 2023 12:54:35 +0100 Subject: [PATCH] feat(theme): allow to load a Docusaurus page with theme from query-string: ?docusaurus-theme=dark (#8708) --- .../docusaurus-theme-classic/src/index.ts | 16 +++++-- website/_dogfooding/_pages tests/embeds.tsx | 43 +++++++++++++++++++ website/_dogfooding/_pages tests/index.mdx | 1 + website/docs/styling-layout.mdx | 11 +++++ .../src/components/BrowserWindow/index.tsx | 14 ++++-- 5 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 website/_dogfooding/_pages tests/embeds.tsx diff --git a/packages/docusaurus-theme-classic/src/index.ts b/packages/docusaurus-theme-classic/src/index.ts index b85fddbbf2c3..68b4cf4e82bc 100644 --- a/packages/docusaurus-theme-classic/src/index.ts +++ b/packages/docusaurus-theme-classic/src/index.ts @@ -26,6 +26,8 @@ const ContextReplacementPlugin = requireFromDocusaurusCore( // Need to be inlined to prevent dark mode FOUC // Make sure the key is the same as the one in `/theme/hooks/useTheme.js` const ThemeStorageKey = 'theme'; +const ThemeQueryStringKey = 'docusaurus-theme'; + const noFlashColorMode = ({ defaultMode, respectPrefersColorScheme, @@ -39,6 +41,14 @@ const noFlashColorMode = ({ document.documentElement.setAttribute('data-theme', theme); } + function getQueryStringTheme() { + var theme = null; + try { + theme = new URLSearchParams(window.location.search).get('${ThemeQueryStringKey}') + } catch(e) {} + return theme; + } + function getStoredTheme() { var theme = null; try { @@ -47,9 +57,9 @@ const noFlashColorMode = ({ return theme; } - var storedTheme = getStoredTheme(); - if (storedTheme !== null) { - setDataThemeAttribute(storedTheme); + var initialTheme = getQueryStringTheme() || getStoredTheme(); + if (initialTheme !== null) { + setDataThemeAttribute(initialTheme); } else { if ( respectPrefersColorScheme && diff --git a/website/_dogfooding/_pages tests/embeds.tsx b/website/_dogfooding/_pages tests/embeds.tsx new file mode 100644 index 000000000000..36bc06668862 --- /dev/null +++ b/website/_dogfooding/_pages tests/embeds.tsx @@ -0,0 +1,43 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import React from 'react'; +import Layout from '@theme/Layout'; +import Heading from '@theme/Heading'; +import BrowserWindow from '@site/src/components/BrowserWindow'; + +function IframeTest({url}: {url: string}) { + return ( +
+ +