跳至正文
Wener Notes

styled-jsx

约 1 分钟阅读
  • 默认不支持传递样式到 children
    • .ment-item > a 如果 a 是在一个组件里,无法影响到
    • 使用 .menu-item > :global(a)
    • 或者传递 css.resolve 返回的 className 到子组件
TSX
import css from 'styled-jsx/css';

// 全局样式 - 不使用 className 限定
const body = css.global`
  body {
    margin: 0;
  }
`;

// babel-plugin-macros 可作为 babel macros
const { className, styles } = css.resolve`
  a {
    color: green;
  }
`;
export default () => (
  <div>
    <style jsx>{`
      /* 通过不修饰 a 影响子组件 */
      div > :global(a) {
        // 通过 styled-jsx-plugin-postcss 使用 postcss - 使用 tailwind
        @apply text-red;
      }
    `}</style>

    {/* 通过传递 className 影响子组件 */}
    <Link className={className}>About</Link>
    {styles}

    {/* 渲染 global */}
    <style jsx global>
      {body}
    </style>
  </div>
);

tailwind

  • 启用后不会使用 SWC 编译 - 推荐使用 css module
.babelrc
{
  "presets": [
    [
      "next/babel",
      {
        "styled-jsx": {
          "plugins": ["styled-jsx-plugin-postcss"]
        }
      }
    ]
  ]
}

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • Web Awesome
    笔记 · styled-jsx

    https://www.c82.net/iconography/ · https://www.photopea.com/ · ryohey/signal · MIT, TS · Online MIDI Editor: signal · http://oskarstalberg.com/Townscaper/

参考资料

GitHub2 条
最近更新commit 970526b编辑