@tarojs/react 3.5.0-canary.1 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react.esm.js CHANGED
@@ -1,297 +1,311 @@
1
+ import { isString, isObject, isFunction, capitalize, toCamelCase, internalComponents, isNumber, EMPTY_ARR, isBoolean, isUndefined, noop, ensure } from '@tarojs/shared';
2
+ import { FormElement, document } from '@tarojs/runtime';
1
3
  import Reconciler from 'react-reconciler';
2
4
  import * as scheduler from 'scheduler';
3
- import { FormElement, document } from '@tarojs/runtime';
4
- import { isString, isObject, isFunction, capitalize, toCamelCase, internalComponents, isNumber, EMPTY_ARR, isBoolean, isUndefined, noop, ensure } from '@tarojs/shared';
5
5
 
6
- function isEventName(s) {
7
- return s[0] === 'o' && s[1] === 'n';
8
- }
9
- const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord/i;
10
- function updateProps(dom, oldProps, newProps) {
11
- let i;
12
- for (i in oldProps) {
13
- if (!(i in newProps)) {
14
- setProperty(dom, i, null, oldProps[i]);
15
- }
16
- }
17
- const isFormElement = dom instanceof FormElement;
18
- for (i in newProps) {
19
- if (oldProps[i] !== newProps[i] || (isFormElement && i === 'value')) {
20
- setProperty(dom, i, newProps[i], oldProps[i]);
21
- }
22
- }
23
- }
24
- // function eventProxy (e: CommonEvent) {
25
- // const el = document.getElementById(e.currentTarget.id)
26
- // const handlers = el!.__handlers[e.type]
27
- // handlers[0](e)
28
- // }
29
- function setEvent(dom, name, value, oldValue) {
30
- const isCapture = name.endsWith('Capture');
31
- let eventName = name.toLowerCase().slice(2);
32
- if (isCapture) {
33
- eventName = eventName.slice(0, -7);
34
- }
35
- const compName = capitalize(toCamelCase(dom.tagName.toLowerCase()));
36
- if (eventName === 'click' && compName in internalComponents) {
37
- eventName = 'tap';
38
- }
39
- if (isFunction(value)) {
40
- if (!oldValue) {
41
- dom.addEventListener(eventName, value, isCapture);
42
- }
43
- if (eventName === 'regionchange') {
44
- dom.__handlers.begin[0] = value;
45
- dom.__handlers.end[0] = value;
46
- }
47
- else {
48
- dom.__handlers[eventName][0] = value;
49
- }
50
- }
51
- else {
52
- dom.removeEventListener(eventName, oldValue);
53
- }
54
- }
55
- function setStyle(style, key, value) {
56
- if (key[0] === '-') {
57
- style.setProperty(key, value.toString());
58
- }
59
- style[key] =
60
- isNumber(value) && IS_NON_DIMENSIONAL.test(key) === false
61
- ? value + 'px'
62
- : value == null
63
- ? ''
64
- : value;
65
- }
66
- function setProperty(dom, name, value, oldValue) {
67
- var _a, _b, _c, _d;
68
- name = name === 'className' ? 'class' : name;
69
- if (name === 'key' ||
70
- name === 'children' ||
71
- name === 'ref') ;
72
- else if (name === 'style') {
73
- const style = dom.style;
74
- if (isString(value)) {
75
- style.cssText = value;
76
- }
77
- else {
78
- if (isString(oldValue)) {
79
- style.cssText = '';
80
- oldValue = null;
81
- }
82
- if (isObject(oldValue)) {
83
- for (const i in oldValue) {
84
- if (!(value && i in value)) {
85
- setStyle(style, i, '');
86
- }
87
- }
88
- }
89
- if (isObject(value)) {
90
- for (const i in value) {
91
- if (!oldValue || value[i] !== oldValue[i]) {
92
- setStyle(style, i, value[i]);
93
- }
94
- }
95
- }
96
- }
97
- }
98
- else if (isEventName(name)) {
99
- setEvent(dom, name, value, oldValue);
100
- }
101
- else if (name === 'dangerouslySetInnerHTML') {
102
- const newHtml = (_b = (_a = value) === null || _a === void 0 ? void 0 : _a.__html) !== null && _b !== void 0 ? _b : '';
103
- const oldHtml = (_d = (_c = oldValue) === null || _c === void 0 ? void 0 : _c.__html) !== null && _d !== void 0 ? _d : '';
104
- if (newHtml || oldHtml) {
105
- if (oldHtml !== newHtml) {
106
- dom.innerHTML = newHtml;
107
- }
108
- }
109
- }
110
- else if (!isFunction(value)) {
111
- if (value == null) {
112
- dom.removeAttribute(name);
113
- }
114
- else {
115
- dom.setAttribute(name, value);
116
- }
117
- }
6
+ function isEventName(s) {
7
+ return s[0] === 'o' && s[1] === 'n';
8
+ }
9
+ const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord/i;
10
+ function updateProps(dom, oldProps, newProps) {
11
+ let i;
12
+ for (i in oldProps) {
13
+ if (!(i in newProps)) {
14
+ setProperty(dom, i, null, oldProps[i]);
15
+ }
16
+ }
17
+ const isFormElement = dom instanceof FormElement;
18
+ for (i in newProps) {
19
+ if (oldProps[i] !== newProps[i] || (isFormElement && i === 'value')) {
20
+ setProperty(dom, i, newProps[i], oldProps[i]);
21
+ }
22
+ }
23
+ }
24
+ // function eventProxy (e: CommonEvent) {
25
+ // const el = document.getElementById(e.currentTarget.id)
26
+ // const handlers = el!.__handlers[e.type]
27
+ // handlers[0](e)
28
+ // }
29
+ function setEvent(dom, name, value, oldValue) {
30
+ const isCapture = name.endsWith('Capture');
31
+ let eventName = name.toLowerCase().slice(2);
32
+ if (isCapture) {
33
+ eventName = eventName.slice(0, -7);
34
+ }
35
+ const compName = capitalize(toCamelCase(dom.tagName.toLowerCase()));
36
+ if (eventName === 'click' && compName in internalComponents) {
37
+ eventName = 'tap';
38
+ }
39
+ if (isFunction(value)) {
40
+ if (oldValue) {
41
+ dom.removeEventListener(eventName, oldValue, false);
42
+ dom.addEventListener(eventName, value, { isCapture, sideEffect: false });
43
+ }
44
+ else {
45
+ dom.addEventListener(eventName, value, isCapture);
46
+ }
47
+ }
48
+ else {
49
+ dom.removeEventListener(eventName, oldValue);
50
+ }
51
+ }
52
+ function setStyle(style, key, value) {
53
+ if (key[0] === '-') {
54
+ style.setProperty(key, value.toString());
55
+ }
56
+ style[key] =
57
+ isNumber(value) && IS_NON_DIMENSIONAL.test(key) === false
58
+ ? value + 'px'
59
+ : value == null
60
+ ? ''
61
+ : value;
62
+ }
63
+ function setProperty(dom, name, value, oldValue) {
64
+ var _a, _b;
65
+ name = name === 'className' ? 'class' : name;
66
+ if (name === 'key' ||
67
+ name === 'children' ||
68
+ name === 'ref') ;
69
+ else if (name === 'style') {
70
+ const style = dom.style;
71
+ if (isString(value)) {
72
+ style.cssText = value;
73
+ }
74
+ else {
75
+ if (isString(oldValue)) {
76
+ style.cssText = '';
77
+ oldValue = null;
78
+ }
79
+ if (isObject(oldValue)) {
80
+ for (const i in oldValue) {
81
+ if (!(value && i in value)) {
82
+ setStyle(style, i, '');
83
+ }
84
+ }
85
+ }
86
+ if (isObject(value)) {
87
+ for (const i in value) {
88
+ if (!oldValue || value[i] !== oldValue[i]) {
89
+ setStyle(style, i, value[i]);
90
+ }
91
+ }
92
+ }
93
+ }
94
+ }
95
+ else if (isEventName(name)) {
96
+ setEvent(dom, name, value, oldValue);
97
+ }
98
+ else if (name === 'dangerouslySetInnerHTML') {
99
+ const newHtml = (_a = value === null || value === void 0 ? void 0 : value.__html) !== null && _a !== void 0 ? _a : '';
100
+ const oldHtml = (_b = oldValue === null || oldValue === void 0 ? void 0 : oldValue.__html) !== null && _b !== void 0 ? _b : '';
101
+ if (newHtml || oldHtml) {
102
+ if (oldHtml !== newHtml) {
103
+ dom.innerHTML = newHtml;
104
+ }
105
+ }
106
+ }
107
+ else if (!isFunction(value)) {
108
+ if (value == null) {
109
+ dom.removeAttribute(name);
110
+ }
111
+ else {
112
+ dom.setAttribute(name, value);
113
+ }
114
+ }
118
115
  }
119
116
 
120
- /* eslint-disable @typescript-eslint/indent */
121
- const { unstable_now: now } = scheduler;
122
- function returnFalse() {
123
- return false;
124
- }
125
- const hostConfig = {
126
- createInstance(type) {
127
- return document.createElement(type);
128
- },
129
- createTextInstance(text) {
130
- return document.createTextNode(text);
131
- },
132
- getPublicInstance(inst) {
133
- return inst;
134
- },
135
- getRootHostContext() {
136
- return {};
137
- },
138
- getChildHostContext() {
139
- return {};
140
- },
141
- appendChild(parent, child) {
142
- parent.appendChild(child);
143
- },
144
- appendInitialChild(parent, child) {
145
- parent.appendChild(child);
146
- },
147
- appendChildToContainer(parent, child) {
148
- parent.appendChild(child);
149
- },
150
- removeChild(parent, child) {
151
- parent.removeChild(child);
152
- },
153
- removeChildFromContainer(parent, child) {
154
- parent.removeChild(child);
155
- },
156
- insertBefore(parent, child, refChild) {
157
- parent.insertBefore(child, refChild);
158
- },
159
- insertInContainerBefore(parent, child, refChild) {
160
- parent.insertBefore(child, refChild);
161
- },
162
- commitTextUpdate(textInst, _, newText) {
163
- textInst.nodeValue = newText;
164
- },
165
- finalizeInitialChildren(dom, _, props) {
166
- updateProps(dom, {}, props);
167
- return false;
168
- },
169
- prepareUpdate() {
170
- return EMPTY_ARR;
171
- },
172
- commitUpdate(dom, _payload, _type, oldProps, newProps) {
173
- updateProps(dom, oldProps, newProps);
174
- },
175
- hideInstance(instance) {
176
- const style = instance.style;
177
- style.setProperty('display', 'none');
178
- },
179
- unhideInstance(instance, props) {
180
- const styleProp = props.style;
181
- let display = (styleProp === null || styleProp === void 0 ? void 0 : styleProp.hasOwnProperty('display')) ? styleProp.display : null;
182
- display = display == null || isBoolean(display) || display === '' ? '' : ('' + display).trim();
183
- // eslint-disable-next-line dot-notation
184
- instance.style['display'] = display;
185
- },
186
- clearContainer(element) {
187
- if (element.childNodes.length > 0) {
188
- element.textContent = '';
189
- }
190
- },
191
- queueMicrotask: !isUndefined(Promise)
192
- ? callback => Promise.resolve(null)
193
- .then(callback)
194
- .catch(function (error) {
195
- setTimeout(() => {
196
- throw error;
197
- });
198
- })
199
- : setTimeout,
200
- shouldSetTextContent: returnFalse,
201
- prepareForCommit(..._) { return null; },
202
- resetAfterCommit: noop,
203
- commitMount: noop,
204
- now,
205
- cancelTimeout: clearTimeout,
206
- scheduleTimeout: setTimeout,
207
- preparePortalMount: noop,
208
- noTimeout: -1,
209
- supportsMutation: true,
210
- supportsPersistence: false,
211
- isPrimaryRenderer: true,
212
- supportsHydration: false
213
- };
214
- const TaroReconciler = Reconciler(hostConfig);
215
- if (process.env.NODE_ENV !== 'production') {
216
- const foundDevTools = TaroReconciler.injectIntoDevTools({
217
- bundleType: 1,
218
- version: '17.0.2',
219
- rendererPackageName: 'taro-react'
220
- });
221
- if (!foundDevTools) {
222
- // eslint-disable-next-line no-console
223
- console.info('%cDownload the React DevTools ' + 'for a better development experience: ' + 'https://reactjs.org/link/react-devtools', 'font-weight:bold');
224
- }
117
+ /* eslint-disable @typescript-eslint/indent */
118
+ const { unstable_now: now } = scheduler;
119
+ function returnFalse() {
120
+ return false;
121
+ }
122
+ const hostConfig = {
123
+ createInstance(type) {
124
+ return document.createElement(type);
125
+ },
126
+ createTextInstance(text) {
127
+ return document.createTextNode(text);
128
+ },
129
+ getPublicInstance(inst) {
130
+ return inst;
131
+ },
132
+ getRootHostContext() {
133
+ return {};
134
+ },
135
+ getChildHostContext() {
136
+ return {};
137
+ },
138
+ getCurrentEventPriority() {
139
+ // 因 @types/react-reconciler 未更新,ts会报错,这里直接返回16
140
+ return 16; // import { DefaultEventPriority } from 'react-reconciler/constants'
141
+ },
142
+ detachDeletedInstance() {
143
+ // noop
144
+ },
145
+ appendChild(parent, child) {
146
+ parent.appendChild(child);
147
+ },
148
+ appendInitialChild(parent, child) {
149
+ parent.appendChild(child);
150
+ },
151
+ appendChildToContainer(parent, child) {
152
+ parent.appendChild(child);
153
+ },
154
+ removeChild(parent, child) {
155
+ parent.removeChild(child);
156
+ },
157
+ removeChildFromContainer(parent, child) {
158
+ parent.removeChild(child);
159
+ },
160
+ insertBefore(parent, child, refChild) {
161
+ parent.insertBefore(child, refChild);
162
+ },
163
+ insertInContainerBefore(parent, child, refChild) {
164
+ parent.insertBefore(child, refChild);
165
+ },
166
+ commitTextUpdate(textInst, _, newText) {
167
+ textInst.nodeValue = newText;
168
+ },
169
+ finalizeInitialChildren(dom, _, props) {
170
+ updateProps(dom, {}, props);
171
+ return false;
172
+ },
173
+ prepareUpdate() {
174
+ return EMPTY_ARR;
175
+ },
176
+ commitUpdate(dom, _payload, _type, oldProps, newProps) {
177
+ updateProps(dom, oldProps, newProps);
178
+ },
179
+ hideInstance(instance) {
180
+ const style = instance.style;
181
+ style.setProperty('display', 'none');
182
+ },
183
+ unhideInstance(instance, props) {
184
+ const styleProp = props.style;
185
+ let display = (styleProp === null || styleProp === void 0 ? void 0 : styleProp.hasOwnProperty('display')) ? styleProp.display : null;
186
+ display = display == null || isBoolean(display) || display === '' ? '' : ('' + display).trim();
187
+ // eslint-disable-next-line dot-notation
188
+ instance.style['display'] = display;
189
+ },
190
+ clearContainer(element) {
191
+ if (element.childNodes.length > 0) {
192
+ element.textContent = '';
193
+ }
194
+ },
195
+ queueMicrotask: !isUndefined(Promise)
196
+ ? callback => Promise.resolve(null)
197
+ .then(callback)
198
+ .catch(function (error) {
199
+ setTimeout(() => {
200
+ throw error;
201
+ });
202
+ })
203
+ : setTimeout,
204
+ shouldSetTextContent: returnFalse,
205
+ prepareForCommit(..._) { return null; },
206
+ resetAfterCommit: noop,
207
+ commitMount: noop,
208
+ now,
209
+ cancelTimeout: clearTimeout,
210
+ scheduleTimeout: setTimeout,
211
+ preparePortalMount: noop,
212
+ noTimeout: -1,
213
+ supportsMutation: true,
214
+ supportsPersistence: false,
215
+ isPrimaryRenderer: true,
216
+ supportsHydration: false
217
+ };
218
+ const TaroReconciler = Reconciler(hostConfig);
219
+ if (process.env.NODE_ENV !== 'production') {
220
+ const foundDevTools = TaroReconciler.injectIntoDevTools({
221
+ bundleType: 1,
222
+ version: '18.0.0',
223
+ rendererPackageName: 'taro-react'
224
+ });
225
+ if (!foundDevTools) {
226
+ // eslint-disable-next-line no-console
227
+ console.info('%cDownload the React DevTools ' + 'for a better development experience: ' + 'https://reactjs.org/link/react-devtools', 'font-weight:bold');
228
+ }
225
229
  }
226
230
 
227
- const ContainerMap = new WeakMap();
228
- class Root {
229
- constructor(renderer, domContainer) {
230
- this.renderer = renderer;
231
- this.internalRoot = renderer.createContainer(domContainer, 0 /** LegacyRoot: react-reconciler/src/ReactRootTags.js */, false, null);
232
- }
233
- render(children, cb) {
234
- const { renderer, internalRoot } = this;
235
- renderer.updateContainer(children, internalRoot, null, cb);
236
- return renderer.getPublicRootInstance(internalRoot);
237
- }
238
- unmount(cb) {
239
- this.renderer.updateContainer(null, this.internalRoot, null, cb);
240
- }
241
- }
242
- function render(element, domContainer, cb) {
243
- const oldRoot = ContainerMap.get(domContainer);
244
- if (oldRoot != null) {
245
- return oldRoot.render(element, cb);
246
- }
247
- const root = new Root(TaroReconciler, domContainer);
248
- ContainerMap.set(domContainer, root);
249
- return root.render(element, cb);
231
+ const ContainerMap = new WeakMap();
232
+ class Root {
233
+ constructor(renderer, domContainer, isConcurrentRoot = false) {
234
+ this.renderer = renderer;
235
+ /** ConcurrentRoot & LegacyRoot: react-reconciler/src/ReactRootTags.js */
236
+ this.internalRoot = renderer.createContainer(domContainer, isConcurrentRoot ? 1 : 0, false, null);
237
+ }
238
+ render(children, cb) {
239
+ const { renderer, internalRoot } = this;
240
+ renderer.updateContainer(children, internalRoot, null, cb);
241
+ return renderer.getPublicRootInstance(internalRoot);
242
+ }
243
+ unmount(cb) {
244
+ this.renderer.updateContainer(null, this.internalRoot, null, cb);
245
+ }
246
+ }
247
+ function render(element, domContainer, cb) {
248
+ const oldRoot = ContainerMap.get(domContainer);
249
+ if (oldRoot != null) {
250
+ return oldRoot.render(element, cb);
251
+ }
252
+ const root = new Root(TaroReconciler, domContainer);
253
+ ContainerMap.set(domContainer, root);
254
+ return root.render(element, cb);
255
+ }
256
+ function createRoot(domContainer) {
257
+ const oldRoot = ContainerMap.get(domContainer);
258
+ if (oldRoot != null) {
259
+ return oldRoot;
260
+ }
261
+ const root = new Root(TaroReconciler, domContainer, true);
262
+ ContainerMap.set(domContainer, root);
263
+ return root;
250
264
  }
251
265
 
252
- /* eslint-disable @typescript-eslint/no-unused-vars */
253
- const unstable_batchedUpdates = TaroReconciler.batchedUpdates;
254
- function unmountComponentAtNode(dom) {
255
- ensure(dom && [1, 8, 9, 11].includes(dom.nodeType), 'unmountComponentAtNode(...): Target container is not a DOM element.');
256
- const root = ContainerMap.get(dom);
257
- if (!root)
258
- return false;
259
- unstable_batchedUpdates(() => {
260
- root.unmount(() => {
261
- ContainerMap.delete(dom);
262
- });
263
- }, null);
264
- return true;
265
- }
266
- function findDOMNode(comp) {
267
- if (comp == null) {
268
- return null;
269
- }
270
- const nodeType = comp.nodeType;
271
- if (nodeType === 1 || nodeType === 3) {
272
- return comp;
273
- }
274
- return TaroReconciler.findHostInstance(comp);
275
- }
276
- const portalType = isFunction(Symbol) && Symbol.for
277
- ? Symbol.for('react.portal')
278
- : 0xeaca;
279
- function createPortal(children, containerInfo, key) {
280
- return {
281
- $$typeof: portalType,
282
- key: key == null ? null : String(key),
283
- children,
284
- containerInfo,
285
- implementation: null
286
- };
287
- }
288
- var index = {
289
- render,
290
- unstable_batchedUpdates,
291
- unmountComponentAtNode,
292
- findDOMNode,
293
- createPortal
266
+ const unstable_batchedUpdates = TaroReconciler.batchedUpdates;
267
+ function unmountComponentAtNode(dom) {
268
+ ensure(dom && [1, 8, 9, 11].includes(dom.nodeType), 'unmountComponentAtNode(...): Target container is not a DOM element.');
269
+ const root = ContainerMap.get(dom);
270
+ if (!root)
271
+ return false;
272
+ unstable_batchedUpdates(() => {
273
+ root.unmount(() => {
274
+ ContainerMap.delete(dom);
275
+ });
276
+ }, null);
277
+ return true;
278
+ }
279
+ function findDOMNode(comp) {
280
+ if (comp == null) {
281
+ return null;
282
+ }
283
+ const nodeType = comp.nodeType;
284
+ if (nodeType === 1 || nodeType === 3) {
285
+ return comp;
286
+ }
287
+ return TaroReconciler.findHostInstance(comp);
288
+ }
289
+ const portalType = isFunction(Symbol) && Symbol.for
290
+ ? Symbol.for('react.portal')
291
+ : 0xeaca;
292
+ function createPortal(children, containerInfo, key) {
293
+ return {
294
+ $$typeof: portalType,
295
+ key: key == null ? null : String(key),
296
+ children,
297
+ containerInfo,
298
+ implementation: null
299
+ };
300
+ }
301
+ var index = {
302
+ render,
303
+ createRoot,
304
+ unstable_batchedUpdates,
305
+ unmountComponentAtNode,
306
+ findDOMNode,
307
+ createPortal
294
308
  };
295
309
 
296
- export { createPortal, index as default, findDOMNode, render, unmountComponentAtNode, unstable_batchedUpdates };
310
+ export { createPortal, createRoot, index as default, findDOMNode, render, unmountComponentAtNode, unstable_batchedUpdates };
297
311
  //# sourceMappingURL=react.esm.js.map