-
Notifications
You must be signed in to change notification settings - Fork 532
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
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2ceb0b8
add link to system.css?
shawnbot 515cfc7
make theme.js work with CommonJS
shawnbot 567283d
add build:css run-script; run it prestart
shawnbot ba02b82
add script/build-css, docs/system.css
shawnbot 1be5d3f
install postcss, stylefmt
shawnbot 322794f
convert build-css to a postcss plugin
shawnbot 036b888
configure rollup to provide named exports from theme.js
shawnbot 660953f
nix .bg-* utilities for non-bg colors
shawnbot d61442d
lint
shawnbot 1fd4fab
move postcss-plugin from src/ to lib/
shawnbot 3308443
fix rbga() formatting
shawnbot 3eb8aed
tidy up CSS generation; add responsive font-size utils
shawnbot dadafe6
silence postcss warnings about from
shawnbot cba934b
px function
shawnbot 8b02ae3
refactor build-css script
shawnbot 1613cea
lint
shawnbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.