tw-mrg 1.0.0
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/LICENSE +9 -0
- package/README.md +197 -0
- package/dist/index.d.ts +650 -0
- package/dist/index.js +2897 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class value that can be passed to tw(), twJoin(), etc.
|
|
3
|
+
* Supports strings, arrays, objects (clsx-style), and nested combinations.
|
|
4
|
+
*/
|
|
5
|
+
type ClassValue = string | number | bigint | boolean | null | undefined | ClassValue[] | Record<string, boolean | null | undefined>;
|
|
6
|
+
/**
|
|
7
|
+
* A parsed class token representing a single Tailwind class.
|
|
8
|
+
*/
|
|
9
|
+
interface ClassToken {
|
|
10
|
+
/** Original class string (e.g., "hover:md:!text-red-500") */
|
|
11
|
+
raw: string;
|
|
12
|
+
/** Extracted modifiers (e.g., ["hover", "md"]) */
|
|
13
|
+
modifiers: string[];
|
|
14
|
+
/** Pre-computed sorted modifier key for conflict resolution (e.g., "hover:md") */
|
|
15
|
+
modifierKey: string;
|
|
16
|
+
/** Whether this class has an !important modifier */
|
|
17
|
+
hasImportant: boolean;
|
|
18
|
+
/** The utility part without modifiers (e.g., "text-red-500") */
|
|
19
|
+
utility: string;
|
|
20
|
+
/** The class group this belongs to, or null if unknown */
|
|
21
|
+
group: string | null;
|
|
22
|
+
/** Whether this is an arbitrary value (e.g., [color:red]) */
|
|
23
|
+
isArbitrary: boolean;
|
|
24
|
+
/** For arbitrary values, the CSS property (e.g., "color") */
|
|
25
|
+
arbitraryProperty?: string;
|
|
26
|
+
/** Specificity level for conflict resolution (higher = more specific) */
|
|
27
|
+
specificity: number;
|
|
28
|
+
/** Original position in input for stable sorting */
|
|
29
|
+
position: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Result of resolving conflicts between class tokens.
|
|
33
|
+
*/
|
|
34
|
+
interface ResolvedClasses {
|
|
35
|
+
/** Tokens that were kept after resolution */
|
|
36
|
+
kept: ClassToken[];
|
|
37
|
+
/** Tokens that were removed due to conflicts */
|
|
38
|
+
removed: RemovedClass[];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Information about a class that was removed during resolution.
|
|
42
|
+
*/
|
|
43
|
+
interface RemovedClass {
|
|
44
|
+
/** The removed class token */
|
|
45
|
+
token: ClassToken;
|
|
46
|
+
/** The class that overrode this one */
|
|
47
|
+
overriddenBy: ClassToken;
|
|
48
|
+
/** Reason for removal */
|
|
49
|
+
reason: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Tailwind merge mode configuration.
|
|
53
|
+
* - 'full': All Tailwind classes supported (~7KB gzipped)
|
|
54
|
+
* - 'minimal': Common classes only (~1.5KB gzipped)
|
|
55
|
+
* - false: Disable merging (just concatenate)
|
|
56
|
+
*/
|
|
57
|
+
type TwMergeMode = 'full' | 'minimal' | false;
|
|
58
|
+
/**
|
|
59
|
+
* Configuration for createTw().
|
|
60
|
+
*/
|
|
61
|
+
interface TwConfig {
|
|
62
|
+
/** Tailwind prefix (e.g., "tw-") */
|
|
63
|
+
prefix?: string;
|
|
64
|
+
/** Cache size for merge results (0 to disable) */
|
|
65
|
+
cacheSize?: number;
|
|
66
|
+
/** Enable debug mode by default */
|
|
67
|
+
debug?: boolean;
|
|
68
|
+
/** Merge mode: 'full', 'minimal', or false to disable */
|
|
69
|
+
mode?: TwMergeMode;
|
|
70
|
+
/** Extensions to default configuration */
|
|
71
|
+
extend?: TwConfigExtension;
|
|
72
|
+
/** Overrides for default configuration */
|
|
73
|
+
override?: TwConfigOverride;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Extensions to add to the default configuration.
|
|
77
|
+
*/
|
|
78
|
+
interface TwConfigExtension {
|
|
79
|
+
/** Additional class groups */
|
|
80
|
+
classGroups?: Record<string, string[] | RegExp[]>;
|
|
81
|
+
/** Additional conflict mappings */
|
|
82
|
+
conflictingClassGroups?: Record<string, string[]>;
|
|
83
|
+
/** Additional prefix mappings for trie */
|
|
84
|
+
prefixes?: Record<string, {
|
|
85
|
+
groupId: string;
|
|
86
|
+
specificity?: number;
|
|
87
|
+
}>;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Overrides to replace parts of the default configuration.
|
|
91
|
+
*/
|
|
92
|
+
interface TwConfigOverride {
|
|
93
|
+
/** Replace class groups entirely */
|
|
94
|
+
classGroups?: Record<string, string[] | RegExp[]>;
|
|
95
|
+
/** Replace conflict mappings entirely */
|
|
96
|
+
conflictingClassGroups?: Record<string, string[]>;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Configuration for twStates().
|
|
100
|
+
*/
|
|
101
|
+
interface TwStatesConfig<V extends VariantsSchema = VariantsSchema> {
|
|
102
|
+
/** Base classes always applied */
|
|
103
|
+
base?: ClassValue;
|
|
104
|
+
/** Variant definitions */
|
|
105
|
+
variants?: V;
|
|
106
|
+
/** Default variant values */
|
|
107
|
+
defaultVariants?: VariantProps<V>;
|
|
108
|
+
/** Compound variants for specific combinations */
|
|
109
|
+
compoundVariants?: CompoundVariant<V>[];
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Schema for variant definitions.
|
|
113
|
+
*/
|
|
114
|
+
type VariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
115
|
+
/**
|
|
116
|
+
* Converts variant keys to their accepted prop types.
|
|
117
|
+
* Handles boolean-like keys: 'true'/'false' accept actual booleans.
|
|
118
|
+
*/
|
|
119
|
+
type VariantKeyToProp<K> = K extends 'true' | 'false' ? boolean | K : K;
|
|
120
|
+
/**
|
|
121
|
+
* Props derived from a variants schema.
|
|
122
|
+
*/
|
|
123
|
+
type VariantProps<V extends VariantsSchema> = {
|
|
124
|
+
[K in keyof V]?: VariantKeyToProp<keyof V[K]> | null | undefined;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* A compound variant that applies when multiple variants match.
|
|
128
|
+
* Uses the same prop types as VariantProps for consistency.
|
|
129
|
+
*/
|
|
130
|
+
type CompoundVariant<V extends VariantsSchema> = VariantProps<V> & {
|
|
131
|
+
class?: ClassValue;
|
|
132
|
+
className?: ClassValue;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Return type of twStates() - a function that generates class strings.
|
|
136
|
+
*/
|
|
137
|
+
type TwStatesResult<V extends VariantsSchema> = (props?: VariantProps<V> & {
|
|
138
|
+
class?: ClassValue;
|
|
139
|
+
className?: ClassValue;
|
|
140
|
+
}) => string;
|
|
141
|
+
/**
|
|
142
|
+
* Result of twDebug() for introspection.
|
|
143
|
+
*/
|
|
144
|
+
interface TwDebugResult {
|
|
145
|
+
/** Final merged output string */
|
|
146
|
+
output: string;
|
|
147
|
+
/** Classes that were removed due to conflicts */
|
|
148
|
+
removed: TwDebugRemovedClass[];
|
|
149
|
+
/** Classes that were kept in the output */
|
|
150
|
+
kept: TwDebugKeptClass[];
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Information about a removed class in debug output.
|
|
154
|
+
*/
|
|
155
|
+
interface TwDebugRemovedClass {
|
|
156
|
+
/** The class that was removed */
|
|
157
|
+
class: string;
|
|
158
|
+
/** Reason for removal */
|
|
159
|
+
reason: string;
|
|
160
|
+
/** The class that caused this to be removed */
|
|
161
|
+
conflictsWith: string;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Information about a kept class in debug output.
|
|
165
|
+
*/
|
|
166
|
+
interface TwDebugKeptClass {
|
|
167
|
+
/** The class that was kept */
|
|
168
|
+
class: string;
|
|
169
|
+
/** The group it belongs to, or null if unknown */
|
|
170
|
+
group: string | null;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Interface for the LRU cache.
|
|
174
|
+
*/
|
|
175
|
+
interface Cache<K, V> {
|
|
176
|
+
/** Get a value from the cache */
|
|
177
|
+
get(key: K): V | undefined;
|
|
178
|
+
/** Set a value in the cache */
|
|
179
|
+
set(key: K, value: V): void;
|
|
180
|
+
/** Check if a key exists */
|
|
181
|
+
has(key: K): boolean;
|
|
182
|
+
/** Clear all entries */
|
|
183
|
+
clear(): void;
|
|
184
|
+
/** Current number of entries */
|
|
185
|
+
readonly size: number;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
declare function tw(...inputs: ClassValue[]): string;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Joins class values without conflict resolution (clsx-style).
|
|
192
|
+
* Faster than `tw()` when conflicts don't need resolving.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* twJoin('base', isActive && 'active', { hidden: isHidden })
|
|
196
|
+
* twJoin('p-4', 'p-8') // => 'p-4 p-8' (both preserved, unlike tw)
|
|
197
|
+
*/
|
|
198
|
+
declare function twJoin(...inputs: ClassValue[]): string;
|
|
199
|
+
|
|
200
|
+
declare function twPrefix(modifier: string, classes: string): string;
|
|
201
|
+
declare function twPrefix(modifierMap: Record<string, string>): string;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Creates a variant-based styling function (CVA-like) integrated with tw().
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* const button = twStates({
|
|
208
|
+
* base: 'px-4 py-2 rounded',
|
|
209
|
+
* variants: {
|
|
210
|
+
* variant: { primary: 'bg-blue-500', secondary: 'bg-gray-200' },
|
|
211
|
+
* size: { sm: 'text-sm', lg: 'text-lg' },
|
|
212
|
+
* },
|
|
213
|
+
* defaultVariants: { variant: 'primary', size: 'sm' },
|
|
214
|
+
* })
|
|
215
|
+
*
|
|
216
|
+
* button() // uses defaults
|
|
217
|
+
* button({ variant: 'secondary', size: 'lg', className: 'mt-4' })
|
|
218
|
+
*/
|
|
219
|
+
declare function twStates<V extends VariantsSchema>(config: TwStatesConfig<V>): TwStatesResult<V>;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Returns detailed information about class merging decisions.
|
|
223
|
+
* Useful for debugging why classes were removed or kept.
|
|
224
|
+
*
|
|
225
|
+
* Unlike `tw()`, this function does not cache results and provides
|
|
226
|
+
* full information about the resolution process.
|
|
227
|
+
*
|
|
228
|
+
* @param inputs - Class strings, objects, arrays, or falsy values
|
|
229
|
+
* @returns Debug result with output, removed, and kept information
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```tsx
|
|
233
|
+
* const result = twDebug('p-4 px-2 p-8')
|
|
234
|
+
* // => {
|
|
235
|
+
* // output: 'px-2 p-8',
|
|
236
|
+
* // removed: [
|
|
237
|
+
* // { class: 'p-4', reason: 'overridden by p-8', conflictsWith: 'p-8' }
|
|
238
|
+
* // ],
|
|
239
|
+
* // kept: [
|
|
240
|
+
* // { class: 'px-2', group: 'padding-x' },
|
|
241
|
+
* // { class: 'p-8', group: 'padding' }
|
|
242
|
+
* // ]
|
|
243
|
+
* // }
|
|
244
|
+
*
|
|
245
|
+
* // Check why a class was removed
|
|
246
|
+
* const { removed } = twDebug('text-sm text-lg text-red-500')
|
|
247
|
+
* removed.forEach(r => console.log(`${r.class} was ${r.reason}`))
|
|
248
|
+
*
|
|
249
|
+
* // Inspect group assignments
|
|
250
|
+
* const { kept } = twDebug('hover:bg-blue-500 focus:ring-2')
|
|
251
|
+
* kept.forEach(k => console.log(`${k.class} -> ${k.group}`))
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
declare function twDebug(...inputs: ClassValue[]): TwDebugResult;
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Creates a customized tw function with extended or overridden configuration.
|
|
258
|
+
*
|
|
259
|
+
* Use this when you need to:
|
|
260
|
+
* - Add custom class groups for your design system
|
|
261
|
+
* - Change conflict resolution behavior
|
|
262
|
+
* - Add a Tailwind prefix
|
|
263
|
+
* - Adjust cache size
|
|
264
|
+
* - Use minimal mode for smaller bundle
|
|
265
|
+
*
|
|
266
|
+
* @param config - Configuration options
|
|
267
|
+
* @returns A customized tw function
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```tsx
|
|
271
|
+
* // Add custom class groups
|
|
272
|
+
* const tw = createTw({
|
|
273
|
+
* extend: {
|
|
274
|
+
* classGroups: {
|
|
275
|
+
* 'custom-size': ['size-xs', 'size-sm', 'size-md', 'size-lg', 'size-xl'],
|
|
276
|
+
* },
|
|
277
|
+
* },
|
|
278
|
+
* })
|
|
279
|
+
*
|
|
280
|
+
* tw('size-sm size-lg') // => 'size-lg'
|
|
281
|
+
*
|
|
282
|
+
* // Use minimal mode for smaller bundle
|
|
283
|
+
* const tw = createTw({
|
|
284
|
+
* mode: 'minimal',
|
|
285
|
+
* })
|
|
286
|
+
*
|
|
287
|
+
* // With Tailwind prefix
|
|
288
|
+
* const tw = createTw({
|
|
289
|
+
* prefix: 'tw-',
|
|
290
|
+
* })
|
|
291
|
+
*
|
|
292
|
+
* tw('tw-p-4 tw-p-8') // => 'tw-p-8'
|
|
293
|
+
*
|
|
294
|
+
* // Disable caching
|
|
295
|
+
* const tw = createTw({
|
|
296
|
+
* cacheSize: 0,
|
|
297
|
+
* })
|
|
298
|
+
*
|
|
299
|
+
* // Disable merging entirely (just join classes)
|
|
300
|
+
* const tw = createTw({
|
|
301
|
+
* mode: false,
|
|
302
|
+
* })
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
declare function createTw(config?: TwConfig): (...inputs: ClassValue[]) => string;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Specificity levels for composite class handling.
|
|
309
|
+
* Higher numbers = more specific = won't be overridden by lower.
|
|
310
|
+
*/
|
|
311
|
+
declare const SPECIFICITY: {
|
|
312
|
+
/** All sides (p-4, m-4, rounded) */
|
|
313
|
+
readonly ALL: 1;
|
|
314
|
+
/** Axis (px-4, my-4, rounded-t) */
|
|
315
|
+
readonly AXIS: 2;
|
|
316
|
+
/** Single side (pt-4, mr-4, rounded-tl) */
|
|
317
|
+
readonly SIDE: 3;
|
|
318
|
+
/** Arbitrary values are highly specific */
|
|
319
|
+
readonly ARBITRARY: 10;
|
|
320
|
+
};
|
|
321
|
+
/**
|
|
322
|
+
* A prefix entry for the trie-based classifier.
|
|
323
|
+
*/
|
|
324
|
+
interface PrefixEntry {
|
|
325
|
+
/** The group ID this prefix maps to */
|
|
326
|
+
groupId: string;
|
|
327
|
+
/** Specificity level for this prefix */
|
|
328
|
+
specificity: number;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Conflict mapping from a group ID to groups it overrides.
|
|
332
|
+
*/
|
|
333
|
+
type ConflictMap = Record<string, string[]>;
|
|
334
|
+
/**
|
|
335
|
+
* A complete class group configuration.
|
|
336
|
+
*/
|
|
337
|
+
interface ClassGroupConfig {
|
|
338
|
+
/** Prefix-to-group mappings for trie lookup */
|
|
339
|
+
prefixes: Record<string, PrefixEntry>;
|
|
340
|
+
/** Exact match mappings for standalone classes */
|
|
341
|
+
exactMatches: Record<string, PrefixEntry>;
|
|
342
|
+
/** Regex patterns for complex matching (fallback) */
|
|
343
|
+
patterns: Array<{
|
|
344
|
+
pattern: RegExp;
|
|
345
|
+
groupId: string;
|
|
346
|
+
specificity: number;
|
|
347
|
+
}>;
|
|
348
|
+
/** Conflict mappings */
|
|
349
|
+
conflicts: ConflictMap;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Sets the merge mode and reinitializes the classifier.
|
|
354
|
+
*/
|
|
355
|
+
declare function setMergeMode(mode: TwMergeMode): void;
|
|
356
|
+
/**
|
|
357
|
+
* Classifies a utility class into a group and returns its specificity.
|
|
358
|
+
*
|
|
359
|
+
* @param utility - The utility class (without modifiers)
|
|
360
|
+
* @returns The group ID (or null) and specificity level
|
|
361
|
+
*/
|
|
362
|
+
declare function classifyClass(utility: string): {
|
|
363
|
+
group: string | null;
|
|
364
|
+
specificity: number;
|
|
365
|
+
};
|
|
366
|
+
/**
|
|
367
|
+
* Gets the groups that a given group conflicts with.
|
|
368
|
+
*
|
|
369
|
+
* @param groupId - The group ID to check
|
|
370
|
+
* @returns Array of conflicting group IDs
|
|
371
|
+
*/
|
|
372
|
+
declare function getConflictingGroups(groupId: string): string[];
|
|
373
|
+
/**
|
|
374
|
+
* Gets the current merge mode.
|
|
375
|
+
*/
|
|
376
|
+
declare function getMergeMode(): TwMergeMode;
|
|
377
|
+
/**
|
|
378
|
+
* Resets the classifier to default state (Useful for testing).
|
|
379
|
+
*/
|
|
380
|
+
declare function resetClassifier(): void;
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* CSS Generator using Tailwind's design system.
|
|
384
|
+
* Works with any Tailwind v4 project - no config required.
|
|
385
|
+
*/
|
|
386
|
+
interface CSSGeneratorOptions {
|
|
387
|
+
/**
|
|
388
|
+
* Custom CSS to initialize the design system with.
|
|
389
|
+
* Defaults to '@import "tailwindcss";' which includes full default theme.
|
|
390
|
+
*/
|
|
391
|
+
css?: string;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Initialize the CSS generator.
|
|
395
|
+
* Call once at app startup before using twPrefix().
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* // Basic usage (uses Tailwind defaults)
|
|
399
|
+
* await initCSSGenerator()
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* // With custom theme
|
|
403
|
+
* await initCSSGenerator({
|
|
404
|
+
* css: `
|
|
405
|
+
* @import "tailwindcss";
|
|
406
|
+
* @theme {
|
|
407
|
+
* --color-brand: #ff6600;
|
|
408
|
+
* }
|
|
409
|
+
* `
|
|
410
|
+
* })
|
|
411
|
+
*/
|
|
412
|
+
declare function initCSSGenerator(options?: CSSGeneratorOptions): Promise<void>;
|
|
413
|
+
/** Generate CSS for a single Tailwind class. Returns null if invalid or uninitialized. */
|
|
414
|
+
declare function generateCSS(className: string): string | null;
|
|
415
|
+
/** Generate CSS for multiple classes at once (more efficient than repeated generateCSS calls). */
|
|
416
|
+
declare function generateCSSBatch(classNames: string[]): (string | null)[];
|
|
417
|
+
declare function isInitialized(): boolean;
|
|
418
|
+
/** Get a theme value by path (e.g., '--color-red-500' → 'oklch(63.7% 0.237 25.331)'). */
|
|
419
|
+
declare function getThemeValue(path: string): string | undefined;
|
|
420
|
+
/**
|
|
421
|
+
* Validates if a class is a valid Tailwind utility.
|
|
422
|
+
* Returns true if the class produces CSS output.
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* isValidClass('p-4') // => true
|
|
426
|
+
* isValidClass('invalid-class') // => false
|
|
427
|
+
*/
|
|
428
|
+
declare function isValidClass(className: string): boolean;
|
|
429
|
+
/**
|
|
430
|
+
* Validates multiple classes at once.
|
|
431
|
+
* Returns an array of booleans indicating validity.
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* validateClasses(['p-4', 'invalid', 'm-2']) // => [true, false, true]
|
|
435
|
+
*/
|
|
436
|
+
declare function validateClasses(classNames: string[]): boolean[];
|
|
437
|
+
/**
|
|
438
|
+
* Gets the sort order for classes (useful for deterministic ordering).
|
|
439
|
+
* Returns null for invalid classes.
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* getClassOrder(['p-4', 'm-2']) // => [['p-4', 123n], ['m-2', 456n]]
|
|
443
|
+
*/
|
|
444
|
+
declare function getClassOrder(classNames: string[]): [string, bigint | null][];
|
|
445
|
+
/**
|
|
446
|
+
* Canonicalizes classes by collapsing redundant utilities.
|
|
447
|
+
* For example, mt-2 mr-2 mb-2 ml-2 → m-2
|
|
448
|
+
*
|
|
449
|
+
* Note: Requires the CSS generator to be initialized.
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* canonicalizeClasses(['mt-2', 'mr-2', 'mb-2', 'ml-2']) // => ['m-2']
|
|
453
|
+
*/
|
|
454
|
+
declare function canonicalizeClasses(classNames: string[]): string[];
|
|
455
|
+
|
|
456
|
+
/** Error handler for CSS injection failures */
|
|
457
|
+
type StyleInjectorErrorHandler = (error: Error, context: string) => void;
|
|
458
|
+
/**
|
|
459
|
+
* Sets a custom error handler for CSS injection failures.
|
|
460
|
+
* Useful for debugging when CSS injection silently fails.
|
|
461
|
+
*
|
|
462
|
+
* @param handler - Error handler function, or null to disable
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* ```ts
|
|
466
|
+
* setStyleInjectorErrorHandler((error, context) => {
|
|
467
|
+
* console.warn(`CSS injection failed in ${context}:`, error)
|
|
468
|
+
* })
|
|
469
|
+
* ```
|
|
470
|
+
*/
|
|
471
|
+
declare function setStyleInjectorErrorHandler(handler: StyleInjectorErrorHandler | null): void;
|
|
472
|
+
declare function getStyleElement(): HTMLStyleElement | null;
|
|
473
|
+
/**
|
|
474
|
+
* Injects a CSS rule for a class.
|
|
475
|
+
* Uses insertRule for better performance when available.
|
|
476
|
+
*/
|
|
477
|
+
declare function injectCSS(className: string, cssRule: string): void;
|
|
478
|
+
/**
|
|
479
|
+
* Batched injection for performance.
|
|
480
|
+
* Uses insertRule when available, falls back to textContent.
|
|
481
|
+
*/
|
|
482
|
+
declare function injectCSSBatch(classNames: string[], cssRules: (string | null)[]): void;
|
|
483
|
+
declare function hasInjectedClass(className: string): boolean;
|
|
484
|
+
declare function clearInjectedStyles(): void;
|
|
485
|
+
declare function getInjectedClasses(): string[];
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Simplified LRU (Least Recently Used) cache using ES6 Map's insertion order.
|
|
489
|
+
*
|
|
490
|
+
* ES6 Maps maintain insertion order, so we can implement LRU by:
|
|
491
|
+
* - On get: delete and re-set to move item to end (most recent)
|
|
492
|
+
* - On set: add to end, delete from beginning if over capacity
|
|
493
|
+
*
|
|
494
|
+
* This is simpler than a doubly-linked list implementation and
|
|
495
|
+
* leverages highly optimized native Map operations.
|
|
496
|
+
*
|
|
497
|
+
* - get: O(1) amortized
|
|
498
|
+
* - set: O(1) amortized
|
|
499
|
+
* - has: O(1)
|
|
500
|
+
*/
|
|
501
|
+
declare class LRUCache<K, V> implements Cache<K, V> {
|
|
502
|
+
private cache;
|
|
503
|
+
private readonly maxSize;
|
|
504
|
+
constructor(maxSize?: number);
|
|
505
|
+
/** Gets a value, moving it to most recently used position. */
|
|
506
|
+
get(key: K): V | undefined;
|
|
507
|
+
/** Sets a value, evicting the oldest entry if at capacity. */
|
|
508
|
+
set(key: K, value: V): void;
|
|
509
|
+
has(key: K): boolean;
|
|
510
|
+
clear(): void;
|
|
511
|
+
get size(): number;
|
|
512
|
+
}
|
|
513
|
+
declare function clearCaches(): void;
|
|
514
|
+
/**
|
|
515
|
+
* Returns current cache statistics for debugging/monitoring.
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* ```ts
|
|
519
|
+
* const stats = getCacheStats()
|
|
520
|
+
* console.log(`Parse cache: ${stats.parseSize}, Merge cache: ${stats.mergeSize}`)
|
|
521
|
+
* ```
|
|
522
|
+
*/
|
|
523
|
+
declare function getCacheStats(): {
|
|
524
|
+
parseSize: number;
|
|
525
|
+
mergeSize: number;
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Flattens ClassValue inputs into a single string.
|
|
530
|
+
* Handles strings, arrays, objects, and nested combinations.
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* ```ts
|
|
534
|
+
* flattenClassValue('a b c') // => 'a b c'
|
|
535
|
+
* flattenClassValue(['a', 'b']) // => 'a b'
|
|
536
|
+
* flattenClassValue({ a: true, b: false }) // => 'a'
|
|
537
|
+
* flattenClassValue(['a', { b: true }, ['c', 'd']]) // => 'a b c d'
|
|
538
|
+
* ```
|
|
539
|
+
*/
|
|
540
|
+
declare function flattenClassValue(value: ClassValue): string;
|
|
541
|
+
/**
|
|
542
|
+
* Flattens multiple ClassValue inputs into a single string.
|
|
543
|
+
*
|
|
544
|
+
* @param inputs - ClassValue inputs to flatten
|
|
545
|
+
* @returns Combined class string
|
|
546
|
+
*/
|
|
547
|
+
declare function flattenClassValues(...inputs: ClassValue[]): string;
|
|
548
|
+
/**
|
|
549
|
+
* Splits a class string into individual class names.
|
|
550
|
+
* Handles whitespace and filters empty strings.
|
|
551
|
+
*
|
|
552
|
+
* @param classString - The class string to split
|
|
553
|
+
* @returns Array of individual class names
|
|
554
|
+
*/
|
|
555
|
+
declare function splitClasses(classString: string): string[];
|
|
556
|
+
/**
|
|
557
|
+
* Extracts modifiers and !important flag from a class name.
|
|
558
|
+
* Modifiers are colon-separated prefixes like "hover:", "md:", etc.
|
|
559
|
+
* !important can appear as "!" modifier or "!" prefix on utility.
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```ts
|
|
563
|
+
* extractModifiers('hover:md:text-red-500')
|
|
564
|
+
* // => { modifiers: ['hover', 'md'], utility: 'text-red-500', hasImportant: false }
|
|
565
|
+
*
|
|
566
|
+
* extractModifiers('hover:!bg-red-500')
|
|
567
|
+
* // => { modifiers: ['hover'], utility: 'bg-red-500', hasImportant: true }
|
|
568
|
+
*
|
|
569
|
+
* extractModifiers('!p-4')
|
|
570
|
+
* // => { modifiers: [], utility: 'p-4', hasImportant: true }
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
declare function extractModifiers(className: string): {
|
|
574
|
+
modifiers: string[];
|
|
575
|
+
utility: string;
|
|
576
|
+
hasImportant: boolean;
|
|
577
|
+
};
|
|
578
|
+
/**
|
|
579
|
+
* Base parsing result without classification.
|
|
580
|
+
*/
|
|
581
|
+
interface ParsedClass {
|
|
582
|
+
raw: string;
|
|
583
|
+
modifiers: string[];
|
|
584
|
+
modifierKey: string;
|
|
585
|
+
hasImportant: boolean;
|
|
586
|
+
utility: string;
|
|
587
|
+
utilityForClassification: string;
|
|
588
|
+
isArbitrary: boolean;
|
|
589
|
+
arbitraryProperty?: string;
|
|
590
|
+
position: number;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Parses a single class name without classification.
|
|
594
|
+
* Used by createTw() which does its own classification.
|
|
595
|
+
*
|
|
596
|
+
* @param className - The class name to parse
|
|
597
|
+
* @param position - Original position in the input
|
|
598
|
+
* @returns Parsed class info (without group/specificity)
|
|
599
|
+
*/
|
|
600
|
+
declare function parseClassBase(className: string, position: number): ParsedClass;
|
|
601
|
+
/**
|
|
602
|
+
* Parses a single class name into a ClassToken.
|
|
603
|
+
* Includes classification using the global classifier.
|
|
604
|
+
*
|
|
605
|
+
* @param className - The class name to parse
|
|
606
|
+
* @param position - Original position in the input
|
|
607
|
+
* @returns Parsed ClassToken with group and specificity
|
|
608
|
+
*/
|
|
609
|
+
declare function parseClass(className: string, position: number): ClassToken;
|
|
610
|
+
/**
|
|
611
|
+
* Parses a class string into an array of ClassTokens.
|
|
612
|
+
* Results are cached for performance.
|
|
613
|
+
*
|
|
614
|
+
* @param classString - The class string to parse
|
|
615
|
+
* @returns Array of ClassTokens
|
|
616
|
+
*/
|
|
617
|
+
declare function parseClasses(classString: string): ClassToken[];
|
|
618
|
+
|
|
619
|
+
/** Resolves conflicts with full debug info about removed classes. */
|
|
620
|
+
declare function resolveConflicts(tokens: ClassToken[]): ResolvedClasses;
|
|
621
|
+
/** Resolves conflicts returning only kept tokens (faster, no debug info). */
|
|
622
|
+
declare function resolveConflictsSimple(tokens: ClassToken[]): ClassToken[];
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Minimal class group configuration.
|
|
626
|
+
* Covers the most commonly used Tailwind utilities:
|
|
627
|
+
* - Layout (display, position, visibility)
|
|
628
|
+
* - Spacing (padding, margin, gap)
|
|
629
|
+
* - Sizing (width, height)
|
|
630
|
+
* - Flexbox & Grid
|
|
631
|
+
* - Typography
|
|
632
|
+
* - Colors
|
|
633
|
+
* - Borders
|
|
634
|
+
*
|
|
635
|
+
* Bundle size: ~1.5KB gzipped
|
|
636
|
+
*/
|
|
637
|
+
declare const minimalConfig: ClassGroupConfig;
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Full class group configuration.
|
|
641
|
+
* Covers all Tailwind CSS v4 utilities.
|
|
642
|
+
*
|
|
643
|
+
* Bundle size: ~7KB gzipped
|
|
644
|
+
*
|
|
645
|
+
* Uses lazy initialization via a Proxy to defer computation until first access,
|
|
646
|
+
* reducing startup time when the full config is imported but not immediately used.
|
|
647
|
+
*/
|
|
648
|
+
declare const fullConfig: ClassGroupConfig;
|
|
649
|
+
|
|
650
|
+
export { type CSSGeneratorOptions, type ClassGroupConfig, type ClassValue, type CompoundVariant, type ConflictMap, LRUCache, type ParsedClass, type PrefixEntry, SPECIFICITY, type TwConfig, type TwConfigExtension, type TwConfigOverride, type TwDebugKeptClass, type TwDebugRemovedClass, type TwDebugResult, type TwMergeMode, type TwStatesConfig, type TwStatesResult, type VariantProps, type VariantsSchema, canonicalizeClasses, classifyClass, clearCaches, clearInjectedStyles, createTw, extractModifiers, flattenClassValue, flattenClassValues, fullConfig, generateCSS, generateCSSBatch, getCacheStats, getClassOrder, getConflictingGroups, getInjectedClasses, getMergeMode, getStyleElement, getThemeValue, hasInjectedClass, initCSSGenerator, injectCSS, injectCSSBatch, isInitialized, isValidClass, minimalConfig, parseClass, parseClassBase, parseClasses, resetClassifier, resolveConflicts, resolveConflictsSimple, setMergeMode, setStyleInjectorErrorHandler, splitClasses, tw, twDebug, twJoin, twPrefix, twStates, validateClasses };
|