Skip to content

feat(toast) Callback on Toast expiration #4210

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

Open
wants to merge 7 commits into
base: v3
Choose a base branch
from

Conversation

mhelbich
Copy link

πŸ”— Linked issue

Resolves #4206

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

I also remember that there used to be a callback functionality in v2 of Nuxt UI. So here is a simple implementation of that previous behavior. You can provide a callback (Function) as a prop for toasts again. They are executed after the timer expires or the user clicks on the close button manually. NextTick is used to make sure the callback executes after the closing of the Toast (onClose).

This is my first time trying to contribute to an open source project. Please inform me about any mistakes I made.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Copy link

pkg-pr-new bot commented May 23, 2025

npm i https://pkg.pr.new/@nuxt/ui@4210

commit: 6ecbf6e

/**
* Optional callback function that is called when the toast is closed/dismissed.
*/
callback?: (...args: unknown[]) => unknown
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it would be cleaner if the Toast component emitted an event which would call the callback within Toaster component? Or rather it called the callback within Toaster's onUpdateOpen function which is already called when the toast is closed.

It could also be useful to have more options here such as close (immediate) and closed (after transitions are over and the toast truly disappears from the screen) callbacks.

Disclaimer: take my opinion just as a note, I'm not the author nor a member of NuxtUI

Copy link
Author

Choose a reason for hiding this comment

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

Seems like a decent idea to put the call of the callback function in onUpdateOpen - though I don't quite see how different it is compared to just calling it in the useToast#remove function.

As for the timing of the invocation - could be useful to offer different options.
This was just an attempt to provide a simple implementation of the previous callback behavior πŸ˜…
If there's anyone on the Nuxt UI team that could give some guidance on this, I'd be happy to discuss!

Copy link
Member

Choose a reason for hiding this comment

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

Actually you can already achieve that using the onUpdate:open function:

  useToast().add({
    title: 'Toast',
    description: 'This is the toast',
    'onUpdate:open'(open) {
      console.log(open)
    }
  })

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.

[useToast] include callback when dynamically creating toasts
3 participants