-
-
Notifications
You must be signed in to change notification settings - Fork 552
/
utils.ts
155 lines (136 loc) · 3.99 KB
/
utils.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Fragment, createContext, createElement, isValidElement } from 'react';
import { createPortal } from 'react-dom';
import type { ComponentProps, ReactNode, Ref } from 'react';
import type { VNode } from '../million';
import type { MillionPortal } from '../types';
import { REGISTRY, RENDER_SCOPE } from './constants';
export const scopedContext = createContext<boolean>(false)
// TODO: access perf impact of this
export const processProps = (
props: ComponentProps<any>,
ref: Ref<any>,
portals: MillionPortal[]
): ComponentProps<any> => {
const processedProps: ComponentProps<any> = { ref };
let currentIndex = 0;
for (const key in props) {
const value = props[key];
if (
isValidElement(value) ||
(Array.isArray(value) && value.length && isValidElement(value[0]))
) {
processedProps[key] = renderReactScope(
value,
false,
portals,
currentIndex++
);
continue;
}
processedProps[key] = props[key];
}
return processedProps;
};
const wrap = (vnode: ReactNode): ReactNode => {
return createElement(RENDER_SCOPE, { suppressHydrationWarning: true }, vnode);
};
export const renderReactScope = (
vnode: ReactNode,
unstable: boolean,
portals: MillionPortal[] | undefined,
currentIndex: number,
scoped?: boolean
) => {
const el = portals?.[currentIndex]?.current;
const isBlock =
isValidElement(vnode) &&
typeof vnode.type === 'function' &&
'_c' in vnode.type;
const isCallable = isBlock && (vnode.type as any)._c;
if (typeof window === 'undefined') {
if (isBlock) {
if (isCallable) {
return vnode;
}
return wrap(wrap(vnode));
}
return wrap(vnode);
}
if (isCallable) {
const puppetComponent = (vnode.type as any)(vnode.props);
if (REGISTRY.has(puppetComponent.type)) {
const puppetBlock = REGISTRY.get(puppetComponent.type)!;
if (typeof puppetBlock === 'function') {
return puppetBlock(puppetComponent.props);
}
}
}
const current = el ?? document.createElement(RENDER_SCOPE);
const reactPortal = createPortal(
createElement(scopedContext.Provider, { value: scoped }, vnode),
current
);
const millionPortal = {
foreign: true as const,
current,
portal: reactPortal,
unstable,
};
if (portals) {
portals[currentIndex] = millionPortal;
}
return millionPortal;
};
export const unwrap = (vnode: JSX.Element | null): VNode => {
if (typeof vnode !== 'object' || vnode === null || !('type' in vnode)) {
if (typeof vnode === 'number') {
return String(vnode);
}
return vnode;
}
let type = vnode.type;
if (typeof type === 'function') {
return unwrap(type(vnode.props ?? {}));
}
if (typeof type === 'object' && '$' in type) return type;
const props = { ...vnode.props };
// emotion support
if ('css' in props && '__EMOTION_TYPE_PLEASE_DO_NOT_USE__' in props) {
props.style = props.css.styles;
type = props.__EMOTION_TYPE_PLEASE_DO_NOT_USE__;
delete props.__EMOTION_TYPE_PLEASE_DO_NOT_USE__;
delete props.css;
}
const children = vnode.props?.children;
if (children !== undefined && children !== null) {
props.children = flatten(vnode.props.children).map((child) =>
unwrap(child)
);
}
return {
type, // lets pretend no function go through
props,
};
};
export const flatten = (rawChildren?: JSX.Element | null): JSX.Element[] => {
if (rawChildren === undefined || rawChildren === null) return [];
if (
typeof rawChildren === 'object' &&
'type' in rawChildren &&
rawChildren.type === Fragment
) {
return flatten(rawChildren.props.children);
}
if (
!Array.isArray(rawChildren) ||
(typeof rawChildren === 'object' && '$' in rawChildren)
) {
return [rawChildren];
}
const flattenedChildren = rawChildren.flat(Infinity);
const children: JSX.Element[] = [];
for (let i = 0, l = flattenedChildren.length; i < l; ++i) {
children.push(...flatten(flattenedChildren[i]));
}
return children;
};