Created
May 23, 2020 19:08
-
-
Save viktor-yakubiv/e94e1ba19f172650b8676fe3dce3960d to your computer and use it in GitHub Desktop.
CSS Modules Utils
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
class ClassName extends String { | |
and(s) { | |
const merged = [this.toString(), s].filter((meaningful) => meaningful).join(' ') | |
return new ClassName(merged) | |
} | |
} | |
const createModule = (map) => (strings, ...args) => { | |
const classList = [...strings.flatMap(s => s.split(/\s+/)), ...args] | |
.filter((meaningful) => meaningful) | |
.map((name) => (map[name] || name)) | |
return new ClassName(classList.join(' ')) | |
} | |
// Test | |
const scoped = createModule({ | |
button: "button-202005", | |
contained: "contained-202005", | |
outlined: "outlined-202005", | |
text: "text-202005", | |
disabled: "disabled-202005", | |
}) | |
const props = { | |
disabled: true, | |
className: 'button' | |
} | |
console.log(scoped`button outlined ${props.disabled && 'disabled'}`.and(props.className)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment