Skip to content

Commit fbca2b7

Browse files
authored
feat: add closeDrawerOn property to handle navigation (#184)
Fixes vaadin/vaadin-app-layout#140
1 parent 9750a78 commit fbca2b7

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

packages/vaadin-app-layout/src/vaadin-app-layout.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export type AppLayoutOverlayChanged = CustomEvent<{ value: boolean }>;
1717
*/
1818
export type AppLayoutPrimarySectionChanged = CustomEvent<{ value: 'navbar' | 'drawer' }>;
1919

20-
2120
export interface AppLayoutElementEventMap {
2221
'drawer-opened-changed': AppLayoutDrawerOpenedChanged;
2322

@@ -94,6 +93,7 @@ export type AppLayoutEventMap = HTMLElementEventMap & AppLayoutElementEventMap;
9493
* ### Navigation
9594
*
9695
* As the drawer opens as an overlay in small devices, it makes sense to close it once a navigation happens.
96+
* If you are using Vaadin Router, this will happen automatically unless you change the `closeDrawerOn` event name.
9797
*
9898
* In order to do so, there are two options:
9999
* - If the `vaadin-app-layout` instance is available, then `drawerOpened` can be set to `false`
@@ -139,6 +139,14 @@ declare class AppLayoutElement extends ElementMixin(ThemableMixin(HTMLElement))
139139
*/
140140
readonly overlay: boolean;
141141

142+
/**
143+
* A global event that causes the drawer to close (be hidden) when it is in overlay mode.
144+
* - The default is `vaadin-router-location-changed` dispatched by Vaadin Router
145+
*
146+
* @attr {string} close-drawer-on
147+
*/
148+
closeDrawerOn: string;
149+
142150
/**
143151
* Helper static method that dispatches a `close-overlay-drawer` event
144152
*/

packages/vaadin-app-layout/src/vaadin-app-layout.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import './detect-ios-navbar.js';
7777
* ### Navigation
7878
*
7979
* As the drawer opens as an overlay in small devices, it makes sense to close it once a navigation happens.
80+
* If you are using Vaadin Router, this will happen automatically unless you change the `closeDrawerOn` event name.
8081
*
8182
* In order to do so, there are two options:
8283
* - If the `vaadin-app-layout` instance is available, then `drawerOpened` can be set to `false`
@@ -349,6 +350,19 @@ class AppLayoutElement extends ElementMixin(ThemableMixin(PolymerElement)) {
349350
readOnly: true,
350351
value: false,
351352
reflectToAttribute: true
353+
},
354+
355+
/**
356+
* A global event that causes the drawer to close (be hidden) when it is in overlay mode.
357+
* - The default is `vaadin-router-location-changed` dispatched by Vaadin Router
358+
*
359+
* @attr {string} close-drawer-on
360+
* @type {string}
361+
*/
362+
closeDrawerOn: {
363+
type: String,
364+
value: 'vaadin-router-location-changed',
365+
observer: '_closeDrawerOnChanged'
352366
}
353367
};
354368
}
@@ -530,6 +544,16 @@ class AppLayoutElement extends ElementMixin(ThemableMixin(PolymerElement)) {
530544
// TODO(jouni): ARIA attributes. The drawer should act similar to a modal dialog when in ”overlay” mode
531545
}
532546

547+
/** @private */
548+
_closeDrawerOnChanged(closeDrawerOn, oldCloseDrawerOn) {
549+
if (oldCloseDrawerOn) {
550+
window.removeEventListener(oldCloseDrawerOn, this.__closeOverlayDrawerListener);
551+
}
552+
if (closeDrawerOn) {
553+
window.addEventListener(closeDrawerOn, this.__closeOverlayDrawerListener);
554+
}
555+
}
556+
533557
/** @protected */
534558
_close() {
535559
this.drawerOpened = false;

packages/vaadin-app-layout/test/app-layout.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ describe('vaadin-app-layout', () => {
157157
expect(drawer.hasAttribute('hidden')).to.be.true;
158158
});
159159

160+
it('should not close on navigation event when not in overlay mode', () => {
161+
layout.drawerOpened = true;
162+
window.dispatchEvent(new CustomEvent('vaadin-router-location-changed'));
163+
expect(layout.drawerOpened).to.be.true;
164+
});
165+
160166
describe('overlay mode', () => {
161167
beforeEach(() => {
162168
// force overlay=true to show backdrop
@@ -208,6 +214,21 @@ describe('vaadin-app-layout', () => {
208214
layout._updateOverlayMode();
209215
expect(layout.drawerOpened).to.be.true;
210216
});
217+
218+
it('should close on navigation event when in overlay mode', () => {
219+
layout.drawerOpened = true;
220+
window.dispatchEvent(new CustomEvent('vaadin-router-location-changed'));
221+
expect(layout.drawerOpened).to.be.false;
222+
});
223+
224+
it('should only close on custom navigation event when in overlay mode', () => {
225+
layout.drawerOpened = true;
226+
layout.closeDrawerOn = 'foo-bar';
227+
window.dispatchEvent(new CustomEvent('vaadin-router-location-changed'));
228+
expect(layout.drawerOpened).to.be.true;
229+
window.dispatchEvent(new CustomEvent('foo-bar'));
230+
expect(layout.drawerOpened).to.be.false;
231+
});
211232
});
212233
});
213234
});

0 commit comments

Comments
 (0)