77
88const 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+
1043module . 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} ;
0 commit comments