简体   繁体   English

SASS 在 Next.JS CSS 模块中无法正常工作

[英]SASS not working properly within Next.JS CSS modules

For the first time, I'm using Next.JS's CSS modules, and they are very useful.这是我第一次使用 Next.JS 的 CSS 模块,它们非常有用。

But I'm experiencing a strange behaviour when trying to apply styles to children elements.但是当我尝试将 styles 应用于子元素时,我遇到了一种奇怪的行为。

For example, if I have the following HTML例如,如果我有以下 HTML

<Offcanvas show={menu} onHide={closeMenu} className={styles.mobile_menu}>
    <Offcanvas.Header closeButton className={styles.mobile_menu_header}>
        <Offcanvas.Title className={styles.mobile_menu_title}>
            <h1>Menu</h1>
        </Offcanvas.Title>
    </Offcanvas.Header>
    <Offcanvas.Body>
        <ul className="list-unstyled">
            { links.map((link, key) => (
                <li key={"nav-link-" + key} className="mb-3">
                    <Link href={link.href} className={[styles.link, router.pathname == link.href ? "active" : ""].join(" ")}>{ link.label }</Link>
                </li>
            )) }
        </ul>
    </Offcanvas.Body>
</Offcanvas>

And this SCSS而这个 SCSS

.mobile_menu {
    background-color: rgb(57, 70, 78) !important;
    color: #fff !important;
}

.mobile_menu_header {
    border-bottom: solid 1px rgb(148, 162, 170);

    button.btn-close {
        filter: brightness(0) invert(1) !important;
        opacity: 1 !important;
        width: 1.6rem !important;
        height: 1.6rem !important;
        background-size: 100% !important;
    }
}

.mobile_menu_title {
    h1 {
        font-size: 3.2rem;
    }
}

.link {
    color: #fff;
    text-transform: uppercase;
    font-size: 1.7rem;
    transition: color 0.3s ease-in-out;

    &.active {
        border-bottom: solid 1rem rgb(148, 162, 170) !important;
    }

    &:hover {
        color: rgb(148, 162, 170);
    }
}

The properties applyed to the button.btn-close (that's auto generated by adding the closeButton flag to Offcanvas.Header ) aren't applyed, also links with active class hasn't a bottom border as expected.应用于button.btn-close的属性(通过将closeButton标志添加到Offcanvas.Header自动生成)未应用,与active class 的链接也没有预期的底部边框。

So this makes me think the SASS in Next.JS's CSS modules aren't using nested/child/ selectors.所以这让我觉得 Next.JS 的 CSS 模块中的 SASS 没有使用嵌套/子/选择器。 But, for some reason, the h1 inside .mobile_menu_title is styled, and, when I try to remove the .btn-close from selector (leaving only the button ), it's applied!但是,由于某种原因, .mobile_menu_title中的h1被设置了样式,并且当我尝试从选择器中删除.btn-close (仅保留button )时,它被应用了!

So, is this a bug or something I don't know about?那么,这是错误还是我不知道的事情? Nested styles using SASS only work with "pure" elements, not to selectors that have classes/IDs.使用 SASS 的嵌套 styles 仅适用于“纯”元素,不适用于具有类/ID 的选择器。

Can someone help me with this?有人可以帮我弄这个吗?

Thanks!谢谢!

Note: I'm using react-bootstrap package.注意:我使用的是 react-bootstrap package。

Well, what you can do is the following那么,你可以做的是以下

In your JavaScript file在您的 JavaScript 文件中


...

<Offcanvas.Header closeButton className={styles.mobile_menu_header}>

  ...

</Offcanvas.Header>

...

In your style file在你的样式文件中

.mobile_menu_header {
  border-bottom: solid 1px rgb(148, 162, 170);
  :global {
    button.btn-close {
      filter: brightness(0) invert(1) !important;
      opacity: 1 !important;
      width: 1.6rem !important;
      height: 1.6rem !important;
      background-size: 100% !important;
    }
  }
}

Why this?为什么这个?

By adding the :global rule, you are, basically, telling your React application: "In the element with computed class name mobile_menu_header (the actual class name will be like index_mobile_menu_header_xxxxxxxx ), find the element with class name btn-close and apply the style specified".通过添加:global规则,你基本上是在告诉你的 React 应用程序:“在计算出class 名称的元素中mobile_menu_header (实际的 class 名称将类似于index_mobile_menu_header_xxxxxxxx ),找到名称为 class 的元素btn-close并应用样式指定的”。

Wrapping everything in a mobile_menu_header , as you did, can be helpful to avoid overwriting btn-close in the whole application and have them just overwritten in this component.像您所做的那样,将所有内容包装在mobile_menu_header中,有助于避免覆盖整个应用程序中的btn-close并让它们在此组件中被覆盖。

This can be a strategy to adapt when the only way to overwrite a CSS library is to overwrite the styles themself (so if you can not customize the library in other ways like styled-components or theme settings) but you also need to face with CSS modules (the one that are causing the name to be like当覆盖 CSS 库的唯一方法是自己覆盖 styles 时,这可能是一种适应策略(因此,如果您不能通过样式组件或主题设置等其他方式自定义库),但您还需要面对 CSS模块(导致名称类似于instead of just the class name).而不仅仅是 class 名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM