tailwind-to-style 2.12.1 → 2.12.3

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.
@@ -1,11 +1,11 @@
1
1
  /**
2
- * tailwind-to-style v2.12.1
2
+ * tailwind-to-style v2.12.3
3
3
  * Convert tailwind classes to inline style
4
4
  *
5
5
  * @author Bigetion
6
6
  * @license MIT
7
7
  */
8
- import React, { useMemo, useEffect, useState, createElement, createContext, useContext } from 'react';
8
+ import React, { useContext, createContext, useMemo, useEffect, useState, useRef, createElement } from 'react';
9
9
 
10
10
  /**
11
11
  * Tailwind Variants (tv) - Type-safe variant system
@@ -1724,9 +1724,9 @@ const theme = {
1724
1724
  };
1725
1725
 
1726
1726
  const vars = {
1727
- transform: `transform: translateX(var(--transform-translate-x, 0)) translateY(var(--transform-translate-y, 0)) rotate(var(--transform-rotate, 0)) skewX(var(--transform-skew-x, 0)) skewY(var(--transform-skew-y, 0)) scaleX(var(--transform-scale-x, 1)) scaleY(var(--transform-scale-y, 1));`,
1728
- filter: `filter: blur(var(--blur, 0)) brightness(var(--brightness, 1)) contrast(var(--contrast, 1)) grayscale(var(--grayscale, 0)) hue-rotate(var(--hue-rotate, 0deg)) invert(var(--invert, 0)) saturate(var(--saturate, 1)) sepia(var(--sepia, 0)) drop-shadow(var(--drop-shadow, 0 0 #0000));`,
1729
- backdropFilter: `-webkit-backdrop-filter: blur(var(--backdrop-blur, 0)) brightness(var(--backdrop-brightness, 1)) contrast(var(--backdrop-contrast, 1)) grayscale(var(--backdrop-grayscale, 0)) hue-rotate(var(--backdrop-hue-rotate, 0deg)) invert(var(--backdrop-invert, 0)) opacity(var(--backdrop-opacity, 1)) saturate(var(--backdrop-saturate, 1)) sepia(var(--backdrop-sepia, 0)); backdrop-filter: blur(var(--backdrop-blur, 0)) brightness(var(--backdrop-brightness, 1)) contrast(var(--backdrop-contrast, 1)) grayscale(var(--backdrop-grayscale, 0)) hue-rotate(var(--backdrop-hue-rotate, 0deg)) invert(var(--backdrop-invert, 0)) opacity(var(--backdrop-opacity, 1)) saturate(var(--backdrop-saturate, 1)) sepia(var(--backdrop-sepia, 0));`
1727
+ transform: "transform: translateX(var(--transform-translate-x, 0)) translateY(var(--transform-translate-y, 0)) rotate(var(--transform-rotate, 0)) skewX(var(--transform-skew-x, 0)) skewY(var(--transform-skew-y, 0)) scaleX(var(--transform-scale-x, 1)) scaleY(var(--transform-scale-y, 1));",
1728
+ filter: "filter: blur(var(--blur, 0)) brightness(var(--brightness, 1)) contrast(var(--contrast, 1)) grayscale(var(--grayscale, 0)) hue-rotate(var(--hue-rotate, 0deg)) invert(var(--invert, 0)) saturate(var(--saturate, 1)) sepia(var(--sepia, 0)) drop-shadow(var(--drop-shadow, 0 0 #0000));",
1729
+ backdropFilter: "-webkit-backdrop-filter: blur(var(--backdrop-blur, 0)) brightness(var(--backdrop-brightness, 1)) contrast(var(--backdrop-contrast, 1)) grayscale(var(--backdrop-grayscale, 0)) hue-rotate(var(--backdrop-hue-rotate, 0deg)) invert(var(--backdrop-invert, 0)) opacity(var(--backdrop-opacity, 1)) saturate(var(--backdrop-saturate, 1)) sepia(var(--backdrop-sepia, 0)); backdrop-filter: blur(var(--backdrop-blur, 0)) brightness(var(--backdrop-brightness, 1)) contrast(var(--backdrop-contrast, 1)) grayscale(var(--backdrop-grayscale, 0)) hue-rotate(var(--backdrop-hue-rotate, 0deg)) invert(var(--backdrop-invert, 0)) opacity(var(--backdrop-opacity, 1)) saturate(var(--backdrop-saturate, 1)) sepia(var(--backdrop-sepia, 0));"
1730
1730
  };
1731
1731
 
1732
1732
  const configOptions = {
@@ -1916,6 +1916,14 @@ const logger = new Logger(isProduction ? "error" : "warn");
1916
1916
  */
1917
1917
 
1918
1918
 
1919
+ // Import clearConfigCache - will be set after module loads
1920
+ let clearConfigCache$1 = null;
1921
+
1922
+ // Lazy import to avoid circular dependency
1923
+ function setClearConfigCache(fn) {
1924
+ clearConfigCache$1 = fn;
1925
+ }
1926
+
1919
1927
  /**
1920
1928
  * User configuration state
1921
1929
  */
@@ -1928,6 +1936,9 @@ let userConfig = {
1928
1936
  prefix: ""
1929
1937
  };
1930
1938
 
1939
+ // Cache for extended theme to avoid redundant lookups
1940
+ const extendedThemeCache = new Map();
1941
+
1931
1942
  /**
1932
1943
  * Deep merge two objects
1933
1944
  * @param {Object} target - Target object
@@ -1983,6 +1994,9 @@ function configure() {
1983
1994
  return;
1984
1995
  }
1985
1996
 
1997
+ // Clear extended theme cache when config changes
1998
+ extendedThemeCache.clear();
1999
+
1986
2000
  // Merge user config with defaults
1987
2001
  if (config.theme) {
1988
2002
  if (config.theme.extend) {
@@ -2019,6 +2033,11 @@ function configure() {
2019
2033
 
2020
2034
  // Reset cache to apply new configuration
2021
2035
  resetTailwindCache();
2036
+
2037
+ // Also clear config options cache if available
2038
+ if (clearConfigCache$1) {
2039
+ clearConfigCache$1();
2040
+ }
2022
2041
  logger.info("Configuration applied successfully");
2023
2042
  } catch (error) {
2024
2043
  logger.error("Error applying configuration:", error);
@@ -2042,7 +2061,15 @@ function getConfig() {
2042
2061
  * @returns {Object} Extended theme values
2043
2062
  */
2044
2063
  function getExtendedTheme(key) {
2045
- return userConfig.theme.extend[key] || {};
2064
+ // Check cache first
2065
+ if (extendedThemeCache.has(key)) {
2066
+ return extendedThemeCache.get(key);
2067
+ }
2068
+ const result = userConfig.theme.extend[key] || {};
2069
+
2070
+ // Cache the result
2071
+ extendedThemeCache.set(key, result);
2072
+ return result;
2046
2073
  }
2047
2074
 
2048
2075
  /**
@@ -2128,7 +2155,7 @@ function getConfigOptions() {
2128
2155
  }
2129
2156
  function generateCssString$1() {
2130
2157
  let getCssString = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : () => {};
2131
- let orientationPrefix = "";
2158
+ const orientationPrefix = "";
2132
2159
  const hexToRgb = hex => {
2133
2160
  const rgba = hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function () {
2134
2161
  return "#" + (arguments.length <= 1 ? undefined : arguments[1]) + (arguments.length <= 1 ? undefined : arguments[1]) + (arguments.length <= 2 ? undefined : arguments[2]) + (arguments.length <= 2 ? undefined : arguments[2]) + (arguments.length <= 3 ? undefined : arguments[3]) + (arguments.length <= 3 ? undefined : arguments[3]);
@@ -2148,7 +2175,7 @@ function generateCssString$1() {
2148
2175
  let str = "";
2149
2176
  Object.entries(nOptions).forEach(_ref => {
2150
2177
  let [key, value] = _ref;
2151
- str += getStr(key.replace("/", `\\/`).replace(".", "\\."), value);
2178
+ str += getStr(key.replace("/", "\\/").replace(".", "\\."), value);
2152
2179
  });
2153
2180
  return str;
2154
2181
  };
@@ -4310,7 +4337,7 @@ function generator$1N() {
4310
4337
  let {
4311
4338
  getCssByOptions
4312
4339
  } = _ref;
4313
- let cssString = getCssByOptions(propertyOptions, (key, value) => `
4340
+ const cssString = getCssByOptions(propertyOptions, (key, value) => `
4314
4341
  ${prefix}-${key} {
4315
4342
  float: ${value};
4316
4343
  }
@@ -4596,7 +4623,7 @@ function generator$1D() {
4596
4623
  prefix: globalPrefix,
4597
4624
  theme = {}
4598
4625
  } = configOptions;
4599
- let prefix = `${globalPrefix}auto-cols`;
4626
+ const prefix = `${globalPrefix}auto-cols`;
4600
4627
  const {
4601
4628
  gridAutoColumns = {}
4602
4629
  } = theme;
@@ -4646,7 +4673,7 @@ function generator$1B() {
4646
4673
  prefix: globalPrefix,
4647
4674
  theme = {}
4648
4675
  } = configOptions;
4649
- let prefix = `${globalPrefix}auto-rows`;
4676
+ const prefix = `${globalPrefix}auto-rows`;
4650
4677
  const {
4651
4678
  gridAutoRows = {}
4652
4679
  } = theme;
@@ -4670,7 +4697,7 @@ function generator$1A() {
4670
4697
  prefix: globalPrefix,
4671
4698
  theme = {}
4672
4699
  } = configOptions;
4673
- let prefix = `${globalPrefix}col`;
4700
+ const prefix = `${globalPrefix}col`;
4674
4701
  const {
4675
4702
  gridColumn = {}
4676
4703
  } = theme;
@@ -4694,7 +4721,7 @@ function generator$1z() {
4694
4721
  prefix: globalPrefix,
4695
4722
  theme = {}
4696
4723
  } = configOptions;
4697
- let prefix = `${globalPrefix}col-end`;
4724
+ const prefix = `${globalPrefix}col-end`;
4698
4725
  const {
4699
4726
  gridColumnEnd = {}
4700
4727
  } = theme;
@@ -4718,7 +4745,7 @@ function generator$1y() {
4718
4745
  prefix: globalPrefix,
4719
4746
  theme = {}
4720
4747
  } = configOptions;
4721
- let prefix = `${globalPrefix}col-start`;
4748
+ const prefix = `${globalPrefix}col-start`;
4722
4749
  const {
4723
4750
  gridColumnStart = {}
4724
4751
  } = theme;
@@ -4742,7 +4769,7 @@ function generator$1x() {
4742
4769
  prefix: globalPrefix,
4743
4770
  theme = {}
4744
4771
  } = configOptions;
4745
- let prefix = `${globalPrefix}row`;
4772
+ const prefix = `${globalPrefix}row`;
4746
4773
  const {
4747
4774
  gridRow = {}
4748
4775
  } = theme;
@@ -4766,7 +4793,7 @@ function generator$1w() {
4766
4793
  prefix: globalPrefix,
4767
4794
  theme = {}
4768
4795
  } = configOptions;
4769
- let prefix = `${globalPrefix}row-end`;
4796
+ const prefix = `${globalPrefix}row-end`;
4770
4797
  const {
4771
4798
  gridRowEnd = {}
4772
4799
  } = theme;
@@ -4790,7 +4817,7 @@ function generator$1v() {
4790
4817
  prefix: globalPrefix,
4791
4818
  theme = {}
4792
4819
  } = configOptions;
4793
- let prefix = `${globalPrefix}row-start`;
4820
+ const prefix = `${globalPrefix}row-start`;
4794
4821
  const {
4795
4822
  gridRowStart = {}
4796
4823
  } = theme;
@@ -4814,7 +4841,7 @@ function generator$1u() {
4814
4841
  prefix: globalPrefix,
4815
4842
  theme = {}
4816
4843
  } = configOptions;
4817
- let prefix = `${globalPrefix}grid-cols`;
4844
+ const prefix = `${globalPrefix}grid-cols`;
4818
4845
  const {
4819
4846
  gridTemplateColumns = {}
4820
4847
  } = theme;
@@ -4822,7 +4849,7 @@ function generator$1u() {
4822
4849
  let {
4823
4850
  getCssByOptions
4824
4851
  } = _ref;
4825
- let cssString = getCssByOptions(gridTemplateColumns, (key, value) => `
4852
+ const cssString = getCssByOptions(gridTemplateColumns, (key, value) => `
4826
4853
  ${prefix}-${key} {
4827
4854
  grid-template-columns: ${isNaN(value) ? value : `repeat(${value}, minmax(0, 1fr))`};
4828
4855
  }
@@ -4838,7 +4865,7 @@ function generator$1t() {
4838
4865
  prefix: globalPrefix,
4839
4866
  theme = {}
4840
4867
  } = configOptions;
4841
- let prefix = `${globalPrefix}grid-rows`;
4868
+ const prefix = `${globalPrefix}grid-rows`;
4842
4869
  const {
4843
4870
  gridTemplateRows = {}
4844
4871
  } = theme;
@@ -4846,7 +4873,7 @@ function generator$1t() {
4846
4873
  let {
4847
4874
  getCssByOptions
4848
4875
  } = _ref;
4849
- let cssString = getCssByOptions(gridTemplateRows, (key, value) => `
4876
+ const cssString = getCssByOptions(gridTemplateRows, (key, value) => `
4850
4877
  ${prefix}-${key} {
4851
4878
  grid-template-rows: ${isNaN(value) ? value : `repeat(${value}, minmax(0, 1fr));`};
4852
4879
  }
@@ -8028,6 +8055,17 @@ function resolveCssToClearCss(cssString) {
8028
8055
 
8029
8056
  // Cache for getConfigOptions - use LRU cache
8030
8057
  const configOptionsCache = new LRUCache(500);
8058
+
8059
+ /**
8060
+ * Clear config options cache (internal use)
8061
+ * Called when user configuration changes
8062
+ */
8063
+ function clearConfigCache() {
8064
+ configOptionsCache.clear();
8065
+ }
8066
+
8067
+ // Register clearConfigCache with userConfig module
8068
+ setClearConfigCache(clearConfigCache);
8031
8069
  function generateTailwindCssString() {
8032
8070
  var _options$theme, _userConfigData$theme;
8033
8071
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
@@ -9070,12 +9108,24 @@ function autoInjectCss(cssString) {
9070
9108
  */
9071
9109
 
9072
9110
 
9111
+ // Create context for config change notifications
9112
+ const TwsxConfigContext = /*#__PURE__*/createContext(null);
9113
+
9073
9114
  // Global CSS cache to prevent duplicate injections
9074
9115
  const globalCssCache = new Map();
9075
9116
  const globalKeyframesCache = new Map(); // Track injected keyframes: name -> css
9076
9117
  let globalStyleElement = null;
9077
9118
  let globalKeyframesElement = null;
9078
9119
 
9120
+ // Export function to clear caches (called when config changes)
9121
+ function clearTwsxCache() {
9122
+ // Clear internal caches so new CSS can be generated
9123
+ globalCssCache.clear();
9124
+ globalKeyframesCache.clear();
9125
+ // DON'T clear DOM content - let new CSS replace it naturally
9126
+ // This prevents white flash when navigating between pages
9127
+ }
9128
+
9079
9129
  /**
9080
9130
  * Simple hash function for CSS content
9081
9131
  * @param {string} str - String to hash
@@ -9193,14 +9243,23 @@ function updateGlobalKeyframes(keyframes) {
9193
9243
  if (hasNewKeyframes) {
9194
9244
  // Rebuild all keyframes CSS from cache
9195
9245
  const allKeyframesCSS = Array.from(globalKeyframesCache.values()).join("\n");
9196
- keyframesElement.textContent = allKeyframesCSS;
9246
+
9247
+ // Only update if content actually changed
9248
+ if (keyframesElement.textContent !== allKeyframesCSS) {
9249
+ keyframesElement.textContent = allKeyframesCSS;
9250
+ }
9197
9251
  }
9198
9252
  }
9199
9253
  function updateGlobalCSS() {
9200
9254
  if (typeof document === "undefined") return;
9201
9255
  const styleElement = getOrCreateGlobalStyleElement();
9256
+ // Rebuild CSS from cache - this replaces old content with fresh content
9202
9257
  const allCSS = Array.from(globalCssCache.values()).join("\n");
9203
- styleElement.textContent = allCSS;
9258
+
9259
+ // Only update if content actually changed to avoid unnecessary reflows
9260
+ if (styleElement.textContent !== allCSS) {
9261
+ styleElement.textContent = allCSS;
9262
+ }
9204
9263
  }
9205
9264
 
9206
9265
  /**
@@ -9215,22 +9274,34 @@ function updateGlobalCSS() {
9215
9274
  */
9216
9275
  function useTwsx(styles) {
9217
9276
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
9277
+ // Get config version from context to trigger re-generation when config changes
9278
+ const configContext = useContext(TwsxConfigContext);
9279
+ const configVersion = (configContext === null || configContext === void 0 ? void 0 : configContext.version) || 0;
9280
+
9281
+ // Stable options reference to prevent unnecessary re-renders
9282
+ const injectOption = options.inject !== false; // Default to true
9283
+
9284
+ // Serialize styles for stable dependency checking
9285
+ const stylesKey = useMemo(() => {
9286
+ if (!styles) return null;
9287
+ return typeof styles === "string" ? styles : JSON.stringify(styles);
9288
+ }, [styles]);
9289
+
9218
9290
  // Generate CSS with memoization for performance
9291
+ // Include configVersion in deps to regenerate when config changes
9219
9292
  const css = useMemo(() => {
9220
9293
  if (!styles) return "";
9221
- console.log("useTwsx - Input styles:", styles);
9222
9294
  try {
9223
9295
  const result = twsx(styles, {
9224
9296
  inject: false,
9225
9297
  ...options
9226
9298
  });
9227
- console.log("useTwsx - Generated CSS:", result);
9228
9299
  return result;
9229
9300
  } catch (error) {
9230
9301
  console.error("TWSX Error:", error);
9231
9302
  return "";
9232
9303
  }
9233
- }, [styles, options]);
9304
+ }, [stylesKey, configVersion]); // Remove options from deps
9234
9305
 
9235
9306
  // Create a unique key for this CSS based on content hash
9236
9307
  const cssKey = useMemo(() => {
@@ -9241,7 +9312,7 @@ function useTwsx(styles) {
9241
9312
 
9242
9313
  // Auto-inject CSS into global style element (unless inject: false)
9243
9314
  useEffect(() => {
9244
- if (!css || !cssKey || options.inject === false) return;
9315
+ if (!css || !cssKey || !injectOption) return;
9245
9316
 
9246
9317
  // Extract keyframes from CSS
9247
9318
  const keyframes = extractKeyframes(css);
@@ -9256,9 +9327,6 @@ function useTwsx(styles) {
9256
9327
  if (!globalCssCache.has(cssKey)) {
9257
9328
  globalCssCache.set(cssKey, cssWithoutKeyframes);
9258
9329
  updateGlobalCSS();
9259
- console.log("useTwsx - Injected NEW CSS with key:", cssKey);
9260
- } else {
9261
- console.log("useTwsx - CSS already cached with key:", cssKey);
9262
9330
  }
9263
9331
 
9264
9332
  // Cleanup function - use ref counting to track usage
@@ -9266,7 +9334,8 @@ function useTwsx(styles) {
9266
9334
  // Note: We don't delete immediately as other components might use same styles
9267
9335
  // In a production implementation, you'd want ref counting here
9268
9336
  };
9269
- }, [css, cssKey, options.inject]);
9337
+ }, [css, cssKey, injectOption]); // Stable dependencies
9338
+
9270
9339
  return css;
9271
9340
  }
9272
9341
 
@@ -9293,23 +9362,39 @@ function TwsxProvider(_ref) {
9293
9362
  } = _ref;
9294
9363
  const [currentConfig, setCurrentConfig] = useState(null);
9295
9364
  const [isConfigured, setIsConfigured] = useState(false);
9365
+ const [configVersion, setConfigVersion] = useState(0);
9366
+
9367
+ // Use ref to store callback without triggering re-renders
9368
+ const onConfigChangeRef = useRef(onConfigChange);
9369
+ useEffect(() => {
9370
+ onConfigChangeRef.current = onConfigChange;
9371
+ }, [onConfigChange]);
9372
+
9373
+ // Serialize config for stable comparison
9374
+ const configKey = useMemo(() => {
9375
+ return config ? JSON.stringify(config) : null;
9376
+ }, [config]);
9296
9377
 
9297
9378
  // Apply configuration on mount and when config changes
9298
9379
  useEffect(() => {
9299
- if (config) {
9300
- try {
9301
- configure(config);
9302
- setCurrentConfig(config);
9303
- setIsConfigured(true);
9304
- if (onConfigChange) {
9305
- onConfigChange(config);
9306
- }
9307
- } catch (error) {
9308
- console.error("TWSX Configuration Error:", error);
9309
- setIsConfigured(false);
9380
+ if (!config) return;
9381
+ try {
9382
+ // Clear caches before applying new config
9383
+ clearTwsxCache();
9384
+ configure(config);
9385
+ setCurrentConfig(config);
9386
+ setIsConfigured(true);
9387
+
9388
+ // Increment version to trigger useTwsx re-generation
9389
+ setConfigVersion(prev => prev + 1);
9390
+ if (onConfigChangeRef.current) {
9391
+ onConfigChangeRef.current(config);
9310
9392
  }
9393
+ } catch (error) {
9394
+ console.error("TWSX Configuration Error:", error);
9395
+ setIsConfigured(false);
9311
9396
  }
9312
- }, [config, onConfigChange]);
9397
+ }, [configKey]); // Only re-run when config actually changes
9313
9398
 
9314
9399
  // Function to update configuration dynamically
9315
9400
  const updateConfig = newConfig => {
@@ -9332,9 +9417,16 @@ function TwsxProvider(_ref) {
9332
9417
  updateConfig,
9333
9418
  isConfigured
9334
9419
  };
9420
+
9421
+ // Provide config version for useTwsx to detect changes
9422
+ const configContextValue = {
9423
+ version: configVersion
9424
+ };
9335
9425
  return /*#__PURE__*/createElement(TwsxContext.Provider, {
9336
9426
  value: contextValue
9337
- }, children);
9427
+ }, /*#__PURE__*/createElement(TwsxConfigContext.Provider, {
9428
+ value: configContextValue
9429
+ }, children));
9338
9430
  }
9339
9431
 
9340
9432
  /**