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

[popups] onOpenChangeComplete prop #1305

Merged
merged 22 commits into from
Feb 4, 2025

Conversation

atomiks
Copy link
Contributor

@atomiks atomiks commented Jan 8, 2025

Closes #1208

New alternative to #1235 to support both symmetric open/close cases, which has a more intuitive API.

  • Reworks useAfterExitAnimation to support both open/close animations
  • onOpenChangeComplete is called with open: boolean

@mui-bot
Copy link

mui-bot commented Jan 8, 2025

Netlify deploy preview

https://deploy-preview-1305--base-ui.netlify.app/

Generated by 🚫 dangerJS against 30d8bad

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 8, 2025
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 10, 2025
Copy link

netlify bot commented Jan 10, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit ca3823b
🔍 Latest deploy log https://app.netlify.com/sites/base-ui/deploys/6780847a864eef0008694eee
😎 Deploy Preview https://deploy-preview-1305--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jan 10, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 0a2f764
🔍 Latest deploy log https://app.netlify.com/sites/base-ui/deploys/67a1a875b6ca62000849af58
😎 Deploy Preview https://deploy-preview-1305--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Jan 14, 2025
@atomiks atomiks force-pushed the feat/onOpenChangeComplete branch from 05afb45 to aec975d Compare January 14, 2025 00:37
@atomiks atomiks changed the title [popups] Rename onCloseComplete to onOpenChangeComplete [popups] onOpenChangeComplete prop Jan 14, 2025
@atomiks atomiks marked this pull request as ready for review January 14, 2025 02:08
@zannager zannager added component: dialog This is the name of the generic UI component, not the React module! component: popover The React component. labels Jan 15, 2025
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 22, 2025
Signed-off-by: atomiks <cc.glows@gmail.com>
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 23, 2025
* Calls the provided function when the CSS open animation or transition completes.
*/
export function useOpenChangeComplete(parameters: useOpenChangeComplete.Parameters) {
const { open, change = 'close', ref, onComplete: onCompleteParam } = parameters;
Copy link
Member

Choose a reason for hiding this comment

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

It may be a bit more readable if this change param didn't have a default, but it probably doesn't matter outside of looking at this code in Github

anyway I tested onOpenChangeComplete in a bunch of our hero demos and it works well (they all use CSS transitions) 👍

CC @michaldudak to review the implementation as well 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was a small optimization since it's called in many of the root components

Copy link
Member

Choose a reason for hiding this comment

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

I agree with Albert, not having this parameter present when calling the hook makes it more confusing: we're calling useOpenChangeComplete, but its onComplete handles just the closing part.
TBH the change prop itself isn't super clear and I had to spend some time to understand what it's for. Would it be possible to remove it and make the hook handle both cases at the same time? The second parameter to runOnceAnimationsFinish could perhaps be derived from open?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed it so the caller with onComplete specified needs to check the change direction

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 28, 2025
packages/react/src/utils/useOpenChangeComplete.tsx Outdated Show resolved Hide resolved
useTooltipRootContext();
const { side, align } = useTooltipPositionerContext();

useOpenChangeComplete({
Copy link
Member

Choose a reason for hiding this comment

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

Why this can't be handled in useTooltipRoot, as the close animation?

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 timing of the ref assignment means it needs to be called in the popup component to be accessible

* Calls the provided function when the CSS open animation or transition completes.
*/
export function useOpenChangeComplete(parameters: useOpenChangeComplete.Parameters) {
const { open, change = 'close', ref, onComplete: onCompleteParam } = parameters;
Copy link
Member

Choose a reason for hiding this comment

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

I agree with Albert, not having this parameter present when calling the hook makes it more confusing: we're calling useOpenChangeComplete, but its onComplete handles just the closing part.
TBH the change prop itself isn't super clear and I had to spend some time to understand what it's for. Would it be possible to remove it and make the hook handle both cases at the same time? The second parameter to runOnceAnimationsFinish could perhaps be derived from open?

atomiks and others added 2 commits January 29, 2025 21:10
Co-authored-by: Michał Dudak <michal.dudak@gmail.com>
Signed-off-by: atomiks <cc.glows@gmail.com>
Signed-off-by: atomiks <cc.glows@gmail.com>
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 29, 2025
@@ -63,9 +63,10 @@ const AlertDialogPopup = React.forwardRef(function AlertDialogPopup(
useOpenChangeComplete({
open,
ref: popupRef,
change: 'open',
onComplete() {
Copy link
Member

Choose a reason for hiding this comment

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

The most straightforward and idiomatic way to use this would be to have a parameter on the onComplete callback that determines if the popup was opened or closed.

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 thought of that, but it's not actually necessary because you can just use the outer variable that's passed as open. The useEventCallback ensures it's not stale.

@atomiks atomiks merged commit 58a9a13 into mui:master Feb 4, 2025
22 checks passed
@atomiks atomiks deleted the feat/onOpenChangeComplete branch February 4, 2025 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: dialog This is the name of the generic UI component, not the React module! component: popover The React component.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Dialog, Popover] Provide a callback for open/close completion
5 participants