Last active
July 20, 2018 14:42
-
-
Save zaydek-old/8f2bbc2d4969b95aa43e266ec9779f37 to your computer and use it in GitHub Desktop.
An idea for how to meta-program a CSS framework. See output.css below
This file contains hidden or 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
| // $var and $class generate a new CSS variable/class | |
| const $var = ($obj, $key) => `--${$key}: ${$obj.$vars[$key]};` | |
| const $class = ($obj, $key) => `.${$key} { ${$obj.$prop}: var(--${$key}); }` | |
| // $factory builds a CSS string | |
| function $factory($pre, $obj, $fn) { | |
| return Object | |
| .keys($obj.$vars) | |
| .map ($key => $fn($obj, $key)) | |
| .join(`\n${$pre}`) | |
| } | |
| // $vars and $classes call $var and $class with arguments | |
| const $vars = ($obj) => `:root {\n\t${$factory("\t", $obj, $var)}\n}` | |
| const $classes = ($obj) => `\n\n${$factory("", $obj, $class)}` | |
| // $generate builds a CSS string with vars and classes | |
| const $generate = ($obj) => $vars($obj) + $classes($obj) | |
| const $lineHeight = { | |
| $prop: "line-height", | |
| $vars: { | |
| "single-spaced": "1.2", | |
| "normal-spaced": "1.5", | |
| "double-spaced": "2.0" | |
| } | |
| } | |
| console.log($generate($lineHeight)) |
This file contains hidden or 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
| :root { | |
| --single-spaced: 1.2; | |
| --normal-spaced: 1.5; | |
| --double-spaced: 2.0; | |
| } | |
| .single-spaced { line-height: var(--single-spaced); } | |
| .normal-spaced { line-height: var(--normal-spaced); } | |
| .double-spaced { line-height: var(--double-spaced); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment