tailwind-to-style 2.5.3 → 2.6.2

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.
package/README.md CHANGED
@@ -340,6 +340,25 @@ const styles = twsx({
340
340
 
341
341
  The `@css` feature provides a powerful way to bridge the gap between Tailwind's utility classes and custom CSS when needed, without leaving the `twsx` syntax.
342
342
 
343
+ ### Inject Option (Browser Only)
344
+
345
+ By default, every call to `twsx` in the browser will automatically inject the generated CSS into a `<style id="twsx-auto-style">` tag in the document `<head>`. This makes it easy to use dynamic styles in browser or CDN scenarios without manual CSS management.
346
+
347
+ You can control this behavior with the `inject` option:
348
+
349
+ ```js
350
+ // Auto-inject (default)
351
+ twsx({ ... }) // CSS is injected automatically
352
+
353
+ // Disable auto-inject
354
+ twsx({ ... }, { inject: false }) // CSS is NOT injected, just returned as string
355
+ ```
356
+
357
+ - **inject: true** (default): CSS is injected into the page (browser only).
358
+ - **inject: false**: CSS is only returned as a string, not injected. Useful for SSR, testing, or custom handling.
359
+
360
+ > Note: This option only affects browser usage. In Node.js or SSR, no injection occurs.
361
+
343
362
  ## License
344
363
 
345
364
  ## Contributing
@@ -1,5 +1,5 @@
1
1
  /**
2
- * tailwind-to-style v2.5.3
2
+ * tailwind-to-style v2.6.2
3
3
  * Convert tailwind classes to inline style
4
4
  *
5
5
  * @author Bigetion
@@ -6115,13 +6115,17 @@ var tailwindToStyle = (function (exports) {
6115
6115
  * Menghasilkan string CSS dari objek style dengan sintaks mirip SCSS
6116
6116
  * Mendukung nested selectors, state variants, responsive variants, dan @css directives
6117
6117
  * @param {Object} obj - Objek dengan format style mirip SCSS
6118
+ * @param {Object} [options] - Opsi tambahan, misal { inject: true/false }
6118
6119
  * @returns {string} String CSS yang dihasilkan
6119
6120
  */
6120
- function twsx(obj) {
6121
+ function twsx(obj, options = {}) {
6121
6122
  if (!obj || typeof obj !== "object") {
6122
6123
  console.warn("twsx: Expected an object but received:", obj);
6123
6124
  return "";
6124
6125
  }
6126
+ const {
6127
+ inject = true
6128
+ } = options;
6125
6129
  const styles = {};
6126
6130
  function expandGroupedClass(input) {
6127
6131
  function expandDirectiveGroups(str) {
@@ -6275,7 +6279,7 @@ var tailwindToStyle = (function (exports) {
6275
6279
  if (selector.includes("@css")) {
6276
6280
  const parts = selector.split("@css");
6277
6281
  const baseSelector = parts[0].trim();
6278
- const cssProperty = parts[1]?.trim();
6282
+ const cssProperty = parts[1] ? parts[1].trim() : null;
6279
6283
  result = {
6280
6284
  baseSelector,
6281
6285
  cssProperty
@@ -6376,7 +6380,42 @@ var tailwindToStyle = (function (exports) {
6376
6380
  }
6377
6381
  cssString += `}`;
6378
6382
  }
6379
- return cssString.trim();
6383
+ cssString = cssString.trim();
6384
+ if (inject && typeof window !== "undefined" && typeof document !== "undefined") {
6385
+ autoInjectCss(cssString);
6386
+ }
6387
+ return cssString;
6388
+ }
6389
+
6390
+ // Fungsi hashCode sederhana untuk deduplikasi CSS
6391
+ function getCssHash(str) {
6392
+ let hash = 0,
6393
+ i,
6394
+ chr;
6395
+ if (str.length === 0) return hash;
6396
+ for (i = 0; i < str.length; i++) {
6397
+ chr = str.charCodeAt(i);
6398
+ hash = (hash << 5) - hash + chr;
6399
+ hash |= 0; // Convert to 32bit integer
6400
+ }
6401
+ return hash;
6402
+ }
6403
+
6404
+ // Cache untuk deduplikasi auto-inject CSS pakai hash
6405
+ const injectedCssHashSet = new Set();
6406
+ function autoInjectCss(cssString) {
6407
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
6408
+ const cssHash = getCssHash(cssString);
6409
+ if (injectedCssHashSet.has(cssHash)) return;
6410
+ injectedCssHashSet.add(cssHash);
6411
+ let styleTag = document.getElementById("twsx-auto-style");
6412
+ if (!styleTag) {
6413
+ styleTag = document.createElement("style");
6414
+ styleTag.id = "twsx-auto-style";
6415
+ document.head.appendChild(styleTag);
6416
+ }
6417
+ styleTag.textContent += `\n${cssString}`;
6418
+ }
6380
6419
  }
6381
6420
 
6382
6421
  // Daftarkan versi debounced dari fungsi-fungsi export
package/dist/index.cjs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * tailwind-to-style v2.5.3
2
+ * tailwind-to-style v2.6.2
3
3
  * Convert tailwind classes to inline style
4
4
  *
5
5
  * @author Bigetion
@@ -6116,13 +6116,17 @@ function tws(classNames, convertToJson) {
6116
6116
  * Menghasilkan string CSS dari objek style dengan sintaks mirip SCSS
6117
6117
  * Mendukung nested selectors, state variants, responsive variants, dan @css directives
6118
6118
  * @param {Object} obj - Objek dengan format style mirip SCSS
6119
+ * @param {Object} [options] - Opsi tambahan, misal { inject: true/false }
6119
6120
  * @returns {string} String CSS yang dihasilkan
6120
6121
  */
6121
- function twsx(obj) {
6122
+ function twsx(obj, options = {}) {
6122
6123
  if (!obj || typeof obj !== "object") {
6123
6124
  console.warn("twsx: Expected an object but received:", obj);
6124
6125
  return "";
6125
6126
  }
6127
+ const {
6128
+ inject = true
6129
+ } = options;
6126
6130
  const styles = {};
6127
6131
  function expandGroupedClass(input) {
6128
6132
  function expandDirectiveGroups(str) {
@@ -6276,7 +6280,7 @@ function twsx(obj) {
6276
6280
  if (selector.includes("@css")) {
6277
6281
  const parts = selector.split("@css");
6278
6282
  const baseSelector = parts[0].trim();
6279
- const cssProperty = parts[1]?.trim();
6283
+ const cssProperty = parts[1] ? parts[1].trim() : null;
6280
6284
  result = {
6281
6285
  baseSelector,
6282
6286
  cssProperty
@@ -6377,7 +6381,42 @@ function twsx(obj) {
6377
6381
  }
6378
6382
  cssString += `}`;
6379
6383
  }
6380
- return cssString.trim();
6384
+ cssString = cssString.trim();
6385
+ if (inject && typeof window !== "undefined" && typeof document !== "undefined") {
6386
+ autoInjectCss(cssString);
6387
+ }
6388
+ return cssString;
6389
+ }
6390
+
6391
+ // Fungsi hashCode sederhana untuk deduplikasi CSS
6392
+ function getCssHash(str) {
6393
+ let hash = 0,
6394
+ i,
6395
+ chr;
6396
+ if (str.length === 0) return hash;
6397
+ for (i = 0; i < str.length; i++) {
6398
+ chr = str.charCodeAt(i);
6399
+ hash = (hash << 5) - hash + chr;
6400
+ hash |= 0; // Convert to 32bit integer
6401
+ }
6402
+ return hash;
6403
+ }
6404
+
6405
+ // Cache untuk deduplikasi auto-inject CSS pakai hash
6406
+ const injectedCssHashSet = new Set();
6407
+ function autoInjectCss(cssString) {
6408
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
6409
+ const cssHash = getCssHash(cssString);
6410
+ if (injectedCssHashSet.has(cssHash)) return;
6411
+ injectedCssHashSet.add(cssHash);
6412
+ let styleTag = document.getElementById("twsx-auto-style");
6413
+ if (!styleTag) {
6414
+ styleTag = document.createElement("style");
6415
+ styleTag.id = "twsx-auto-style";
6416
+ document.head.appendChild(styleTag);
6417
+ }
6418
+ styleTag.textContent += `\n${cssString}`;
6419
+ }
6381
6420
  }
6382
6421
 
6383
6422
  // Daftarkan versi debounced dari fungsi-fungsi export
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * tailwind-to-style v2.5.3
2
+ * tailwind-to-style v2.6.2
3
3
  * Convert tailwind classes to inline style
4
4
  *
5
5
  * @author Bigetion
@@ -6293,13 +6293,15 @@ function tws(classNames, convertToJson) {
6293
6293
  * Menghasilkan string CSS dari objek style dengan sintaks mirip SCSS
6294
6294
  * Mendukung nested selectors, state variants, responsive variants, dan @css directives
6295
6295
  * @param {Object} obj - Objek dengan format style mirip SCSS
6296
+ * @param {Object} [options] - Opsi tambahan, misal { inject: true/false }
6296
6297
  * @returns {string} String CSS yang dihasilkan
6297
6298
  */
6298
- function twsx(obj) {
6299
+ function twsx(obj, options = {}) {
6299
6300
  if (!obj || typeof obj !== "object") {
6300
6301
  console.warn("twsx: Expected an object but received:", obj);
6301
6302
  return "";
6302
6303
  }
6304
+ const { inject = true } = options;
6303
6305
 
6304
6306
  const styles = {};
6305
6307
 
@@ -6519,7 +6521,7 @@ function twsx(obj) {
6519
6521
  if (selector.includes("@css")) {
6520
6522
  const parts = selector.split("@css");
6521
6523
  const baseSelector = parts[0].trim();
6522
- const cssProperty = parts[1]?.trim();
6524
+ const cssProperty = parts[1] ? parts[1].trim() : null;
6523
6525
  result = { baseSelector, cssProperty };
6524
6526
  } else {
6525
6527
  result = { baseSelector: selector, cssProperty: null };
@@ -6623,7 +6625,40 @@ function twsx(obj) {
6623
6625
  }
6624
6626
  cssString += `}`;
6625
6627
  }
6626
- return cssString.trim();
6628
+ cssString = cssString.trim();
6629
+ if (inject && typeof window !== "undefined" && typeof document !== "undefined") {
6630
+ autoInjectCss(cssString);
6631
+ }
6632
+ return cssString;
6633
+ }
6634
+
6635
+ // Fungsi hashCode sederhana untuk deduplikasi CSS
6636
+ function getCssHash(str) {
6637
+ let hash = 0, i, chr;
6638
+ if (str.length === 0) return hash;
6639
+ for (i = 0; i < str.length; i++) {
6640
+ chr = str.charCodeAt(i);
6641
+ hash = ((hash << 5) - hash) + chr;
6642
+ hash |= 0; // Convert to 32bit integer
6643
+ }
6644
+ return hash;
6645
+ }
6646
+
6647
+ // Cache untuk deduplikasi auto-inject CSS pakai hash
6648
+ const injectedCssHashSet = new Set();
6649
+ function autoInjectCss(cssString) {
6650
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
6651
+ const cssHash = getCssHash(cssString);
6652
+ if (injectedCssHashSet.has(cssHash)) return;
6653
+ injectedCssHashSet.add(cssHash);
6654
+ let styleTag = document.getElementById("twsx-auto-style");
6655
+ if (!styleTag) {
6656
+ styleTag = document.createElement("style");
6657
+ styleTag.id = "twsx-auto-style";
6658
+ document.head.appendChild(styleTag);
6659
+ }
6660
+ styleTag.textContent += `\n${cssString}`;
6661
+ }
6627
6662
  }
6628
6663
 
6629
6664
  // Daftarkan versi debounced dari fungsi-fungsi export