Skip to content

Commit 28c4623

Browse files
committed
wip: noflash poc
1 parent 3559a11 commit 28c4623

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

packages/docusaurus-theme-classic/src/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@
77

88
const path = require('path');
99

10+
const noFlash = `(function() {
11+
// Change these if you use something different in your hook.
12+
var storageKey = 'theme';
13+
14+
function setDataThemeAttribute(theme) {
15+
document.querySelector('html').setAttribute('data-theme', theme);
16+
}
17+
18+
var preferDarkQuery = '(prefers-color-scheme: dark)';
19+
var mql = window.matchMedia(preferDarkQuery);
20+
var supportsColorSchemeQuery = mql.media === preferDarkQuery;
21+
var localStorageTheme = null;
22+
try {
23+
localStorageTheme = localStorage.getItem(storageKey);
24+
} catch (err) {}
25+
var localStorageExists = localStorageTheme !== null;
26+
27+
// Determine the source of truth
28+
if (localStorageExists) {
29+
// source of truth from localStorage
30+
setDataThemeAttribute(localStorageTheme);
31+
} else if (supportsColorSchemeQuery) {
32+
// source of truth from system
33+
var theme = mql.matches ? 'dark' : '';
34+
setDataThemeAttribute(theme);
35+
localStorage.setItem(storageKey, theme);
36+
} else {
37+
// source of truth from document
38+
var theme = document.querySelector('html').getAttribute('data-theme');
39+
localStorage.setItem(storageKey, theme);
40+
}
41+
})();`;
42+
1043
module.exports = function(context, options) {
1144
const {customCss} = options || {};
1245
return {
@@ -19,5 +52,19 @@ module.exports = function(context, options) {
1952
getClientModules() {
2053
return ['infima/dist/css/default/default.css', customCss];
2154
},
55+
56+
injectHtmlTags() {
57+
return {
58+
preBodyTags: [
59+
{
60+
tagName: 'script',
61+
attributes: {
62+
type: 'text/javascript',
63+
},
64+
innerHTML: noFlash,
65+
},
66+
],
67+
};
68+
},
2269
};
2370
};

packages/docusaurus-theme-classic/src/theme/Toggle/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@
88
import React from 'react';
99
import Toggle from 'react-toggle';
1010

11+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
12+
1113
import classnames from 'classnames';
1214
import styles from './styles.module.css';
1315

1416
const Moon = () => <span className={classnames(styles.toggle, styles.moon)} />;
1517
const Sun = () => <span className={classnames(styles.toggle, styles.sun)} />;
1618

1719
export default function(props) {
18-
return (
20+
const {isClient} = useDocusaurusContext();
21+
return isClient ? (
1922
<Toggle
2023
icons={{
2124
checked: <Moon />,
2225
unchecked: <Sun />,
2326
}}
2427
{...props}
2528
/>
26-
);
29+
) : null;
2730
}

packages/docusaurus/src/client/App.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React from 'react';
8+
import React, {useState, useEffect} from 'react';
99

1010
import Head from '@docusaurus/Head';
1111
import routes from '@generated/routes';
@@ -17,9 +17,14 @@ import PendingNavigation from './PendingNavigation';
1717
import './client-lifecycles-dispatcher';
1818

1919
function App() {
20+
const [isClient, setIsClient] = useState(false);
2021
const {stylesheets, scripts} = siteConfig;
22+
23+
useEffect(() => {
24+
setIsClient(true);
25+
}, []);
2126
return (
22-
<DocusaurusContext.Provider value={{siteConfig}}>
27+
<DocusaurusContext.Provider value={{siteConfig, isClient}}>
2328
{(stylesheets || scripts) && (
2429
<Head>
2530
{stylesheets &&

0 commit comments

Comments
 (0)