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

Create new Popover component #1791

Merged
merged 1 commit into from
Nov 26, 2024
Merged

Create new Popover component #1791

merged 1 commit into from
Nov 26, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Nov 20, 2024

Depends on #1789

Extract the popover element and the logic to dynamically position it from Select and into its own Popover module.

This will allow us to use this component in other contexts.

Considerations

The popover currently requires an anchor element to be provided, and calculates its position and size based on that.

For now, I decided to keep some decisions made, like the fact that the popover appears below the anchor element unless there's not enough space, in which case it appears above it.

It also matches the anchor element's size, unless it can't fit its content, in which case it grows.

It should be possible to customize both of those (define initial position, determine how to calculate size, etc.), but I'm not going to change that as part of this PR.

We may also need to support a different way to determine the anchor, like providing a rectangle rather than an anchor element.

Out of scope

  1. I have finally decided not to add a documentation page for Popover in this PR, as it has enough changes already.
    Separately, I will add a documentation page explaining the component, how it works and behaves by default, and including some examples.
    EDIT: Addressed in Add documentation page for Popover component #1793
  2. I will also make the Popover render its children only when it's open, making the behavior transparent for the Select and consistent anywhere where the Popover is used.
    EDIT: This is addressed in Do not render Popover children while closed #1792

Testing steps

Sanity check that all examples in http://localhost:4001/input-select keep working as expected

TODO

  • Add tests for popover
  • Remove some tests from Select, if they overlap with Popover tests

Base automatically changed from select-explicit-popover to main November 20, 2024 13:04
@acelaya acelaya force-pushed the popover-component branch 4 times, most recently from 5c46c89 to 97596e2 Compare November 21, 2024 09:04
Copy link

codecov bot commented Nov 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (08dc645) to head (8e767d5).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1791   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           67        68    +1     
  Lines         1203      1220   +17     
  Branches       455       465   +10     
=========================================
+ Hits          1203      1220   +17     

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

@acelaya acelaya force-pushed the popover-component branch 4 times, most recently from b9c0276 to ce4e691 Compare November 21, 2024 09:45
popover={listboxAsPopover ? 'auto' : undefined}
data-testid="select-popover"
<Popover
anchorElementRef={wrapperRef}
Copy link
Contributor Author

@acelaya acelaya Nov 21, 2024

Choose a reason for hiding this comment

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

This change is not related with the extraction of Popover specifically (this would have been anchorElementRef={buttonRef}), but I realized the border is set in the container, not the button, which makes popover positioning and sizing calculation fail if it's done relative to the button.

It is easier to see the issue with a thicker border.

Using the button as the anchor element:
Captura desde 2024-11-21 10-21-42

Using the wrapper as the anchor element:
Captura desde 2024-11-21 10-21-13

@acelaya acelaya force-pushed the popover-component branch 12 times, most recently from 2da07d5 to 59a92b2 Compare November 21, 2024 15:58
Comment on lines +21 to +32
{open &&
(children ?? (
<>
Content of popover
<button
data-testid="inner-button"
onClick={() => setOpen(prev => !prev)}
>
Focusable element inside popover
</button>
</>
))}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The popover should not render its children when it's closed, but a bunch of Select tests break if that behavior is changed.

I will tackle that separately, checking what's the best way to fix the Select tests. Perhaps mocking the Popover component in that case is the right approach, now that the Popover has its own test.

For now, this condition allows to test it as if it was already behaving like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@acelaya acelaya requested a review from robertknight November 22, 2024 09:07
@acelaya acelaya marked this pull request as ready for review November 22, 2024 09:07
export type PopoverProps = {
children?: ComponentChildren;
classes?: string | string[];
variant?: 'panel' | 'custom'; // TODO 'tooltip'
Copy link
Contributor Author

@acelaya acelaya Nov 22, 2024

Choose a reason for hiding this comment

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

"Panel" variant adds the style used by Select, but it's likely the one we'll use for at-mentions as well.

It has:

  • White background
  • Box shadow
  • Border
  • Rounded corners
  • Padding

The "custom" variant will simply leave it unstyled, and we could add other variants in future, like a "tooltip" one which sets black transparent background, smaller padding, etc.

@acelaya acelaya force-pushed the popover-component branch 2 times, most recently from 48f3633 to 0dcf3ce Compare November 22, 2024 10:16
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All test cases that have been removed from here are now part of Popover-test.js (with a few variables renamed and titles changed to reference "popover" instead of "listbox").

However, I left a couple of overlapping tests that have helped catch regressions in the past, like the test that checks focus returns to the toggle button when the listbox is closed.

Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

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

LGTM. Just a few minor questions to clarify comments.

src/components/feedback/Popover.tsx Outdated Show resolved Hide resolved
src/components/feedback/Popover.tsx Outdated Show resolved Hide resolved
src/components/feedback/Popover.tsx Show resolved Hide resolved
src/components/feedback/Popover.tsx Outdated Show resolved Hide resolved
src/components/feedback/Popover.tsx Show resolved Hide resolved
src/components/feedback/test/Popover-test.js Outdated Show resolved Hide resolved
src/components/feedback/test/Popover-test.js Outdated Show resolved Hide resolved
src/components/feedback/test/Popover-test.js Outdated Show resolved Hide resolved
src/components/feedback/test/Popover-test.js Outdated Show resolved Hide resolved
src/components/feedback/test/Popover-test.js Outdated Show resolved Hide resolved
@acelaya acelaya merged commit 7abad76 into main Nov 26, 2024
4 checks passed
@acelaya acelaya deleted the popover-component branch November 26, 2024 08:37
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