-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: polymorphic component (#211)
* refactor: polymorphic component * cleanup
- Loading branch information
Showing
4 changed files
with
91 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Taken from: https://www.benmvp.com/blog/forwarding-refs-polymorphic-react-component-typescript/ | ||
|
||
// A more precise version of just React.ComponentPropsWithoutRef on its own | ||
export type PropsOf< | ||
C extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any> | ||
> = JSX.LibraryManagedAttributes<C, React.ComponentPropsWithoutRef<C>> | ||
|
||
type AsProp<C extends React.ElementType> = { | ||
/** | ||
* An override of the default HTML tag. | ||
* Can also be another React component. | ||
*/ | ||
as?: C | ||
} | ||
|
||
/** | ||
* Allows for extending a set of props (`ExtendedProps`) by an overriding set of props | ||
* (`OverrideProps`), ensuring that any duplicates are overridden by the overriding | ||
* set of props. | ||
*/ | ||
export type ExtendableProps< | ||
ExtendedProps = {}, | ||
OverrideProps = {} | ||
> = OverrideProps & Omit<ExtendedProps, keyof OverrideProps> | ||
|
||
/** | ||
* Allows for inheriting the props from the specified element type so that | ||
* props like children, className & style work, as well as element-specific | ||
* attributes like aria roles. The component (`C`) must be passed in. | ||
*/ | ||
export type InheritableElementProps< | ||
C extends React.ElementType, | ||
Props = {} | ||
> = ExtendableProps<PropsOf<C>, Props> | ||
|
||
export type PolymorphicComponentProps< | ||
C extends React.ElementType, | ||
Props = {} | ||
> = InheritableElementProps<C, Props & AsProp<C>> | ||
|
||
/** | ||
* Utility type to extract the `ref` prop from a polymorphic component | ||
*/ | ||
export type PolymorphicRef< | ||
C extends React.ElementType | ||
> | ||
= React.ComponentPropsWithRef<C>['ref'] | ||
|
||
/** | ||
* A wrapper of `PolymorphicComponentProps` that also includes the `ref` | ||
* prop for the polymorphic component | ||
*/ | ||
export type PolymorphicComponentPropsWithRef< | ||
C extends React.ElementType, | ||
Props = {} | ||
> | ||
= PolymorphicComponentProps<C, Props> & { ref?: PolymorphicRef<C> } |
28f0271
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nextjs-sanity-fe – ./
nextjs-sanity-fe-git-main-formidable-labs.vercel.app
nextjs-sanity-fe.vercel.app
nextjs-sanity-fe-formidable-labs.vercel.app