forked from vadimdemedes/tailwind-rn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (48 loc) · 1.47 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
'use strict';
// Tailwind started using CSS variables for color opacity since v1.4.0,
// this helper adds a primitive support for these
const useVariables = object => {
const newObject = {};
for (const [key, value] of Object.entries(object)) {
if (!key.startsWith('--')) {
if (typeof value === 'string') {
newObject[key] = value.replace(/var\(([a-zA-Z-]+)\)/, (_, name) => {
return object[name];
});
} else {
newObject[key] = value;
}
}
}
return newObject;
};
const create = styles => {
// Pass a list of class names separated by a space, for example:
// "bg-green-100 text-green-800 font-semibold")
// and receive a styles object for use in React Native views
const tailwind = classNames => {
const object = {};
if (!classNames) {
return object;
}
for (const className of classNames.replace(/\s+/g, ' ').trim().split(' ')) {
if (styles[className]) {
Object.assign(object, styles[className]);
} else {
console.warn(`Unsupported Tailwind class: "${className}"`);
}
}
return useVariables(object);
};
// Pass the name of a color (e.g. "blue-500") and receive a color value (e.g. "#4399e1")
const getColor = name => {
const object = tailwind(`bg-${name}`);
return object.backgroundColor;
};
return {tailwind, getColor};
};
const {tailwind, getColor} = create(require('./styles.json'));
module.exports = tailwind;
module.exports.default = tailwind;
module.exports.getColor = getColor;
module.exports.create = create;