-
Notifications
You must be signed in to change notification settings - Fork 582
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
Overlay: only prevent focus on open when component is open #5636
Conversation
Otherwise, prevents focus from returning to given returnFocusRef.
🦋 Changeset detectedLatest commit: 868ece3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
👋 Hi, this pull request contains changes to the source code that github/github depends on. If you are GitHub staff, we recommend testing these changes with github/github using the integration workflow. Thanks! |
size-limit report 📦
|
6acf793
to
9e6abee
Compare
9e6abee
to
3e9b2f9
Compare
3e9b2f9
to
3a3ea3d
Compare
3a3ea3d
to
8b89620
Compare
494bc84
to
9b745ba
Compare
if (preventFocusOnOpen) { | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines are the most critical change. By returning early here, we prevent the hook from ever applying focus to the given returnFocusRef
. This results in unexpected behavior in components like Overlay when preventFocusOnOpen
and returnFocusRef
props are used together -- specifically, return focus is not applied as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also worth noting -- preventFocusOnOpen
is currently undocumented. However, this has not prevented it from being used in production (for example, in the case that inspired me to open this PR).
Should this prop be documented? 🤔
On the one hand, I suspect we don't want to encourage its use. However, by not documenting it, users are more susceptible to "misusing" the prop, resulting in unexpected behavior. Maybe that's not a concern if these changes are merged, but I wanted to ask anyway since it seems worth considering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By returning early here, we prevent the hook from ever applying focus to the given returnFocusRef
I see! So that meant that returnRef
wasn't defined if preventFocusOnOpen
=== true
?
Should this prop be documented?
I think we have some pretty old and mostly hidden docs for this hook in primer.style. It doesn't really explain too much, so I think it's worth updating! Ideally, we'll need to rely on this hook even less once we utilize popover in Overlay
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that meant that
returnRef
wasn't defined ifpreventFocusOnOpen
===true
?
returnRef
was defined, but returnRef.focus()
was never called if preventFocusOnOpen
=== true
. It just sort of failed silently 😞
I think it's worth updating! Ideally, we'll need to rely on this hook even less once we utilize popover in
Overlay
.
Cool, makes sense! Also thanks for linking to the hooks docs. I never checked outside the component docs 🤦♀️ I'll make sure preventFocusOnOpen
is documented in both places 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hook docs update: primer/design#926
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
component docs updated here: 868ece3
Taking back to draft while I work on integration tests. |
Did some debugging on the integration test branch, and turns out there's a component in github/github that relies on the current (and I would argue, incorrect) behavior, where no focus is applied even on close if I'm going to test whether the change above works with the current Overlay behavior. If yes, then I'll ship the gh/gh PR first, then come back to this one. If no, then I'll need to chat with maintainers about options for moving forward (if any). [update] Good news: the changes are backwards compatible. See https://github.com/github/github/pull/361151. |
👋 Hi from github/github! Your integration PR is ready: https://github.com/github/github/pull/361153 |
🟢 golden-jobs completed with status |
@@ -16,18 +16,21 @@ export function useOpenAndCloseFocus({ | |||
preventFocusOnOpen, | |||
}: UseOpenAndCloseFocusSettings): void { | |||
useEffect(() => { | |||
if (preventFocusOnOpen) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering what exactly preventFocusOnOpen
was being used for 🤔 In your use case, were you utilizing this prop? Seems counter intuitive, but I might not be seeing the full picture 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two cases I'm aware of were both used to keep focus on the "entrypoint" element when an overlay was opened:
- Copilot chat panel. I think it was used by mistake here, because there was also code to apply focus to an input inside the overlay after it was opened 🤦♀️ I recently removed it.
- Autocomplete. Here it was used very intentionally to keep focus on the textarea, where the user is typing, while the autocomplete dialog is open.
Based on hooks docs: * https://primer.style/guides/react/hooks/use-open-and-close-focus Related: * primer/design#926
👋 Hi, there are new commits since the last successful integration test. We recommend running the integration workflow once more, unless you are sure the new changes do not affect github/github. Thanks! |
…ay hooks (#926) * document preventFocusOnOpen prop Related: primer/react#5636 * document prop in useOverlay hook
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Thank you for all the changes and updates to the docs, truly appreciate it ✨
Depends on https://github.com/github/github/pull/361151
Related to https://github.com/github/github/pull/360074
This PR updates the
useOpenAndCloseFocus
hook to apply focus to the given return ref element when thepreventFocusOnOpen
prop is also given.Previously, if
preventFocusOnOpen
argument was given, then focus was not applied as expected to the given return ref element, resulting in accessibility issues. With these changes, focus will be applied to the given return ref in all cases, regardless of whetherpreventFocusOnOpen
argument is included, allowing these arguments to be used together without any unexpected conflicts.Currently, these change affect the Overlay component only, as the sole consumer of the
useOpenAndCloseFocus
hook.Note
The
preventFocusOnOpen
prop is currently undocumented in the Overlay component. It's unclear to me whether this is intentional. I opted not to document it for now, but would appreciate reviewers' thoughts on that.Changelog
New
useOpenAndCloseFocus
hookOverlay
component (which depends onuseOpenAndCloseFocus
hook)PreventFocusOnOpen
story in Overlay.dev.stories.tsxChanged
useOpenAndCloseFocus
hookRemoved
N/A
Rollout strategy
Testing & Reviewing
npm test Overlay
to run component testsnpm test hooks/useOpenAndCloseFocus
to run hook testsMerge checklist