javascript - 如何修复 NextJS 中的暗模式背景颜色闪烁?

标签 javascript reactjs next.js themes user-experience

所以我的问题是 Next.js 无法访问 localStorage在客户端,因此将发送默认情况下有或没有 class="dark" 的 HTML .

这意味着当用户重新加载页面时,<html>简而言之没有class="dark" ,在执行某些 javascript 和 class="dark" 之前,导致背景颜色闪烁。被添加到 <html> .如果我用 class="dark" 发送 HTML ,同样的问题发生但相反:然后亮模式用户将在 class="dark" 之前体验到暗背景颜色的闪烁。从 <html> 中删除.

有没有办法在页面呈现之前执行一些javascript?然后我就可以添加或不添加 class="dark"<html>基于用户的localStorage .

最佳答案

当然,添加一个 noflash.js 文件到你的 public 目录,包含以下内容

(function () {
    // Change these if you use something different in your hook.
    var storageKey = 'darkMode';
    var classNameDark = 'dark-mode';
    var classNameLight = 'light-mode';

    function setClassOnDocumentBody(darkMode) {
        document.body.classList.add(darkMode ? classNameDark : classNameLight);
        document.body.classList.remove(darkMode ? classNameLight : classNameDark);
    }

    var preferDarkQuery = '(prefers-color-scheme: dark)';
    var mql = window.matchMedia(preferDarkQuery);
    var supportsColorSchemeQuery = mql.media === preferDarkQuery;
    var localStorageTheme = null;
    try {
        localStorageTheme = localStorage.getItem(storageKey);
    } catch (err) {}
    var localStorageExists = localStorageTheme !== null;
    if (localStorageExists) {
        localStorageTheme = JSON.parse(localStorageTheme);
    }

    // Determine the source of truth
    if (localStorageExists) {
        // source of truth from localStorage
        setClassOnDocumentBody(localStorageTheme);
    } else if (supportsColorSchemeQuery) {
        // source of truth from system
        setClassOnDocumentBody(mql.matches);
        localStorage.setItem(storageKey, mql.matches);
    } else {
        // source of truth from document.body
        var isDarkMode = document.body.classList.contains(classNameDark);
        localStorage.setItem(storageKey, JSON.stringify(isDarkMode));
    }
})();

// https://github.com/donavon/use-dark-mode/blob/develop/noflash.js.txt

然后,将以下 script src 标记添加到 pages/_document 文件的 Head 类中包装的返回内容

import Document, {
    Head,
    Html,
    Main,
    NextScript,
    DocumentContext
} from 'next/document';

class MyDocument extends Document {
    static async getInitialProps(ctx: DocumentContext) {
        const initialProps = await Document.getInitialProps(ctx);
        return { ...initialProps };
    }
    render() {
        return (
            <Html lang='en-US'>
                <Head>
                    <meta charSet='utf-8' />
                    <script type="text/javascript" src='/noflash.js' />
                </Head>
                <body className='loading'>
                    <Main />
                    <NextScript />
                </body>
            </Html>
        );
    }
}

export default MyDocument;

上述方法有效,但以下方法与 Nextv10+ 完美配合。它只需要将以下配置添加到您的根 next.config.js 文件。

next.config.js

module.exports = {
  env: {
    noflash: fs.readFileSync('/noflash.js').toString()
  }
}

然后,在您的 pages/_document 文件中更改以下脚本标记,如下所示

之前

//
        <Head>
            <meta charSet='utf-8' />
            <script type="text/javascript" src='/noflash.js' />
        </Head>
//

之后

//
        <Head>
            <meta charSet='utf-8' />
            <script type="text/javascript" dangerouslySetInnerHTML={{ __html: process.env.noflash}} />
        </Head>
//

Link to a repo where I use the first approach (从 2020 年秋季开始,在 tailwindcss 内置暗模式支持之前)

关于javascript - 如何修复 NextJS 中的暗模式背景颜色闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67094919/

相关文章:

javascript - CKEditor - 清除第一个焦点的内容

javascript - 使用 firebase 对数字进行排序

javascript - fabricjs 中对象的上下文菜单

javascript - Reactjs this.refs 与 document.getElementById

reactjs - Next.js getServerSideProps 重定向 ERR_HTTP_HEADERS_SENT 错误

JavaScript 如何比较两个数组之间的值?

reactjs - "This express is not callable. Type ' bool 值 ' has no call signatures"

javascript - React 渲染两个 p5.js Canvas

javascript - NextJS 读取本地存储中已存在的项目时出现问题

javascript - 如何避免在React功能组件中不必要地重新渲染 "static components"?