Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 157e0be

Browse files
committed
Add matchUtilities helper, migrate backgroundOpacity
1 parent ec4d952 commit 157e0be

File tree

4 files changed

+60
-56
lines changed

4 files changed

+60
-56
lines changed

src/corePlugins/backgroundColor.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@ const flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette').
44
const withAlphaVariable = require('tailwindcss/lib/util/withAlphaVariable').default
55
const toColorValue = require('tailwindcss/lib/util/toColorValue').default
66
const { asColor } = require('../pluginUtils')
7-
const { newFormat } = require('../pluginUtils')
87

9-
module.exports = function ({ jit: { theme, addUtilities, addVariant, e } }) {
8+
module.exports = function ({ matchUtilities, jit: { theme } }) {
109
let colorPalette = flattenColorPalette(theme.backgroundColor)
1110

12-
addUtilities({
13-
bg: [
14-
newFormat((modifier, { theme }) => {
15-
let value = asColor(modifier, colorPalette)
11+
matchUtilities({
12+
bg: (modifier, { theme }) => {
13+
let value = asColor(modifier, colorPalette)
1614

17-
if (value === undefined) {
18-
return []
19-
}
15+
if (value === undefined) {
16+
return []
17+
}
2018

21-
return {
22-
[nameClass('bg', modifier)]: withAlphaVariable({
23-
color: value,
24-
property: 'background-color',
25-
variable: '--tw-bg-opacity',
26-
}),
27-
}
28-
}),
29-
],
19+
return {
20+
[nameClass('bg', modifier)]: withAlphaVariable({
21+
color: value,
22+
property: 'background-color',
23+
variable: '--tw-bg-opacity',
24+
}),
25+
}
26+
},
3027
})
3128
}

src/corePlugins/backgroundImage.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
const nameClass = require('tailwindcss/lib/util/nameClass').default
2-
const { newFormat } = require('../pluginUtils')
32

4-
module.exports = function ({ jit: { theme, addUtilities, addVariant, e } }) {
5-
addUtilities({
6-
bg: [
7-
newFormat((modifier, { theme }) => {
8-
let value = theme.backgroundImage[modifier]
3+
module.exports = function ({ matchUtilities, jit: { theme } }) {
4+
matchUtilities({
5+
bg: (modifier, { theme }) => {
6+
let value = theme.backgroundImage[modifier]
97

10-
if (value === undefined) {
11-
return []
12-
}
8+
if (value === undefined) {
9+
return []
10+
}
1311

14-
return { [nameClass('bg', modifier)]: { 'background-image': value } }
15-
}),
16-
],
12+
return { [nameClass('bg', modifier)]: { 'background-image': value } }
13+
},
1714
})
1815
}

src/corePlugins/backgroundOpacity.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ const nameClass = require('tailwindcss/lib/util/nameClass').default
22
const transformThemeValue = require('tailwindcss/lib/util/transformThemeValue').default
33
const { asValue } = require('../pluginUtils')
44

5-
module.exports = function ({ jit: { theme, addUtilities, addVariant, e } }) {
5+
module.exports = function ({ matchUtilities, jit: { theme, addVariant, e } }) {
66
let transformValue = transformThemeValue('backgroundOpacity')
77

8-
addUtilities({
9-
'bg-opacity': [
10-
(modifier, { theme }) => {
11-
let value = asValue(modifier, theme.backgroundOpacity)
8+
matchUtilities({
9+
'bg-opacity': (modifier, { theme }) => {
10+
let value = asValue(modifier, theme.backgroundOpacity)
1211

13-
if (value === undefined) {
14-
return []
15-
}
12+
if (value === undefined) {
13+
return []
14+
}
1615

17-
return [[nameClass('bg-opacity', modifier), { '--tw-bg-opacity': value }]]
18-
},
19-
],
16+
return { [nameClass('bg-opacity', modifier)]: { '--tw-bg-opacity': value } }
17+
},
2018
})
2119
}

src/lib/setupContext.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,22 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
342342
return prefixSelector(tailwindConfig.prefix, selector)
343343
}
344344

345+
function addUtilities(utilities) {
346+
let offset = offsets.utilities++
347+
348+
for (let identifier in utilities) {
349+
let value = [].concat(utilities[identifier])
350+
351+
let withOffsets = value.map((rule) => [{ sort: offset, layer: 'utilities' }, rule])
352+
353+
if (!context.candidateRuleMap.has(identifier)) {
354+
context.candidateRuleMap.set(identifier, [])
355+
}
356+
357+
context.candidateRuleMap.get(identifier).push(...withOffsets)
358+
}
359+
}
360+
345361
return {
346362
// Classic plugin API
347363
addVariant(variantName, applyThisVariant, options = {}) {
@@ -430,6 +446,16 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
430446
context.candidateRuleMap.get(identifier).push([{ sort: offset, layer: 'utilities' }, rule])
431447
}
432448
},
449+
matchUtilities: function (utilities) {
450+
utilities = Object.fromEntries(
451+
Object.entries(utilities).map(([key, value]) => {
452+
value.format = 'new'
453+
return [key, value]
454+
})
455+
)
456+
457+
addUtilities(utilities)
458+
},
433459
// ---
434460
jit: {
435461
e: escapeClassName,
@@ -454,21 +480,7 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
454480
context.candidateRuleMap.get(identifier).push(...withOffsets)
455481
}
456482
},
457-
addUtilities(utilities) {
458-
let offset = offsets.utilities++
459-
460-
for (let identifier in utilities) {
461-
let value = [].concat(utilities[identifier])
462-
463-
let withOffsets = value.map((rule) => [{ sort: offset, layer: 'utilities' }, rule])
464-
465-
if (!context.candidateRuleMap.has(identifier)) {
466-
context.candidateRuleMap.set(identifier, [])
467-
}
468-
469-
context.candidateRuleMap.get(identifier).push(...withOffsets)
470-
}
471-
},
483+
addUtilities,
472484
},
473485
}
474486
}

0 commit comments

Comments
 (0)