简体   繁体   English

在 Next.js 中为 styled-jsx 动态添加样式

[英]Dynamically add styles to styled-jsx in Next.js

Please see the code.请看代码。 I want to add some extra style for the about link conditionally, so I am adding it inside ${} of the template string for the tag.我想有条件地为 about 链接添加一些额外的样式,所以我将它添加到标签模板字符串的 ${} 中。 This doesn't apply the new styles.这不适用于新样式。

I have tried by concatenating the template strings.我尝试连接模板字符串。 That didn't work either.那也没有用。

I cannot set style={style} in the tag because then I won't be able to use the :hover style.我无法在标签中设置 style={style} ,因为那样我将无法使用 :hover 样式。

import Link from 'next/link'
import { withRouter } from 'next/router'

const Header = ({ children, router, href }) => {
  const isIndex = router.pathname === "/";
  const isAbout = router.pathname === "/about";
  return (
    <div>
        <Link href="/">
          <a id="home">Timers</a>
        </Link>
        <Link href="/about">
          <a id="about">About</a>
        </Link>
        <style jsx>{
          `
            a {
              font-family: 'Montserrat Subrayada', sans-serif;
              color: #ffffff;
              text-decoration: none;
              font-size: 24px;
              padding: 2px;
              margin-right: 30px;
            }
            a:hover {
              color: #000000;
              background-color: #ffffff;
              border-radius: 2px;
            }
            ${isAbout ? `
            #about {
              background-color: red;
              color: #000000;
            }
            #about:hover {
              background-color: blue;
              color: #ffffff;
            }
            ` : ``}

          `
        }</style>
    </div>
)}

export default withRouter(Header)

How can I apply conditional styles?如何应用条件样式?

The solution is to only change the property values dynamically, rather than add entire new selectors in a single conditional you must do it for each property, like this:解决方案是仅动态更改属性值,而不是在单个条件中添加全新的选择器,您必须为每个属性执行此操作,如下所示:

<style jsx>{
  `
    a {
      font-family: 'Montserrat Subrayada', sans-serif;
      color: #ffffff;
      text-decoration: none;
      font-size: 24px;
      padding: 2px;
      margin-right: 30px;
      border-radius: 2px;
    }
    a:hover {
      color: #000000;
      background-color: #ffffff;
    }
    #home {
      background-color: ${isHome ? "#ffffff" : ""};
      color: ${isHome ? "#000000" : ""};
    }
    #about {
      background-color: ${isAbout ? "#ffffff" : ""};
      color: ${isAbout ? "#000000" : ""};
    }
  `
}</style>

(I figured out the answer to my question while I was asking it, so I thought i'd just post it with my answer anyway in case someone else needs it) (我在提问的时候想出了我的问题的答案,所以我想无论如何我都会把它和我的答案一起发布,以防其他人需要它)

Since styled-jsx compiles styles at build time, it cannot preprocess dynamic styles, this is a limitation that is mentioned in the docs here由于 styled-jsx 在构建时编译样式,因此无法预处理动态样式,这是此处文档中提到的限制

Plugins make it possible to use popular preprocessors like SASS, Less, Stylus, PostCSS or apply custom transformations to the styles at compile time.插件可以使用流行的预处理器,如 SASS、Less、Stylus、PostCSS 或在编译时对样式应用自定义转换。

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

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