GeezerVison is showing github.com — [react-core] Add experimental React Scope component API (#16587) · react/react@bd79be9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/DOMEventResponderSystem.js +1-85Lines changed: 1 addition & 85 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,7 @@ import { | 12 | 12 | PASSIVE_NOT_SUPPORTED, | 13 | 13 | } from 'legacy-events/EventSystemFlags'; | 14 | 14 | import type {AnyNativeEvent} from 'legacy-events/PluginModuleType'; | 15 | | -import {HostComponent, SuspenseComponent} from 'shared/ReactWorkTags'; | | 15 | +import {HostComponent} from 'shared/ReactWorkTags'; | 16 | 16 | import type {EventPriority} from 'shared/ReactTypes'; | 17 | 17 | import type { | 18 | 18 | ReactDOMEventResponder, | @@ -238,28 +238,6 @@ const eventResponderContext: ReactDOMResponderContext = { | 238 | 238 | } | 239 | 239 | } | 240 | 240 | }, | 241 | | - getFocusableElementsInScope(deep: boolean): Array<HTMLElement> { | 242 | | - validateResponderContext(); | 243 | | - const focusableElements = []; | 244 | | - const eventResponderInstance = ((currentInstance: any): ReactDOMEventResponderInstance); | 245 | | - const currentResponder = eventResponderInstance.responder; | 246 | | - let focusScopeFiber = eventResponderInstance.fiber; | 247 | | - if (deep) { | 248 | | - let deepNode = focusScopeFiber.return; | 249 | | - while (deepNode !== null) { | 250 | | - if (doesFiberHaveResponder(deepNode, currentResponder)) { | 251 | | - focusScopeFiber = deepNode; | 252 | | - } | 253 | | - deepNode = deepNode.return; | 254 | | - } | 255 | | - } | 256 | | - const child = focusScopeFiber.child; | 257 | | - | 258 | | - if (child !== null) { | 259 | | - collectFocusableElements(child, focusableElements); | 260 | | - } | 261 | | - return focusableElements; | 262 | | - }, | 263 | 241 | getActiveDocument, | 264 | 242 | objectAssign: Object.assign, | 265 | 243 | getTimeStamp(): number { | @@ -335,33 +313,6 @@ function validateEventValue(eventValue: any): void { | 335 | 313 | } | 336 | 314 | } | 337 | 315 |
| 338 | | -function collectFocusableElements( | 339 | | - node: Fiber, | 340 | | - focusableElements: Array<HTMLElement>, | 341 | | -): void { | 342 | | - if (isFiberSuspenseAndTimedOut(node)) { | 343 | | - const fallbackChild = getSuspenseFallbackChild(node); | 344 | | - if (fallbackChild !== null) { | 345 | | - collectFocusableElements(fallbackChild, focusableElements); | 346 | | - } | 347 | | - } else { | 348 | | - if (isFiberHostComponentFocusable(node)) { | 349 | | - focusableElements.push(node.stateNode); | 350 | | - } else { | 351 | | - const child = node.child; | 352 | | - | 353 | | - if (child !== null) { | 354 | | - collectFocusableElements(child, focusableElements); | 355 | | - } | 356 | | - } | 357 | | - } | 358 | | - const sibling = node.sibling; | 359 | | - | 360 | | - if (sibling !== null) { | 361 | | - collectFocusableElements(sibling, focusableElements); | 362 | | - } | 363 | | -} | 364 | | - | 365 | 316 | function doesFiberHaveResponder( | 366 | 317 | fiber: Fiber, | 367 | 318 | responder: ReactDOMEventResponder, | @@ -382,33 +333,6 @@ function getActiveDocument(): Document { | 382 | 333 | return ((currentDocument: any): Document); | 383 | 334 | } | 384 | 335 |
| 385 | | -function isFiberHostComponentFocusable(fiber: Fiber): boolean { | 386 | | - if (fiber.tag !== HostComponent) { | 387 | | - return false; | 388 | | - } | 389 | | - const {type, memoizedProps} = fiber; | 390 | | - if (memoizedProps.tabIndex === -1 || memoizedProps.disabled) { | 391 | | - return false; | 392 | | - } | 393 | | - if (memoizedProps.tabIndex === 0 || memoizedProps.contentEditable === true) { | 394 | | - return true; | 395 | | - } | 396 | | - if (type === 'a' || type === 'area') { | 397 | | - return !!memoizedProps.href && memoizedProps.rel !== 'ignore'; | 398 | | - } | 399 | | - if (type === 'input') { | 400 | | - return memoizedProps.type !== 'hidden' && memoizedProps.type !== 'file'; | 401 | | - } | 402 | | - return ( | 403 | | - type === 'button' || | 404 | | - type === 'textarea' || | 405 | | - type === 'object' || | 406 | | - type === 'select' || | 407 | | - type === 'iframe' || | 408 | | - type === 'embed' | 409 | | - ); | 410 | | -} | 411 | | - | 412 | 336 | function processTimers( | 413 | 337 | timers: Map<number, ResponderTimer>, | 414 | 338 | delay: number, | @@ -626,14 +550,6 @@ function validateResponderContext(): void { | 626 | 550 | ); | 627 | 551 | } | 628 | 552 |
| 629 | | -function isFiberSuspenseAndTimedOut(fiber: Fiber): boolean { | 630 | | - return fiber.tag === SuspenseComponent && fiber.memoizedState !== null; | 631 | | -} | 632 | | - | 633 | | -function getSuspenseFallbackChild(fiber: Fiber): Fiber | null { | 634 | | - return ((((fiber.child: any): Fiber).sibling: any): Fiber).child; | 635 | | -} | 636 | | - | 637 | 553 | export function dispatchEventForResponderEventSystem( | 638 | 554 | topLevelType: string, | 639 | 555 | targetFiber: null | Fiber, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/README.md -5Lines changed: 0 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -92,11 +92,6 @@ const event = { type: 'press', target, pointerType, x, y }; | 92 | 92 | context.dispatchEvent('onPress', event, DiscreteEvent); | 93 | 93 | ``` | 94 | 94 |
| 95 | | -### getFocusableElementsInScope(): Array<Element> | 96 | | - | 97 | | -Returns every DOM element that can be focused within the scope of the Event | 98 | | -Responder instance. | 99 | | - | 100 | 95 | ### isTargetWithinNode(target: Element, element: Element): boolean | 101 | 96 |
| 102 | 97 | Returns `true` if `target` is a child of `element`. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiber.js +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ import type { | 15 | 15 | ReactEventResponder, | 16 | 16 | ReactEventResponderInstance, | 17 | 17 | ReactFundamentalComponent, | | 18 | + ReactScope, | 18 | 19 | } from 'shared/ReactTypes'; | 19 | 20 | import type {RootTag} from 'shared/ReactRootTags'; | 20 | 21 | import type {WorkTag} from 'shared/ReactWorkTags'; | @@ -32,6 +33,7 @@ import { | 32 | 33 | enableProfilerTimer, | 33 | 34 | enableFundamentalAPI, | 34 | 35 | enableUserTimingAPI, | | 36 | + enableScopeAPI, | 35 | 37 | } from 'shared/ReactFeatureFlags'; | 36 | 38 | import {NoEffect, Placement} from 'shared/ReactSideEffectTags'; | 37 | 39 | import {ConcurrentRoot, BatchedRoot} from 'shared/ReactRootTags'; | @@ -56,6 +58,7 @@ import { | 56 | 58 | SimpleMemoComponent, | 57 | 59 | LazyComponent, | 58 | 60 | FundamentalComponent, | | 61 | + ScopeComponent, | 59 | 62 | } from 'shared/ReactWorkTags'; | 60 | 63 | import getComponentName from 'shared/getComponentName'; | 61 | 64 |
| @@ -86,6 +89,7 @@ import { | 86 | 89 | REACT_MEMO_TYPE, | 87 | 90 | REACT_LAZY_TYPE, | 88 | 91 | REACT_FUNDAMENTAL_TYPE, | | 92 | + REACT_SCOPE_TYPE, | 89 | 93 | } from 'shared/ReactSymbols'; | 90 | 94 |
| 91 | 95 | let hasBadMapPolyfill; | @@ -674,6 +678,16 @@ export function createFiberFromTypeAndProps( | 674 | 678 | ); | 675 | 679 | } | 676 | 680 | break; | | 681 | + case REACT_SCOPE_TYPE: | | 682 | + if (enableScopeAPI) { | | 683 | + return createFiberFromScope( | | 684 | + type, | | 685 | + pendingProps, | | 686 | + mode, | | 687 | + expirationTime, | | 688 | + key, | | 689 | + ); | | 690 | + } | 677 | 691 | } | 678 | 692 | } | 679 | 693 | let info = ''; | @@ -766,6 +780,19 @@ export function createFiberFromFundamental( | 766 | 780 | return fiber; | 767 | 781 | } | 768 | 782 |
| | 783 | +function createFiberFromScope( | | 784 | + scope: ReactScope, | | 785 | + pendingProps: any, | | 786 | + mode: TypeOfMode, | | 787 | + expirationTime: ExpirationTime, | | 788 | + key: null | string, | | 789 | +) { | | 790 | + const fiber = createFiber(ScopeComponent, pendingProps, key, mode); | | 791 | + fiber.type = scope; | | 792 | + fiber.expirationTime = expirationTime; | | 793 | + return fiber; | | 794 | +} | | 795 | + | 769 | 796 | function createFiberFromProfiler( | 770 | 797 | pendingProps: any, | 771 | 798 | mode: TypeOfMode, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.js +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,7 @@ import { | 41 | 41 | LazyComponent, | 42 | 42 | IncompleteClassComponent, | 43 | 43 | FundamentalComponent, | | 44 | + ScopeComponent, | 44 | 45 | } from 'shared/ReactWorkTags'; | 45 | 46 | import { | 46 | 47 | NoEffect, | @@ -63,6 +64,7 @@ import { | 63 | 64 | enableSuspenseServerRenderer, | 64 | 65 | enableFundamentalAPI, | 65 | 66 | warnAboutDefaultPropsOnFunctionComponents, | | 67 | + enableScopeAPI, | 66 | 68 | } from 'shared/ReactFeatureFlags'; | 67 | 69 | import invariant from 'shared/invariant'; | 68 | 70 | import shallowEqual from 'shared/shallowEqual'; | @@ -2672,6 +2674,19 @@ function updateFundamentalComponent( | 2672 | 2674 | return workInProgress.child; | 2673 | 2675 | } | 2674 | 2676 |
| | 2677 | +function updateScopeComponent(current, workInProgress, renderExpirationTime) { | | 2678 | + const nextProps = workInProgress.pendingProps; | | 2679 | + const nextChildren = nextProps.children; | | 2680 | + | | 2681 | + reconcileChildren( | | 2682 | + current, | | 2683 | + workInProgress, | | 2684 | + nextChildren, | | 2685 | + renderExpirationTime, | | 2686 | + ); | | 2687 | + return workInProgress.child; | | 2688 | +} | | 2689 | + | 2675 | 2690 | export function markWorkInProgressReceivedUpdate() { | 2676 | 2691 | didReceiveUpdate = true; | 2677 | 2692 | } | @@ -3158,6 +3173,16 @@ function beginWork( | 3158 | 3173 | } | 3159 | 3174 | break; | 3160 | 3175 | } | | 3176 | + case ScopeComponent: { | | 3177 | + if (enableScopeAPI) { | | 3178 | + return updateScopeComponent( | | 3179 | + current, | | 3180 | + workInProgress, | | 3181 | + renderExpirationTime, | | 3182 | + ); | | 3183 | + } | | 3184 | + break; | | 3185 | + } | 3161 | 3186 | } | 3162 | 3187 | invariant( | 3163 | 3188 | false, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.js +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ import { | 32 | 32 | enableFlareAPI, | 33 | 33 | enableFundamentalAPI, | 34 | 34 | enableSuspenseCallback, | | 35 | + enableScopeAPI, | 35 | 36 | } from 'shared/ReactFeatureFlags'; | 36 | 37 | import { | 37 | 38 | FunctionComponent, | @@ -49,6 +50,7 @@ import { | 49 | 50 | SimpleMemoComponent, | 50 | 51 | SuspenseListComponent, | 51 | 52 | FundamentalComponent, | | 53 | + ScopeComponent, | 52 | 54 | } from 'shared/ReactWorkTags'; | 53 | 55 | import { | 54 | 56 | invokeGuardedCallback, | @@ -617,6 +619,7 @@ function commitLifeCycles( | 617 | 619 | case SuspenseListComponent: | 618 | 620 | case IncompleteClassComponent: | 619 | 621 | case FundamentalComponent: | | 622 | + case ScopeComponent: | 620 | 623 | return; | 621 | 624 | default: { | 622 | 625 | invariant( | @@ -691,6 +694,10 @@ function commitAttachRef(finishedWork: Fiber) { | 691 | 694 | default: | 692 | 695 | instanceToUse = instance; | 693 | 696 | } | | 697 | + // Moved outside to ensure DCE works with this flag | | 698 | + if (enableScopeAPI && finishedWork.tag === ScopeComponent) { | | 699 | + instanceToUse = instance.methods; | | 700 | + } | 694 | 701 | if (typeof ref === 'function') { | 695 | 702 | ref(instanceToUse); | 696 | 703 | } else { | @@ -1383,6 +1390,13 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void { | 1383 | 1390 | } | 1384 | 1391 | return; | 1385 | 1392 | } | | 1393 | + case ScopeComponent: { | | 1394 | + if (enableScopeAPI) { | | 1395 | + const scopeInstance = finishedWork.stateNode; | | 1396 | + scopeInstance.fiber = finishedWork; | | 1397 | + } | | 1398 | + return; | | 1399 | + } | 1386 | 1400 | default: { | 1387 | 1401 | invariant( | 1388 | 1402 | false, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCompleteWork.js +32-1Lines changed: 32 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | import type {Fiber} from './ReactFiber'; | 11 | 11 | import type {ExpirationTime} from './ReactFiberExpirationTime'; | 12 | | -import type {ReactFundamentalComponentInstance} from 'shared/ReactTypes'; | | 12 | +import type { | | 13 | + ReactFundamentalComponentInstance, | | 14 | + ReactScopeInstance, | | 15 | +} from 'shared/ReactTypes'; | 13 | 16 | import type {FiberRoot} from './ReactFiberRoot'; | 14 | 17 | import type { | 15 | 18 | Instance, | @@ -47,6 +50,7 @@ import { | 47 | 50 | LazyComponent, | 48 | 51 | IncompleteClassComponent, | 49 | 52 | FundamentalComponent, | | 53 | + ScopeComponent, | 50 | 54 | } from 'shared/ReactWorkTags'; | 51 | 55 | import {NoMode, BatchedMode} from './ReactTypeOfMode'; | 52 | 56 | import { | @@ -112,6 +116,7 @@ import { | 112 | 116 | enableSuspenseServerRenderer, | 113 | 117 | enableFlareAPI, | 114 | 118 | enableFundamentalAPI, | | 119 | + enableScopeAPI, | 115 | 120 | } from 'shared/ReactFeatureFlags'; | 116 | 121 | import { | 117 | 122 | markSpawnedWork, | @@ -123,6 +128,7 @@ import {createFundamentalStateInstance} from './ReactFiberFundamental'; | 123 | 128 | import {Never} from './ReactFiberExpirationTime'; | 124 | 129 | import {resetChildFibers} from './ReactChildFiber'; | 125 | 130 | import {updateEventListeners} from './ReactFiberEvents'; | | 131 | +import {createScopeMethods} from './ReactFiberScope'; | 126 | 132 |
| 127 | 133 | function markUpdate(workInProgress: Fiber) { | 128 | 134 | // Tag the fiber with an update effect. This turns a Placement into | @@ -1213,6 +1219,31 @@ function completeWork( | 1213 | 1219 | } | 1214 | 1220 | break; | 1215 | 1221 | } | | 1222 | + case ScopeComponent: { | | 1223 | + if (enableScopeAPI) { | | 1224 | + if (current === null) { | | 1225 | + const type = workInProgress.type; | | 1226 | + const scopeInstance: ReactScopeInstance = { | | 1227 | + fiber: workInProgress, | | 1228 | + methods: null, | | 1229 | + }; | | 1230 | + workInProgress.stateNode = scopeInstance; | | 1231 | + scopeInstance.methods = createScopeMethods(type, scopeInstance); | | 1232 | + if (workInProgress.ref !== null) { | | 1233 | + markRef(workInProgress); | | 1234 | + markUpdate(workInProgress); | | 1235 | + } | | 1236 | + } else { | | 1237 | + if (current.ref !== workInProgress.ref) { | | 1238 | + markRef(workInProgress); | | 1239 | + } | | 1240 | + if (workInProgress.ref !== null) { | | 1241 | + markUpdate(workInProgress); | | 1242 | + } | | 1243 | + } | | 1244 | + } | | 1245 | + break; | | 1246 | + } | 1216 | 1247 | default: | 1217 | 1248 | invariant( | 1218 | 1249 | false, | |
|