Skip to content

Instantly share code, notes, and snippets.

@vincentriemer
Created July 22, 2019 03:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincentriemer/85781f87ceb45f950d84e789bb4c71fd to your computer and use it in GitHub Desktop.
Save vincentriemer/85781f87ceb45f950d84e789bb4c71fd to your computer and use it in GitHub Desktop.
instyle
/**
* @flow
*/
opaque type Ruleset = { [string]: string | number };
type CreateInput = {
[styleName: string]: CSSProperties
};
type ToRuleset = () => Ruleset;
function create<T: CreateInput>(input: T): $ObjMap<T, ToRuleset> {
return input;
}
type InStyle = {
(...styles: Array<CSSProperties | Ruleset>): CSSProperties,
create: typeof create
};
function _instyle(...styles: Array<CSSProperties | Ruleset>): CSSProperties {
if (styles.length === 1) {
return styles[0];
}
const result: CSSProperties = styles.reduce((acc, cur) => {
if (cur != null) {
for (const key in cur) {
if (cur.hasOwnProperty(key)) {
acc[key] = cur[key];
}
}
}
return acc;
}, ({}: CSSProperties));
return result;
}
_instyle.create = create;
const instyle: InStyle = _instyle;
export default instyle;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment