Skip to content

Instantly share code, notes, and snippets.

@bbg
Created July 16, 2022 05:37
Show Gist options
  • Save bbg/8a0da59817eeb5ce86b0d3768fea9b86 to your computer and use it in GitHub Desktop.
Save bbg/8a0da59817eeb5ce86b0d3768fea9b86 to your computer and use it in GitHub Desktop.
Generic Component
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