tailwind-to-style 1.0.2 → 1.0.4

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 (3) hide show
  1. package/README.md +2 -2
  2. package/index.js +61 -15
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -24,7 +24,7 @@ yarn add tailwind-to-style
24
24
  ## Usage
25
25
 
26
26
  ```javascript
27
- import tws from "tailwind-to-style";
27
+ import { tws } from "tailwind-to-style";
28
28
 
29
29
  // Convert classes to inline CSS
30
30
  const styleInline = tws(`bg-white mx-auto`);
@@ -41,7 +41,7 @@ Here is an example of how to use `tailwind-to-style` in a React application:
41
41
 
42
42
  ```javascript
43
43
  import React from 'react';
44
- import tws from 'tailwind-to-style';
44
+ import { tws } from 'tailwind-to-style';
45
45
 
46
46
  const App = () => {
47
47
  return (
package/index.js CHANGED
@@ -5615,18 +5615,18 @@ function generateTailwindCssString(options = {}) {
5615
5615
  return cssString;
5616
5616
  }
5617
5617
 
5618
- function generateCssClasses(cssString) {
5619
- const cssClasses = {};
5620
- // eslint-disable-next-line
5621
- const regex = /([a-zA-Z0-9\-]+)\s*{\s*([^}]+)\s*}/g;
5618
+ function convertCssToObject(cssString) {
5619
+ const cssObject = {};
5620
+ const regex = /([a-zA-Z0-9\-\\.]+)\s*{\s*([^}]+)\s*}/g;
5622
5621
  let match;
5623
- while ((match = regex.exec(cssString)) !== null) {
5624
- const className = match[1].trim();
5625
- const cssRules = match[2].trim();
5626
5622
 
5627
- cssClasses[className] = cssRules;
5623
+ while ((match = regex.exec(cssString)) !== null) {
5624
+ const className = match[1].replace(/\\/g, "");
5625
+ const cssRules = match[2].trim().replace(/\s+/g, " ");
5626
+ cssObject[className] = cssRules;
5628
5627
  }
5629
- return cssClasses;
5628
+
5629
+ return cssObject;
5630
5630
  }
5631
5631
 
5632
5632
  function inlineStyleToJson(styleString) {
@@ -5649,7 +5649,6 @@ function inlineStyleToJson(styleString) {
5649
5649
  function jsonToStyle(json) {
5650
5650
  return Object.entries(json)
5651
5651
  .map(([key, value]) => {
5652
- // Convert camelCase to kebab-case for CSS properties
5653
5652
  const kebabCaseKey = key.replace(
5654
5653
  /[A-Z]/g,
5655
5654
  (letter) => `-${letter.toLowerCase()}`
@@ -5665,7 +5664,7 @@ function replaceAndRemoveCSSVariables(styleString) {
5665
5664
  let match;
5666
5665
 
5667
5666
  while ((match = variableRegex.exec(styleString)) !== null) {
5668
- const [_, variableName, value] = match;
5667
+ const [, variableName, value] = match;
5669
5668
  customProperties[variableName] = value.trim();
5670
5669
  }
5671
5670
 
@@ -5683,15 +5682,23 @@ function replaceAndRemoveCSSVariables(styleString) {
5683
5682
  return updatedStyleString;
5684
5683
  }
5685
5684
 
5685
+ const breakpoints = {
5686
+ sm: "@media (min-width: 640px)",
5687
+ md: "@media (min-width: 768px)",
5688
+ lg: "@media (min-width: 1024px)",
5689
+ xl: "@media (min-width: 1280px)",
5690
+ "2xl": "@media (min-width: 1536px)",
5691
+ };
5692
+
5686
5693
  function tws(classNames, convertToJson) {
5687
5694
  const cssString = generateTailwindCssString().replace(/\s\s+/g, " ");
5688
5695
 
5689
- const cssClasses = generateCssClasses(cssString);
5696
+ const cssObject = convertCssToObject(cssString);
5690
5697
 
5691
5698
  const classes = classNames.split(" ");
5692
5699
  let cssResult = classes.map((className) => {
5693
- if (cssClasses[className]) {
5694
- return cssClasses[className];
5700
+ if (cssObject[className]) {
5701
+ return cssObject[className];
5695
5702
  }
5696
5703
  return "";
5697
5704
  });
@@ -5705,4 +5712,43 @@ function tws(classNames, convertToJson) {
5705
5712
  return cssResult;
5706
5713
  }
5707
5714
 
5708
- module.exports = tws;
5715
+ function twsx(selectorsConfig) {
5716
+ return Object.entries(selectorsConfig)
5717
+ .map(([selector, [baseStyles, extensions = {}]]) => {
5718
+ let baseCSS = `${selector} { ${tws(baseStyles)} }`;
5719
+ let extendedCSS = "";
5720
+
5721
+ for (const [key, value] of Object.entries(extensions)) {
5722
+ if (breakpoints[key]) {
5723
+ const mediaQuery = breakpoints[key];
5724
+ extendedCSS += `
5725
+ ${mediaQuery} {
5726
+ ${selector} {
5727
+ ${tws(value)}
5728
+ }
5729
+ }
5730
+ `;
5731
+ } else if (key.startsWith("@media")) {
5732
+ extendedCSS += `
5733
+ ${key} {
5734
+ ${selector} {
5735
+ ${tws(value)}
5736
+ }
5737
+ }
5738
+ `;
5739
+ } else {
5740
+ extendedCSS += `
5741
+ ${selector}${key} {
5742
+ ${tws(value)}
5743
+ }
5744
+ `;
5745
+ }
5746
+ }
5747
+
5748
+ return `${baseCSS} ${extendedCSS}`;
5749
+ })
5750
+ .join(" ");
5751
+ }
5752
+
5753
+ exports.tws = tws;
5754
+ exports.twsx = twsx;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwind-to-style",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Convert tailwind classes to inline style",
5
5
  "main": "index.js",
6
6
  "scripts": {