Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive flatten color and backgroundImage #2549

40 changes: 40 additions & 0 deletions __tests__/flattenColorPalette.test.js
Expand Up @@ -43,3 +43,43 @@ test('it flattens nested color objects', () => {
'blue-3': 'rgb(0,0,100)',
})
})

test('it recusively flattens nested color objects', () => {
lbennett-stacki marked this conversation as resolved.
Show resolved Hide resolved
expect(
flattenColorPalette({
brand: {
primary: {
default: 'rgb(100,0,0)',
50: 'rgba(100,0,0,.5)',
},
},
theme: {
default: {
background: {
default: 'rgb(0,0,0)',
50: 'rgba(0,0,0,.5)',
},
},
'not-default': {
background: {
default: 'rgb(255,255,255)',
50: 'rgba(255,255,255,.5)',
'keep-going': {
default: 'rgb(128,128,128)',
50: 'rgba(128,128,128,.5)',
},
},
},
},
})
).toEqual({
'brand-primary': 'rgb(100,0,0)',
'brand-primary-50': 'rgba(100,0,0,.5)',
'theme-default-background': 'rgb(0,0,0)',
'theme-default-background-50': 'rgba(0,0,0,.5)',
'theme-not-default-background': 'rgb(255,255,255)',
'theme-not-default-background-50': 'rgba(255,255,255,.5)',
'theme-not-default-background-keep-going': 'rgb(128,128,128)',
'theme-not-default-background-keep-going-50': 'rgba(128,128,128,.5)',
})
})
115 changes: 115 additions & 0 deletions __tests__/plugins/backgroundImage.test.js
@@ -0,0 +1,115 @@
import invokePlugin from '../util/invokePlugin'
import plugin from '../../src/plugins/backgroundImage'

test('it creates background-image utilities', () => {
const config = {
target: 'relaxed',
theme: {
backgroundImage: {
hero: 'url("./hero.png")',
},
},
variants: {
backgroundImage: [],
},
}

const { utilities } = invokePlugin(plugin(), config)

expect(utilities).toEqual([
[
{
'.bg-hero': {
'background-image': 'url("./hero.png")',
},
},
[],
],
])
})

test('it creates background-image utilities in ie11 mode', () => {
const config = {
target: 'ie11',
theme: {
backgroundImage: {
hero: 'url("./hero.png")',
},
},
variants: {
backgroundImage: [],
},
}

const { utilities } = invokePlugin(plugin(), config)

expect(utilities).toEqual([
[
{
'.bg-hero': {
'background-image': 'url("./hero.png")',
},
},
[],
],
])
})

test('it creates background-image utilities recusively', () => {
lbennett-stacki marked this conversation as resolved.
Show resolved Hide resolved
const config = {
target: 'relaxed',
theme: {
backgroundImage: {
theme: {
default: {
background: {
default: 'url("./theme-default-background.png")',
alt: 'url("./theme-default-background-alt.png")',
},
},
'not-default': {
background: {
default: 'url("./theme-not-default-background.png")',
alt: 'url("./theme-not-default-background-alt.png")',
'keep-going': {
default: 'url("./theme-not-default-background-keep-going.png")',
alt: 'url("./theme-not-default-background-keep-going-alt.png")',
},
},
},
},
},
},
variants: {
backgroundImage: [],
},
}

const { utilities } = invokePlugin(plugin(), config)

expect(utilities).toEqual([
[
{
'.bg-theme-default-background': {
'background-image': 'url("./theme-default-background.png")',
},
'.bg-theme-default-background-alt': {
'background-image': 'url("./theme-default-background-alt.png")',
},
'.bg-theme-not-default-background': {
'background-image': 'url("./theme-not-default-background.png")',
},
'.bg-theme-not-default-background-alt': {
'background-image': 'url("./theme-not-default-background-alt.png")',
},
'.bg-theme-not-default-background-keep-going': {
'background-image': 'url("./theme-not-default-background-keep-going.png")',
},
'.bg-theme-not-default-background-keep-going-alt': {
'background-image': 'url("./theme-not-default-background-keep-going-alt.png")',
},
},
[],
],
])
})
3 changes: 2 additions & 1 deletion src/plugins/backgroundImage.js
@@ -1,10 +1,11 @@
import _ from 'lodash'
import usesCustomProperties from '../util/usesCustomProperties'
import flattenColorPalette from '../util/flattenColorPalette'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
const utilities = _.fromPairs(
_.map(theme('backgroundImage'), (value, modifier) => {
_.map(flattenColorPalette(theme('backgroundImage')), (value, modifier) => {
if (target('backgroundImage') === 'ie11' && usesCustomProperties(value)) {
return []
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/flattenColorPalette.js
Expand Up @@ -7,7 +7,7 @@ export default function flattenColorPalette(colors) {
return [[name, color]]
}

return _.map(color, (value, key) => {
return _.map(flattenColorPalette(color), (value, key) => {
const suffix = key === 'default' ? '' : `-${key}`
return [`${name}${suffix}`, value]
})
Expand Down