Created
September 28, 2023 09:52
-
-
Save karol-majewski/1451cfb29a8100ad9bb805f04e988e01 to your computer and use it in GitHub Desktop.
Function composition
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
import { FlattenSimpleInterpolation } from 'styled-components'; | |
import { pick } from './objects'; | |
type Stylesheet<K extends string> = Record<K, FlattenSimpleInterpolation>; | |
/** | |
* CSS-in-JS equivalent for `classNames`. | |
* | |
* @example | |
* | |
* ```ts | |
* const stylesheet = { | |
* base: css` | |
* font-weight: bold | |
* `, | |
* h1: css` | |
* margin-bottom: 40px; | |
* `, | |
* }; | |
* | |
* const styles = getStyles(stylesheet)({ | |
* base: true, | |
* h1: false, | |
* }) | |
* ``` | |
*/ | |
export function getStyles<K extends string>(stylesheet: Stylesheet<K>): (configuration: Slice<Record<K, boolean>>) => FlattenSimpleInterpolation[] { | |
return (configuration) => Object.values(pick(stylesheet, configuration)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment