Skip to content

Commit 940f3a9

Browse files
committed
refactor: split utility functions to different file
1 parent 86b83d1 commit 940f3a9

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"eslint-plugin-prettier": "^3.1.4",
5151
"husky": "^4.3.0",
5252
"jest": "^26.6.3",
53+
"kebab-case": "^1.0.2",
5354
"lint-staged": "^10.5.2",
5455
"lodash": "^4.17.20",
5556
"postcss": "^8.1.9",

src/index.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
const plugin = require('tailwindcss/plugin');
22
const colors = require('tailwindcss/colors');
3-
const convert = require('color-convert');
43
const omit = require('lodash/omit');
54
const pick = require('lodash/pick');
65
const isArray = require('lodash/isArray');
76
const isEmpty = require('lodash/isEmpty');
87
const forEach = require('lodash/forEach');
98

10-
function withOpacityValue(variable) {
11-
return ({ opacityValue }) => {
12-
if (opacityValue === undefined) {
13-
return `rgb(var(${variable}))`;
14-
}
15-
return `rgb(var(${variable}) / ${opacityValue})`;
16-
};
17-
}
9+
const { hexToRgb, toKebabCase, withOpacityValue } = require('./utils');
1810

1911
module.exports = plugin.withOptions(
2012
function (options = {}) {
@@ -34,12 +26,12 @@ module.exports = plugin.withOptions(
3426
const baseStyle = {};
3527

3628
forEach(accentColors, (colorShades, name) => {
37-
const selector = `[data-accent='${name}']`;
29+
const selector = `[data-accent='${toKebabCase(name)}']`;
3830
baseStyle[selector] = {};
3931

4032
forEach(colorShades, (value, shade) => {
4133
const cssVar = `--color-accent-${shade}`;
42-
baseStyle[selector][cssVar] = convert.hex.rgb(value).join(' ');
34+
baseStyle[selector][cssVar] = hexToRgb(value);
4335
});
4436
});
4537

src/utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const convert = require('color-convert');
2+
const kebabCase = require('kebab-case');
3+
4+
module.exports.hexToRgb = function (hex) {
5+
return convert.hex.rgb(hex).join(' ');
6+
};
7+
8+
module.exports.toKebabCase = function (string) {
9+
return kebabCase(string);
10+
};
11+
12+
module.exports.withOpacityValue = function (variable) {
13+
return ({ opacityValue }) => {
14+
if (opacityValue === undefined) {
15+
return `rgb(var(${variable}))`;
16+
}
17+
return `rgb(var(${variable}) / ${opacityValue})`;
18+
};
19+
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,11 @@ jsonparse@^1.2.0:
36743674
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
36753675
integrity sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg==
36763676

3677+
kebab-case@^1.0.2:
3678+
version "1.0.2"
3679+
resolved "https://registry.yarnpkg.com/kebab-case/-/kebab-case-1.0.2.tgz#5eac97d5d220acf606d40e3c0ecfea21f1f9e1eb"
3680+
integrity sha512-7n6wXq4gNgBELfDCpzKc+mRrZFs7D+wgfF5WRFLNAr4DA/qtr9Js8uOAVAfHhuLMfAcQ0pRKqbpjx+TcJVdE1Q==
3681+
36773682
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
36783683
version "3.2.2"
36793684
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"

0 commit comments

Comments
 (0)