Skip to content

Commit 512dd5a

Browse files
authored
Zamoore/hds 5575/modal and flyout teardown logic changes (#3339)
1 parent 90e515c commit 512dd5a

File tree

4 files changed

+89
-31
lines changed

4 files changed

+89
-31
lines changed

packages/components/src/components/hds/flyout/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ export default class HdsFlyout extends Component<HdsFlyoutSignature> {
9292
return classes.join(' ');
9393
}
9494

95-
@action registerOnCloseCallback(event: Event) {
96-
if (this.args.onClose && typeof this.args.onClose === 'function') {
97-
this.args.onClose(event);
98-
}
99-
95+
private _performCloseCleanup() {
10096
this._isOpen = false;
10197

10298
// Reset page `overflow` property
@@ -146,7 +142,7 @@ export default class HdsFlyout extends Component<HdsFlyoutSignature> {
146142
return () => {
147143
// if the <dialog> is removed from the dom while open we emulate the close event
148144
if (this._isOpen) {
149-
this._element?.dispatchEvent(new Event('close'));
145+
this._performCloseCleanup();
150146
}
151147

152148
this._element?.removeEventListener(
@@ -158,6 +154,14 @@ export default class HdsFlyout extends Component<HdsFlyoutSignature> {
158154
};
159155
});
160156

157+
@action registerOnCloseCallback(event: Event) {
158+
if (this.args.onClose && typeof this.args.onClose === 'function') {
159+
this.args.onClose(event);
160+
}
161+
162+
this._performCloseCleanup();
163+
}
164+
161165
@action
162166
open(): void {
163167
// Make flyout dialog visible using the native `showModal` method

packages/components/src/components/hds/modal/index.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ export default class HdsModal extends Component<HdsModalSignature> {
110110
return classes.join(' ');
111111
}
112112

113+
private _performCloseCleanup(): void {
114+
this._isOpen = false;
115+
116+
// Reset page `overflow` property
117+
if (this._body) {
118+
this._body.style.removeProperty('overflow');
119+
if (this._bodyInitialOverflowValue === '') {
120+
if (this._body.style.length === 0) {
121+
this._body.removeAttribute('style');
122+
}
123+
} else {
124+
this._body.style.setProperty(
125+
'overflow',
126+
this._bodyInitialOverflowValue
127+
);
128+
}
129+
}
130+
131+
// Return focus to a specific element (if provided)
132+
if (this.args.returnFocusTo) {
133+
const initiator = document.getElementById(this.args.returnFocusTo);
134+
if (initiator) {
135+
initiator.focus();
136+
}
137+
}
138+
}
139+
113140
@action registerOnCloseCallback(event: Event): void {
114141
if (
115142
!this.isDismissDisabled &&
@@ -129,30 +156,7 @@ export default class HdsModal extends Component<HdsModalSignature> {
129156
this._element.showModal();
130157
}
131158
} else {
132-
this._isOpen = false;
133-
134-
// Reset page `overflow` property
135-
if (this._body) {
136-
this._body.style.removeProperty('overflow');
137-
if (this._bodyInitialOverflowValue === '') {
138-
if (this._body.style.length === 0) {
139-
this._body.removeAttribute('style');
140-
}
141-
} else {
142-
this._body.style.setProperty(
143-
'overflow',
144-
this._bodyInitialOverflowValue
145-
);
146-
}
147-
}
148-
149-
// Return focus to a specific element (if provided)
150-
if (this.args.returnFocusTo) {
151-
const initiator = document.getElementById(this.args.returnFocusTo);
152-
if (initiator) {
153-
initiator.focus();
154-
}
155-
}
159+
this._performCloseCleanup();
156160
}
157161
}
158162

@@ -195,7 +199,7 @@ export default class HdsModal extends Component<HdsModalSignature> {
195199
return () => {
196200
// if the <dialog> is removed from the dom while open we emulate the close event
197201
if (this._isOpen) {
198-
this._element?.dispatchEvent(new Event('close'));
202+
this._performCloseCleanup();
199203
}
200204

201205
this._element?.removeEventListener(

showcase/tests/integration/components/hds/flyout/index-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,31 @@ module('Integration | Component | hds/flyout/index', function (hooks) {
407407
assert.ok(closed);
408408
});
409409

410+
test('it should not call `onClose` when the flyout is removed from the DOM directly', async function (assert) {
411+
let closed = false;
412+
413+
this.set('onClose', () => (closed = true));
414+
this.set('isFlyoutRendered', true);
415+
416+
await render(
417+
hbs`
418+
{{#if this.isFlyoutRendered}}
419+
<Hds::Flyout id="test-modal-onclose-no-callback" @onClose={{this.onClose}} as |F|>
420+
<F.Header>Title</F.Header>
421+
</Hds::Flyout>
422+
{{/if}}
423+
`,
424+
);
425+
426+
assert.dom('#test-modal-onclose-no-callback').isVisible();
427+
428+
this.set('isFlyoutRendered', false);
429+
assert.dom('#test-modal-onclose-no-callback').doesNotExist();
430+
431+
await settled();
432+
assert.notOk(closed);
433+
});
434+
410435
// ASSERTIONS
411436

412437
test('it should throw an assertion if an incorrect value for @size is provided', async function (assert) {

showcase/tests/integration/components/hds/modal/index-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,31 @@ module('Integration | Component | hds/modal/index', function (hooks) {
430430
assert.ok(closed);
431431
});
432432

433+
test('it should not call `onClose` when the modal is removed from the DOM directly', async function (assert) {
434+
let closed = false;
435+
436+
this.set('onClose', () => (closed = true));
437+
this.set('isModalRendered', true);
438+
439+
await render(
440+
hbs`
441+
{{#if this.isModalRendered}}
442+
<Hds::Modal id="test-modal-onclose-no-callback" @onClose={{this.onClose}} as |M|>
443+
<M.Header>Title</M.Header>
444+
</Hds::Modal>
445+
{{/if}}
446+
`,
447+
);
448+
449+
assert.dom('#test-modal-onclose-no-callback').isVisible();
450+
451+
this.set('isModalRendered', false);
452+
453+
await settled();
454+
assert.dom('#test-modal-onclose-no-callback').doesNotExist();
455+
assert.notOk(closed);
456+
});
457+
433458
// ASSERTIONS
434459

435460
test('it should throw an assertion if an incorrect value for @size is provided', async function (assert) {

0 commit comments

Comments
 (0)