Skip to content

Instantly share code, notes, and snippets.

@kosciolek
Last active July 19, 2021 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosciolek/91dbdd855e9a6a475a98135f51c59942 to your computer and use it in GitHub Desktop.
Save kosciolek/91dbdd855e9a6a475a98135f51c59942 to your computer and use it in GitHub Desktop.
Styled components variant and iif.
export const iif = <PROPS extends unknown, PROP_NAME extends keyof PROPS>(
prop: PROP_NAME,
val?: PROPS[PROP_NAME]
) => (propsInner: PROPS) => {
if (val !== undefined) return propsInner[prop] === val ? "&" : "&.__NEVER";
return propsInner[prop] ? "&" : "&.__NEVER";
};
export const variant = <PROPS extends unknown>(
prop: PROPS[keyof PROPS],
propName?: keyof PROPS
) => (propsInner: PROPS) => {
// @ts-ignore
return propsInner[propName || "variant"] === prop ? "&" : "&.__NEVER";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment