11import plugin from 'tailwindcss/plugin'
22
3- export = plugin (
3+ export default plugin (
44 function containerQueries ( { matchUtilities, matchVariant, theme } ) {
5- let values : Record < string , string > = theme ( 'containers' ) ?? { }
5+ const values : Record < string , string > = theme ( 'containers' ) ?? { }
66
77 function parseValue ( value : string ) {
8- let numericValue = value . match ( / ^ ( \d + \. \d + | \d + | \. \d + ) \D + / ) ?. [ 1 ] ?? null
8+ const numericValue = value . match ( / ^ ( \d + \. \d + | \d + | \. \d + ) \D + / ) ?. [ 1 ] ?? null
99 if ( numericValue === null ) return null
1010
1111 return parseFloat ( value )
1212 }
1313
14+ const cbFor =
15+ ( selector : string ) =>
16+ ( value = '' , extra : { modifier : string | null } ) => {
17+ const parsed = parseValue ( value )
18+
19+ return parsed !== null ? `@container ${ extra . modifier ?? '' } (${ selector } : ${ value } )` : [ ]
20+ }
21+
22+ const options = {
23+ values,
24+ sort (
25+ aVariant : { value : string ; modifier : string | null } ,
26+ zVariant : { value : string ; modifier : string | null }
27+ ) {
28+ const a = parseFloat ( aVariant . value )
29+ const z = parseFloat ( zVariant . value )
30+
31+ if ( a === null || z === null ) return 0
32+
33+ // Sort values themselves regardless of unit
34+ if ( a - z !== 0 ) return a - z
35+
36+ const aLabel = aVariant . modifier ?? ''
37+ const zLabel = zVariant . modifier ?? ''
38+
39+ // Explicitly move empty labels to the end
40+ if ( aLabel === '' && zLabel !== '' ) {
41+ return 1
42+ } else if ( aLabel !== '' && zLabel === '' ) {
43+ return - 1
44+ }
45+
46+ // Sort labels alphabetically in the English locale
47+ // We are intentionally overriding the locale because we do not want the sort to
48+ // be affected by the machine's locale (be it a developer or CI environment)
49+ return aLabel . localeCompare ( zLabel , 'en' , { numeric : true } )
50+ }
51+ }
52+
1453 matchUtilities (
1554 {
1655 '@container' : ( value , { modifier } ) => {
1756 return {
1857 'container-type' : value ,
19- 'container-name' : modifier ,
58+ 'container-name' : modifier
2059 }
21- } ,
60+ }
2261 } ,
2362 {
2463 values : {
25- DEFAULT : 'inline-size' ,
26- normal : 'normal' ,
64+ DEFAULT : 'size' ,
65+ 'inline-size' : 'inline-size' ,
66+ normal : 'normal'
2767 } ,
28- modifiers : 'any' ,
68+ modifiers : 'any'
2969 }
3070 )
3171
32- matchVariant (
33- '@' ,
34- ( value = '' , { modifier } ) => {
35- let parsed = parseValue ( value )
36-
37- return parsed !== null ? `@container ${ modifier ?? '' } (min-width: ${ value } )` : [ ]
38- } ,
39- {
40- values,
41- sort ( aVariant , zVariant ) {
42- let a = parseFloat ( aVariant . value )
43- let z = parseFloat ( zVariant . value )
44-
45- if ( a === null || z === null ) return 0
46-
47- // Sort values themselves regardless of unit
48- if ( a - z !== 0 ) return a - z
49-
50- let aLabel = aVariant . modifier ?? ''
51- let zLabel = zVariant . modifier ?? ''
52-
53- // Explicitly move empty labels to the end
54- if ( aLabel === '' && zLabel !== '' ) {
55- return 1
56- } else if ( aLabel !== '' && zLabel === '' ) {
57- return - 1
58- }
59-
60- // Sort labels alphabetically in the English locale
61- // We are intentionally overriding the locale because we do not want the sort to
62- // be affected by the machine's locale (be it a developer or CI environment)
63- return aLabel . localeCompare ( zLabel , 'en' , { numeric : true } )
64- } ,
65- }
66- )
72+ matchVariant < string > ( '@w' , cbFor ( 'min-width' ) , options )
73+ matchVariant < string > ( '@h' , cbFor ( 'min-height' ) , options )
6774 } ,
6875 {
6976 theme : {
7077 containers : {
78+ '5xs' : '4rem' ,
79+ '4xs' : '8rem' ,
80+ '3xs' : '12rem' ,
81+ '2xs' : '16rem' ,
7182 xs : '20rem' ,
7283 sm : '24rem' ,
7384 md : '28rem' ,
@@ -78,8 +89,8 @@ export = plugin(
7889 '4xl' : '56rem' ,
7990 '5xl' : '64rem' ,
8091 '6xl' : '72rem' ,
81- '7xl' : '80rem' ,
82- } ,
83- } ,
92+ '7xl' : '80rem'
93+ }
94+ }
8495 }
8596)
0 commit comments