Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Created September 28, 2023 09:52
Show Gist options
  • Save karol-majewski/1451cfb29a8100ad9bb805f04e988e01 to your computer and use it in GitHub Desktop.
Save karol-majewski/1451cfb29a8100ad9bb805f04e988e01 to your computer and use it in GitHub Desktop.
Function composition
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