@@ -293,6 +293,34 @@ export const spacingRule: UtilityRule = (parsed, config) => {
293293 return result
294294}
295295
296+ // Resolve a size/spacing token shared by the sizing utilities: fractions,
297+ // keyword map, theme spacing, off-scale numbers (0.25rem steps), and
298+ // arbitrary values. Unknown words return undefined — they previously
299+ // leaked verbatim (`w-sidebar` emitted `width: sidebar;`).
300+ export function resolveSizeToken (
301+ parsed : { value ?: string , arbitrary : boolean } ,
302+ config : { theme : { spacing : Record < string , string > } } ,
303+ sizeMap : Record < string , string > ,
304+ ) : string | undefined {
305+ const token = parsed . value !
306+ if ( token . includes ( '/' ) && ! parsed . arbitrary ) {
307+ const [ num , denom ] = token . split ( '/' ) . map ( Number )
308+ if ( Number . isNaN ( num ) || Number . isNaN ( denom ) || denom === 0 )
309+ return undefined
310+ return `${ ( num / denom ) * 100 } %`
311+ }
312+ const spacing = config . theme . spacing [ token ]
313+ if ( spacing !== undefined )
314+ return spacing
315+ if ( sizeMap [ token ] )
316+ return sizeMap [ token ]
317+ if ( parsed . arbitrary )
318+ return token
319+ if ( / ^ \d + (?: \. \d + ) ? $ / . test ( token ) )
320+ return `${ Number . parseFloat ( token ) * 0.25 } rem`
321+ return undefined
322+ }
323+
296324// Width and height utilities
297325export const sizingRule : UtilityRule = ( parsed , config ) => {
298326 if ( parsed . utility === 'w' && parsed . value ) {
@@ -310,18 +338,8 @@ export const sizingRule: UtilityRule = (parsed, config) => {
310338 max : 'max-content' ,
311339 fit : 'fit-content' ,
312340 }
313- // Handle fractions: 1/2 -> 50%
314- if ( parsed . value . includes ( '/' ) ) {
315- const [ num , denom ] = parsed . value . split ( '/' ) . map ( Number )
316- // Validate: skip invalid fractions (NaN or division by zero)
317- if ( Number . isNaN ( num ) || Number . isNaN ( denom ) || denom === 0 ) {
318- return undefined
319- }
320- return { width : `${ ( num / denom ) * 100 } %` } as Record < string , string >
321- }
322- // Check spacing config first, then sizeMap, then raw value
323- const value = config . theme . spacing [ parsed . value ] || sizeMap [ parsed . value ] || parsed . value
324- return { width : value } as Record < string , string >
341+ const value = resolveSizeToken ( parsed , config , sizeMap )
342+ return value !== undefined ? { width : value } as Record < string , string > : undefined
325343 }
326344
327345 if ( parsed . utility === 'h' && parsed . value ) {
@@ -337,18 +355,8 @@ export const sizingRule: UtilityRule = (parsed, config) => {
337355 max : 'max-content' ,
338356 fit : 'fit-content' ,
339357 }
340- // Handle fractions: 3/4 -> 75%
341- if ( parsed . value . includes ( '/' ) ) {
342- const [ num , denom ] = parsed . value . split ( '/' ) . map ( Number )
343- // Validate: skip invalid fractions (NaN or division by zero)
344- if ( Number . isNaN ( num ) || Number . isNaN ( denom ) || denom === 0 ) {
345- return undefined
346- }
347- return { height : `${ ( num / denom ) * 100 } %` } as Record < string , string >
348- }
349- // Check spacing config first, then sizeMap, then raw value
350- const value = config . theme . spacing [ parsed . value ] || sizeMap [ parsed . value ] || parsed . value
351- return { height : value } as Record < string , string >
358+ const value = resolveSizeToken ( parsed , config , sizeMap )
359+ return value !== undefined ? { height : value } as Record < string , string > : undefined
352360 }
353361
354362 // Size utility (width + height shorthand)
@@ -360,17 +368,8 @@ export const sizingRule: UtilityRule = (parsed, config) => {
360368 max : 'max-content' ,
361369 fit : 'fit-content' ,
362370 }
363- // Handle fractions: 1/2 -> 50%
364- if ( parsed . value . includes ( '/' ) ) {
365- const [ num , denom ] = parsed . value . split ( '/' ) . map ( Number )
366- if ( Number . isNaN ( num ) || Number . isNaN ( denom ) || denom === 0 ) {
367- return undefined
368- }
369- const percent = `${ ( num / denom ) * 100 } %`
370- return { width : percent , height : percent } as Record < string , string >
371- }
372- const value = config . theme . spacing [ parsed . value ] || sizeMap [ parsed . value ] || parsed . value
373- return { width : value , height : value } as Record < string , string >
371+ const value = resolveSizeToken ( parsed , config , sizeMap )
372+ return value !== undefined ? { width : value , height : value } as Record < string , string > : undefined
374373 }
375374
376375 return undefined
0 commit comments