Automatically scroll on details menu when it opens off-screen #59
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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 withinput
, 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