This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (62 loc) · 1.9 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
const _ = require('lodash');
function hex2RGB (hex) {
"use strict";
if (hex.charAt(0) === '#') {
hex = hex.substr(1);
}
if ((hex.length < 2) || (hex.length > 6)) {
return false;
}
var values = hex.split(''),
r,
g,
b;
if (hex.length === 2) {
r = parseInt(values[0].toString() + values[1].toString(), 16);
g = r;
b = r;
} else if (hex.length === 3) {
r = parseInt(values[0].toString() + values[0].toString(), 16);
g = parseInt(values[1].toString() + values[1].toString(), 16);
b = parseInt(values[2].toString() + values[2].toString(), 16);
} else if (hex.length === 6) {
r = parseInt(values[0].toString() + values[1].toString(), 16);
g = parseInt(values[2].toString() + values[3].toString(), 16);
b = parseInt(values[4].toString() + values[5].toString(), 16);
} else {
return false;
}
return [r, g, b];
}
module.exports = function({variants, opacities, defaultColor = '#000', bgColors}) {
return function({addUtilities, config, e}) {
let colors;
if(!bgColors) {
colors = config('colors');
} else {
colors = bgColors
}
const darkenDefault = _.map(opacities, (opacity, scale) => {
if(hex2RGB(defaultColor)) {
const [r, g, b] = hex2RGB(defaultColor);
return {
[`.bg-darken-${scale}`]: {
boxShadow: `inset 0 0 0 9999px rgba(${r}, ${g}, ${b}, ${opacity})`
}
}
}
})
const darkenWithColors = _.map(colors, (hex, name) => {
if(hex2RGB(hex)) {
const [r, g, b] = hex2RGB(hex)
return _.map(opacities, (opacity, scale) => ({
[`.bg-darken-${scale}--${name}`]: {
boxShadow: `inset 0 0 0 9999px rgba(${r}, ${g}, ${b}, ${opacity})`
}
}))
}
})
const darkenUtilities = darkenDefault.concat(darkenWithColors);
addUtilities(darkenUtilities, variants);
}
}