-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
131 lines (112 loc) · 2.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { base, collection } from "./shadows";
const currentValue = "vBoxShadowValue";
const baseBoxShadow = (el) => {
if (el === undefined) {
el = base;
}
if (el !== undefined) {
const isInset = (el) => {
if (isNaN(el.inset) || el.inset === false) {
return "";
} else {
return "inset ";
}
};
const getColor = (el) => {
if (el.color !== "transparent") {
return " rgb(" + el.color + " / " + el.opacity + "%)";
} else {
return "transparent";
}
};
const shadow = (el) => {
return (
isInset(el) +
el.offsetX +
" " +
el.offsetY +
" " +
el.blur +
" " +
el.spread +
" " +
getColor(el)
);
};
if (el.multiple) {
const em = el.multiple;
return em
.map(function (el) {
return shadow(el);
})
.join(", ");
} else {
return shadow(el);
}
}
};
const numShadows = (el, bindings) => {
let name = bindings.arg;
let num = bindings.value;
if (name) {
const getElName = collection.find((el) => el.name === name);
const setElStyle = {
prop: "--box-shadow",
val: baseBoxShadow(getElName),
};
el.style.setProperty(setElStyle.prop, setElStyle.val);
el.setAttribute("data-shadow", name);
} else {
if (isNaN(num)) {
return;
} else if (num !== el[currentValue]) {
el[currentValue] = num;
if (!bindings) {
console.warn("bindings is not found");
} else {
const setElStyle = {
prop: "--box-shadow",
val: baseBoxShadow(collection[num]),
};
el.style.setProperty(setElStyle.prop, setElStyle.val);
el.setAttribute("data-shadow", num);
}
}
}
};
const VueBoxShadow = {
install(Vue, options) {
options = Object.assign(
{ useClass: false, shadowBaseCustom: baseBoxShadow() },
options
);
const styles = `
--box-shadow: ${options.shadowBaseCustom};
box-shadow: var(--box-shadow);`;
if (options.useClass) {
const stylesheets = window.document.styleSheets;
const rule = `.vue-box-shadow{${styles}}`;
if (stylesheets && stylesheets[0] && stylesheets.insertRule) {
stylesheets.insertRule(rule);
} else {
let styleTag = window.document.createElement("style");
styleTag.setAttribute("type", "text/css");
styleTag.appendChild(window.document.createTextNode(rule));
window.document.head.appendChild(styleTag);
}
}
Vue.directive("box-shadow", {
bind(el) {
if (!options.useClass) {
el.style.cssText += styles;
} else {
el.classList.add("vue-box-shadow");
}
},
inserted: (el, bindings) => numShadows(el, bindings),
updated: (el, bindings) => numShadows(el, bindings),
componentUpdated: (el, bindings) => numShadows(el, bindings),
});
},
};
export default VueBoxShadow;