|
1 | 1 | import type { ElementType, MemoExoticComponent, ReactElement } from 'react' |
| 2 | +import * as React from 'react' |
2 | 3 |
|
3 | 4 | // Directly ported from: |
4 | | -// https://unpkg.com/browse/react-is@18.3.0-canary-ee68446ff-20231115/cjs/react-is.production.js |
| 5 | +// https://unpkg.com/browse/react-is@19.0.0/cjs/react-is.production.js |
5 | 6 | // It's very possible this could change in the future, but given that |
6 | 7 | // we only use these in `connect`, this is a low priority. |
7 | 8 |
|
8 | | -const REACT_ELEMENT_TYPE = Symbol.for('react.element') |
9 | | -const REACT_PORTAL_TYPE = Symbol.for('react.portal') |
10 | | -const REACT_FRAGMENT_TYPE = Symbol.for('react.fragment') |
11 | | -const REACT_STRICT_MODE_TYPE = Symbol.for('react.strict_mode') |
12 | | -const REACT_PROFILER_TYPE = Symbol.for('react.profiler') |
13 | | -const REACT_PROVIDER_TYPE = Symbol.for('react.provider') |
14 | | -const REACT_CONTEXT_TYPE = Symbol.for('react.context') |
15 | | -const REACT_SERVER_CONTEXT_TYPE = Symbol.for('react.server_context') |
16 | | -const REACT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref') |
17 | | -const REACT_SUSPENSE_TYPE = Symbol.for('react.suspense') |
18 | | -const REACT_SUSPENSE_LIST_TYPE = Symbol.for('react.suspense_list') |
19 | | -const REACT_MEMO_TYPE = Symbol.for('react.memo') |
20 | | -const REACT_LAZY_TYPE = Symbol.for('react.lazy') |
21 | | -const REACT_OFFSCREEN_TYPE = Symbol.for('react.offscreen') |
22 | | -const REACT_CLIENT_REFERENCE = Symbol.for('react.client.reference') |
| 9 | +export const IS_REACT_19 = /* @__PURE__ */ React.version.startsWith('19') |
| 10 | + |
| 11 | +const REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for( |
| 12 | + IS_REACT_19 ? 'react.transitional.element' : 'react.element', |
| 13 | +) |
| 14 | +const REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for('react.portal') |
| 15 | +const REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for('react.fragment') |
| 16 | +const REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for('react.strict_mode') |
| 17 | +const REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for('react.profiler') |
| 18 | +const REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for('react.consumer') |
| 19 | +const REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for('react.context') |
| 20 | +const REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for('react.forward_ref') |
| 21 | +const REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for('react.suspense') |
| 22 | +const REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for( |
| 23 | + 'react.suspense_list', |
| 24 | +) |
| 25 | +const REACT_MEMO_TYPE = /* @__PURE__ */ Symbol.for('react.memo') |
| 26 | +const REACT_LAZY_TYPE = /* @__PURE__ */ Symbol.for('react.lazy') |
| 27 | +const REACT_OFFSCREEN_TYPE = /* @__PURE__ */ Symbol.for('react.offscreen') |
| 28 | +const REACT_CLIENT_REFERENCE = /* @__PURE__ */ Symbol.for( |
| 29 | + 'react.client.reference', |
| 30 | +) |
23 | 31 |
|
24 | 32 | export const ForwardRef = REACT_FORWARD_REF_TYPE |
25 | 33 | export const Memo = REACT_MEMO_TYPE |
26 | 34 |
|
27 | 35 | export function isValidElementType(type: any): type is ElementType { |
28 | | - if (typeof type === 'string' || typeof type === 'function') { |
29 | | - return true |
30 | | - } // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill). |
31 | | - |
32 | | - if ( |
| 36 | + return typeof type === 'string' || |
| 37 | + typeof type === 'function' || |
33 | 38 | type === REACT_FRAGMENT_TYPE || |
34 | 39 | type === REACT_PROFILER_TYPE || |
35 | 40 | type === REACT_STRICT_MODE_TYPE || |
36 | 41 | type === REACT_SUSPENSE_TYPE || |
37 | 42 | type === REACT_SUSPENSE_LIST_TYPE || |
38 | | - type === REACT_OFFSCREEN_TYPE |
39 | | - ) { |
40 | | - return true |
41 | | - } |
42 | | - |
43 | | - if (typeof type === 'object' && type !== null) { |
44 | | - if ( |
45 | | - type.$$typeof === REACT_LAZY_TYPE || |
46 | | - type.$$typeof === REACT_MEMO_TYPE || |
47 | | - type.$$typeof === REACT_PROVIDER_TYPE || |
48 | | - type.$$typeof === REACT_CONTEXT_TYPE || |
49 | | - type.$$typeof === REACT_FORWARD_REF_TYPE || // This needs to include all possible module reference object |
50 | | - // types supported by any Flight configuration anywhere since |
51 | | - // we don't know which Flight build this will end up being used |
52 | | - // with. |
53 | | - type.$$typeof === REACT_CLIENT_REFERENCE || |
54 | | - type.getModuleId !== undefined |
55 | | - ) { |
56 | | - return true |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - return false |
| 43 | + type === REACT_OFFSCREEN_TYPE || |
| 44 | + (typeof type === 'object' && |
| 45 | + type !== null && |
| 46 | + (type.$$typeof === REACT_LAZY_TYPE || |
| 47 | + type.$$typeof === REACT_MEMO_TYPE || |
| 48 | + type.$$typeof === REACT_CONTEXT_TYPE || |
| 49 | + type.$$typeof === REACT_CONSUMER_TYPE || |
| 50 | + type.$$typeof === REACT_FORWARD_REF_TYPE || |
| 51 | + type.$$typeof === REACT_CLIENT_REFERENCE || |
| 52 | + type.getModuleId !== undefined)) |
| 53 | + ? !0 |
| 54 | + : !1 |
61 | 55 | } |
62 | 56 |
|
63 | 57 | function typeOf(object: any): symbol | undefined { |
64 | 58 | if (typeof object === 'object' && object !== null) { |
65 | | - const $$typeof = object.$$typeof |
| 59 | + const { $$typeof } = object |
66 | 60 |
|
67 | 61 | switch ($$typeof) { |
68 | | - case REACT_ELEMENT_TYPE: { |
69 | | - const type = object.type |
70 | | - |
71 | | - switch (type) { |
| 62 | + case REACT_ELEMENT_TYPE: |
| 63 | + switch (((object = object.type), object)) { |
72 | 64 | case REACT_FRAGMENT_TYPE: |
73 | 65 | case REACT_PROFILER_TYPE: |
74 | 66 | case REACT_STRICT_MODE_TYPE: |
75 | 67 | case REACT_SUSPENSE_TYPE: |
76 | 68 | case REACT_SUSPENSE_LIST_TYPE: |
77 | | - return type |
78 | | - |
79 | | - default: { |
80 | | - const $$typeofType = type && type.$$typeof |
81 | | - |
82 | | - switch ($$typeofType) { |
83 | | - case REACT_SERVER_CONTEXT_TYPE: |
| 69 | + return object |
| 70 | + default: |
| 71 | + switch (((object = object && object.$$typeof), object)) { |
84 | 72 | case REACT_CONTEXT_TYPE: |
85 | 73 | case REACT_FORWARD_REF_TYPE: |
86 | 74 | case REACT_LAZY_TYPE: |
87 | 75 | case REACT_MEMO_TYPE: |
88 | | - case REACT_PROVIDER_TYPE: |
89 | | - return $$typeofType |
90 | | - |
| 76 | + return object |
| 77 | + case REACT_CONSUMER_TYPE: |
| 78 | + return object |
91 | 79 | default: |
92 | 80 | return $$typeof |
93 | 81 | } |
94 | | - } |
95 | 82 | } |
96 | | - } |
97 | | - |
98 | | - case REACT_PORTAL_TYPE: { |
| 83 | + case REACT_PORTAL_TYPE: |
99 | 84 | return $$typeof |
100 | | - } |
101 | 85 | } |
102 | 86 | } |
103 | | - |
104 | | - return undefined |
105 | 87 | } |
106 | 88 |
|
107 | 89 | export function isContextConsumer(object: any): object is ReactElement { |
108 | | - return typeOf(object) === REACT_CONTEXT_TYPE |
| 90 | + return IS_REACT_19 |
| 91 | + ? typeOf(object) === REACT_CONSUMER_TYPE |
| 92 | + : typeOf(object) === REACT_CONTEXT_TYPE |
109 | 93 | } |
110 | 94 |
|
111 | 95 | export function isMemo(object: any): object is MemoExoticComponent<any> { |
|
0 commit comments