@trackunit/css-tailwind-custom-properties-plugin 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +55 -0
  2. package/package.json +3 -2
package/index.js ADDED
@@ -0,0 +1,55 @@
1
+ import plugin from 'tailwindcss/plugin';
2
+
3
+ /**
4
+ * Converts an array of keys to a valid CSS custom property name.
5
+ *
6
+ * @param keys An array of strings representing the keys to be converted to a CSS custom property name.
7
+ * @returns { string } A string representing the CSS custom property name.
8
+ */
9
+ const keysToCssVariableName = (keys) => `--${keys.join("-")}`.replace(/[^a-zA-Z0-9]/g, "-");
10
+ /**
11
+ * Flattens a nested object containing variables declarations and their values
12
+ * into a flat object where the keys are the CSS variable names and the values are
13
+ * the corresponding CSS variable values.
14
+ *
15
+ * @param {object} values - The object containing the nested variables declarations and their values
16
+ * @param {object} [output] - The flat object to store the generated CSS variable names and values
17
+ * @param {string[]} [keys] - An array of keys representing the current path of the object being processed
18
+ * @returns {VariablesDeclerationObject} - The generated flat object containing the CSS variable names and values
19
+ */
20
+ const flattenVariablesDeclerationValues = (values, output = {}, keys = []) => {
21
+ for (const [key, value] of Object.entries(values)) {
22
+ let flattend = {};
23
+ if (typeof value === "object") {
24
+ flattend = flattenVariablesDeclerationValues(value, output, [...keys, key]);
25
+ }
26
+ else {
27
+ flattend[keysToCssVariableName([...keys, key])] = value;
28
+ }
29
+ output = Object.assign(Object.assign({}, flattend), output);
30
+ }
31
+ return output;
32
+ };
33
+ /**
34
+ * Converts a object containing CSS variables to an array of CSS rules.
35
+ *
36
+ * @param variablesDecleration An object containing the CSS variables and their corresponding values.
37
+ * @returns {CSSRuleObject[]} An array of CSS rules.
38
+ */
39
+ const variablesToCSSRules = (variablesDecleration) => {
40
+ if (!variablesDecleration) {
41
+ return [];
42
+ }
43
+ const cssRules = Object.entries(variablesDecleration).map(([key, value]) => ({
44
+ [key === "DEFAULT" ? ":root" : `.${key}`]: flattenVariablesDeclerationValues(value),
45
+ }));
46
+ return cssRules;
47
+ };
48
+
49
+ const CssTailwindCustomPropertiesPlugin = plugin(({ addBase, theme }) => {
50
+ const variablesDeclerationFromThemeConfig = theme("variables", {});
51
+ const cssRules = variablesToCSSRules(variablesDeclerationFromThemeConfig);
52
+ addBase(cssRules);
53
+ });
54
+
55
+ export { CssTailwindCustomPropertiesPlugin, flattenVariablesDeclerationValues, keysToCssVariableName, variablesToCSSRules };
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@trackunit/css-tailwind-custom-properties-plugin",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "main": "./index.cjs",
5
5
  "repository": "https://github.com/Trackunit/manager",
6
6
  "license": "SEE LICENSE IN LICENSE.txt",
7
- "type": "commonjs",
7
+ "module": "./index.js",
8
+ "type": "module",
8
9
  "types": "./src/index.d.ts"
9
10
  }