英文:
Why is adding a comment in an unrelated file affecting the theme of the app in Next.js?
问题
我正在尝试使用 Next.js 创建一个具有浅色和深色主题的应用程序。我正在使用 TailwindCSS 以及 next-themes
。我的系统首选主题默认为深色模式。然而,当我加载页面时,我得到的是浅色主题的背景。检查器显示 html
属性中启用了深色模式。
当我在一个不相关且未导入的文件中添加特定的注释时,主题会更改为正确的主题。
这里有一个最小示例存储库,描述了这个问题。复现问题的步骤:
- 使用以下命令克隆存储库:
git clone https://github.com/captnTardigrade/MRE.git
- 在文件夹中使用 shell 安装依赖项,运行以下命令:
npm install
- 打开终端并运行
npm run dev
- 观察加载的默认背景颜色是否为浅色。
- 在
app/components/foo.tsx
中添加以下注释:// setTheme("dark");
- 观察背景颜色。它应该是深色的。
注意: 在尝试复制上述步骤之前,请确保您的默认系统首选主题是深色模式。
英文:
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:
-
Clone the repository using
git clone https://github.com/captnTardigrade/MRE.git
-
Install the dependencies using a shell by running the following in the folder
npm install
-
Open a terminal and run
npm run dev
-
Observe that the default background color loaded is light in color.
-
Add the following comment in
app/components/foo.tsx
:// setTheme("dark");
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论