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

Do not render Select listbox children while it's closed #1762

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Nov 4, 2024

This PR improves a little bit the Select's resources usage, by making sure nothing is rendered inside the listbox while it is closed.

The listbox itself needs to be kept in the DOM, for proper size calculations, and because the use of the Popover API requires a reference to be available.

However, all of its children can be safely unmounted and mounted again when opened, allowing them to have their own hooks and even handlers that are released as long as the listbox is closed.

The changes in the Select itself are minimal, but a few tests needed to be updated, as they were interacting with options while the listbox was closed, which doesn't work anymore.

Testing steps

Just a bit of sanity check in http://localhost:4001/input-select to verify everything keeps working as before.

Considerations

This change makes it a bit harder to debug issues with the Select, as the children are removed as soon as you try to inspect the DOM.

This was somewhat a problem already, as the listbox is closed when the focus is removed from it, so debugging was already a multi-step process.

I may add later something in the lines of debug={true} that disables a few behaviors in the Select and allows for an easier debugging in downstream projects if needed.

@acelaya acelaya changed the title Do not render Select listbox children while closed Do not render Select listbox children while it's closed Nov 4, 2024
@acelaya acelaya force-pushed the optimize-closed-select branch 2 times, most recently from 32331e4 to ec1ac26 Compare November 4, 2024 09:13
if (!listboxOpen && listboxRef.current!.contains(document.activeElement)) {
buttonRef.current!.focus();
}
}, [buttonRef, listboxOpen]);
Copy link
Contributor Author

@acelaya acelaya Nov 4, 2024

Choose a reason for hiding this comment

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

We no longer need to manually return the focus to the button once the listbox is closed, because since the listbox content is removed, the focus is automatically returned to the button, which was the last focused element.

Copy link

codecov bot commented Nov 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (51505f0) to head (c3e788a).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1762   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           67        67           
  Lines         1205      1203    -2     
  Branches       454       454           
=========================================
- Hits          1205      1203    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@acelaya acelaya force-pushed the optimize-closed-select branch 3 times, most recently from 40ce817 to 4c4c10c Compare November 4, 2024 09:25
@acelaya acelaya marked this pull request as ready for review November 4, 2024 09:31
@robertknight
Copy link
Member

robertknight commented Nov 4, 2024

This change makes it a bit harder to debug issues with the Select, as the children are removed as soon as you try to inspect the DOM.

Chrome has an "Emulate focused page" feature in devtools specifically to help with this.

@acelaya
Copy link
Contributor Author

acelaya commented Nov 4, 2024

This change makes it a bit harder to debug issues with the Select, as the children are removed as soon as you try to inspect the DOM.

Chrome has an "Emulate focused page" feature in devtools specifically to help with this.

image

Just tested it in http://localhost:4001/input-select. It works great!

@robertknight
Copy link
Member

This PR improves a little bit the Select's resources usage, by making sure nothing is rendered inside the listbox while it is closed.

The primary motivation for the change is rendering efficiency, by avoiding the cost of rendering and diffing the vdom for a potentially large list of items.

@acelaya
Copy link
Contributor Author

acelaya commented Nov 4, 2024

I have tested this change in client and LMS, including cases where dashboard filters loading fails and needs to be retried, and when multiple pages need to be loaded.

All works as expected, so I'll merge this.

@acelaya acelaya merged commit 36d3df1 into main Nov 4, 2024
4 checks passed
@acelaya acelaya deleted the optimize-closed-select branch November 4, 2024 10:46
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.

2 participants