You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use the Offcanvas to show navigation items. Clicking on a navigation item calls hide() and starts a navigation (Angular client-side routing). Now, when the navigation leads to a page that does not have the offcanvas in it, the dispose() is called, too. This happens before the animation of hide() is finished and results in the following error:
I tried to play with your CodePen example and probably managed to find a something to make it work with the following JS code:
constoffcanvasElement=document.querySelector("#offcanvasExample");document.querySelector("#myBtn").addEventListener("click",()=>{constoc=bootstrap.Offcanvas.getOrCreateInstance(offcanvasElement);offcanvasElement.addEventListener("hidden.bs.offcanvas",()=>{oc.dispose();// Recreate instance for future use (optional)bootstrap.Offcanvas.getOrCreateInstance(offcanvasElement);},{once: true});oc.hide();});
From my understanding, the CodePen example JS code fails because it calls dispose() almost immediately after hide(), using setTimeout(). However, hide() triggers a transition and internal cleanup that isn’t finished yet when dispose() runs. As a result, Bootstrap is still using internal references like this._element or this._backdrop, which dispose() has already removed, leading to errors like this._element is null or this._backdrop is null.
The JS code I've tried seems to work because it waits for the Offcanvas to fully finish hiding (using the hidden.bs.offcanvas event) before calling dispose(). After that, it immediately reinitializes the Offcanvas instance, so it’s fully set up again and safe to open the next time. This avoids the race condition and ensures the internal Bootstrap logic stays intact.
Thanks for looking into it. As I described, my use case is a real world example, the CodePen example is just to reproduce the issue. Applying your workaround to my Angular app would mean that the user has to wait the closing animation before the actual navigation to another page can start. As the animation duration is not too long, it would work, yes, but is still a workaround.
What about adding a if (disposed) return; to the callback completeCallback instead? The disposed is something like this._element === null.
Prerequisites
Describe the issue
I use the Offcanvas to show navigation items. Clicking on a navigation item calls
hide()
and starts a navigation (Angular client-side routing). Now, when the navigation leads to a page that does not have the offcanvas in it, thedispose()
is called, too. This happens before the animation ofhide()
is finished and results in the following error:Code line:
bootstrap/js/src/offcanvas.js
Line 145 in 0a005b3
Expectation: Offcanvas should not continue hiding when disposed already.
Reduced test cases
Example here
What operating system(s) are you seeing the problem on?
Windows
What browser(s) are you seeing the problem on?
Firefox
What version of Bootstrap are you using?
5.3.5
The text was updated successfully, but these errors were encountered: