UNPKG

@spark-web/utils

Version:

--- title: Utilities isExperimentalPackage: true ---

21 lines (20 loc) 1.55 kB
import type { ComponentPropsWithoutRef, ElementType, ReactElement, ReactNode, Ref } from 'react'; declare type ElementTagNameMap = HTMLElementTagNameMap & Pick<SVGElementTagNameMap, Exclude<keyof SVGElementTagNameMap, keyof HTMLElementTagNameMap>>; declare type AsProp<Comp extends ElementType> = { as?: Comp; ref?: Ref<Comp extends keyof ElementTagNameMap ? ElementTagNameMap[Comp] : Comp extends new (...args: any) => any ? InstanceType<Comp> : undefined>; } & Omit<ComponentPropsWithoutRef<Comp>, 'as'>; declare type CompWithAsProp<Props, DefaultElementType extends ElementType> = <Comp extends ElementType = DefaultElementType>(props: AsProp<Comp> & Props) => ReactElement; /** * This is a hack for sure. The thing is, getting a component to intelligently * infer props based on a component or JSX string passed into an `as` prop is * kind of a huge pain. Getting it to work and satisfy the constraints of * `forwardRef` seems dang near impossible. To avoid needing to do this awkward * type song-and-dance every time we want to forward a ref into a component * that accepts an `as` prop, we abstract all of that mess to this function for * the time time being. */ export declare const forwardRefWithAs: <DefaultElementType extends ElementType<any, keyof import("react").JSX.IntrinsicElements>, BaseProps>(render: (props: BaseProps & { as?: ElementType<any, keyof import("react").JSX.IntrinsicElements> | undefined; }, ref: Ref<any>) => Exclude<ReactNode, undefined>) => CompWithAsProp<BaseProps, DefaultElementType>; export {};