【问题标题】:How to use @apply in a react component with css modules如何在带有 css 模块的反应组件中使用 @apply
【发布时间】:2017-02-25 10:34:48
【问题描述】:

我在全局 style.css 中有这个:

:root {
    --font: {
        font-family: "Source Sans Pro", sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
}

... 这在反应组件的 style.css 中:

.rightnav {
    @apply --font;
    float: right;
    color: white;
}

在组件本身中:

import React from "react"
import cssModules from "react-css-modules"
import style from "./style.css"

render() {
  return (
        <ul>
          <li className={style.rightnav}>Blog</li>
        </ul>
  )
}

我收到一个错误:没有为 font 声明自定义属性集。

【问题讨论】:

    标签: css reactjs react-css-modules


    【解决方案1】:

    您必须将全局样式表导入本地样式表。因为 postcss-apply 在解析本地文件时无法知道该全局文件。所以最好有一个只有自定义属性和自定义属性集声明的部分。

    你可能想要postcss-import这样的东西

    @import 'path/to/variables.css';
    
    .rightnav {
      @apply --font;
      float: right;
      color: white;
    }
    

    【讨论】:

    • 如果在要从中导入变量的文件中定义了任何类或元素样式,此技术也会复制这些类/元素样式。如果有办法只导入您需要的 css 属性集而不是导入整个文件,那会很好,但我还没有找到方法......
    猜你喜欢
    • 2017-04-27
    • 2019-04-11
    • 1970-01-01
    • 2017-02-17
    • 2016-10-27
    • 2020-08-29
    • 2017-11-06
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多