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

Add onClose property to Popover #1800

Merged
merged 1 commit into from
Nov 26, 2024
Merged

Add onClose property to Popover #1800

merged 1 commit into from
Nov 26, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Nov 26, 2024

Closes #1799
Depends on #1793

Add a new onClose mandatory prop to Popover, which is called when clicking away or pressing Escape.

When the native popover API is used, this is controlled via the toggle event. Otherwise, we fall back to useClickAway and useKeyPress(['Escape']).

image

@acelaya acelaya force-pushed the popover-toggle-close branch from 2517552 to c172e25 Compare November 26, 2024 10:21
Base automatically changed from popover-docs to main November 26, 2024 10:37
@acelaya acelaya force-pushed the popover-toggle-close branch from c172e25 to ac12910 Compare November 26, 2024 10:38
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.

I had a query about useFocusAway and popover close fallback behavior.

* Callback invoked when the popover is automatically closed.
* Use this callback to sync local state.
*/
onClose: () => void;
Copy link
Member

Choose a reason for hiding this comment

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

If the caller fails to update the local state then the open prop will get out of sync with the actual state. We don't currently have any use cases where the caller should "ignore" the close event, so such an event probably indicates a bug. We could improve this in future by logging a warning perhaps.

Copy link
Contributor Author

@acelaya acelaya Nov 26, 2024

Choose a reason for hiding this comment

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

If the caller fails to update the local state then the open prop will get out of sync with the actual state. We don't currently have any use cases where the caller should "ignore" the close event, so such an event probably indicates a bug.

Yeah, this is true. And not only that, in the non-native-popover case, if they fail to update the state, the popover won't actually close at all, but at least the state in that case is still in sync.

I'm not sure what's the solution for this to be honest. Perhaps documenting it for now.

We could improve this in future by logging a warning perhaps.

Do you mean logging a warning if the native popover is closed while open is true?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure what's the solution for this to be honest. Perhaps documenting it for now.

I would suggest to just document it as a required usage pattern for now.

Do you mean logging a warning if the native popover is closed while open is true?

I meant that a warning would be logged if the open prop remained true in the next render after a popover is automatically closed.

One scenario that may turn out to be an issue is if the popover gets automatically re-opened for some reason before a debounced render occurs.

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 meant that a warning would be logged if the open prop remained true in the next render after a popover is automatically closed.

Gotcha

@@ -381,10 +381,8 @@ function SelectMain<T>({
[onChange, closeListbox],
);

// When clicking away, focusing away or pressing `Esc`, close the listbox
useClickAway(wrapperRef, closeListbox);
// Close the listbox when focusing away
useFocusAway(wrapperRef, closeListbox);
Copy link
Member

Choose a reason for hiding this comment

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

Should this focus-away behavior also be part of the popover's built-in fallback?

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 guess it would be reasonable, but for now I decided to only mimic what the native popover does by default.

If we see we always end up adding this affordance, in other use cases, we can revisit this decision and add useFocusAway to the Popover.

@acelaya acelaya force-pushed the popover-toggle-close branch 4 times, most recently from a82be3a to 1d33e1e Compare November 26, 2024 13:54
} finally {
externalButton.remove();
}
});
Copy link
Contributor Author

@acelaya acelaya Nov 26, 2024

Choose a reason for hiding this comment

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

These are now covered in Popover-test.

@acelaya acelaya requested a review from robertknight November 26, 2024 13:57
@acelaya acelaya marked this pull request as ready for review November 26, 2024 13:57
@acelaya acelaya force-pushed the popover-toggle-close branch from 1d33e1e to f634ec2 Compare November 26, 2024 13:58
Copy link

codecov bot commented Nov 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (82734c2) to head (f634ec2).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1800   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           68        68           
  Lines         1220      1231   +11     
  Branches       465       467    +2     
=========================================
+ Hits          1220      1231   +11     

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

@acelaya acelaya force-pushed the popover-toggle-close branch from f634ec2 to 334e266 Compare November 26, 2024 16:43
@acelaya acelaya merged commit cdb6b2f into main Nov 26, 2024
2 checks passed
@acelaya acelaya deleted the popover-toggle-close branch November 26, 2024 16: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.

Popover - Add required onClose prop that is called when the popover gets automatically closed
2 participants