Skip to content

Commit 9bbc3a9

Browse files
authored
Merge pull request #71 from ctaepper/feature/refactor-spacings
remove different scale logic for spacings
2 parents 45cf030 + 93700b4 commit 9bbc3a9

2 files changed

Lines changed: 14 additions & 36 deletions

File tree

config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ module.exports = {
22
"typeScale": [
33
3, 2.25, 1.5, 1.25, 1, 0.875, 0.75
44
],
5-
"spacing": {
6-
"root": 8,
7-
"ratio": 2,
8-
"steps": 7
9-
},
5+
"spacing": [2, 4, 6, 8, 10, 12, 14],
106
"customMedia": [
117
{ "m": 48 },
128
{ "l": 64 }

lib/spacing.js

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const directions = require('./directions')
22
const decls = require('./declarations')
33
const { step, mqSteps } = require('./docs-helper')
4+
const withUnits = require('./units').withUnits
45

56
const docs = (spacing, mqs) => `
67
/*
@@ -23,45 +24,26 @@ const docs = (spacing, mqs) => `
2324
b = bottom
2425
l = left
2526
26-
${
27-
Array.from({ length: spacing.steps + 1 })
28-
.map((_, idx) => step({ zeroth: '/none', nth: 'spacing scale' }, idx)).join('\n ')
29-
}
27+
${ [].concat(0, spacing).map((_, idx) => step({ zeroth: '/none', nth: 'spacing scale' }, idx )).join('\n ') }
3028
3129
Media Query Extensions:
3230
${mqSteps(mqs)}
3331
3432
*/`
3533

36-
const css = (spacing, mediaQueries) => {
37-
const steps = []
34+
const css = spacing =>
35+
[].concat(0, spacing).map((spacing, idx) =>
36+
Object.keys(directions).map(d =>
37+
[
38+
`.p${directions[d]}${idx} { ${fullDecl(d, spacing, 'padding')} }`,
39+
`.m${directions[d]}${idx} { ${fullDecl(d, spacing, 'margin')} }`,
40+
`.n${directions[d]}${idx} { ${fullDecl(d, `-${spacing}`, 'margin')} }`
41+
].join('\n')
42+
).join('\n')
43+
)
3844

39-
for (let i = 0; i <= spacing.steps; i++) {
40-
steps.push(spacingDecls(i, spacing.ratio))
41-
}
4245

43-
return steps.join('\n')
44-
}
45-
46-
const spacingDecls = (step, baseScale, opts) => {
47-
opts = opts || {}
48-
49-
const postfix = opts.postfix || ''
50-
const spacing = []
51-
const paddingClass = 'p' // TODO: Make configurable
52-
const marginClass = 'm'
53-
const negativeMarginClass = 'n'
54-
const size = step * baseScale
55-
56-
// TODO: Don't create negative margin classes for horiz/vert directions
57-
return Object.keys(directions).map(d => `
58-
.${paddingClass}${directions[d]}${step}${postfix} { ${fullDecl(d, size, 'padding')} }
59-
.${marginClass}${directions[d]}${step}${postfix} { ${fullDecl(d, size, 'margin')} }
60-
.${negativeMarginClass}${directions[d]}${step}${postfix} { ${fullDecl(d, -size, 'margin')} }
61-
`).join('\n')
62-
}
63-
64-
const fullDecl = (d, size, prop) => decls[d].map(dir => `${prop}${dir}: ${size}rem;`).join('\n')
46+
const fullDecl = (d, size, prop) => decls[d].map(dir => `${prop}${dir}: ${withUnits(size, 'rem')};`).join('\n')
6547

6648
module.exports = {
6749
css,

0 commit comments

Comments
 (0)