Skip to content

Commit 1ec5438

Browse files
authored
Improve arbitrary value support (#5568)
* simplify `inset` plugin * run `prettier` on stub file * simplify `align` utility * improve arbitrary support for outline This will allow us to use `outline-[OUTLINE,OPTIONAL_OFFSET]` Input: ```html outline-[2px_solid_black] ``` Output: ```css .outline-\[2px_solid_black\] { outline: 2px solid black; outline-offset: 0; } ``` --- Input: ```html outline-[2px_solid_black,2px] ``` Output: ```css .outline-\[2px_solid_black\2c 2px\] { outline: 2px solid black; outline-offset: 2px; } ``` * remove default `type` * simplify createUtilityPlugin, use types directly * find first matching type when coercing the value * introduce css data types Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Types These data types will be used to "guess" the type of an arbitrary value if there is some ambiguity going on. For example: ``` bg-[#0088cc] -> This is a `color` -> `background-color` bg-[url('...')] -> This is a `url` -> `background-image` ``` If you are using css variables, then there is no way of knowing which type it is referring to, in that case you can be explicit: ``` bg-[color:var(--value)] -> This is a `color` -> `background-color` bg-[url:var(--value)] -> This is a `url` -> `background-image` ``` When you explicitly pass a data type, then we bypass the type system and assume you are right. This is nice in a way because now we don't have to run all of the guessing type code. On the other hand, you can introduce runtime issues that we are not able to detect: ``` :root { --value: 12px; } /* Later... */ bg-[color:var(--value)] -> Assumes `color` -> *eventually* -> `background-color: 12px` ``` * add a bunch of new tests for advanced arbitrary values
1 parent ab17c6c commit 1ec5438

9 files changed

+1114
-246
lines changed

src/corePlugins.js

+52-74
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import {
1616
transformAllSelectors,
1717
transformAllClasses,
1818
transformLastClasses,
19-
asLength,
20-
asURL,
21-
asLookupValue,
2219
} from './util/pluginUtils'
2320
import packageJson from '../package.json'
2421
import log from './util/log'
@@ -520,35 +517,19 @@ export let position = ({ addUtilities }) => {
520517
})
521518
}
522519

523-
export let inset = ({ matchUtilities, theme }) => {
524-
let options = {
525-
values: theme('inset'),
526-
type: 'any',
527-
}
528-
529-
matchUtilities(
530-
{ inset: (value) => ({ top: value, right: value, bottom: value, left: value }) },
531-
options
532-
)
533-
534-
matchUtilities(
535-
{
536-
'inset-x': (value) => ({ left: value, right: value }),
537-
'inset-y': (value) => ({ top: value, bottom: value }),
538-
},
539-
options
540-
)
541-
542-
matchUtilities(
543-
{
544-
top: (top) => ({ top }),
545-
right: (right) => ({ right }),
546-
bottom: (bottom) => ({ bottom }),
547-
left: (left) => ({ left }),
548-
},
549-
options
550-
)
551-
}
520+
export let inset = createUtilityPlugin('inset', [
521+
['inset', ['top', 'right', 'bottom', 'left']],
522+
[
523+
['inset-x', ['left', 'right']],
524+
['inset-y', ['top', 'bottom']],
525+
],
526+
[
527+
['top', ['top']],
528+
['right', ['right']],
529+
['bottom', ['bottom']],
530+
['left', ['left']],
531+
],
532+
])
552533

553534
export let isolation = ({ addUtilities }) => {
554535
addUtilities({
@@ -633,12 +614,15 @@ export let display = ({ addUtilities }) => {
633614
}
634615

635616
export let aspectRatio = createUtilityPlugin('aspectRatio', [['aspect', ['aspect-ratio']]])
617+
636618
export let height = createUtilityPlugin('height', [['h', ['height']]])
637619
export let maxHeight = createUtilityPlugin('maxHeight', [['max-h', ['maxHeight']]])
638620
export let minHeight = createUtilityPlugin('minHeight', [['min-h', ['minHeight']]])
621+
639622
export let width = createUtilityPlugin('width', [['w', ['width']]])
640623
export let minWidth = createUtilityPlugin('minWidth', [['min-w', ['minWidth']]])
641624
export let maxWidth = createUtilityPlugin('maxWidth', [['max-w', ['maxWidth']]])
625+
642626
export let flex = createUtilityPlugin('flex')
643627
export let flexShrink = createUtilityPlugin('flexShrink', [['flex-shrink', ['flex-shrink']]])
644628
export let flexGrow = createUtilityPlugin('flexGrow', [['flex-grow', ['flex-grow']]])
@@ -992,7 +976,7 @@ export let space = ({ matchUtilities, addUtilities, theme }) => {
992976
}
993977
},
994978
},
995-
{ values: theme('space'), type: 'any' }
979+
{ values: theme('space') }
996980
)
997981

998982
addUtilities({
@@ -1029,7 +1013,7 @@ export let divideWidth = ({ matchUtilities, addUtilities, theme }) => {
10291013
}
10301014
},
10311015
},
1032-
{ values: theme('divideWidth'), type: 'length' }
1016+
{ values: theme('divideWidth'), type: ['line-width', 'length'] }
10331017
)
10341018

10351019
addUtilities({
@@ -1089,7 +1073,7 @@ export let divideOpacity = ({ matchUtilities, theme }) => {
10891073
return { [`& > :not([hidden]) ~ :not([hidden])`]: { '--tw-divide-opacity': value } }
10901074
},
10911075
},
1092-
{ values: theme('divideOpacity'), type: 'any' }
1076+
{ values: theme('divideOpacity') }
10931077
)
10941078
}
10951079

@@ -1215,7 +1199,7 @@ export let borderWidth = createUtilityPlugin(
12151199
['border-l', [['@defaults border-width', {}], 'border-left-width']],
12161200
],
12171201
],
1218-
{ resolveArbitraryValue: asLength }
1202+
{ type: ['line-width', 'length'] }
12191203
)
12201204

12211205
export let borderStyle = ({ addUtilities }) => {
@@ -1265,7 +1249,7 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
12651249
},
12661250
{
12671251
values: (({ DEFAULT: _, ...colors }) => colors)(flattenColorPalette(theme('borderColor'))),
1268-
type: 'color',
1252+
type: ['color'],
12691253
}
12701254
)
12711255

@@ -1362,7 +1346,7 @@ export let backgroundOpacity = createUtilityPlugin('backgroundOpacity', [
13621346
export let backgroundImage = createUtilityPlugin(
13631347
'backgroundImage',
13641348
[['bg', ['background-image']]],
1365-
{ resolveArbitraryValue: [asLookupValue, asURL] }
1349+
{ type: ['lookup', 'image', 'url'] }
13661350
)
13671351
export let gradientColorStops = (() => {
13681352
function transparentTo(value) {
@@ -1415,7 +1399,7 @@ export let boxDecorationBreak = ({ addUtilities }) => {
14151399
}
14161400

14171401
export let backgroundSize = createUtilityPlugin('backgroundSize', [['bg', ['background-size']]], {
1418-
resolveArbitraryValue: asLookupValue,
1402+
type: ['lookup', 'length', 'percentage'],
14191403
})
14201404

14211405
export let backgroundAttachment = ({ addUtilities }) => {
@@ -1438,7 +1422,7 @@ export let backgroundClip = ({ addUtilities }) => {
14381422
export let backgroundPosition = createUtilityPlugin(
14391423
'backgroundPosition',
14401424
[['bg', ['background-position']]],
1441-
{ resolveArbitraryValue: asLookupValue }
1425+
{ type: ['lookup', 'position'] }
14421426
)
14431427

14441428
export let backgroundRepeat = ({ addUtilities }) => {
@@ -1478,12 +1462,12 @@ export let stroke = ({ matchUtilities, theme }) => {
14781462
return { stroke: toColorValue(value) }
14791463
},
14801464
},
1481-
{ values: flattenColorPalette(theme('stroke')), type: 'color' }
1465+
{ values: flattenColorPalette(theme('stroke')), type: ['color', 'url'] }
14821466
)
14831467
}
14841468

14851469
export let strokeWidth = createUtilityPlugin('strokeWidth', [['stroke', ['stroke-width']]], {
1486-
resolveArbitraryValue: [asLength, asURL],
1470+
type: ['length', 'number', 'percentage'],
14871471
})
14881472

14891473
export let objectFit = ({ addUtilities }) => {
@@ -1534,18 +1518,11 @@ export let verticalAlign = ({ addUtilities, matchUtilities }) => {
15341518
'.align-super': { 'vertical-align': 'super' },
15351519
})
15361520

1537-
matchUtilities(
1538-
{
1539-
align: (value) => ({
1540-
'vertical-align': value,
1541-
}),
1542-
},
1543-
{ values: {}, type: 'any' }
1544-
)
1521+
matchUtilities({ align: (value) => ({ 'vertical-align': value }) })
15451522
}
15461523

15471524
export let fontFamily = createUtilityPlugin('fontFamily', [['font', ['fontFamily']]], {
1548-
resolveArbitraryValue: asLookupValue,
1525+
type: ['lookup', 'generic-name', 'family-name'],
15491526
})
15501527

15511528
export let fontSize = ({ matchUtilities, theme }) => {
@@ -1564,12 +1541,12 @@ export let fontSize = ({ matchUtilities, theme }) => {
15641541
}
15651542
},
15661543
},
1567-
{ values: theme('fontSize'), type: 'length' }
1544+
{ values: theme('fontSize'), type: ['absolute-size', 'relative-size', 'length', 'percentage'] }
15681545
)
15691546
}
15701547

15711548
export let fontWeight = createUtilityPlugin('fontWeight', [['font', ['fontWeight']]], {
1572-
resolveArbitraryValue: asLookupValue,
1549+
type: ['lookup', 'number'],
15731550
})
15741551

15751552
export let textTransform = ({ addUtilities }) => {
@@ -1691,7 +1668,7 @@ export let placeholderOpacity = ({ matchUtilities, theme }) => {
16911668
return { ['&::placeholder']: { '--tw-placeholder-opacity': value } }
16921669
},
16931670
},
1694-
{ values: theme('placeholderOpacity'), type: 'any' }
1671+
{ values: theme('placeholderOpacity') }
16951672
)
16961673
}
16971674

@@ -1799,12 +1776,13 @@ export let outline = ({ matchUtilities, theme }) => {
17991776
matchUtilities(
18001777
{
18011778
outline: (value) => {
1779+
value = Array.isArray(value) ? value : value.split(',')
18021780
let [outline, outlineOffset = '0'] = Array.isArray(value) ? value : [value]
18031781

18041782
return { outline, 'outline-offset': outlineOffset }
18051783
},
18061784
},
1807-
{ values: theme('outline'), type: 'any' }
1785+
{ values: theme('outline') }
18081786
)
18091787
}
18101788

@@ -1881,7 +1859,7 @@ export let ringOpacity = createUtilityPlugin(
18811859
export let ringOffsetWidth = createUtilityPlugin(
18821860
'ringOffsetWidth',
18831861
[['ring-offset', ['--tw-ring-offset-width']]],
1884-
{ resolveArbitraryValue: asLength }
1862+
{ type: 'length' }
18851863
)
18861864

18871865
export let ringOffsetColor = ({ matchUtilities, theme }) => {
@@ -1908,7 +1886,7 @@ export let blur = ({ matchUtilities, theme }) => {
19081886
}
19091887
},
19101888
},
1911-
{ values: theme('blur'), type: 'any' }
1889+
{ values: theme('blur') }
19121890
)
19131891
}
19141892

@@ -1923,7 +1901,7 @@ export let brightness = ({ matchUtilities, theme }) => {
19231901
}
19241902
},
19251903
},
1926-
{ values: theme('brightness'), type: 'any' }
1904+
{ values: theme('brightness') }
19271905
)
19281906
}
19291907

@@ -1938,7 +1916,7 @@ export let contrast = ({ matchUtilities, theme }) => {
19381916
}
19391917
},
19401918
},
1941-
{ values: theme('contrast'), type: 'any' }
1919+
{ values: theme('contrast') }
19421920
)
19431921
}
19441922

@@ -1970,7 +1948,7 @@ export let grayscale = ({ matchUtilities, theme }) => {
19701948
}
19711949
},
19721950
},
1973-
{ values: theme('grayscale'), type: 'any' }
1951+
{ values: theme('grayscale') }
19741952
)
19751953
}
19761954

@@ -1985,7 +1963,7 @@ export let hueRotate = ({ matchUtilities, theme }) => {
19851963
}
19861964
},
19871965
},
1988-
{ values: theme('hueRotate'), type: 'any' }
1966+
{ values: theme('hueRotate') }
19891967
)
19901968
}
19911969

@@ -2000,7 +1978,7 @@ export let invert = ({ matchUtilities, theme }) => {
20001978
}
20011979
},
20021980
},
2003-
{ values: theme('invert'), type: 'any' }
1981+
{ values: theme('invert') }
20041982
)
20051983
}
20061984

@@ -2015,7 +1993,7 @@ export let saturate = ({ matchUtilities, theme }) => {
20151993
}
20161994
},
20171995
},
2018-
{ values: theme('saturate'), type: 'any' }
1996+
{ values: theme('saturate') }
20191997
)
20201998
}
20211999

@@ -2030,7 +2008,7 @@ export let sepia = ({ matchUtilities, theme }) => {
20302008
}
20312009
},
20322010
},
2033-
{ values: theme('sepia'), type: 'any' }
2011+
{ values: theme('sepia') }
20342012
)
20352013
}
20362014

@@ -2076,7 +2054,7 @@ export let backdropBlur = ({ matchUtilities, theme }) => {
20762054
}
20772055
},
20782056
},
2079-
{ values: theme('backdropBlur'), type: 'any' }
2057+
{ values: theme('backdropBlur') }
20802058
)
20812059
}
20822060

@@ -2091,7 +2069,7 @@ export let backdropBrightness = ({ matchUtilities, theme }) => {
20912069
}
20922070
},
20932071
},
2094-
{ values: theme('backdropBrightness'), type: 'any' }
2072+
{ values: theme('backdropBrightness') }
20952073
)
20962074
}
20972075

@@ -2106,7 +2084,7 @@ export let backdropContrast = ({ matchUtilities, theme }) => {
21062084
}
21072085
},
21082086
},
2109-
{ values: theme('backdropContrast'), type: 'any' }
2087+
{ values: theme('backdropContrast') }
21102088
)
21112089
}
21122090

@@ -2121,7 +2099,7 @@ export let backdropGrayscale = ({ matchUtilities, theme }) => {
21212099
}
21222100
},
21232101
},
2124-
{ values: theme('backdropGrayscale'), type: 'any' }
2102+
{ values: theme('backdropGrayscale') }
21252103
)
21262104
}
21272105

@@ -2136,7 +2114,7 @@ export let backdropHueRotate = ({ matchUtilities, theme }) => {
21362114
}
21372115
},
21382116
},
2139-
{ values: theme('backdropHueRotate'), type: 'any' }
2117+
{ values: theme('backdropHueRotate') }
21402118
)
21412119
}
21422120

@@ -2151,7 +2129,7 @@ export let backdropInvert = ({ matchUtilities, theme }) => {
21512129
}
21522130
},
21532131
},
2154-
{ values: theme('backdropInvert'), type: 'any' }
2132+
{ values: theme('backdropInvert') }
21552133
)
21562134
}
21572135

@@ -2166,7 +2144,7 @@ export let backdropOpacity = ({ matchUtilities, theme }) => {
21662144
}
21672145
},
21682146
},
2169-
{ values: theme('backdropOpacity'), type: 'any' }
2147+
{ values: theme('backdropOpacity') }
21702148
)
21712149
}
21722150

@@ -2181,7 +2159,7 @@ export let backdropSaturate = ({ matchUtilities, theme }) => {
21812159
}
21822160
},
21832161
},
2184-
{ values: theme('backdropSaturate'), type: 'any' }
2162+
{ values: theme('backdropSaturate') }
21852163
)
21862164
}
21872165

@@ -2196,7 +2174,7 @@ export let backdropSepia = ({ matchUtilities, theme }) => {
21962174
}
21972175
},
21982176
},
2199-
{ values: theme('backdropSepia'), type: 'any' }
2177+
{ values: theme('backdropSepia') }
22002178
)
22012179
}
22022180

@@ -2252,7 +2230,7 @@ export let transitionProperty = ({ matchUtilities, theme }) => {
22522230
}
22532231
},
22542232
},
2255-
{ values: theme('transitionProperty'), type: 'any' }
2233+
{ values: theme('transitionProperty') }
22562234
)
22572235
}
22582236

0 commit comments

Comments
 (0)