-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathwordpress-component.ts
More file actions
62 lines (59 loc) · 1.96 KB
/
Copy pathwordpress-component.ts
File metadata and controls
62 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* External dependencies
*/
import type * as React from 'react';
// Based on https://github.com/ariakit/ariakit/blob/reakit/packages/reakit-utils/src/types.ts
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
? // The `children` prop is being explicitly omitted since it is otherwise implicitly added
// by `ComponentPropsWithRef`. The context is that components should require the `children`
// prop explicitly when needed (see https://github.com/WordPress/gutenberg/pull/31817).
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;
}
: {} );
export type WordPressComponent<
T extends React.ElementType | null,
O,
IsPolymorphic extends boolean,
> = {
< TT extends React.ElementType >(
props: WordPressComponentProps< O, TT, IsPolymorphic > &
( IsPolymorphic extends true ? { as: TT } : {} )
): React.ReactNode;
( 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 }`;
};
export type WordPressComponentFromProps<
Props,
ForwardsRef extends boolean = true,
> = Props extends WordPressComponentProps< infer P, infer T, infer I >
? WordPressComponent<
T,
P & ( ForwardsRef extends true ? React.RefAttributes< any > : {} ),
I
>
: never;