UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

1 lines 1.7 kB
{"version":3,"file":"create-polymorphic-component.mjs","sources":["../../../src/core/factory/create-polymorphic-component.ts"],"sourcesContent":["type ExtendedProps<Props = {}, OverrideProps = {}> = OverrideProps &\n Omit<Props, keyof OverrideProps>;\n\ntype ElementType = keyof React.JSX.IntrinsicElements | React.JSXElementConstructor<any>;\n\ntype PropsOf<C extends ElementType> = React.JSX.LibraryManagedAttributes<\n C,\n React.ComponentPropsWithoutRef<C>\n>;\n\ntype ComponentProp<C> = {\n component?: C;\n};\n\ntype InheritedProps<C extends ElementType, Props = {}> = ExtendedProps<PropsOf<C>, Props>;\n\nexport type PolymorphicRef<C> = C extends React.ElementType\n ? React.ComponentPropsWithRef<C>['ref']\n : never;\n\nexport type PolymorphicComponentProps<C, Props = {}> = C extends React.ElementType\n ? InheritedProps<C, Props & ComponentProp<C>> & {\n ref?: PolymorphicRef<C>;\n renderRoot?: (props: any) => any;\n }\n : Props & { component: React.ElementType; renderRoot?: (props: Record<string, any>) => any };\n\nexport function createPolymorphicComponent<\n ComponentDefaultType,\n Props,\n StaticComponents = Record<string, never>,\n>(component: any) {\n type ComponentProps<C> = PolymorphicComponentProps<C, Props>;\n\n type _PolymorphicComponent = <C = ComponentDefaultType>(\n props: ComponentProps<C>\n ) => React.ReactElement;\n\n type ComponentProperties = Omit<React.FunctionComponent<ComponentProps<any>>, never>;\n\n type PolymorphicComponent = _PolymorphicComponent & ComponentProperties & StaticComponents;\n\n return component as PolymorphicComponent;\n}\n"],"names":[],"mappings":"AA2BO,SAAS,2BAId,SAAgB,EAAA;AAWhB,EAAO,OAAA,SAAA;AACT;;;;"}