黑白梦黑白梦

  • 文章
  • 专栏
  • 文章
  • 专栏
全部文章

@emotion/react 配置 css props ,优雅的 css in js 方案

发布于 2024-11-19约 1 分钟

css in js 有很多写法,此前主要接触的是 styled 写法,但这个写法不太习惯,也需要额外的 IDE 插件支持。emotion 的 css props 则相当于拓展了原生 style 标签,没有任何心智负担,也不影响现有项目的样式模式,可以无缝接入。

https://emotion.sh/

css props 写法

<div
  css={{
    backgroundColor: 'red',
    '&:hover': {
      color: 'yellow'
    }
  }}
>
  鼠标移入后变色
</div>

vite 设置

export default defineConfig({
  plugins: [
    react({
      jsxImportSource: "@emotion/react",
      babel: {
        plugins: ["@emotion/babel-plugin"],
      },
    }),
  ],
});

eslint 配置

{
  "rules": {
    "react/no-unknown-property": ["error", { "ignore": ["css"] }]
  }
}

TypeScript 配置

compilerOptions 中设置:

"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"
目录
css props 写法vite 设置eslint 配置TypeScript 配置

本文收录于专栏

一些好用的 npm 前端开源库

收集一些好用的前端开源库,主要是 npm 包

0 篇文章更新于 2025-10-20
上一篇使用 Cloudflare Workers 创建 Hono 服务,结合 Drizzle ORM 配置 D1 数据库,Pages 部署静态页面下一篇编写 Chrome 插件,自定义右键菜单功能,如实现划词跳转翻译网站

©2015-2026 黑白梦 粤ICP备15018165号

联系: heibaimeng@foxmail.com