// Borrowed from Mantine (MIT) // https://github.com/mantinedev/mantine/blob/master/src/mantine-utils/src/create-polymorphic-component/create-polymorphic-component.ts import React from "react"; type ExtendedProps = OverrideProps & Omit; type ElementType = | keyof JSX.IntrinsicElements | React.JSXElementConstructor; type PropsOf = JSX.LibraryManagedAttributes< C, React.ComponentPropsWithoutRef >; type ComponentProp = { component?: C; }; type InheritedProps = ExtendedProps< PropsOf, Props >; type PolymorphicRef = C extends React.ElementType ? React.ComponentPropsWithRef["ref"] : never; type PolymorphicComponentProps = C extends React.ElementType ? InheritedProps> & { ref?: PolymorphicRef } : Props & { component: React.ElementType }; export function createPolymorphicComponent< ComponentDefaultType, Props, StaticComponents = Record >(component: any) { type ComponentProps = PolymorphicComponentProps; type _PolymorphicComponent = ( props: ComponentProps ) => React.ReactElement; type ComponentProperties = Omit< React.FunctionComponent>, never >; type PolymorphicComponent = _PolymorphicComponent & ComponentProperties & StaticComponents; return component as PolymorphicComponent; }