如果我在特定的主体类上引用它,则 CSS 模块声明不起作用

Nag*_*Nag 4 javascript reactjs gatsby

我是 React 和 Gatsby.js 的新手。我正在用 Gatsby 建立一个博客,我的博客有一个在明暗模式之间切换的开关。我通过<body>使用 JavaScript在标签上切换一个类来实现这一点。本质上,如果是浅色模式,标签将是<body>,如果是深色模式,标签将是<body class="darkMode">

通过使用 vanilla JavaScript 设置和删除标签.darkMode上的类,我设法让它正常工作<body>。但是,我在基于body.darkModeCSS 模块设置样式元素时遇到了一些麻烦。

在我的 index.module.css 文件中,我有以下类:

.section {
  color: #141414;
}

body.darkMode .section {
  color: #ebebeb;
}
Run Code Online (Sandbox Code Playgroud)

在我的 index.js 组件中,我导入了 CSS 模块并应用了这样的样式:

<section className={indexStyles.section}></section>

虽然.section类中的 color 属性有效,但我注意到body.darkMode .section当我将.darkModeCSS 类添加到<body>标签时声明无效。为什么是这样?帮助表示赞赏。谢谢。

Nag*_*Nag 6

我想出了解决办法。事实证明,Gatsby 正在将 编译body.darkMode .section成一些任意的类名。实际上,我希望它忽略body.darkMode但引用本地生成的.section.

我在这里找到了答案 - https://github.com/webpack-contrib/css-loader#scope。我能够通过将原始 CSS 转换为以下内容来解决我的特定问题:

.section {
  color: #141414;
}

body:global(.darkMode) :local(.section) {
  color: #ebebeb;
}
Run Code Online (Sandbox Code Playgroud)

如您所见, :global 和 :local 选择器允许您正确定位 HTML。


小智 1

如果你想在深色和浅色模式之间切换,请参阅此代码

 var checkbox = document.querySelector('input[name=mode]');

        checkbox.addEventListener('change', function() {
            if(this.checked) {
                trans()
                document.documentElement.setAttribute('data-theme', 'dark')
            } else {
                trans()
                document.documentElement.setAttribute('data-theme', 'light')
            }
        })

        let trans = () => {
            document.documentElement.classList.add('transition');
            window.setTimeout(() => {
                document.documentElement.classList.remove('transition');
            }, 1000)
        }
Run Code Online (Sandbox Code Playgroud)
@import url("https://fonts.googleapis.com/css?family=Poppins:300,400,900&display=swap");
html {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: var(--lightBg);
  --lightBtn: #FE016C;
  --lightBg: #fff;
  --lightColor: #232323;
}

html[data-theme='dark'] {
  background: var(--lightBg);
  --lightBtn: #FFBD07;
  --lightBg: #232323;
  --lightColor: #fff;
}

h1 {
  font-family: 'Poppins', sans-serif;
  font-weight: 300;
  color: var(--lightColor);
}

input[type=checkbox] {
  height: 0;
  width: 0;
  visibility: hidden;
}

label {
  cursor: pointer;
  text-indent: -9999px;
  width: 55px;
  height: 30px;
  background: var(--lightBtn);
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
  -webkit-border-radius: 100px;
  -moz-border-radius: 100px;
  border-radius: 100px;
  position: relative;
}

label:after {
  content: '';
  background: #fff;
  width: 20px;
  height: 20px;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  border-radius: 50%;
  position: absolute;
  top: 5px;
  left: 4px;
  -webkit-transition: ease-in-out 200ms;
  -moz-transition: ease-in-out 200ms;
  -ms-transition: ease-in-out 200ms;
  -o-transition: ease-in-out 200ms;
  transition: ease-in-out 200ms;
}

input:checked + label {
  background: #FFBD07;
}

input:checked + label:after {
  left: calc(100% - 5px);
  -webkit-transform: translateX(-100%);
  -moz-transform: translateX(-100%);
  -ms-transform: translateX(-100%);
  -o-transform: translateX(-100%);
  transform: translateX(-100%);
}

html.transition,
html.transition *,
html.transition *:before,
html.transition *:after {
  -webkit-transition: ease-in-out 200ms !important;
  -moz-transition: ease-in-out 200ms !important;
  -ms-transition: ease-in-out 200ms !important;
  -o-transition: ease-in-out 200ms !important;
  transition: ease-in-out 200ms !important;
  transition-delay: 0 !important;
}
Run Code Online (Sandbox Code Playgroud)
 <div class="container">
        <h1>Light / Dark Mode</h1>

        <input class="container_toggle" type="checkbox" id="switch" name="mode">
        <label for="switch">Toggle</label>
    </div>
Run Code Online (Sandbox Code Playgroud)