1
1
import { JsxProps , VNode } from 'dom-renderer' ;
2
2
import { observable } from 'mobx' ;
3
- import { FC , WebCellProps , attribute , component , observer } from 'web-cell' ;
4
- import { uniqueID } from 'web-utility' ;
3
+ import {
4
+ FC ,
5
+ WebCell ,
6
+ WebCellProps ,
7
+ attribute ,
8
+ component ,
9
+ observer
10
+ } from 'web-cell' ;
11
+ import { delegate , uniqueID } from 'web-utility' ;
5
12
6
13
import { Container , ContainerProps } from './Grid' ;
7
14
import {
@@ -73,12 +80,14 @@ export interface OffcanvasNavbarProps
73
80
brand ?: VNode ;
74
81
}
75
82
83
+ export interface OffcanvasNavbar extends WebCell { }
84
+
76
85
@component ( {
77
86
tagName : 'offcanvas-navbar' ,
78
87
mode : 'open'
79
88
} )
80
89
@observer
81
- export class OffcanvasNavbar extends HTMLElement {
90
+ export class OffcanvasNavbar extends HTMLElement implements WebCell {
82
91
declare props : OffcanvasNavbarProps ;
83
92
84
93
@attribute
@@ -124,6 +133,30 @@ export class OffcanvasNavbar extends HTMLElement {
124
133
@observable
125
134
accessor closeButton = true ;
126
135
136
+ connectedCallback ( ) {
137
+ globalThis . addEventListener ?.( 'keyup' , this . close , true ) ;
138
+
139
+ this . addEventListener ( 'click' , this . handleLink ) ;
140
+ }
141
+
142
+ disconnectedCallback ( ) {
143
+ globalThis . removeEventListener ?.( 'keyup' , this . close , true ) ;
144
+
145
+ this . addEventListener ( 'click' , this . handleLink ) ;
146
+ }
147
+
148
+ close = ( event ?: KeyboardEvent | MouseEvent ) => {
149
+ if (
150
+ event instanceof KeyboardEvent &&
151
+ ! [ 'Escape' , 'Enter' ] . includes ( event . key )
152
+ )
153
+ return ;
154
+
155
+ this . open = false ;
156
+ } ;
157
+
158
+ handleLink = delegate ( 'a[href].nav-link' , this . close ) ;
159
+
127
160
renderContent ( ) {
128
161
const { variant, bg, expand, fixed, sticky, fluid, brand } = this ,
129
162
{ title, titleId, offcanvasId, open, closeButton } = this ;
@@ -141,6 +174,7 @@ export class OffcanvasNavbar extends HTMLElement {
141
174
id = { offcanvasId }
142
175
aria-labelledby = { titleId }
143
176
show = { open }
177
+ onHide = { this . close }
144
178
>
145
179
< OffcanvasHeader
146
180
closeButton = { closeButton }
0 commit comments