@appbaseio/vue-emotion

中文文档
0.6.0-alpha.6发布于 2023/01/09MITESM + CJS近 30 天下载 3079

Seamlessly use emotion (CSS-in-JS) with Vue.js

原始使用文档
文档目录10 节
  1. Install
  2. Table of Contents
  3. Usage
  4. Theming
  5. Global styles
  6. Server-side rendering
  7. Caveats
  8. Component selector doesn't work (yet)
  9. Contributing
  10. Author

vue-emotion

NPM version NPM downloads CircleCI donate

emotion is the Next Generation of CSS-in-JS.

Support For Vue3.

Install

yarn add @egoist/vue-emotion

Table of Contents

Usage

Use the plugin:

import { VueEmotion } from '@egoist/vue-emotion'

import { createApp } from 'vue'
import App from './App.vue'
import { VueEmotion } from '../../src/index'

import './index.css'

const app = createApp(App, { mixins: [VueEmotion] })

app.mount('#app')

Create your styled component:

import { styled } from '@egoist/vue-emotion'

const Button = styled('button')`
  font-size: 15px;
`

const PinkButton = styled(Button)`
  color: hotpink;
`

const Component = {
  components: {
    Button,
    PinkButton,
  },
  setup() {
    return () => {
      return (
        <div>
          <Button>normal button</Button>
          <PinkButton>pink button</PinkButton>
        </div>
      )
    }
  }
}

Refer to https://emotion.sh for more docs.

Theming

Using provide/inject:

const Component = {
  components: {
    Button,
    PinkButton,
  },
  provide() {
    return {
      theme: this.theme
    }
  },
  data() {
    return {
      theme: 'dark'
    }
  },
  setup() {
    return () => {
      const Button = styled('button')`
        color: ${props => (props.theme === 'dark' ? 'white' : 'black')};
      `
       return <Button>Hello</Button>
    }
  }
}

I do know that provide/inject is NOT recommended in generic application code, but before we find a better solution, use it as a work-around.

Global styles

<template>
  <div id="app">
    <GlobalStyle />
    <!-- rest of your app -->
  </div>
</template>

<script>
import { createGlobalStyle } from '@egoist/vue-emotion'

const GlobalStyle = createGlobalStyle`
  body {
    background: red;
  }
`

export default {
  components: {
    GlobalStyle
  }
}
</script>

Server-side rendering

You can extract critical CSS like this during server-side rendering:

const { renderStyle } = require('@egoist/vue-emotion/server')

// `html` is the rendered HTML by vue-server-renderer
const style = renderStyle(html)
// <style>...</style>

Caveats

Component selector doesn't work (yet)

const Container = styled.div`
  ${Button} {
    color: red;
  }
`

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

vue-emotion © EGOIST, Released under the MIT License.
Authored and maintained by EGOIST with help from contributors (list).

github.com/egoist · GitHub @EGOIST · Twitter @_egoistlily

版本与发布记录

@appbaseio/vue-emotion 共发布 8 个版本,首次发布于 2019/08/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.0-alpha.6latest2023/01/093 年前
0.6.0-alpha.52023/01/093 年前
0.6.0-alpha.42023/01/093 年前
0.6.0-alpha.32022/12/303 年前
0.6.0-alpha.22022/12/293 年前
0.6.0-alpha.12022/12/293 年前

关于 @appbaseio/vue-emotion 的常见问题

@appbaseio/vue-emotion 是什么?

Seamlessly use emotion (CSS-in-JS) with Vue.js

如何安装 @appbaseio/vue-emotion?

在项目目录运行 npm install @appbaseio/vue-emotion,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(>=3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@appbaseio/vue-emotion 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/appbaseio__vue-emotion 是否存在。

@appbaseio/vue-emotion 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@appbaseio/vue-emotion 可以商用吗?

@appbaseio/vue-emotion 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@appbaseio/vue-emotion 的下载量有多少?

根据 npm 官方下载统计,@appbaseio/vue-emotion 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,079 次,日均约 103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appbaseio/vue-emotion 还在维护吗?

最新版本 0.6.0-alpha.6 发布于 2023/01/09(3 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。