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

Fix no blur called on activeelement removed from dom #3393

Closed
wants to merge 25 commits into from

Conversation

snowystinger
Copy link
Member

@snowystinger snowystinger commented Aug 9, 2022

Closes
// Some browsers do not fire onBlur when the document.activeElement is removed from the dom.
// Firefox has had a bug open about it for 13 years https://bugzilla.mozilla.org/show_bug.cgi?id=559561
// A Safari bug has been logged for it as well https://bugs.webkit.org/show_bug.cgi?id=243749

This results in FocusScope losing contain sometimes.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Go to FocusableFirstInScope story in FocusScope stories
Tab to the button, hit enter
Repeat until just a blank area is left with a focus outline
This should work across Chrome/FF/Safari

🧢 Your Project:

@snowystinger snowystinger marked this pull request as draft August 9, 2022 23:56
@devongovett
Copy link
Member

devongovett commented Aug 10, 2022

Related #874, #2015

@snowystinger snowystinger marked this pull request as ready for review August 11, 2022 19:22
}
});
}, [scopeRef, focusedNode]);
let onSyntheticFocus = useSyntheticBlurEvent(onBlur);
Copy link
Member Author

Choose a reason for hiding this comment

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

need two of these or target ends up becoming stale

@adobe-bot
Copy link

todo: turn into single mutation observer
@adobe-bot
Copy link

@adobe-bot
Copy link

@adobe-bot
Copy link

@adobe-bot
Copy link

@adobe-bot
Copy link

@adobe-bot
Copy link

@adobe-bot
Copy link

<h1 id={`heading-${index}`}>Dialog {index + 1}</h1>
{index === 2 ?
{index > 2 ?
Copy link
Collaborator

Choose a reason for hiding this comment

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

This case is not showing in storybook example. There are 3 instances of Dialog content, so the "end of the road" statement should come when index === 2.

Suggested change
{index > 2 ?
{index === 2 ?

@adobe-bot
Copy link

@adobe-bot
Copy link

LFDanLu
LFDanLu previously approved these changes Sep 2, 2022
Copy link
Member

@LFDanLu LFDanLu left a comment

Choose a reason for hiding this comment

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

LGTM, tested the FocusScope story in Chrome, FF, and Safari. Confirmed that disabled buttons don't keep their focus ring still via the DatePicker stories as well

@adobe-bot
Copy link

}
}
}
let domNodeRemovedObserver = new DOMNodeRemovedObserver();
Copy link
Member

Choose a reason for hiding this comment

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

Making this a singleton means it can never be tree shaken, even if useSyntheticBlurEvent is unused. I'd initialize this on first use if we can.

Copy link
Member

Choose a reason for hiding this comment

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

I also wonder how much making this a singleton actually helps? I assume it was done for perf? But we still create a mutation observer every time for the disabled input/button case?

Copy link
Member Author

Choose a reason for hiding this comment

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

correct, this one is a document level mutation observer, so we only need one of them. The ones created for disabled inputs/buttons is specifically targeted at those individual elements and don't watch childLists or subTrees, so it's not too bad creating a bunch of them

interesting about the singleton not being tree shaken, good news is we only start observing on first use, so even though the mutation observer object is created here, it won't do anything and won't affect anything.

Copy link
Member

Choose a reason for hiding this comment

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

yeah but if we move the new DOMNodeRemovedObserver to first use as well, then it can be tree-shaken.

}
if (target) {
target.removeEventListener('focusout', this.onBlurHandler);
}
Copy link
Member

Choose a reason for hiding this comment

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

do we need to reset this.target to null here? I think if we don't line 184 won't do anything when a new target is set.

@@ -215,9 +215,10 @@ describe('DatePicker', function () {
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(new CalendarDate(2019, 2, 4));
expect(getTextValue(combobox)).toBe('2/3/2019'); // controlled
await act(async () => Promise.resolve());
Copy link
Member

Choose a reason for hiding this comment

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

All these extra promises in the tests worry me... why are they needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

these are cases where focus used to be lost, now it's being handled
the promise is to get the mutation observer to fire

}
setTarget(target: HTMLElement) {
if (!this.target && target && this.observer) {
this.observer.observe(document, {childList: true, subtree: true, attributes: false, characterData: false});
Copy link
Member

Choose a reason for hiding this comment

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

Still slightly concerned about the impacts of having a document level mutation observer pretty much at all times (while anything is focused, which is basically always?). 🤔

@devongovett
Copy link
Member

Also can you make sure all the browser bugs you linked to in the code get added to the wiki page?

if (this.target) {
for (const mutation of mutationList) {
for (const node of mutation.removedNodes) {
if (node?.contains?.(this.target)) {
Copy link
Member

Choose a reason for hiding this comment

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

Small perf optimization could be to do if (!document.body.contains(this.target) rather than looping through all the mutations. There could potentially be a lot of mutations at once.

@devongovett
Copy link
Member

Two other perf ideas:

  1. we could make tracking element removal opt-in
  2. for FocusScope, we could track only at the scope level rather than the whole document, and use layouteffect cleanup to detect if the scope itself is removed.

but still might be a lot of mutations to process in cases like Virtualizer scrolling, where we’re adding and removing elements all the time.

# Conflicts:
#	packages/@react-aria/focus/src/FocusScope.tsx
@adobe-bot
Copy link

@adobe-bot
Copy link

@snowystinger
Copy link
Member Author

Too scary right now, closing until we decide we want to tackle this again

@snowystinger snowystinger deleted the fix-no-blur-for-activeelement-being-removed branch July 10, 2023 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants