-
-
Save lifeiscontent/84bbce3bd14d04b5140382378dafc35e to your computer and use it in GitHub Desktop.
This file contains 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 * as React from "react"; | |
// Props for Polymorphic Tag property | |
export type TagProps<TTag extends React.ElementType> = { | |
Tag?: TTag; | |
}; | |
// React.ComponentProps but for use with Polymorphic `Tag` property | |
export type ComponentPropsWithoutRef< | |
TTag extends React.ElementType, | |
TProps | |
> = TProps & | |
TagProps<TTag> & | |
Omit<React.ComponentPropsWithoutRef<TTag>, keyof (TProps & TagProps<TTag>)>; | |
// React.ComponentPropsWithRef but for use with Polymorphic `Tag` property | |
export type ComponentPropsWithRef< | |
TTag extends React.ElementType, | |
TProps | |
> = ComponentPropsWithoutRef<TTag, TProps> & { | |
ref?: ForwardedRef<TTag>; | |
}; | |
// Helper function to lookup the ref type of a component | |
export type ForwardedRef<TTag extends React.ElementType> = React.ForwardedRef< | |
React.ElementRef<TTag> | |
>; | |
export const forwardRef = React.forwardRef as < | |
TDefaultTag extends TElementType, | |
TProps, | |
TElementType extends React.ElementType = React.ElementType | |
>( | |
renderFn: ( | |
props: ComponentPropsWithoutRef<TElementType, TProps>, | |
ref: ForwardedRef<TElementType> | |
) => React.ReactElement | null | |
) => <TTag extends TElementType = TDefaultTag>( | |
props: ComponentPropsWithRef<TTag, TProps> | |
) => React.ReactElement | null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment