为什么在一个无关的文件中添加注释会影响 Next.js 应用程序的主题?

huangapple go评论74阅读模式
英文:

Why is adding a comment in an unrelated file affecting the theme of the app in Next.js?

问题

我正在尝试使用 Next.js 创建一个具有浅色和深色主题的应用程序。我正在使用 TailwindCSS 以及 next-themes。我的系统首选主题默认为深色模式。然而,当我加载页面时,我得到的是浅色主题的背景。检查器显示 html 属性中启用了深色模式。

当我在一个不相关且未导入的文件中添加特定的注释时,主题会更改为正确的主题。

这里有一个最小示例存储库,描述了这个问题。复现问题的步骤:

  1. 使用以下命令克隆存储库:
    git clone https://github.com/captnTardigrade/MRE.git
    
  2. 在文件夹中使用 shell 安装依赖项,运行以下命令:
    npm install
    
  3. 打开终端并运行 npm run dev
  4. 观察加载的默认背景颜色是否为浅色。
  5. app/components/foo.tsx 中添加以下注释:
    // setTheme("dark");
    
  6. 观察背景颜色。它应该是深色的。

注意: 在尝试复制上述步骤之前,请确保您的默认系统首选主题是深色模式。

英文:

I'm trying to implement a Next.js application with light and dark themes. I'm using TailwindCSS along with next-themes. My system preference theme is dark mode by default. However, when I load the page, I get the light themed background. The inspector shows that dark mode is enabled in the html attribute.

When I add a particular comment in an unrelated and un-imported file, the theme changes to the correct one.

Here's a minimal example repository that describes the issue. Steps to reproduce the problem:

  1. Clone the repository using

    git clone https://github.com/captnTardigrade/MRE.git
    
  2. Install the dependencies using a shell by running the following in the folder

    npm install
    
  3. Open a terminal and run npm run dev

  4. Observe that the default background color loaded is light in color.

  5. Add the following comment in app/components/foo.tsx:

     // setTheme("dark");
    
  6. Observe the background color. It should be dark.

> NOTE: Ensure that your default system preference is dark mode before trying to replicate the above steps.

答案1

得分: 1

在构建项目时,我们收到以下警告。

警告 - 在您的源文件中未检测到任何实用程序类。

在您的 globals.css 文件中,您将 .dark 类放在 @layer base 中,但由于在任何源文件中都找不到 dark,它不会包含在最终输出中。

当您在 app/components/foo.tsx 中添加注释时,Tailwind 会检测到 dark 类。

这在文档中有解释。

https://tailwindcss.com/docs/content-configuration#class-detection-in-depth

> Tailwind扫描您的源代码以查找类的方式是故意非常简单的——实际上我们不解析或执行您所编写的代码,只是使用正则表达式提取可能是类名的每个字符串。

英文:

When building the project we get the following warning.

warn - No utility classes were detected in your source files.

In your globals.css file you have put the .dark class inside an @layer base but as dark is not found in any source files it is not included in the final output.

When you add the comment in app/components/foo.tsx Tailwind detects the dark class.

This is explained in the documentation.

https://tailwindcss.com/docs/content-configuration#class-detection-in-depth

> The way Tailwind scans your source code for classes is intentionally very simple — we don’t actually parse or execute any of your code in the language it’s written in, we just use regular expressions to extract every string that could possibly be a class name.

huangapple
  • 本文由 发表于 2023年6月22日 01:18:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定