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

Split Select dual popover/listbox element in two #1789

Merged
merged 1 commit into from
Nov 20, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Nov 19, 2024

This PR is the first of a series aiming to simplify/extract capabilities from the Select component.

Here we are splitting the listbox-as-popover element into two independent elements:

  1. A div which acts as the popover, using the popover API when possible, and handling positioning and openness logic.
  2. A ul with the listbox role, which handles accessibility aspects to handle a list of items, like aria attributes and keyboard navigation. It is the only children of the popover element.

For now, this split is done inside of the Select component, keeping it unchanged from a consumer or user point of view.

In follow-up PRs we'll we extracting some/all of those components.

Note

This PR renames many references to the listbox concept to popover, but there are a few grey areas that I left unchanged.
I expect some of those to be changed in upcoming PRs, where it will become more obvious as we start extracting sub-components.

Tip

This PR is easier to review ignoring whitespaces

Testing steps

Sanity-check that everything in http://localhost:4001/input-select looks and behaves the same as before this change.

@acelaya acelaya force-pushed the select-explicit-popover branch from cdfe295 to 701fe9c Compare November 19, 2024 11:28
Copy link

codecov bot commented Nov 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (7caf916) to head (1018f95).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1789   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           67        67           
  Lines         1204      1203    -1     
  Branches       455       455           
=========================================
- Hits          1204      1203    -1     

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


🚨 Try these New Features:

@acelaya acelaya force-pushed the select-explicit-popover branch 3 times, most recently from 4e705bc to e655188 Compare November 19, 2024 11:43
*/
function useListboxPositioning(
function usePopoverPositioning(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed all references to "listbox" from this hook, because it is likely going to be extracted and decoupled from that.

@acelaya acelaya force-pushed the select-explicit-popover branch from e655188 to 87fe3a5 Compare November 19, 2024 13:57
@@ -257,16 +257,16 @@ function SelectOption<T>({

SelectOption.displayName = 'Select.Option';

/** Small space to apply between the toggle button and the listbox */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR is full of listbox/popover replacements.

.getDOMNode()
.getBoundingClientRect();

assert.isTrue(listboxWidth < window.innerWidth);
assert.isBelow(popoverWidth, window.innerWidth);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

assert.isBelow(a, b) generates a better error than assert.isTrue(a < b) if the assertion fails.

@acelaya acelaya requested a review from robertknight November 19, 2024 14:01
@acelaya acelaya marked this pull request as ready for review November 19, 2024 14:01
@acelaya acelaya force-pushed the select-explicit-popover branch from 87fe3a5 to 1018f95 Compare November 19, 2024 14:32
@acelaya acelaya mentioned this pull request Nov 20, 2024
2 tasks
// We don't want the listbox to ever render outside the viewport,
// and we give it a 16px gap
'max-w-[calc(100%-16px)]',
// Overwrite [popover] default styles
Copy link
Member

Choose a reason for hiding this comment

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

What are the popover default styles?

Copy link
Contributor Author

@acelaya acelaya Nov 20, 2024

Choose a reason for hiding this comment

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

It sets margin: auto so that popovers are horizontally and vertically centered in the top layer. Unfortunately, it messes the positioning calculation.

It also sets padding: 0.25rem, which adds-up to the manually set padding in the listbox.

Surprisingly, it didn't have any effect when [popover] was used in a ul element. I suspect it was because of the style resets that tailwind sets.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Surprisingly, it didn't have any effect when [popover] was used in a ul element. I suspect it was because of the style resets that tailwind sets.

Yep, exactly.

image

aria-orientation="vertical"
data-testid="select-listbox"
data-listbox-open={listboxOpen}
ref={popoverRef}
// nb. Use `undefined` rather than `false` because Preact doesn't
// handle boolean values correctly for this attribute (it will set
// `popover="false"` instead of removing the attribute).
Copy link
Member

Choose a reason for hiding this comment

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

Incidentally I think this might be fixed now, but it needs testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. I considered changing it, but I think we can tackle it individually

@acelaya acelaya merged commit 08dc645 into main Nov 20, 2024
4 checks passed
@acelaya acelaya deleted the select-explicit-popover branch November 20, 2024 13:04
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