Skip to content

Commit ea074a4

Browse files
committed
Add support for generating background-image utilities
1 parent 6b0441e commit ea074a4

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ module.exports = function () {
66
addUtilities, addComponents, addBase, addVariant,
77
e, prefix, theme, variants, config,
88
}) {
9-
const pluginUtilities = {
10-
// '.bg': {},
11-
// '.bg-image': {},
9+
const buildObjectFromTheme = themeKey => {
10+
const buildObject = ([ modifier, value ]) => [ modifier, { [themeKey]: value } ]
11+
const themeEntries = Object.entries(theme(themeKey, {})).map(entry => buildObject(entry))
12+
return _.fromPairs(themeEntries)
13+
}
1214

15+
const pluginUtilities = {
16+
image: buildObjectFromTheme('backgroundImage'),
1317
clip: {
1418
border: { backgroundClip: 'border-box' },
1519
padding: { backgroundClip: 'padding-box' },
@@ -23,14 +27,16 @@ module.exports = function () {
2327
},
2428
}
2529

26-
Object.entries(pluginUtilities).forEach(([ modifier, values ]) => {
27-
const variantName = _.camelCase(`background-${modifier}`)
28-
const utilities = flatten(
29-
{ [`.${e(`bg-${modifier}`)}`]: values },
30-
{ delimiter: '-', maxDepth: 2 },
31-
)
30+
Object.entries(pluginUtilities)
31+
.filter(([ modifier, values ]) => !_.isEmpty(values))
32+
.forEach(([ modifier, values ]) => {
33+
const variantName = _.camelCase(`background-${modifier}`)
34+
const utilities = flatten(
35+
{ [`.${e(`bg-${modifier}`)}`]: values },
36+
{ delimiter: '-', maxDepth: 2 },
37+
)
3238

33-
addUtilities(utilities, variants(variantName, ['responsive']))
34-
})
39+
addUtilities(utilities, variants(variantName, ['responsive']))
40+
})
3541
}
3642
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "tailwindcss-background-extended",
33
"version": "0.1.0",
44
"scripts": {
5-
"test": "jest"
5+
"test": "jest",
6+
"dev": "jest --watch"
67
},
78
"description": "This plugin adds all of the missing background utilities to Tailwind CSS.",
89
"license": "MIT",

test.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _ = require('lodash')
2+
13
const plugin = require('./index.js')
24
const postcss = require('postcss')
35
const tailwindcss = require('tailwindcss')
@@ -10,7 +12,7 @@ const generatePluginCss = (testConfig = {}, pluginOptions = {}) => {
1012
plugins: [ plugin(pluginOptions) ],
1113
}
1214
const postcssPlugins =[
13-
tailwindcss({ ...sandboxConfig, ...testConfig }),
15+
tailwindcss(_.merge(sandboxConfig, testConfig)),
1416
]
1517

1618
return postcss(postcssPlugins)
@@ -20,7 +22,7 @@ const generatePluginCss = (testConfig = {}, pluginOptions = {}) => {
2022

2123
expect.extend({ toMatchCss: require('jest-matcher-css') })
2224

23-
test('the plugin generates some utilities and responsive variants by default', () => {
25+
test('generates default utilities and responsive variants', () => {
2426
const testConfig = {}
2527
const expectedCss = `
2628
.bg-clip-border { background-clip: border-box }
@@ -76,3 +78,40 @@ test('variants can be customized', () => {
7678

7779
return generatePluginCss(testConfig).then(css => expect(css).toMatchCss(expectedCss))
7880
})
81+
82+
test('utilities can be customized', () => {
83+
const testConfig = {
84+
theme: {
85+
backgroundImage: {
86+
tailwindcss: "url('https://avatars0.githubusercontent.com/u/30317862')",
87+
},
88+
},
89+
}
90+
const expectedCss = `
91+
.bg-image-tailwindcss { background-image: url('https://avatars0.githubusercontent.com/u/30317862') }
92+
93+
.bg-clip-border { background-clip: border-box }
94+
.bg-clip-padding { background-clip: padding-box }
95+
.bg-clip-content { background-clip: content-box }
96+
.bg-clip-text { background-clip: text }
97+
98+
.bg-origin-border { background-origin: border-box }
99+
.bg-origin-padding { background-origin: padding-box }
100+
.bg-origin-content { background-origin: content-box }
101+
102+
@media (min-width: 640px) {
103+
.sm\\:bg-image-tailwindcss { background-image: url('https://avatars0.githubusercontent.com/u/30317862') }
104+
105+
.sm\\:bg-clip-border { background-clip: border-box }
106+
.sm\\:bg-clip-padding { background-clip: padding-box }
107+
.sm\\:bg-clip-content { background-clip: content-box }
108+
.sm\\:bg-clip-text { background-clip: text }
109+
110+
.sm\\:bg-origin-border { background-origin: border-box }
111+
.sm\\:bg-origin-padding { background-origin: padding-box }
112+
.sm\\:bg-origin-content { background-origin: content-box }
113+
}
114+
`
115+
116+
return generatePluginCss(testConfig).then(css => expect(css).toMatchCss(expectedCss))
117+
})

0 commit comments

Comments
 (0)