Giter Site home page Giter Site logo

styled-vue's Introduction

ATTENTION: this project is not actively maintained for now (I still push some code once in a while), if you want to see improvements or get support, please consider sponsoring me.

styled-vue


NPM version NPM downloads CircleCI chat

Please consider donating to this project's author, EGOIST, to show your ❤️ and support.

Features

  • Zero-runtime CSS-in-JS for Vue SFC without compromise
  • Scoped CSS by default
  • CSS preprocessors support
  • Dynamic style just works (no IE support)
  • Working with SSR out-of-the-box
  • Hot module replacement still works
  • You get all the features without any config!

Table of Contents

Install

yarn add styled-vue --dev

Then register the Vue plugin (optional):

import Vue from 'vue'
import { StyledVue } from 'styled-vue'

Vue.use(StyledVue)

So far the plugin is only required for globalStyle, if you only need scoped style, you can safely skip this.

Example

<template>
  <div><h1 class="title">hello there!</h1></div>
</template>

<script>
import { css } from 'styled-vue'
import { modularScale } from 'polished'

export default {
  style: css`
    .title {
      /* You can access component's "this" via "vm" */
      color: ${vm => vm.$store.state.themeColor};
      font-size: ${modularScale(2)};
      margin: 0;
    }
  `
}
</script>

And that's it, it's like writing .vue file's scoped CSS in the <script> tag.

How to use

Using with webpack

I did say that styled-vue works without config, that might be a clickbait because you do need a single line of config in your webpack.config.js:

module.exports = {
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          compiler: require('styled-vue/compiler') // <- here
        }
      }
    ]
  }
}

Using with Vue CLI

In your vue.config.js:

module.exports = {
  chainWebpack(config) {
    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => {
        options.compiler = require('styled-vue/compiler') // <- here
        return options
      })
  }
}

Using with Poi

In your poi.config.js:

module.exports = {
  plugins: ['styled-vue/poi']
}

Using with Nuxt.js

In your nuxt.config.js:

export default {
  modules: ['styled-vue/nuxt']
}

How does it work

Input:

<template>
  <h1>hello</h1>
</template>

<script>
import { css } from 'styled-vue'

export default {
  style: css`
    h1 {
      color: ${vm => vm.color};
      width: ${200 + 1}px;
    }
  `
}
</script>

Output:

<template>
  <h1 :style="$options.style(this)">hello</h1>
</template>

<script>
export default {
  style: function(vm) {
    var v0 = vm => vm.color
    var v1 = 200 + 1
    return {
      '--v0': v0(vm),
      '--v1': v1 + 'px'
    }
  }
}
</script>

<style scoped>
h1 {
  color: var(--v0);
  width: var(--v1);
}
</style>

Under the hood, it uses CSS variables for dynamic styles, that's why this feature is not supported in IE.

CSS Preprocessors

import { less, sass, scss, stylus } from 'styled-vue'

Just use corresponding exports from styled-vue.

The CSS will be passed to vue-loader and parsed by PostCSS if a postcss.config.js exists in your project, so it really just works like <style scoped>.

Global Styles

Use globalStyle instead of style on your component:

import { css } from 'styled-vue'

export default {
  globalStyle: css`
    body {
      color: ${vm => vm.bodyColor};
    }
  `
}

globalStyle relies on the Vue plugin, make sure to register it first:

import Vue from 'vue'
import { StyledVue } from 'styled-vue'

Vue.use(StyledVue)

For Nuxt users we automatically register it for you.

This only adds ~100 bytes to your application.

TypeScript

We use Babel to parse your code, so TypeScript should work out-of-the-box, however there're some caveats.

Editor Plugins

VSCode

Atom

Inspirations

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

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

Website · GitHub @EGOIST · Twitter @_egoistlily

styled-vue's People

Contributors

egoist avatar pi0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

styled-vue's Issues

Is this possible? RedComponent = Component.class`text-ted`

Hello people.

I have, say, a blue button. In 10 places of my app the button should be red. Here is what I'm doing atm:

<template>
<Button class="text-white bg-red hover:text-orange shadow-red"/>
</template>

I'm looking to avoid writing this class="text-white bg-red hover:text-orange shadow-red" 10 times. I'd rather have something like:

export default const RedButton = Button.class`text-white bg-red hover:text-orange shadow-red`;

And then just reuse it everywhere:

<template>
<RedButton/>
</template>

The above approach is the syntax I really like in React's Styled-Components.

Is it possible with (or without?) styled-vue?

Global Styles

Yeah. I know. We shouldn't use this approach.

But...

I was thinking of using styled-vue along with a UI Framework that I'm developing. The main idea is to have dynamic styles that can be changed when using the UI Framework. As an example, let's think in a theme solution:

Vue.use(UiFramework, {
  colors: {
    primary: '#313131'
    // and so on...
  }
})

Then, inside a particular UI Component, I can use those "dynamic" data and generate the CSS. This already works, but I would like to generate the classes without the scoped attribute. This is only to make easier to override the CSS later (in userland).
For example, .ui-title[data-v-3bf6d6bb] have higher specificity than .ui-title.

With that in mind, it would be awesome to have an option to toggle the scoped option when passing to vue-template-compiler.

Maybe another expression called globalCss or even a config inside vue.config.js (it is also possible to configure it through webpack, poi and nuxt)

What do you think? I can easily submit a PR if makes sense for you.

Thanks :)

Preprocessors import don't work for me

I try

import { scss } from 'styled-vue'

and getting

error  scss not found in 'styled-vue'  import/named

my package.json

"dependencies": {
    "cross-env": "^5.2.0",
    "nuxt": "^2.3.4"
  },
  "devDependencies": {
    "@nuxtjs/eslint-config": "^0.0.1",
    "babel-eslint": "^8.2.1",
    "eslint": "^5.0.1",
    "eslint-config-standard": ">=12.0.0",
    "eslint-loader": "^2.0.0",
    "eslint-plugin-import": ">=2.14.0",
    "eslint-plugin-jest": ">=21.24.1",
    "eslint-plugin-node": ">=7.0.1",
    "eslint-plugin-promise": ">=4.0.1",
    "eslint-plugin-standard": ">=4.0.0",
    "eslint-plugin-vue": "^5.0.0",
    "node-sass": "^4.11.0",
    "nodemon": "^1.18.9",
    "pug": "^2.0.3",
    "pug-plain-loader": "^1.0.0",
    "sass-loader": "^7.1.0",
    "styled-vue": "^0.1.4"
  }

vue-template-compiler issue

Building a nuxt project with this package gives me the error:

Vue packages version mismatch:
  
  - [email protected]
  - [email protected]

including nuxts version of vue-template-compiler in the projects package.json fixes it.

Shouldn't vue-template-compiler be in devDependencies in styled-vue?

Provide TypeScript typings

We need to add types for:

  • exports: css scss less sass stylus StyledVue
  • component option: style globalStyle

Jsx support

Is it atm possible tu use it with jsx/class components or will it be ?

Let's hack `<style>`

In:

<template>
  <h1>hello</h1>
</template>

<script>
export default {
  props: ['color', 'fontSize']
}
</script>

<style scoped>
h1 {
  color: eval("color");
  font-size: eval("fontSize * 2");
}
</style>

Out:

<template>
  <h1 :style="{'--v0': color, '--v1': fontSize * 2}">hello</h1>
</template>

<script>
export default {
  props: ['color', 'fontSize']
}
</script>

<style scoped>
h1 {
  color: var(--v0);
  font-size: var(--v1);
}
</style>

Variables don't work in nuxt.js

I try

style: scss`
    .index {
      width: 100vh;
      height: 100vh;
      background: ${vm => vm.$store.state.theme.colors.primary};
      color: ${vm => vm.color};
    }
  `

but variables don't work
screen shot 2019-02-08 at 12 48 22

Dynamic imports & Nuxt.js: Syntax Error: 'dynamicImport'

This package looks awesome and I'd love to use it but it seems that it doesn't support dynamic imports. After installing it in my Nuxt.js application, I get the following error:

Syntax Error: 'dynamicImport' (26:34)                                                      friendly-errors 17:41:18

                                                                                           friendly-errors 17:41:18
 @ ./node_modules/babel-loader/lib??ref--2-0!./node_modules/vue-loader/lib??vue-loader-options!./pages/index.vue?vue&type=script&lang=js& 11:0-74 15:22-38
 @ ./pages/index.vue?vue&type=script&lang=js&
 @ ./pages/index.vue
 @ ./.nuxt/router.js
 @ ./.nuxt/index.js
 @ ./.nuxt/client.js
 @ multi webpack-hot-middleware/client?name=client&reload=true&timeout=30000&path=/__webpack_hmr/client ./.nuxt/client.js

Am I right that this package does not work correctly when using dynamically imported components?

Support object literal

import { css } from 'styled-vue'

export default {
  style: css({
    h1: {
	  color: 'red',
      fontSize: vm => vm.fontSize
	}
  })
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.