如何为程序化触发的UIkit 3 Notification绑定Close事件?
Absolutely! The close event for UIkit Notification does work with programmatically created instances—including the persistent one you’ve set up with timeout: null (which stays open until manually dismissed or closed via code).
Here’s how to make it work properly with your existing setup:
Capture the notification instance (as you already started doing):
const notification = UIkit.notification({ message: 'my-message!', status: 'primary', timeout: null });Bind the
closeevent to the instance using UIkit’s built-in event handling:notification.on('close', () => { console.log('Notification has been closed!'); // Add your custom logic here (e.g., update UI, trigger another action) });
This event will fire in all scenarios where the notification is closed:
- When the user clicks the notification’s built-in close button
- When you call
notification.close()programmatically (e.g., from a button click or other trigger) - Even if you had set a
timeoutvalue (the event fires when the auto-dismissal completes)
For example, if you wanted to trigger the close action via a button:
// Add a button in your HTML: <button id="close-notif">Close Notification</button> document.querySelector('#close-notif').addEventListener('click', () => { notification.close(); // The close event handler above will run automatically after this });
The key point is that the returned instance from UIkit.notification() gives you full access to all the component’s events and methods, including close.
内容的提问来源于stack exchange,提问作者frumbert

