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

Generate "system" CSS #147

Closed
wants to merge 16 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,021 changes: 3,021 additions & 0 deletions docs/system.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions html.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = ({
<link rel='stylesheet' href='https://unpkg.com/primer-product/build/build.css'/>
<link rel='stylesheet' href='https://unpkg.com/primer-tooltips/build/build.css'/>
<link rel='stylesheet' href='https://unpkg.com/primer-utilities/build/build.css'/>
<link rel='stylesheet' href='${publicPath}system.css' />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you end up getting this to work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, not yet. I'm going to try inlining it via our Styles component.

<link rel='icon' href='assets/favicon.png' />
<link rel='apple-touch-icon' href='assets/apple-touch-icon.png' />
<meta name='og:title' content='Primer React' />
Expand Down
136 changes: 136 additions & 0 deletions lib/postcss-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const postcss = require('postcss')
const defaultTheme = require('../src/theme')

const px = d => `${d}px`

const directions = {
t: 'top',
r: 'right',
b: 'bottom',
l: 'left'
}

const edges = {
t: ['top-left', 'top-right'],
r: ['top-right', 'bottom-right'],
b: ['bottom-left', 'bottom-right'],
l: ['top-left', 'bottom-left']
}

const verticalHorizontal = {
x: ['right', 'left'],
y: ['top', 'bottom']
}

module.exports = postcss.plugin('primer-generate', opts => {
const {theme = defaultTheme} = opts || {}

const {breakpoints, fontSizes, radii, space} = theme
const {border, bg, ...namedColors} = theme.colors
const spacers = space.map(px)
const breaks = ['sm', 'md', 'lg', 'xl']

const colors = entries(namedColors).reduce((map, [name, values]) => {
if (Array.isArray(values)) {
for (const [i, value] of entries(values)) {
map[`${name}-${i}`] = value
}
} else {
map[name] = values
}
return map
}, {})

const props = [
{select: '.bg-{key}', property: 'background-color', values: bg},
{select: '.border-{key}', property: 'border-color', values: border},
{select: '.color-{key}', property: 'color', values: colors},
{select: '.f{brk}-{key}', property: 'font-size', values: fontSizes.map(px), breaks},
{select: '.m{dir}{brk}-{key}', property: 'margin', values: spacers, breaks, directions},
{select: '.p{dir}{brk}-{key}', property: 'padding', values: spacers, breaks, directions},
{select: '.m{dir}{brk}-{key}', property: 'margin', values: spacers, breaks, edges: verticalHorizontal},
{select: '.p{dir}{brk}-{key}', property: 'padding', values: spacers, breaks, edges: verticalHorizontal},
{select: '.r{brk}-{key}', property: 'border-radius', values: radii.map(px), breaks},
{select: '.r{dir}{brk}-{key}', property: 'border-{dir}-radius', values: radii.map(px), breaks, edges}
]

return root => {
const atMedia = breaks.reduce((map, name, i) => {
map[name] = postcss.atRule({name: 'media', params: `(min-width: ${breakpoints[i]})`})
return map
}, {})

for (const {select, property, values, breaks, directions, edges} of props) {
let variants = []
for (const [key, value] of entries(values)) {
variants.push({prop: property, key, value})
}
if (directions) {
variants = cross(variants, Object.keys(directions), ({prop, ...d}, direction) => {
return merge(d, {prop: `${prop}-${directions[direction]}`, direction})
})
} else if (edges) {
variants = cross(variants, Object.keys(edges), ({prop, ...d}, edge) => {
const dirs = edges[edge]
const props = prop.includes('{dir}')
? dirs.map(dir => expand(prop, {dir}))
: dirs.map(dir => `${prop}-${dir}`)
return merge(d, {prop: props, direction: edge})
})
}
if (breaks) {
variants = cross(variants, [null, ...breaks], (d, breakpoint) => merge(d, {breakpoint}))
}
for (const {prop, key, value, direction, breakpoint} of variants) {
const selector = expand(select, {
key,
value,
dir: direction,
brk: breakpoint && `-${breakpoint}`
})
const rule = postcss.rule({selector})
if (Array.isArray(prop)) {
const decls = prop.map(p => postcss.decl({prop: p, value, important: true}))
for (const decl of decls) {
rule.append(decl)
}
} else {
const decl = postcss.decl({prop, value, important: true})
rule.append(decl)
}
const parent = breakpoint ? atMedia[breakpoint] : root
parent.append(rule)
}
}

for (const name of Object.keys(atMedia)) {
root.append(atMedia[name])
}
}
})

function entries(d) {
if (Array.isArray(d)) {
return d.map((v, i) => [i, v])
} else if (d && typeof d === 'object') {
return [...Object.entries(d)]
} else {
return [[d, d]]
}
}

function cross(a, b, join) {
return a.reduce((res, A) => res.concat(b.map(B => join(A, B))), [])
}

function merge(...args) {
return Object.assign({}, ...args)
}

function expand(template, values) {
return template.replace(/{(\w+)}/g, (_, key) => defined(values[key], ''))
}

function defined(value, fallback) {
return value === null || typeof value === 'undefined' ? fallback : value
}
Loading