Skip to content

Commit

Permalink
feat: support global installation
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jun 29, 2018
1 parent 8896531 commit 0e6174d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
5 changes: 5 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import Vue from 'vue'
import CSSModules from '../src'

import renderFn from './renderFn'
import renderFnFunctional from './renderFn-functional'
import templateWithOuterModules from './template-with-outer-modules'
import templateWithInlineModules from './template-with-inline-modules'
import templateWithInlineModulesGlobal from './template-with-inline-modules-global'

Vue.use(CSSModules)

const demos = {
'renderFn-functional': renderFnFunctional,
renderFn: renderFn,
'template-with-inline-modules': templateWithInlineModules,
'template-with-inline-modules-global': templateWithInlineModulesGlobal,
'template-with-outer-modules': templateWithOuterModules
}

Expand Down
42 changes: 42 additions & 0 deletions demo/template-with-inline-modules-global.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<template>
<div
styleName="@button :disabled mini=isMini"
@click="disabled=!disabled">
BUTTON
</div>
</template>

<script>
export default {
name: 'demo-button-4',
data() {
return {
disabled: false
}
},
computed: {
isMini() {
return this.disabled
}
}
}
</script>

<style module>
.button {
display: inline-block;
font-size: 18px;
padding: 10px;
border: 1px solid green;
}
.disabled {
opacity: .5;
}
.mini {
font-size: 12px;
}
</style>
2 changes: 0 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const INJECTED = '__CSSModules__'

export const INJECT_ATTR = 'styleName'
40 changes: 21 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/* eslint camelcase: 0 */
import createElement from './create-element'
import { INJECTED } from './config'

export default styles => {
return {
beforeCreate() {
if (this[INJECTED]) return

this[INJECTED] = true

this.$createElement = createElement.bind(this, {
createElement: this.$createElement,
context: this,
styles
})
this._c = createElement.bind(this, {
createElement: this._c,
context: this,
styles
})
}
const CSSModules = styles => ({
beforeCreate() {
this.original$createElement = this.original$createElement || this.$createElement
this.original_c = this.original_c || this._c
this.$createElement = createElement.bind(this, {
createElement: this.original$createElement,
context: this,
styles
})
this._c = createElement.bind(this, {
createElement: this.original_c,
context: this,
styles
})
}
})

CSSModules.install = Vue => {
Vue.mixin(CSSModules())
}

export default CSSModules

0 comments on commit 0e6174d

Please sign in to comment.