Created
September 20, 2023 16:28
-
-
Save ibnlanre/403993196715fd755d0834ca0b83949e to your computer and use it in GitHub Desktop.
Utility to get the styles assigned to a certain size
This file contains hidden or 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 { CSSProperties } from "react"; | |
| type Size = string | number; | |
| type Property = keyof CSSProperties; | |
| type CSSProps<Type> = Partial<Record<Property, Type>>; | |
| type Transformer<Type, Value> = (value: Type) => Value; | |
| export type Sizes<Type> = Record<Size, CSSProps<Type>>; | |
| export function getSizeStyles<Type>(sizes: Sizes<Type>) { | |
| return function getStyles(size: Size) { | |
| const styles = sizes[size]; | |
| if (!styles) return (property: Property) => undefined; | |
| function getStyle(property: Property): Type | undefined; | |
| function getStyle<Type, Value>( | |
| property: Property, | |
| transformer: Transformer<Type, Value> | |
| ): Value; | |
| function getStyle<Type, Value>( | |
| property: Property, | |
| transformer?: Transformer<Type, Value> | |
| ) { | |
| const value = styles[property]; | |
| if (typeof value === "undefined") return undefined; | |
| return transformer ? transformer(value as Type) : value; | |
| } | |
| return getStyle; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment