vue-styletron
Use the Styletron CSS-in-JS library with
Vue.js
About
This is a very small amount of code to make
Styletron easier to use within
Vue.js components.
Installation
npm install @appjumpstart/vue-styletron --save
Usage
Client:
import VueStyletron from '@appjumpstart/vue-styletron'
Vue.use(new VueStyletron())
Server (optional, for SSR):
const VueStyletron = require('@appjumpstart/vue-styletron/server')
const vueStyletron = new VueStyletron()
Vue.use(vueStyletron)
let html = renderer.renderToString(context)
html = vueStyletron.insertStyles(html)
res.type('text/html').send(html)
NOTE: You'll also need to insert a tag into your HTML template which
defaults to {styles}
but can be configured as the 3rd parameter to
insertStyles.
Component:
<template>
<div :class="`${styles.hello} mr-5`">
Hello!
</div>
</template>
<script>
export default {
styles: { hello: { fontSize: '48px', textAlign: 'center' } }
}
</script>
Output:
<!DOCTYPE html>
<html>
<head>
<title>Test Template</title>
<style class="_styletron_hydrate_">.ae{font-size:48px}.af{text-align:center}</style>
</head>
<body>
<div data-server-rendered="true" class="ae af mr-5">
Hello!
</div>
</body>
</html>
Server-Side Rendering (SSR)
Note: If you are using
webpack-node-externals in the
Webpack config make sure to add '@appjumpstart/vue-styletron'
to the
whitelist.