From a5a7ffd874c0293c9fbf0dd3ab5ce1cf65eea1d2 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Thu, 18 Mar 2021 00:26:58 +0200 Subject: [PATCH] make offcanvas to open on window load event --- .bundlewatch.config.json | 2 +- js/src/offcanvas.js | 2 +- js/tests/unit/offcanvas.spec.js | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.bundlewatch.config.json b/.bundlewatch.config.json index 58de657940b2..97e9b1c7bbfb 100644 --- a/.bundlewatch.config.json +++ b/.bundlewatch.config.json @@ -50,7 +50,7 @@ }, { "path": "./dist/js/bootstrap.js", - "maxSize": "29 kB" + "maxSize": "29.02 kB" }, { "path": "./dist/js/bootstrap.min.js", diff --git a/js/src/offcanvas.js b/js/src/offcanvas.js index c3a4f7c0604c..1824b3e3b49c 100644 --- a/js/src/offcanvas.js +++ b/js/src/offcanvas.js @@ -265,7 +265,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function ( }) EventHandler.on(window, EVENT_LOAD_DATA_API, () => { - SelectorEngine.find(OPEN_SELECTOR).forEach(el => new Offcanvas(el).show()) + SelectorEngine.find(OPEN_SELECTOR).forEach(el => (Data.get(el, DATA_KEY) || new Offcanvas(el)).show()) }) /** diff --git a/js/tests/unit/offcanvas.spec.js b/js/tests/unit/offcanvas.spec.js index 99bb069803af..20beca5d1259 100644 --- a/js/tests/unit/offcanvas.spec.js +++ b/js/tests/unit/offcanvas.spec.js @@ -230,6 +230,23 @@ describe('Offcanvas', () => { offCanvas.show() }) + + it('on window load, should make visible an offcanvas element, if its markup contains class "show"', done => { + fixtureEl.innerHTML = '
' + + const offCanvasEl = fixtureEl.querySelector('div') + spyOn(Offcanvas.prototype, 'show').and.callThrough() + + offCanvasEl.addEventListener('shown.bs.offcanvas', () => { + done() + }) + + window.dispatchEvent(createEvent('load')) + + const instance = Offcanvas.getInstance(offCanvasEl) + expect(instance).not.toBeNull() + expect(Offcanvas.prototype.show).toHaveBeenCalled() + }) }) describe('hide', () => {