Skip to content

Allow users to override a core plugin's configuration #644

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

Merged
merged 7 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions __tests__/configurePlugins.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import configurePlugins from '../src/util/configurePlugins'

test('setting a plugin to false removes it', () => {
const plugins = {
fontSize: options => {
return {
plugin: 'fontSize',
options,
}
},
display: options => {
return {
plugin: 'display',
options,
}
},
backgroundPosition: options => {
return {
plugin: 'backgroundPosition',
options,
}
},
}

const configuredPlugins = configurePlugins(plugins, {
fontSize: {},
display: false,
backgroundPosition: {},
})

expect(configuredPlugins).toEqual([
{ plugin: 'fontSize', options: {} },
{ plugin: 'backgroundPosition', options: {} },
])
})

test('setting a plugin to an object configures that plugin', () => {
const plugins = {
fontSize: options => {
return {
plugin: 'fontSize',
options,
}
},
display: options => {
return {
plugin: 'display',
options,
}
},
backgroundPosition: options => {
return {
plugin: 'backgroundPosition',
options,
}
},
}

const configuredPlugins = configurePlugins(plugins, {
fontSize: {
variants: ['responsive', 'hover'],
values: { '12': '12px', '14': '14px', '16': '16px' },
},
display: { variants: ['responsive'] },
backgroundPosition: {},
})

expect(configuredPlugins).toEqual([
{
plugin: 'fontSize',
options: {
variants: ['responsive', 'hover'],
values: { '12': '12px', '14': '14px', '16': '16px' },
},
},
{ plugin: 'display', options: { variants: ['responsive'] } },
{ plugin: 'backgroundPosition', options: {} },
])
})

test('plugins are configured with their default configuration if no custom config is provided', () => {
const plugins = {
fontSize: options => {
return {
plugin: 'fontSize',
options,
}
},
display: options => {
return {
plugin: 'display',
options,
}
},
backgroundPosition: options => {
return {
plugin: 'backgroundPosition',
options,
}
},
}

const configuredPlugins = configurePlugins(
plugins,
{
fontSize: {
variants: ['responsive', 'hover'],
values: { '12': '12px', '14': '14px', '16': '16px' },
},
backgroundPosition: {},
},
{
display: { variants: ['responsive'] },
}
)

expect(configuredPlugins).toEqual([
{
plugin: 'fontSize',
options: {
variants: ['responsive', 'hover'],
values: { '12': '12px', '14': '14px', '16': '16px' },
},
},
{ plugin: 'display', options: { variants: ['responsive'] } },
{ plugin: 'backgroundPosition', options: {} },
])
})

test('custom plugin configuration overrides default plugin configuration', () => {
const plugins = {
fontSize: options => {
return {
plugin: 'fontSize',
options,
}
},
display: options => {
return {
plugin: 'display',
options,
}
},
backgroundPosition: options => {
return {
plugin: 'backgroundPosition',
options,
}
},
}

const configuredPlugins = configurePlugins(
plugins,
{
fontSize: {
variants: ['responsive', 'hover'],
values: { '12': '12px', '14': '14px', '16': '16px' },
},
display: { variants: ['responsive'] },
backgroundPosition: {},
},
{
fontSize: {
variants: ['focus', 'active'],
values: { sm: '.75rem', md: '1rem', lg: '1.5rem' },
},
}
)

expect(configuredPlugins).toEqual([
{
plugin: 'fontSize',
options: {
variants: ['responsive', 'hover'],
values: { '12': '12px', '14': '14px', '16': '16px' },
},
},
{ plugin: 'display', options: { variants: ['responsive'] } },
{ plugin: 'backgroundPosition', options: {} },
])
})
18 changes: 12 additions & 6 deletions src/corePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,21 @@ import whitespace from './plugins/whitespace'
import width from './plugins/width'
import zIndex from './plugins/zIndex'

import _ from 'lodash'
import configurePlugins from './util/configurePlugins'

function loadPlugins({ theme, variants, corePlugins }, plugins) {
return Object.keys(plugins)
.filter(plugin => corePlugins[plugin] !== false)
.map(plugin =>
plugins[plugin]({
const defaultCorePluginConfig = _.fromPairs(
Object.keys(plugins).map(plugin => [
plugin,
{
values: theme[plugin],
variants: variants[plugin],
})
)
},
])
)

return configurePlugins(plugins, corePlugins, defaultCorePluginConfig)
}

export default function(config) {
Expand Down
11 changes: 11 additions & 0 deletions src/util/configurePlugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import _ from 'lodash'

export default function(plugins, pluginConfig, defaultPluginConfig = {}) {
return Object.keys(plugins)
.filter(pluginName => {
return pluginConfig[pluginName] !== false
})
.map(pluginName => {
return plugins[pluginName](_.get(pluginConfig, pluginName, defaultPluginConfig[pluginName]))
})
}