@interop-ui/react-polymorphic
Advanced tools
Comparing version 0.0.1-3 to 0.1.0-rc.1
import * as React from "react"; | ||
type NeverKeys<T> = { | ||
[P in keyof T]: T[P] extends never ? P : never; | ||
}[keyof T]; | ||
type MergeProps<P1 = {}, P2 = {}> = Omit<P1, keyof P2> & (NeverKeys<P2> extends never ? P2 : Omit<P2, NeverKeys<P2>>); | ||
type MergeWithDOMProps<E extends React.ElementType, P = {}> = MergeProps<React.ComponentPropsWithRef<E>, P>; | ||
export interface ForwardRefExoticComponentWithAs<DefaultElement extends keyof JSX.IntrinsicElements, OwnProps | ||
export type Merge<P1 = {}, P2 = {}> = Omit<P1, keyof P2> & P2; | ||
/** | ||
* Infers the OwnProps if E is a ForwardRefExoticComponentWithAs | ||
*/ | ||
export type OwnProps<E> = E extends ForwardRefComponent<any, infer P> ? P : {}; | ||
/** | ||
* Infers the JSX.IntrinsicElement if E is a ForwardRefExoticComponentWithAs | ||
*/ | ||
export type IntrinsicElement<E> = E extends ForwardRefComponent<infer I, any> ? I : never; | ||
type ForwardRefExoticComponent<E, OwnProps> = React.ForwardRefExoticComponent<Merge<E extends React.ElementType ? React.ComponentPropsWithRef<E> : never, OwnProps & { | ||
as?: E; | ||
}>>; | ||
export interface ForwardRefComponent<IntrinsicElementString, OwnProps = {} | ||
/** | ||
* Extends original type to ensure built in React types play nice | ||
* with polymorphic components still e.g. `React.ElementRef` etc. | ||
*/ | ||
> extends React.ForwardRefExoticComponent<MergeWithDOMProps<DefaultElement, OwnProps & { | ||
as?: DefaultElement; | ||
}>> { | ||
> extends ForwardRefExoticComponent<IntrinsicElementString, OwnProps> { | ||
/** | ||
* When passing an `as` prop as a string, use this overload. | ||
* Merges orignal own props (without DOM props) and the inferred props | ||
* When `as` prop is passed, use this overload. | ||
* Merges original own props (without DOM props) and the inferred props | ||
* from `as` element with the own props taking precendence. | ||
* | ||
* We explicitly define a `JSX.IntrinsicElements` overload so that | ||
* events are typed for consumers. | ||
* We explicitly avoid `React.ElementType` and manually narrow the prop types | ||
* so that events are typed when using JSX.IntrinsicElements. | ||
*/ | ||
<As extends keyof JSX.IntrinsicElements>(props: MergeWithDOMProps<As, OwnProps & { | ||
<As = IntrinsicElementString>(props: As extends '' ? { | ||
as: keyof JSX.IntrinsicElements; | ||
} : As extends React.ComponentType<infer P> ? Merge<P, OwnProps & { | ||
as: As; | ||
}>): JSX.Element; | ||
/** | ||
* When passing an `as` prop as a component, use this overload. | ||
* Merges orignal own props (without DOM props) and the inferred props | ||
* from `as` element with the own props taking precendence. | ||
* | ||
* We don't use `React.ComponentType` here as we get type errors | ||
* when consumers try to do inline `as` components. | ||
*/ | ||
<As extends React.ElementType<any>>(props: MergeWithDOMProps<As, OwnProps & { | ||
}> : As extends keyof JSX.IntrinsicElements ? Merge<JSX.IntrinsicElements[As], OwnProps & { | ||
as: As; | ||
}>): JSX.Element; | ||
}> : never): React.ReactElement | null; | ||
} | ||
/** | ||
* Infers the JSX.IntrinsicElement if E is a ForwardRefExoticComponentWithAs | ||
*/ | ||
type IntrinsicElement<E> = E extends ForwardRefExoticComponentWithAs<infer T, any> ? T : E; | ||
/** | ||
* If E is a ForwardRefExoticComponentWithAs then we know we are trying to forward to | ||
* a polymorphic component. When this happens we merge the new polymorphic's OwnProps | ||
* with the original polymorphic's OwnProps, ensuring the new props take precedence. | ||
*/ | ||
type ExtendedProps<E, OwnProps> = E extends ForwardRefExoticComponentWithAs<any, infer P> ? MergeProps<P, OwnProps> : OwnProps; | ||
/** | ||
* @example when creating a new polymorphic component | ||
* const Box = forwardRefWithAs<'div', { variant?: Variant }>() | ||
* | ||
* @example when extending an existing polymorphic component | ||
* const Flex = forwardRefWithAs<typeof Box, { direction?: FlexDirection }>() | ||
*/ | ||
export function forwardRefWithAs<E extends keyof JSX.IntrinsicElements | ForwardRefExoticComponentWithAs<any, any>, OwnProps = {}>(component: React.ForwardRefRenderFunction<React.ElementRef<IntrinsicElement<E>>, MergeProps<React.ComponentPropsWithoutRef<IntrinsicElement<E>>, ExtendedProps<E, OwnProps> & { | ||
as?: IntrinsicElement<E>; | ||
}>>): ForwardRefExoticComponentWithAs<IntrinsicElement<E>, ExtendedProps<E, OwnProps>>; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
var e,r,t=(e={},r=require("react"),Object.keys(r).forEach((function(t){"default"!==t&&"__esModule"!==t&&Object.defineProperty(e,t,{enumerable:!0,get:function(){return r[t]}})})),e);exports.forwardRefWithAs=function(e){return t.forwardRef(e)}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import*as r from"react";export function forwardRefWithAs(f){return r.forwardRef(f)} | ||
//# sourceMappingURL=index.module.js.map |
{ | ||
"name": "@interop-ui/react-polymorphic", | ||
"version": "0.0.1-3", | ||
"version": "0.1.0-rc.1", | ||
"license": "MIT", | ||
@@ -15,14 +15,20 @@ "source": "src/index.ts", | ||
"scripts": { | ||
"build": "parcel build src/index.ts --no-cache", | ||
"clean": "rm -rf dist", | ||
"version": "yarn version", | ||
"prepublish": "yarn clean && yarn build" | ||
"version": "yarn version" | ||
}, | ||
"devDependencies": { | ||
"@testing-library/react": "^10.4.8", | ||
"parcel": "^2.0.0-beta.1" | ||
"@testing-library/react": "^10.4.8" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.8 || ^17.0" | ||
} | ||
}, | ||
"homepage": "https://radix-ui.com/primitives", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/radix-ui/primitives.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/radix-ui/primitives/issues" | ||
}, | ||
"stableVersion": "0.0.14" | ||
} |
@@ -13,2 +13,2 @@ # `react-polymorphic` | ||
This is an internal utility, not intended for public usage. | ||
View docs [here](https://radix-ui.com/primitives/docs/utilities/polymorphic). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
1
1
1
0
4128
38
1