1
1
import plugin from 'tailwindcss/plugin'
2
2
3
- export = plugin (
3
+ export default plugin (
4
4
function containerQueries ( { matchUtilities, matchVariant, theme } ) {
5
- let values : Record < string , string > = theme ( 'containers' ) ?? { }
5
+ const values : Record < string , string > = theme ( 'containers' ) ?? { }
6
6
7
7
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
9
9
if ( numericValue === null ) return null
10
10
11
11
return parseFloat ( value )
12
12
}
13
13
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
+
14
53
matchUtilities (
15
54
{
16
55
'@container' : ( value , { modifier } ) => {
17
56
return {
18
57
'container-type' : value ,
19
- 'container-name' : modifier ,
58
+ 'container-name' : modifier
20
59
}
21
- } ,
60
+ }
22
61
} ,
23
62
{
24
63
values : {
25
- DEFAULT : 'inline-size' ,
26
- normal : 'normal' ,
64
+ DEFAULT : 'size' ,
65
+ 'inline-size' : 'inline-size' ,
66
+ normal : 'normal'
27
67
} ,
28
- modifiers : 'any' ,
68
+ modifiers : 'any'
29
69
}
30
70
)
31
71
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 )
67
74
} ,
68
75
{
69
76
theme : {
70
77
containers : {
78
+ '5xs' : '4rem' ,
79
+ '4xs' : '8rem' ,
80
+ '3xs' : '12rem' ,
81
+ '2xs' : '16rem' ,
71
82
xs : '20rem' ,
72
83
sm : '24rem' ,
73
84
md : '28rem' ,
@@ -78,8 +89,8 @@ export = plugin(
78
89
'4xl' : '56rem' ,
79
90
'5xl' : '64rem' ,
80
91
'6xl' : '72rem' ,
81
- '7xl' : '80rem' ,
82
- } ,
83
- } ,
92
+ '7xl' : '80rem'
93
+ }
94
+ }
84
95
}
85
96
)
0 commit comments