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

Don't treat the default key as special in createUtilityPlugin(), add mapTheme argument and convertDefaultKey helper function for plugins that need it #1384

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/plugins/transitionProperty.js
@@ -1,5 +1,9 @@
import createUtilityPlugin from '../util/createUtilityPlugin'
import createUtilityPlugin, { convertDefaultKey } from '../util/createUtilityPlugin'

export default function() {
return createUtilityPlugin('transitionProperty', [['transition', ['transitionProperty']]])
return createUtilityPlugin(
'transitionProperty',
[['transition', ['transitionProperty']]],
convertDefaultKey
)
}
15 changes: 12 additions & 3 deletions src/util/createUtilityPlugin.js
@@ -1,9 +1,10 @@
import _ from 'lodash'
import fromPairs from 'lodash/fromPairs'
import toPairs from 'lodash/toPairs'
import castArray from 'lodash/castArray'

function className(classPrefix, key) {
if (key === 'default') {
if (key === '') {
return classPrefix
}

Expand All @@ -14,11 +15,15 @@ function className(classPrefix, key) {
return `${classPrefix}-${key}`
}

export default function createUtilityPlugin(themeKey, utilityVariations) {
export default function createUtilityPlugin(
themeKey,
utilityVariations,
mapTheme = theme => theme
) {
return function({ e, addUtilities, variants, theme }) {
const utilities = utilityVariations.map(([classPrefix, properties]) => {
return fromPairs(
toPairs(theme(themeKey)).map(([key, value]) => {
toPairs(mapTheme(theme(themeKey))).map(([key, value]) => {
return [
`.${e(className(classPrefix, key))}`,
fromPairs(castArray(properties).map(property => [property, value])),
Expand All @@ -29,3 +34,7 @@ export default function createUtilityPlugin(themeKey, utilityVariations) {
return addUtilities(utilities, variants(themeKey))
}
}

export function convertDefaultKey(theme) {
return _.mapKeys(theme, (value, key) => (key === 'default' ? '' : key))
}