UNPKG

5.16 kBJavaScriptView Raw
1/*!
2 * vue-css-modules v1.2.0
3 * (c) 2018-present fjc0k <fjc0kb@gmail.com> (https://github.com/fjc0k)
4 * Released under the MIT License.
5 */
6function includes(arrayLike, element, fromIndex) {
7 if (fromIndex === void 0) {
8 fromIndex = 0;
9 }
10
11 for (var i = fromIndex, len = arrayLike.length; i < len; i++) {
12 if (arrayLike[i] === element) {
13 return true;
14 }
15 }
16
17 return false;
18}
19function isObject(value) {
20 return value !== null && typeof value === 'object';
21}
22function isFunction(value) {
23 return typeof value === 'function';
24}
25function isString(value) {
26 return typeof value === 'string';
27}
28var camelCaseCache = Object.create(null);
29function camelCase(value) {
30 if (camelCaseCache[value]) return camelCaseCache[value];
31 var result = '';
32 var shouldUpperCase = false;
33
34 for (var i = 0, len = value.length; i < len; i++) {
35 var char = value[i];
36
37 if (char === '-') {
38 shouldUpperCase = true;
39 } else {
40 result += result && shouldUpperCase ? char.toUpperCase() : char;
41 shouldUpperCase = false;
42 }
43 }
44
45 camelCaseCache[value] = result;
46 return result;
47}
48
49var cache = Object.create(null);
50var parseClassExpression = (function (expression) {
51 if (cache[expression]) return cache[expression];
52 var className;
53 var binding;
54 var bindingValue;
55 var role;
56
57 if (includes(expression, '=', 1)) {
58 // eg: disabled=isDisabled
59 var _expression$split = expression.split('=');
60
61 className = _expression$split[0];
62 binding = _expression$split[1];
63 } else {
64 var modifier = expression[0];
65
66 if (modifier === '$') {
67 // eg: $type
68 binding = expression.substr(1);
69 bindingValue = true;
70 } else if (modifier === '@') {
71 // eg: @button
72 className = expression.substr(1);
73 role = className;
74 } else if (modifier === ':') {
75 // eg: :disabled
76 className = expression.substr(1);
77 binding = camelCase(className);
78 } else {
79 className = expression;
80 }
81 }
82
83 cache[expression] = {
84 className: className,
85 binding: binding,
86 bindingValue: bindingValue,
87 role: role
88 };
89 return cache[expression];
90});
91
92var INJECT_ATTR = 'styleName';
93
94/* eslint max-depth: 0 guard-for-in: 0 */
95
96function createElement(_) {
97 var args = [].slice.call(arguments, 1); // for functional component
98
99 if (isFunction(_)) {
100 return createElement.bind(_, {
101 functional: true,
102 createElement: _,
103 styles: args[0],
104 context: args[1]
105 });
106 }
107
108 var _$functional = _.functional,
109 functional = _$functional === void 0 ? false : _$functional,
110 h = _.createElement,
111 _$context = _.context,
112 context = _$context === void 0 ? {} : _$context,
113 _$styles = _.styles,
114 styles = _$styles === void 0 ? context.$style || {} : _$styles;
115
116 if (isString(styles)) {
117 styles = (functional ? (context.injections || {})[styles] : context[styles]) || {};
118 }
119
120 if (functional) {
121 context = context.props || {};
122 }
123
124 var data = args[1];
125
126 if (isObject(data)) {
127 if (!data.staticClass) {
128 data.staticClass = '';
129 }
130
131 if (!data.attrs) {
132 data.attrs = {};
133 }
134
135 var modules = data[INJECT_ATTR] || data.attrs[INJECT_ATTR] || '';
136
137 if (modules.length) {
138 var _modules = Array.isArray(modules) ? modules : [modules];
139
140 for (var i in _modules) {
141 var module = _modules[i];
142
143 if (module && typeof module === 'string') {
144 var classExpressions = module.split(/\s+/g);
145
146 for (var _i in classExpressions) {
147 var classExpression = classExpressions[_i];
148
149 var _parseClassExpression = parseClassExpression(classExpression),
150 className = _parseClassExpression.className,
151 binding = _parseClassExpression.binding,
152 bindingValue = _parseClassExpression.bindingValue,
153 role = _parseClassExpression.role;
154
155 if (bindingValue) {
156 className = context[binding];
157 binding = undefined;
158 }
159
160 if ((binding ? context[binding] : true) && styles[className]) {
161 data.staticClass += " " + styles[className];
162 data.staticClass = data.staticClass.trim();
163 }
164
165 if (role) {
166 data.attrs["data-component-" + role] = '';
167 }
168 }
169 }
170 }
171 } // remove styleName attr
172
173
174 delete data[INJECT_ATTR];
175 delete data.attrs[INJECT_ATTR];
176 }
177
178 return h.apply(null, args);
179}
180
181/* eslint camelcase: 0 */
182
183var CSSModules = function CSSModules(styles) {
184 return {
185 beforeCreate: function beforeCreate() {
186 this.original$createElement = this.original$createElement || this.$createElement;
187 this.original_c = this.original_c || this._c;
188 this.$createElement = createElement.bind(this, {
189 createElement: this.original$createElement,
190 context: this,
191 styles: styles
192 });
193 this._c = createElement.bind(this, {
194 createElement: this.original_c,
195 context: this,
196 styles: styles
197 });
198 }
199 };
200};
201
202CSSModules.install = function (Vue) {
203 Vue.mixin(CSSModules());
204};
205
206export default CSSModules;