@wordpress/components
Version:
UI components for WordPress.
51 lines • 2.86 kB
TypeScript
import type * as React from 'react';
/**
* Loose intrinsic attribute bag used when a polymorphic component is rendered
* with an `as` prop. Previous implementations enabled precise typings for the
* given `as` element (e.g. `as="label"` unlocks `htmlFor`), but doing so
* contributed to high memory usage and slow compilation times.
*/
type PolymorphicIntrinsicProps = Omit<React.AllHTMLAttributes<Element> & React.SVGAttributes<Element>, 'as' | 'children'>;
export type WordPressComponentProps<
/** Prop types. */
P,
/** The HTML element to inherit props from. */
T extends React.ElementType | null,
/** Supports polymorphism through the `as` prop. */
IsPolymorphic extends boolean = true> = P & (T extends React.ElementType ? Omit<React.ComponentPropsWithoutRef<T>, 'as' | keyof P | 'children'> : {}) & (IsPolymorphic extends true ? {
/** The HTML element or React component to render the component as. */
as?: T | keyof React.JSX.IntrinsicElements | React.JSXElementConstructor<any>;
} : {});
export type WordPressComponent<T extends React.ElementType | null, O, IsPolymorphic extends boolean> = (IsPolymorphic extends true ? {
/**
* Intrinsic `as` (e.g. `as="label"`): accept a flat HTML/SVG attribute
* bag instead of remapping to that tag's props.
*/
(props: WordPressComponentProps<O, T, false> & {
/** The HTML element to render the component as. */
as: keyof React.JSX.IntrinsicElements;
} & Omit<PolymorphicIntrinsicProps, keyof O>): React.ReactNode;
/**
* Component `as` (e.g. `as={ Item }`): remap to that component's props.
*/
<C extends React.JSXElementConstructor<any>>(props: WordPressComponentProps<O, T, false> & {
/** The React component to render the component as. */
as: C;
} & Omit<React.ComponentPropsWithoutRef<C>, 'as' | keyof O | 'children'>): React.ReactNode;
} : unknown) & {
(props: WordPressComponentProps<O, T, IsPolymorphic>): React.ReactNode;
displayName?: string;
/**
* A CSS selector used to fake component interpolation in styled components
* for components not generated by `styled`. Anything passed to `contextConnect`
* will get this property.
*
* We restrict it to a class to align with the already existing class names that
* are generated by the context system.
*/
selector?: `.${string}`;
};
type RefProps<ForwardsRef extends boolean> = ForwardsRef extends true ? Pick<React.RefAttributes<any>, 'ref'> : {};
export type WordPressComponentFromProps<Props, ForwardsRef extends boolean = true> = 'as' extends keyof Props ? Props extends WordPressComponentProps<infer P, infer T, true> ? WordPressComponent<T, P & RefProps<ForwardsRef>, true> : never : WordPressComponent<null, Props & RefProps<ForwardsRef>, false>;
export {};
//# sourceMappingURL=wordpress-component.d.ts.map