Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event API: follow up fixes for FocusScope + context changes #15496

Merged
merged 5 commits into from
Apr 25, 2019

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Apr 25, 2019

This PR contains some follow up fixes as per comments in #15487 from @giuseppeg. This also adds a new helper method for getting the current active document context.getActiveDocument, which removes a bunch of code in places. I also added context.isTargetWithinEventResponderScope and context.requestResponderOwnership to deal with ownership control of the same responder.

Ref #15257

@sizebot
Copy link

sizebot commented Apr 25, 2019

ReactDOM: size: 0.0%, gzip: -0.0%

Details of bundled changes.

Comparing: 1eb2b89...503984a

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.3% +0.2% 815.68 KB 818.3 KB 185.91 KB 186.34 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 103.65 KB 103.66 KB 33.68 KB 33.68 KB UMD_PROD
react-dom.profiling.min.js 0.0% 0.0% 106.79 KB 106.8 KB 34.66 KB 34.66 KB UMD_PROFILING
react-dom.development.js +0.3% +0.2% 810.16 KB 812.78 KB 184.37 KB 184.79 KB NODE_DEV
react-dom.production.min.js 0.0% 0.0% 103.64 KB 103.65 KB 33.13 KB 33.13 KB NODE_PROD
react-dom.profiling.min.js 0.0% 0.0% 106.97 KB 106.98 KB 33.97 KB 33.97 KB NODE_PROFILING
ReactDOM-dev.js +0.3% +0.3% 834.49 KB 837.26 KB 185.77 KB 186.25 KB FB_WWW_DEV
ReactDOM-prod.js 🔺+0.6% 🔺+0.6% 342.01 KB 344.21 KB 63.28 KB 63.66 KB FB_WWW_PROD
ReactDOM-profiling.js +0.6% +0.6% 347.4 KB 349.6 KB 64.25 KB 64.64 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.3% +0.2% 816 KB 818.62 KB 186.04 KB 186.48 KB UMD_DEV
react-dom-unstable-fire.production.min.js 0.0% -0.0% 103.66 KB 103.67 KB 33.69 KB 33.69 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js 0.0% 0.0% 106.81 KB 106.82 KB 34.66 KB 34.67 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.3% +0.2% 810.48 KB 813.1 KB 184.51 KB 184.93 KB NODE_DEV
react-dom-unstable-fire.production.min.js 0.0% 0.0% 103.66 KB 103.67 KB 33.14 KB 33.14 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js 0.0% 0.0% 106.98 KB 106.99 KB 33.98 KB 33.98 KB NODE_PROFILING
ReactFire-dev.js +0.3% +0.2% 833.68 KB 836.45 KB 185.79 KB 186.21 KB FB_WWW_DEV
ReactFire-prod.js 🔺+0.7% 🔺+0.7% 330 KB 332.21 KB 60.84 KB 61.25 KB FB_WWW_PROD
ReactFire-profiling.js +0.7% +0.7% 335.37 KB 337.57 KB 61.81 KB 62.22 KB FB_WWW_PROFILING
react-dom-test-utils.development.js 0.0% 0.0% 54.23 KB 54.23 KB 14.98 KB 14.98 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 10.56 KB 10.56 KB 3.89 KB 3.89 KB UMD_PROD
react-dom-test-utils.development.js 0.0% 0.0% 53.95 KB 53.95 KB 14.91 KB 14.91 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% 0.0% 10.35 KB 10.35 KB 3.82 KB 3.82 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.76 KB 60.76 KB 15.85 KB 15.85 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 0.0% 10.69 KB 10.69 KB 3.67 KB 3.67 KB UMD_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.43 KB 60.43 KB 15.72 KB 15.72 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 🔺+0.1% 10.42 KB 10.42 KB 3.56 KB 3.57 KB NODE_PROD
react-dom-server.browser.development.js 0.0% 0.0% 136.42 KB 136.42 KB 35.9 KB 35.9 KB UMD_DEV
react-dom-server.browser.production.min.js 0.0% 0.0% 19.09 KB 19.09 KB 7.2 KB 7.2 KB UMD_PROD
react-dom-server.browser.production.min.js 0.0% 0.0% 19.02 KB 19.02 KB 7.19 KB 7.19 KB NODE_PROD
ReactDOMServer-dev.js 0.0% 0.0% 134.71 KB 134.71 KB 34.6 KB 34.61 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% 0.0% 46.88 KB 46.88 KB 10.76 KB 10.76 KB FB_WWW_PROD
react-dom-server.node.production.min.js 0.0% 0.0% 19.88 KB 19.88 KB 7.49 KB 7.49 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.66 KB 3.66 KB 1.45 KB 1.45 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.1% 1.21 KB 1.21 KB 706 B 707 B UMD_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.2% 1.05 KB 1.05 KB 637 B 638 B NODE_PROD
react-dom-unstable-fizz.node.development.js 0.0% +0.1% 3.74 KB 3.74 KB 1.43 KB 1.43 KB NODE_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% 🔺+0.1% 1.1 KB 1.1 KB 667 B 668 B NODE_PROD

react-events

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-events.development.js 0.0% -0.2% 1.16 KB 1.16 KB 618 B 617 B UMD_DEV
react-events.production.min.js 0.0% 🔺+0.5% 682 B 682 B 430 B 432 B UMD_PROD
react-events.development.js 0.0% -0.2% 996 B 996 B 556 B 555 B NODE_DEV
react-events.production.min.js 0.0% 🔺+0.3% 512 B 512 B 353 B 354 B NODE_PROD
react-events-Press.development.js +0.1% +0.6% 17.57 KB 17.58 KB 4.48 KB 4.51 KB UMD_DEV
react-events-Press.production.min.js -0.2% -0.1% 6.8 KB 6.79 KB 2.57 KB 2.56 KB UMD_PROD
react-events-Press.development.js +0.1% +0.6% 17.4 KB 17.41 KB 4.45 KB 4.47 KB NODE_DEV
react-events-Press.production.min.js -0.2% -0.2% 6.64 KB 6.63 KB 2.51 KB 2.5 KB NODE_PROD
ReactEventsPress-dev.js +0.1% +0.8% 15.46 KB 15.47 KB 3.71 KB 3.74 KB FB_WWW_DEV
ReactEventsPress-prod.js -0.9% -0.1% 13.11 KB 12.99 KB 2.86 KB 2.86 KB FB_WWW_PROD
react-events-Hover.development.js +0.4% +1.6% 9.45 KB 9.48 KB 2.33 KB 2.36 KB UMD_DEV
react-events-Hover.production.min.js -0.0% -0.2% 3.89 KB 3.89 KB 1.45 KB 1.45 KB UMD_PROD
react-events-Hover.development.js +0.4% +1.6% 9.28 KB 9.31 KB 2.28 KB 2.32 KB NODE_DEV
react-events-Hover.production.min.js -0.0% -0.1% 3.73 KB 3.73 KB 1.41 KB 1.4 KB NODE_PROD
ReactEventsHover-dev.js +0.3% +1.5% 9.3 KB 9.33 KB 2.29 KB 2.32 KB FB_WWW_DEV
ReactEventsHover-prod.js -0.3% -0.1% 7.74 KB 7.71 KB 1.83 KB 1.83 KB FB_WWW_PROD
react-events-Focus.development.js 0.0% +0.1% 4.21 KB 4.21 KB 1.31 KB 1.32 KB UMD_DEV
react-events-Focus.production.min.js 0.0% 🔺+0.2% 1.75 KB 1.75 KB 807 B 809 B UMD_PROD
react-events-Focus.development.js 0.0% +0.2% 4.04 KB 4.04 KB 1.27 KB 1.27 KB NODE_DEV
react-events-FocusScope.development.js +10.8% +11.1% 4.39 KB 4.86 KB 1.38 KB 1.53 KB UMD_DEV
react-events-FocusScope.production.min.js 🔺+9.8% 🔺+4.9% 1.82 KB 2 KB 916 B 961 B UMD_PROD
react-events-FocusScope.development.js +11.2% +11.6% 4.21 KB 4.69 KB 1.33 KB 1.48 KB NODE_DEV
react-events-FocusScope.production.min.js 🔺+9.0% 🔺+6.8% 1.64 KB 1.79 KB 849 B 907 B NODE_PROD
ReactEventsFocusScope-dev.js +12.5% +13.1% 4.16 KB 4.68 KB 1.3 KB 1.48 KB FB_WWW_DEV
ReactEventsFocusScope-prod.js 🔺+8.7% 🔺+6.3% 3.32 KB 3.61 KB 1.04 KB 1.11 KB FB_WWW_PROD
react-events-Swipe.development.js -0.2% -0.3% 8.41 KB 8.39 KB 2.57 KB 2.56 KB UMD_DEV
react-events-Swipe.production.min.js -0.3% -0.2% 3.52 KB 3.51 KB 1.61 KB 1.6 KB UMD_PROD
react-events-Swipe.development.js -0.2% -0.3% 8.24 KB 8.22 KB 2.54 KB 2.53 KB NODE_DEV
react-events-Swipe.production.min.js -0.3% 0.0% 3.36 KB 3.35 KB 1.55 KB 1.55 KB NODE_PROD
ReactEventsSwipe-dev.js -0.2% -0.5% 6.33 KB 6.32 KB 1.8 KB 1.79 KB FB_WWW_DEV
ReactEventsSwipe-prod.js -1.2% -1.0% 6.01 KB 5.93 KB 1.54 KB 1.53 KB FB_WWW_PROD
react-events-Drag.development.js -0.2% -0.3% 7.95 KB 7.94 KB 2.46 KB 2.45 KB UMD_DEV
react-events-Drag.production.min.js -0.3% -0.2% 3.38 KB 3.37 KB 1.53 KB 1.53 KB UMD_PROD
react-events-Drag.development.js -0.2% -0.3% 7.79 KB 7.77 KB 2.42 KB 2.42 KB NODE_DEV
react-events-Drag.production.min.js -0.3% -0.5% 3.22 KB 3.21 KB 1.47 KB 1.47 KB NODE_PROD
ReactEventsDrag-dev.js -0.3% -0.5% 6.04 KB 6.03 KB 1.72 KB 1.71 KB FB_WWW_DEV
ReactEventsDrag-prod.js -0.3% -0.5% 5.51 KB 5.5 KB 1.43 KB 1.43 KB FB_WWW_PROD

Generated by 🚫 dangerJS

@trueadm trueadm merged commit d1f667a into facebook:master Apr 25, 2019
@trueadm trueadm deleted the focus-scope-extras branch April 25, 2019 19:10
Copy link
Contributor

@necolas necolas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use FocusScope around the Pressables in my sandbox (using the latest React builds) and it crashed when focus moved into the scope's subtree with the following error.

Uncaught Invariant Violation: addRootEventTypes() found a duplicate root event type of "focus". This might be because the event type exists in the event responder "rootEventTypes" array or because of a previous addRootEventTypes() using this root event type.

https://fburl.com/2t1wrbsr

if (props.restoreFocus) {
state.nodeToRestore = document.activeElement;
state.nodeToRestore = context.getActiveDocument().activeElement;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need to do something like check for blur events that have a relatedTarget within the event component subtree, that way we can know the last element focused (blurEvent.target) before focus moved into the FocusScope instance. This should give the expected result in cases where the FocusScope is mounted well before the focus moves into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. We should do this later at some point.

if (props.restoreFocus && state.nodeToRestore !== null) {
): void {
if (
props.restoreFocus &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could both simplify the responsibilities of this component and make it more flexible for users if we instead had a callback API so we can leave it up to people to shift the focus themselves, in case they need custom behaviour.

const onFocusIn = (event) => {
  // if there's a cancel button in the scope, move focus there instead of to the first focusable element
  if (cancelRef.current != null) {
    focusElement(cancelRef.current);
  }
};

const onFocusOut = (event) => {
  if (event.target != null) {
    // restore focus if menu open-button if it is still open
    focusElement(event.target);
  } else {
    // shift focus to menu open-button if menu auto-collapsed when modal opened
    focusElement(fallbackElement);
  }
};

<FocusScope onFocusIn={onFocusIn} onFocusOut={onFocusOut}>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a feature I was going to add anyway. I just added this to add a simple autofocus for modal cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants