Skip to content

Commit

Permalink
feat(theme): allow to load a Docusaurus page with theme from query-st…
Browse files Browse the repository at this point in the history
…ring: ?docusaurus-theme=dark (#8708)
  • Loading branch information
slorber committed Mar 3, 2023
1 parent 50b1dc7 commit 0f7552a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
16 changes: 13 additions & 3 deletions packages/docusaurus-theme-classic/src/index.ts
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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 &&
Expand Down
43 changes: 43 additions & 0 deletions 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 (
<div style={{padding: 10}}>
<BrowserWindow
url={url}
style={{minWidth: '40vw', maxWidth: 400}}
bodyStyle={{padding: 0}}>
<iframe src={url} title={url} style={{width: '100%', height: 300}} />
</BrowserWindow>
</div>
);
}

// See https://github.com/facebook/docusaurus/issues/8672
export default function Embeds(): JSX.Element {
return (
<Layout>
<div style={{padding: 10}}>
<Heading as="h1">Test Embeds</Heading>
<div style={{display: 'flex', flexWrap: 'wrap'}}>
<IframeTest url="/?docusaurus-theme=light" />
<IframeTest url="/?docusaurus-theme=dark" />
<IframeTest url="/?docusaurus-theme=unexpected-value" />
<IframeTest url="/" />
<IframeTest url="https://docusaurus.io/" />
<IframeTest url="https://tutorial.docusaurus.io/" />
</div>
</div>
</Layout>
);
}
1 change: 1 addition & 0 deletions website/_dogfooding/_pages tests/index.mdx
Expand Up @@ -33,3 +33,4 @@ import Readme from "../README.mdx"
- [Head metadata tests](/tests/pages/head-metadata)
- [Unlisted page](/tests/pages/unlisted)
- [Analytics](/tests/pages/analytics)
- [Embeds](/tests/pages/embeds)
11 changes: 11 additions & 0 deletions website/docs/styling-layout.mdx
Expand Up @@ -127,6 +127,17 @@ In light mode, the `<html>` element has a `data-theme="light"` attribute; in dar
}
```

:::tip

It is possible to initialize the Docusaurus theme directly from a `docusaurus-theme` query string parameter.

Examples:

- [`https://docusaurus.io/?docusaurus-theme=dark`](https://docusaurus.io/?docusaurus-theme=dark)
- [`https://docusaurus.io/docs/configuration?docusaurus-theme=light`](https://docusaurus.io/docs/configuration?docusaurus-theme=light)

:::

### Mobile View {#mobile-view}

Docusaurus uses `996px` as the cutoff between mobile screen width and desktop. If you want your layout to be different in the mobile view, you can use media queries.
Expand Down
14 changes: 10 additions & 4 deletions website/src/components/BrowserWindow/index.tsx
Expand Up @@ -5,24 +5,28 @@
* LICENSE file in the root directory of this source tree.
*/

import React, {type ReactNode} from 'react';
import React, {type CSSProperties, type ReactNode} from 'react';
import clsx from 'clsx';

import styles from './styles.module.css';

interface Props {
children: ReactNode;
minHeight: number;
minHeight?: number;
url: string;
style?: CSSProperties;
bodyStyle?: CSSProperties;
}

export default function BrowserWindow({
children,
minHeight,
url = 'http://localhost:3000',
style,
bodyStyle,
}: Props): JSX.Element {
return (
<div className={styles.browserWindow} style={{minHeight}}>
<div className={styles.browserWindow} style={{...style, minHeight}}>
<div className={styles.browserWindowHeader}>
<div className={styles.buttons}>
<span className={styles.dot} style={{background: '#f25f58'}} />
Expand All @@ -41,7 +45,9 @@ export default function BrowserWindow({
</div>
</div>

<div className={styles.browserWindowBody}>{children}</div>
<div className={styles.browserWindowBody} style={bodyStyle}>
{children}
</div>
</div>
);
}

0 comments on commit 0f7552a

Please sign in to comment.