tailwind-to-style 2.7.0 → 2.7.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/dist/index.cjs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * tailwind-to-style v2.7.0
2
+ * tailwind-to-style v2.7.2
3
3
  * Convert tailwind classes to inline style
4
4
  *
5
5
  * @author Bigetion
@@ -6343,13 +6343,13 @@ function resolveCssToClearCss(cssString) {
6343
6343
  }).join(" ");
6344
6344
  }
6345
6345
 
6346
- // Cache untuk getConfigOptions
6346
+ // Cache for getConfigOptions
6347
6347
  const configOptionsCache = new Map();
6348
6348
  const cacheKey = options => JSON.stringify(options);
6349
6349
  function generateTailwindCssString() {
6350
6350
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
6351
6351
  const pluginKeys = Object.keys(plugins);
6352
- // Menggunakan cache untuk mencegah pemrosesan ulang yang tidak perlu
6352
+ // Use cache to prevent unnecessary reprocessing
6353
6353
  const key = cacheKey(options);
6354
6354
  if (!configOptionsCache.has(key)) {
6355
6355
  configOptionsCache.set(key, getConfigOptions(options, pluginKeys));
@@ -6410,7 +6410,7 @@ const selectorVariants = {
6410
6410
  number: arg => `> :nth-child(${arg})`
6411
6411
  };
6412
6412
 
6413
- // Mengoptimalkan encoding/decoding bracket values dengan memoization
6413
+ // Optimize encoding/decoding bracket values with memoization
6414
6414
  const encodeBracketCache = new Map();
6415
6415
  function encodeBracketValues(input) {
6416
6416
  if (!input) return input;
@@ -6477,7 +6477,7 @@ function inlineStyleToJson(styleString) {
6477
6477
  return styleObject;
6478
6478
  }
6479
6479
 
6480
- // Cache untuk CSS resolusi
6480
+ // Cache for CSS resolution
6481
6481
  const cssResolutionCache = new Map();
6482
6482
 
6483
6483
  // Enhanced cache management with performance monitoring
@@ -6485,7 +6485,7 @@ function limitCacheSize(cache) {
6485
6485
  let maxSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
6486
6486
  if (cache.size > maxSize) {
6487
6487
  const cleanupMarker = performanceMonitor.start("cache:cleanup");
6488
- // Hapus 20% entri yang paling lama
6488
+ // Remove 20% of the oldest entries
6489
6489
  const entriesToRemove = Math.floor(cache.size * 0.2);
6490
6490
  const keys = Array.from(cache.keys()).slice(0, entriesToRemove);
6491
6491
  keys.forEach(key => cache.delete(key));
@@ -6524,14 +6524,14 @@ function debounce(func) {
6524
6524
  function separateAndResolveCSS(arr) {
6525
6525
  const marker = performanceMonitor.start("css:resolve");
6526
6526
  try {
6527
- // Perbaiki: cacheKey harus unik untuk setiap input array
6527
+ // Fix: cacheKey must be unique for each input array
6528
6528
  const cacheKey = Array.isArray(arr) ? arr.join("|") : String(arr);
6529
6529
  if (cssResolutionCache.has(cacheKey)) {
6530
6530
  performanceMonitor.end(marker);
6531
6531
  return cssResolutionCache.get(cacheKey);
6532
6532
  }
6533
6533
 
6534
- // Batasi ukuran cache untuk menghindari memory leak
6534
+ // Limit cache size to avoid memory leaks
6535
6535
  limitCacheSize(cssResolutionCache);
6536
6536
  const cssProperties = {};
6537
6537
  arr.forEach(item => {
@@ -6544,7 +6544,7 @@ function separateAndResolveCSS(arr) {
6544
6544
  const key = declaration.substring(0, colonIndex).trim();
6545
6545
  const value = declaration.substring(colonIndex + 1).trim();
6546
6546
  if (key && value) {
6547
- // Prioritaskan nilai yang lebih spesifik (misalnya !important)
6547
+ // Prioritize more specific values (e.g., !important)
6548
6548
  if (value.includes("!important") || !cssProperties[key]) {
6549
6549
  cssProperties[key] = value;
6550
6550
  }
@@ -6595,10 +6595,75 @@ function separateAndResolveCSS(arr) {
6595
6595
  }
6596
6596
 
6597
6597
  /**
6598
- * Mengkonversi string kelas Tailwind menjadi inline styles CSS atau objek JSON
6599
- * @param {string} classNames - String berisi kelas Tailwind yang akan dikonversi
6600
- * @param {boolean} convertToJson - Jika true, hasil akan menjadi objek JSON, jika false menjadi string CSS
6601
- * @returns {string|Object} String CSS inline atau objek style JSON
6598
+ * Process opacity modifier from class name (e.g., text-red-500/50 -> 50% opacity)
6599
+ * @param {string} className - Class name with potential opacity modifier
6600
+ * @param {string} cssDeclaration - CSS declaration to modify
6601
+ * @returns {string} Modified CSS declaration with opacity applied
6602
+ */
6603
+ function processOpacityModifier(className, cssDeclaration) {
6604
+ const opacityMatch = className.match(/\/(\d+)$/);
6605
+ if (!opacityMatch) return cssDeclaration;
6606
+ const opacityValue = parseInt(opacityMatch[1], 10);
6607
+ if (opacityValue < 0 || opacityValue > 100) return cssDeclaration;
6608
+ const alphaValue = (opacityValue / 100).toString();
6609
+
6610
+ // Handle Tailwind's CSS custom property pattern
6611
+ let modifiedDeclaration = cssDeclaration;
6612
+
6613
+ // Replace opacity custom properties
6614
+ const opacityProperties = ['--text-opacity', '--bg-opacity', '--border-opacity', '--ring-opacity', '--divide-opacity', '--placeholder-opacity', '--text-decoration-opacity', '--outline-opacity', '--accent-opacity', '--caret-opacity'];
6615
+ opacityProperties.forEach(prop => {
6616
+ const propRegex = new RegExp(`${prop}\\s*:\\s*[\\d.]+`, 'gi');
6617
+ modifiedDeclaration = modifiedDeclaration.replace(propRegex, `${prop}: ${alphaValue}`);
6618
+ });
6619
+
6620
+ // Also handle direct color values that might not use CSS variables
6621
+ const colorProperties = ['color', 'background-color', 'border-color', 'text-decoration-color', 'outline-color', 'fill', 'stroke', 'caret-color', 'accent-color'];
6622
+ colorProperties.forEach(prop => {
6623
+ // Match rgb(), rgba(), hsl(), hsla() functions
6624
+ const rgbRegex = new RegExp(`(${prop}\\s*:\\s*)rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)`, 'gi');
6625
+ const rgbaRegex = new RegExp(`(${prop}\\s*:\\s*)rgba\\((\\d+),\\s*(\\d+),\\s*(\\d+),\\s*[\\d.]+\\)`, 'gi');
6626
+ const hslRegex = new RegExp(`(${prop}\\s*:\\s*)hsl\\((\\d+),\\s*([\\d.]+%),\\s*([\\d.]+%)\\)`, 'gi');
6627
+ const hslaRegex = new RegExp(`(${prop}\\s*:\\s*)hsla\\((\\d+),\\s*([\\d.]+%),\\s*([\\d.]+%),\\s*[\\d.]+\\)`, 'gi');
6628
+
6629
+ // Convert rgb to rgba with opacity
6630
+ modifiedDeclaration = modifiedDeclaration.replace(rgbRegex, `$1rgba($2, $3, $4, ${alphaValue})`);
6631
+
6632
+ // Update existing rgba opacity
6633
+ modifiedDeclaration = modifiedDeclaration.replace(rgbaRegex, `$1rgba($2, $3, $4, ${alphaValue})`);
6634
+
6635
+ // Convert hsl to hsla with opacity
6636
+ modifiedDeclaration = modifiedDeclaration.replace(hslRegex, `$1hsla($2, $3, $4, ${alphaValue})`);
6637
+
6638
+ // Update existing hsla opacity
6639
+ modifiedDeclaration = modifiedDeclaration.replace(hslaRegex, `$1hsla($2, $3, $4, ${alphaValue})`);
6640
+
6641
+ // Handle hex colors
6642
+ const hexRegex = new RegExp(`(${prop}\\s*:\\s*)(#[0-9a-fA-F]{3,6})`, 'gi');
6643
+ modifiedDeclaration = modifiedDeclaration.replace(hexRegex, (match, propPart, hexColor) => {
6644
+ // Convert hex to rgba
6645
+ const hex = hexColor.replace('#', '');
6646
+ let r, g, b;
6647
+ if (hex.length === 3) {
6648
+ r = parseInt(hex[0] + hex[0], 16);
6649
+ g = parseInt(hex[1] + hex[1], 16);
6650
+ b = parseInt(hex[2] + hex[2], 16);
6651
+ } else {
6652
+ r = parseInt(hex.substring(0, 2), 16);
6653
+ g = parseInt(hex.substring(2, 4), 16);
6654
+ b = parseInt(hex.substring(4, 6), 16);
6655
+ }
6656
+ return `${propPart}rgba(${r}, ${g}, ${b}, ${alphaValue})`;
6657
+ });
6658
+ });
6659
+ return modifiedDeclaration;
6660
+ }
6661
+
6662
+ /**
6663
+ * Convert Tailwind class string to inline CSS styles or JSON object
6664
+ * @param {string} classNames - String containing Tailwind classes to convert
6665
+ * @param {boolean} convertToJson - If true, result will be JSON object, if false becomes CSS string
6666
+ * @returns {string|Object} Inline CSS string or style JSON object
6602
6667
  */
6603
6668
  function tws(classNames, convertToJson) {
6604
6669
  const totalMarker = performanceMonitor.start("tws:total");
@@ -6610,10 +6675,10 @@ function tws(classNames, convertToJson) {
6610
6675
  let classes;
6611
6676
  try {
6612
6677
  const parseMarker = performanceMonitor.start("tws:parse");
6613
- classes = classNames.match(/[\w-\/]+(?:\[[^\]]+\])?/g);
6678
+ classes = classNames.match(/[\w-\/]+(?:\/\d+)?(?:\[[^\]]+\])?/g);
6614
6679
  performanceMonitor.end(parseMarker);
6615
6680
 
6616
- // Jika tidak ada class yang valid ditemukan
6681
+ // If no valid classes are found
6617
6682
  if (!classes || classes.length === 0) {
6618
6683
  console.warn(`No valid Tailwind classes found in input: "${classNames}"`);
6619
6684
  performanceMonitor.end(totalMarker);
@@ -6628,16 +6693,27 @@ function tws(classNames, convertToJson) {
6628
6693
  // Process classes with performance monitoring
6629
6694
  const processMarker = performanceMonitor.start("tws:process");
6630
6695
  let cssResult = classes.map(className => {
6631
- let result = cssObject[className] || cssObject[className.replace(/(\/)/g, "\\$1")] || cssObject[className.replace(/\./g, "\\.")];
6696
+ // Extract base class name without opacity modifier
6697
+ const baseClassName = className.replace(/\/\d+$/, '');
6698
+ let result = cssObject[baseClassName] || cssObject[baseClassName.replace(/(\/)/g, "\\$1")] || cssObject[baseClassName.replace(/\./g, "\\.")];
6632
6699
  if (result) {
6700
+ // Apply opacity modifier if present
6701
+ if (className.includes('/') && /\/\d+$/.test(className)) {
6702
+ result = processOpacityModifier(className, result);
6703
+ }
6633
6704
  return resolveCssToClearCss(result);
6634
- } else if (className.includes("[")) {
6635
- const match = className.match(/\[([^\]]+)\]/);
6705
+ } else if (baseClassName.includes("[")) {
6706
+ const match = baseClassName.match(/\[([^\]]+)\]/);
6636
6707
  if (match) {
6637
6708
  const customValue = match[1];
6638
- const baseKey = className.split("[")[0];
6709
+ const baseKey = baseClassName.split("[")[0];
6639
6710
  if (cssObject[`${baseKey}custom`]) {
6640
- return cssObject[`${baseKey}custom`].replace(/custom_value/g, customValue);
6711
+ let customResult = cssObject[`${baseKey}custom`].replace(/custom_value/g, customValue);
6712
+ // Apply opacity modifier to custom values too
6713
+ if (className.includes('/') && /\/\d+$/.test(className)) {
6714
+ customResult = processOpacityModifier(className, customResult);
6715
+ }
6716
+ return customResult;
6641
6717
  }
6642
6718
  }
6643
6719
  }
@@ -6768,9 +6844,12 @@ function processClass(cls, selector, styles) {
6768
6844
  media,
6769
6845
  finalSelector
6770
6846
  } = resolveVariants(selector, rawVariants);
6771
- let declarations = cssObject[pureClassName] || cssObject[pureClassName.replace(/(\/)/g, "\\$1")] || cssObject[pureClassName.replace(/\./g, "\\.")];
6772
- if (!declarations && pureClassName.includes("[")) {
6773
- const match = pureClassName.match(/^(.+?)\[(.+)\]$/);
6847
+
6848
+ // Extract base class name without opacity modifier for CSS lookup
6849
+ const baseClassName = pureClassName.replace(/\/\d+$/, '');
6850
+ let declarations = cssObject[baseClassName] || cssObject[baseClassName.replace(/(\/)/g, "\\$1")] || cssObject[baseClassName.replace(/\./g, "\\.")];
6851
+ if (!declarations && baseClassName.includes("[")) {
6852
+ const match = baseClassName.match(/^(.+?)\[(.+)\]$/);
6774
6853
  if (match) {
6775
6854
  const [, prefix, dynamicValue] = match;
6776
6855
  const customKey = `${prefix}custom`;
@@ -6781,17 +6860,22 @@ function processClass(cls, selector, styles) {
6781
6860
  }
6782
6861
  }
6783
6862
  if (!declarations) {
6784
- declarations = parseCustomClassWithPatterns(pureClassName);
6863
+ declarations = parseCustomClassWithPatterns(baseClassName);
6785
6864
  }
6786
6865
  if (!declarations) {
6787
6866
  return;
6788
6867
  }
6868
+
6869
+ // Apply opacity modifier if present
6870
+ if (pureClassName.includes('/') && /\/\d+$/.test(pureClassName)) {
6871
+ declarations = processOpacityModifier(pureClassName, declarations);
6872
+ }
6789
6873
  if (isImportant) {
6790
6874
  declarations = declarations.replace(/([^:;]+):([^;]+)(;?)/g, (_, prop, value) => {
6791
6875
  return prop.trim().startsWith("--") ? `${prop}:${value};` : `${prop}:${value.trim()} !important;`;
6792
6876
  });
6793
6877
  }
6794
- const isSpaceOrDivide = ["space-x-", "-space-x-", "space-y-", "-space-y-", "divide-"].some(prefix => pureClassName.startsWith(prefix));
6878
+ const isSpaceOrDivide = ["space-x-", "-space-x-", "space-y-", "-space-y-", "divide-"].some(prefix => baseClassName.startsWith(prefix));
6795
6879
  const expandedSelector = replaceSelector(finalSelector);
6796
6880
  const targetSelector = isSpaceOrDivide ? `${expandedSelector} > :not([hidden]) ~ :not([hidden])` : expandedSelector;
6797
6881
  if (media) {
@@ -6959,11 +7043,11 @@ function generateCssString(styles) {
6959
7043
  }
6960
7044
 
6961
7045
  /**
6962
- * Menghasilkan string CSS dari objek style dengan sintaks mirip SCSS
6963
- * Mendukung nested selectors, state variants, responsive variants, dan @css directives
6964
- * @param {Object} obj - Objek dengan format style mirip SCSS
6965
- * @param {Object} [options] - Opsi tambahan, misal { inject: true/false }
6966
- * @returns {string} String CSS yang dihasilkan
7046
+ * Generate CSS string from style object with SCSS-like syntax
7047
+ * Supports nested selectors, state variants, responsive variants, and @css directives
7048
+ * @param {Object} obj - Object with SCSS-like style format
7049
+ * @param {Object} [options] - Additional options, e.g. { inject: true/false }
7050
+ * @returns {string} Generated CSS string
6967
7051
  */
6968
7052
  function twsx(obj) {
6969
7053
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -7023,7 +7107,7 @@ function twsx(obj) {
7023
7107
  }
7024
7108
  }
7025
7109
 
7026
- // Fungsi hashCode sederhana untuk deduplikasi CSS
7110
+ // Simple hashCode function for CSS deduplication
7027
7111
  function getCssHash(str) {
7028
7112
  let hash = 0,
7029
7113
  i,
@@ -7037,7 +7121,7 @@ function getCssHash(str) {
7037
7121
  return hash;
7038
7122
  }
7039
7123
 
7040
- // Enhanced auto-inject CSS dengan performance monitoring
7124
+ // Enhanced auto-inject CSS with performance monitoring
7041
7125
  const injectedCssHashSet = new Set();
7042
7126
  function autoInjectCss(cssString) {
7043
7127
  const marker = performanceMonitor.start("css:inject");
@@ -7069,24 +7153,24 @@ function autoInjectCss(cssString) {
7069
7153
  }
7070
7154
  }
7071
7155
 
7072
- // Enhanced debounced functions dengan konfigurasi performance monitoring
7156
+ // Enhanced debounced functions with performance monitoring configuration
7073
7157
  /**
7074
- * Versi debounced dari fungsi tws dengan performance monitoring
7075
- * @param {string} classNames - String berisi kelas Tailwind yang akan dikonversi
7076
- * @param {boolean} convertToJson - Jika true, hasil akan menjadi objek JSON, jika false menjadi string CSS
7077
- * @returns {string|Object} String CSS inline atau objek style JSON
7158
+ * Debounced version of tws function with performance monitoring
7159
+ * @param {string} classNames - String containing Tailwind classes to convert
7160
+ * @param {boolean} convertToJson - If true, result will be JSON object, if false becomes CSS string
7161
+ * @returns {string|Object} Inline CSS string or style JSON object
7078
7162
  */
7079
7163
  const debouncedTws = debounce(tws, 50); // Faster debounce for tws
7080
7164
 
7081
7165
  /**
7082
- * Versi debounced dari fungsi twsx dengan performance monitoring
7083
- * @param {Object} obj - Objek dengan format style mirip SCSS
7084
- * @param {Object} [options] - Opsi tambahan
7085
- * @returns {string} String CSS yang dihasilkan
7166
+ * Debounced version of twsx function with performance monitoring
7167
+ * @param {Object} obj - Object with SCSS-like style format
7168
+ * @param {Object} [options] - Additional options
7169
+ * @returns {string} Generated CSS string
7086
7170
  */
7087
7171
  const debouncedTwsx = debounce(twsx, 100); // Standard debounce for twsx
7088
7172
 
7089
- // Export performance utilities untuk debugging
7173
+ // Export performance utilities for debugging
7090
7174
  const performanceUtils = {
7091
7175
  getStats() {
7092
7176
  return {
@@ -0,0 +1,82 @@
1
+ // Type definitions for tailwind-to-style
2
+ // Project: https://github.com/your-username/tailwind-to-style
3
+ // Definitions by: Your Name <https://github.com/your-username>
4
+
5
+ export interface TwsxOptions {
6
+ inject?: boolean;
7
+ [key: string]: any;
8
+ }
9
+
10
+ export interface PerformanceStats {
11
+ cacheStats: {
12
+ cssResolution: number;
13
+ configOptions: number;
14
+ parseSelector: number;
15
+ encodeBracket: number;
16
+ decodeBracket: number;
17
+ };
18
+ injectionStats: {
19
+ uniqueStylesheets: number;
20
+ };
21
+ }
22
+
23
+ export interface PerformanceUtils {
24
+ getStats(): PerformanceStats;
25
+ clearCaches(): void;
26
+ enablePerformanceLogging(enabled?: boolean): void;
27
+ }
28
+
29
+ export interface StyleObject {
30
+ [selector: string]: string | StyleObject | Array<string | StyleObject>;
31
+ }
32
+
33
+ /**
34
+ * Converts Tailwind CSS classes to inline styles or JSON object
35
+ * @param classNames - String containing Tailwind classes to convert
36
+ * @param convertToJson - If true, returns JSON object, if false returns CSS string
37
+ * @returns CSS inline string or style JSON object
38
+ */
39
+ export function tws(classNames: string, convertToJson?: false): string;
40
+ export function tws(classNames: string, convertToJson: true): Record<string, string>;
41
+ export function tws(classNames: string, convertToJson?: boolean): string | Record<string, string>;
42
+
43
+ /**
44
+ * Generates CSS string from style object with SCSS-like syntax
45
+ * Supports nested selectors, state variants, responsive variants, and @css directives
46
+ * @param obj - Object with SCSS-like style format
47
+ * @param options - Additional options
48
+ * @returns Generated CSS string
49
+ */
50
+ export function twsx(obj: StyleObject, options?: TwsxOptions): string;
51
+
52
+ /**
53
+ * Debounced version of tws function with performance monitoring
54
+ * @param classNames - String containing Tailwind classes to convert
55
+ * @param convertToJson - If true, returns JSON object, if false returns CSS string
56
+ * @returns CSS inline string or style JSON object
57
+ */
58
+ export const debouncedTws: typeof tws;
59
+
60
+ /**
61
+ * Debounced version of twsx function with performance monitoring
62
+ * @param obj - Object with SCSS-like style format
63
+ * @param options - Additional options
64
+ * @returns Generated CSS string
65
+ */
66
+ export const debouncedTwsx: typeof twsx;
67
+
68
+ /**
69
+ * Performance utilities for debugging and monitoring
70
+ */
71
+ export const performanceUtils: PerformanceUtils;
72
+
73
+ // Default export (if needed)
74
+ declare const tailwindToStyle: {
75
+ tws: typeof tws;
76
+ twsx: typeof twsx;
77
+ debouncedTws: typeof debouncedTws;
78
+ debouncedTwsx: typeof debouncedTwsx;
79
+ performanceUtils: typeof performanceUtils;
80
+ };
81
+
82
+ export default tailwindToStyle;