Skip to content

Automatically scroll on details menu when it opens off-screen #59

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

Closed
wants to merge 1 commit into from

Conversation

elenatanasoiu
Copy link

@elenatanasoiu elenatanasoiu commented Jun 28, 2022

What are you trying to accomplish?

If the details menu button is at the bottom of the page, we don't automatically scroll the page to show you the dropdown options.

Example: dismissing code scanning alerts

When you click the "Dismiss alert" button in the code scanning alerts section, it will show you a list of three options in a dropdown.

However, when the "Dismiss alert" button is at the bottom of the page, we don't automatically scroll the page to show you the dropdown options. This leads to a bad experience as users might not notice there's a dropdown or they get a cut-off view of it:

Fix_input_to_action_by_aeisenberg_·_Pull_Request__1102_·_github_codeql-action_and_Google_Calendar_-_Week_of_June_19__2022

What approach did you choose and why?

To fix this, let's listen for the open event on the dropdown menu and scroll the menu into view when it triggers.

I initially tried doing this by adding the autofocus property to the dropdown as the details component comes with this out-of-the-box. Unfortunately, that didn't work because that property can only be used with input, while we're using a radio group.

I then experimented with adding the autofocus to the first or last element in the radio button list (since it's actually an input) but the scrolling was not very smooth and it didn't really centre the dropdown menu in a pleasant way.

With scrollIntoView, the dropdown is always centred:

Screen.Recording.2022-06-28.at.11.53.19.mov

Testing

I've added a unit test to illustrate how this should be used.

Documentation

I've also added an example to the examples file.

Feedback

Any feedback welcome!

Anything else

This also addresses: #58

@elenatanasoiu elenatanasoiu requested a review from a team as a code owner June 28, 2022 10:55
If the details menu button is at the bottom of the page. we don't
automatically scroll the page to show you the dropdown options.

While there is an `autofocus` attribute we can use, there are two
problems with it:
1. it only works with inputs
2. it doesn't scroll smoothly so the experience not as smooth when
you open the menu.

Let's use scrollIntoView and center on the details menu.
@elenatanasoiu elenatanasoiu force-pushed the elenatanasoiu/scroll-on-open branch from c97f615 to b6f8d92 Compare June 28, 2022 11:54
Copy link
Contributor

@theinterned theinterned left a comment

Choose a reason for hiding this comment

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

I worry that this interaction might be unexpected. Have you considered instead detecting when the menu is close to the bottom of the window, and opening upward instead?

This is what the native select does for example:

Screen Shot 2022-06-28 at 9 12 19 PM

Screenshot from https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

@theinterned
Copy link
Contributor

Follow up, we have a behaviour that can be used for this https://github.com/primer/behaviors/blob/main/docs/anchored-position.md

@elenatanasoiu elenatanasoiu deleted the elenatanasoiu/scroll-on-open branch June 29, 2022 17:11
@elenatanasoiu
Copy link
Author

@theinterned Thanks for linking to that. Will have a proper read!

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.

3 participants