Created
July 16, 2022 05:37
-
-
Save bbg/8a0da59817eeb5ce86b0d3768fea9b86 to your computer and use it in GitHub Desktop.
Generic Component
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 clsx from 'clsx'; | |
type GenericOwnProps<E extends React.ElementType = React.ElementType> = { | |
children: string; | |
as?: E; | |
cls?: string | Record<string, string> | string[]; | |
}; | |
type GenericProps<E extends React.ElementType> = GenericOwnProps<E> & | |
Omit<React.ComponentProps<E>, keyof GenericOwnProps>; | |
const __DEFAULT_ELEMENT__ = 'div'; | |
function Title<E extends React.ElementType = typeof __DEFAULT_ELEMENT__>({ | |
children, | |
as, | |
cls, | |
...props | |
}: GenericProps<E>) { | |
const Component = as || __DEFAULT_ELEMENT__; | |
return ( | |
<Component | |
className={clsx(cls)} | |
{...props} | |
> | |
{children} | |
</Component> | |
); | |
} | |
export default Title; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment