Skip to content

Commit

Permalink
Add center and padding options to container plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Mar 13, 2018
1 parent c254d97 commit 9ee6a30
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 4 deletions.
162 changes: 162 additions & 0 deletions __tests__/containerPlugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import _ from 'lodash'
import postcss from 'postcss'
import processPlugins from '../src/util/processPlugins'
import container from '../src/plugins/container'

function css(nodes) {
return postcss.root({ nodes }).toString()
}

function processPluginsWithValidConfig(config) {
return processPlugins(
_.defaultsDeep(config, {
screens: {
sm: '576px',
md: '768px',
lg: '992px',
xl: '1200px',
},
options: {
prefix: '',
important: false,
separator: ':',
},
})
)
}

test('options are not required', () => {
const [components] = processPluginsWithValidConfig({
plugins: [container()],
})

expect(css(components)).toMatchCss(`
.container { width: 100% }
@media (min-width: 576px) {
.container { max-width: 576px }
}
@media (min-width: 768px) {
.container { max-width: 768px }
}
@media (min-width: 992px) {
.container { max-width: 992px }
}
@media (min-width: 1200px) {
.container { max-width: 1200px }
}
`)
})

test('screens can be specified explicitly', () => {
const [components] = processPluginsWithValidConfig({
plugins: [
container({
screens: {
sm: '400px',
lg: '500px',
},
}),
],
})

expect(css(components)).toMatchCss(`
.container { width: 100% }
@media (min-width: 400px) {
.container { max-width: 400px }
}
@media (min-width: 500px) {
.container { max-width: 500px }
}
`)
})

test('the container can be centered by default', () => {
const [components] = processPluginsWithValidConfig({
plugins: [
container({
center: true,
}),
],
})

expect(css(components)).toMatchCss(`
.container {
width: 100%;
margin-right: auto;
margin-left: auto
}
@media (min-width: 576px) {
.container { max-width: 576px }
}
@media (min-width: 768px) {
.container { max-width: 768px }
}
@media (min-width: 992px) {
.container { max-width: 992px }
}
@media (min-width: 1200px) {
.container { max-width: 1200px }
}
`)
})

test('horizontal padding can be included by default', () => {
const [components] = processPluginsWithValidConfig({
plugins: [
container({
padding: '2rem',
}),
],
})

expect(css(components)).toMatchCss(`
.container {
width: 100%;
padding-right: 2rem;
padding-left: 2rem
}
@media (min-width: 576px) {
.container { max-width: 576px }
}
@media (min-width: 768px) {
.container { max-width: 768px }
}
@media (min-width: 992px) {
.container { max-width: 992px }
}
@media (min-width: 1200px) {
.container { max-width: 1200px }
}
`)
})

test('setting all options at once', () => {
const [components] = processPluginsWithValidConfig({
plugins: [
container({
screens: {
sm: '400px',
lg: '500px',
},
center: true,
padding: '2rem',
}),
],
})

expect(css(components)).toMatchCss(`
.container {
width: 100%;
margin-right: auto;
margin-left: auto;
padding-right: 2rem;
padding-left: 2rem
}
@media (min-width: 400px) {
.container { max-width: 400px }
}
@media (min-width: 500px) {
.container { max-width: 500px }
}
`)
})
5 changes: 4 additions & 1 deletion defaultConfig.stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,10 @@ module.exports = {
*/

plugins: [
require('./plugins/container')(),
require('./plugins/container')({
// center: true,
// padding: '1rem',
}),
],


Expand Down
10 changes: 7 additions & 3 deletions src/plugins/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ module.exports = function(options) {

addComponents([
{
'.container': {
width: '100%',
},
'.container': Object.assign(
{ width: '100%' },
_.get(options, 'center', false) ? { marginRight: 'auto', marginLeft: 'auto' } : {},
_.has(options, 'padding')
? { paddingRight: options.padding, paddingLeft: options.padding }
: {}
),
},
...atRules,
])
Expand Down

0 comments on commit 9ee6a30

Please sign in to comment.