Skip to content

Commit 951cc70

Browse files
author
Ryan A. Johnson
committed
fix(Modal): fix ESC-to-close
Fixes: SURF-1166
1 parent 5453285 commit 951cc70

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

docs/components/modals/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h3>Demo Modal Header</h3>
2828
</p>
2929
</hx-modalbody>
3030
<hx-modalfoot>
31-
<button class="hxBtn hxBtn--primary">Confirm</button>
31+
<button class="hxBtn hxPrimary">Confirm</button>
3232
<button class="hxBtn">Cancel</button>
3333
</hx-modalfoot>
3434
</hx-modal>
@@ -49,7 +49,7 @@ <h3>Demo Modal Header</h3>
4949
</p>
5050
</hx-modalbody>
5151
<hx-modalfoot>
52-
<button class="hxBtn hxBtn--primary">Confirm</button>
52+
<button class="hxBtn hxPrimary">Confirm</button>
5353
<button class="hxBtn">Cancel</button>
5454
</hx-modalfoot>
5555
</hx-modal>

src/helix-ui/elements/HXModalElement.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,28 @@ export class HXModalElement extends HXElement {
3737
return `<style>${shadowStyles}</style>${shadowMarkup}`;
3838
}
3939

40-
static get observedAttributes () {
40+
static get $observedAttributes () {
4141
return [ 'open' ];
4242
}
4343

44-
constructor () {
45-
super();
46-
this._close = this._close.bind(this);
47-
this._keyUp = this._keyUp.bind(this);
44+
$onCreate () {
45+
this._onBtnClose = this._onBtnClose.bind(this);
46+
this._onKeyUp = this._onKeyUp.bind(this);
4847
}
4948

50-
connectedCallback () {
49+
$onConnect () {
5150
this.$upgradeProperty('open');
5251
this.setAttribute('aria-hidden', !this.open);
53-
54-
this._btnClose.addEventListener('click', this._close);
55-
document.addEventListener('keyup', this._keyUp);
52+
this._btnClose.addEventListener('click', this._onBtnClose);
5653
}
5754

58-
disconnectedCallback () {
59-
this._btnClose.removeEventListener('click', this._close);
60-
document.removeEventListener('keyup', this._keyUp);
55+
$onDisconnect () {
56+
this._btnClose.removeEventListener('click', this._onBtnClose);
6157
}
6258

63-
attributeChangedCallback (attr, oldVal, newVal) {
64-
let isOpen = (newVal !== null);
65-
this.setAttribute('aria-hidden', !isOpen);
66-
67-
if (newVal !== oldVal) {
68-
this.$emit(isOpen ? 'open' : 'close');
69-
}
70-
}//attributeChangedCallback
71-
72-
_close (evt) {
73-
evt.preventDefault();
74-
75-
this.open = false;
76-
}
77-
78-
_keyUp (event) {
79-
if (event.keyCode === KEYS.Escape) {
80-
this._close();
59+
$onAttributeChange (attr, oldVal, newVal) {
60+
if (attr === 'open') {
61+
this._changeAttrOpen(oldVal, newVal);
8162
}
8263
}
8364

@@ -97,4 +78,31 @@ export class HXModalElement extends HXElement {
9778
get _btnClose () {
9879
return this.shadowRoot.getElementById('hxClose');
9980
}
81+
82+
/** @private */
83+
_onBtnClose (evt) {
84+
evt.preventDefault();
85+
86+
this.open = false;
87+
}
88+
89+
/** @private */
90+
_onKeyUp (event) {
91+
if (event.keyCode === KEYS.Escape) {
92+
this.open = false;
93+
}
94+
}
95+
96+
/** @private */
97+
_changeAttrOpen (oldVal, newVal) {
98+
if (newVal !== null) {
99+
this.$emit('open');
100+
this.setAttribute('aria-hidden', false);
101+
document.addEventListener('keyup', this._onKeyUp);
102+
} else {
103+
this.$emit('close');
104+
this.setAttribute('aria-hidden', true);
105+
document.removeEventListener('keyup', this._onKeyUp);
106+
}
107+
}
100108
}

0 commit comments

Comments
 (0)