Skip to content

Commit 9ee6a30

Browse files
committed
Add center and padding options to container plugin
1 parent c254d97 commit 9ee6a30

File tree

3 files changed

+173
-4
lines changed

3 files changed

+173
-4
lines changed

__tests__/containerPlugin.test.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import _ from 'lodash'
2+
import postcss from 'postcss'
3+
import processPlugins from '../src/util/processPlugins'
4+
import container from '../src/plugins/container'
5+
6+
function css(nodes) {
7+
return postcss.root({ nodes }).toString()
8+
}
9+
10+
function processPluginsWithValidConfig(config) {
11+
return processPlugins(
12+
_.defaultsDeep(config, {
13+
screens: {
14+
sm: '576px',
15+
md: '768px',
16+
lg: '992px',
17+
xl: '1200px',
18+
},
19+
options: {
20+
prefix: '',
21+
important: false,
22+
separator: ':',
23+
},
24+
})
25+
)
26+
}
27+
28+
test('options are not required', () => {
29+
const [components] = processPluginsWithValidConfig({
30+
plugins: [container()],
31+
})
32+
33+
expect(css(components)).toMatchCss(`
34+
.container { width: 100% }
35+
@media (min-width: 576px) {
36+
.container { max-width: 576px }
37+
}
38+
@media (min-width: 768px) {
39+
.container { max-width: 768px }
40+
}
41+
@media (min-width: 992px) {
42+
.container { max-width: 992px }
43+
}
44+
@media (min-width: 1200px) {
45+
.container { max-width: 1200px }
46+
}
47+
`)
48+
})
49+
50+
test('screens can be specified explicitly', () => {
51+
const [components] = processPluginsWithValidConfig({
52+
plugins: [
53+
container({
54+
screens: {
55+
sm: '400px',
56+
lg: '500px',
57+
},
58+
}),
59+
],
60+
})
61+
62+
expect(css(components)).toMatchCss(`
63+
.container { width: 100% }
64+
@media (min-width: 400px) {
65+
.container { max-width: 400px }
66+
}
67+
@media (min-width: 500px) {
68+
.container { max-width: 500px }
69+
}
70+
`)
71+
})
72+
73+
test('the container can be centered by default', () => {
74+
const [components] = processPluginsWithValidConfig({
75+
plugins: [
76+
container({
77+
center: true,
78+
}),
79+
],
80+
})
81+
82+
expect(css(components)).toMatchCss(`
83+
.container {
84+
width: 100%;
85+
margin-right: auto;
86+
margin-left: auto
87+
}
88+
@media (min-width: 576px) {
89+
.container { max-width: 576px }
90+
}
91+
@media (min-width: 768px) {
92+
.container { max-width: 768px }
93+
}
94+
@media (min-width: 992px) {
95+
.container { max-width: 992px }
96+
}
97+
@media (min-width: 1200px) {
98+
.container { max-width: 1200px }
99+
}
100+
`)
101+
})
102+
103+
test('horizontal padding can be included by default', () => {
104+
const [components] = processPluginsWithValidConfig({
105+
plugins: [
106+
container({
107+
padding: '2rem',
108+
}),
109+
],
110+
})
111+
112+
expect(css(components)).toMatchCss(`
113+
.container {
114+
width: 100%;
115+
padding-right: 2rem;
116+
padding-left: 2rem
117+
}
118+
@media (min-width: 576px) {
119+
.container { max-width: 576px }
120+
}
121+
@media (min-width: 768px) {
122+
.container { max-width: 768px }
123+
}
124+
@media (min-width: 992px) {
125+
.container { max-width: 992px }
126+
}
127+
@media (min-width: 1200px) {
128+
.container { max-width: 1200px }
129+
}
130+
`)
131+
})
132+
133+
test('setting all options at once', () => {
134+
const [components] = processPluginsWithValidConfig({
135+
plugins: [
136+
container({
137+
screens: {
138+
sm: '400px',
139+
lg: '500px',
140+
},
141+
center: true,
142+
padding: '2rem',
143+
}),
144+
],
145+
})
146+
147+
expect(css(components)).toMatchCss(`
148+
.container {
149+
width: 100%;
150+
margin-right: auto;
151+
margin-left: auto;
152+
padding-right: 2rem;
153+
padding-left: 2rem
154+
}
155+
@media (min-width: 400px) {
156+
.container { max-width: 400px }
157+
}
158+
@media (min-width: 500px) {
159+
.container { max-width: 500px }
160+
}
161+
`)
162+
})

defaultConfig.stub.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,10 @@ module.exports = {
899899
*/
900900

901901
plugins: [
902-
require('./plugins/container')(),
902+
require('./plugins/container')({
903+
// center: true,
904+
// padding: '1rem',
905+
}),
903906
],
904907

905908

src/plugins/container.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ module.exports = function(options) {
4040

4141
addComponents([
4242
{
43-
'.container': {
44-
width: '100%',
45-
},
43+
'.container': Object.assign(
44+
{ width: '100%' },
45+
_.get(options, 'center', false) ? { marginRight: 'auto', marginLeft: 'auto' } : {},
46+
_.has(options, 'padding')
47+
? { paddingRight: options.padding, paddingLeft: options.padding }
48+
: {}
49+
),
4650
},
4751
...atRules,
4852
])

0 commit comments

Comments
 (0)