Skip to content

Commit ab8a44d

Browse files
authored
Merge pull request #2485 from hashicorp/showcase-modal-flyout-isDismissDisabled-demo
`Modal` - Fix functionality for `isDismissDisabled`
2 parents bd7c0c1 + 6dd0f83 commit ab8a44d

File tree

6 files changed

+92
-28
lines changed

6 files changed

+92
-28
lines changed

.changeset/eighty-geese-camp.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@hashicorp/design-system-components": patch
3+
---
4+
5+
`Modal` - Fixed `isDismissDisabled` functionality
6+
`Flyout` - Removed `isDismissDisabled` from signature (not an actual argument)

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

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const SIZES: string[] = Object.values(HdsFlyoutSizesValues);
2727

2828
export interface HdsFlyoutSignature {
2929
Args: {
30-
isDismissDisabled?: boolean;
3130
size?: HdsFlyoutSizes;
3231
onOpen?: () => void;
3332
onClose?: (event: Event) => void;

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

+4-25
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,14 @@ export interface HdsModalSignature {
5757

5858
export default class HdsModal extends Component<HdsModalSignature> {
5959
@tracked isOpen = false;
60-
@tracked isDismissDisabled = this.args.isDismissDisabled ?? false;
6160
element!: HTMLDialogElement;
6261
body!: HTMLElement;
6362
bodyInitialOverflowValue = '';
6463

65-
/**
66-
* Sets the size of the modal dialog
67-
* Accepted values: small, medium, large
68-
*
69-
* @param size
70-
* @type {string}
71-
* @default 'medium'
72-
*/
64+
get isDismissDisabled(): boolean {
65+
return this.args.isDismissDisabled ?? false;
66+
}
67+
7368
get size(): HdsModalSizes {
7469
const { size = DEFAULT_SIZE } = this.args;
7570

@@ -83,14 +78,6 @@ export default class HdsModal extends Component<HdsModalSignature> {
8378
return size;
8479
}
8580

86-
/**
87-
* Sets the color of the modal dialog
88-
* Accepted values: neutral, warning, critical
89-
*
90-
* @param color
91-
* @type {string}
92-
* @default 'neutral'
93-
*/
9481
get color(): HdsModalColors {
9582
const { color = DEFAULT_COLOR } = this.args;
9683

@@ -104,18 +91,10 @@ export default class HdsModal extends Component<HdsModalSignature> {
10491
return color;
10592
}
10693

107-
/**
108-
* Calculates the unique ID to assign to the title
109-
*/
11094
get id(): string {
11195
return getElementId(this);
11296
}
11397

114-
/**
115-
* Get the class names to apply to the component.
116-
* @method classNames
117-
* @return {string} The "class" attribute to apply to the component.
118-
*/
11998
get classNames(): string {
12099
const classes = ['hds-modal'];
121100

showcase/app/controllers/components/modal.js

+15
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,29 @@ export default class ModalController extends Controller {
1616
@tracked superselectModalActive1 = false;
1717
@tracked superselectModalActive2 = false;
1818
@tracked superselectModalActive3 = false;
19+
@tracked dismissDisabledModalActive = false;
20+
@tracked isDismissDisabled;
1921

2022
@action
2123
activateModal(modal) {
2224
this[modal] = true;
25+
26+
if (modal === 'dismissDisabledModalActive') {
27+
this.isDismissDisabled = true;
28+
}
2329
}
2430

2531
@action
2632
deactivateModal(modal) {
2733
this[modal] = false;
34+
35+
if (modal === 'dismissDisabledModalActive') {
36+
this.isDismissDisabled = undefined;
37+
}
38+
}
39+
40+
@action
41+
resetIsDismissDisabled() {
42+
this.isDismissDisabled = false;
2843
}
2944
}

showcase/app/templates/components/modal.hbs

+41
Original file line numberDiff line numberDiff line change
@@ -468,5 +468,46 @@
468468
</Hds::Modal>
469469
{{/if}}
470470

471+
<br />
472+
<br />
473+
474+
<button type="button" {{on "click" (fn this.activateModal "dismissDisabledModalActive")}}>Open non-dismissable modal</button>
475+
476+
{{#if this.dismissDisabledModalActive}}
477+
<Hds::Modal
478+
id="dismiss-disabled-modal"
479+
@isDismissDisabled={{this.isDismissDisabled}}
480+
@onClose={{fn this.deactivateModal "dismissDisabledModalActive"}}
481+
as |M|
482+
>
483+
<M.Header>
484+
Try to close this modal
485+
</M.Header>
486+
<M.Body>
487+
<p class="hds-typography-body-200 hds-foreground-primary" {{style margin-bottom="12px"}}>When this modal is
488+
opened, the
489+
<code>isDismissDisabled</code>
490+
argument is set to
491+
<code>true</code>, so it can't be dismissed (not by clicking the cancel button or the close button, nor
492+
clicking on the overlay area or via
493+
<code>esc</code>
494+
key).</p>
495+
<p class="hds-typography-body-200 hds-foreground-primary" {{style margin="12px 0"}}>Click this button to reset
496+
the variable to
497+
<code>false</code>
498+
and go back to its normal state, where you should be able to close it.</p>
499+
<button type="button" {{style padding=".25rem"}} {{on "click" this.resetIsDismissDisabled}}>Reset
500+
<code>isDismissDisabled</code></button>
501+
<pre>this.isDismissDisabled = {{this.isDismissDisabled}}</pre>
502+
</M.Body>
503+
<M.Footer as |F|>
504+
<Hds::ButtonSet>
505+
<Hds::Button type="button" @text="Confirm" {{on "click" F.close}} />
506+
<Hds::Button type="button" @text="Cancel" @color="secondary" {{on "click" F.close}} />
507+
</Hds::ButtonSet>
508+
</M.Footer>
509+
</Hds::Modal>
510+
{{/if}}
511+
471512
</section>
472513
{{! template-lint-enable no-autofocus-attribute }}

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { setupRenderingTest } from 'ember-qunit';
88
import {
99
click,
1010
render,
11+
rerender,
12+
triggerKeyEvent,
1113
resetOnerror,
1214
setupOnerror,
1315
settled,
@@ -138,11 +140,33 @@ module('Integration | Component | hds/modal/index', function (hooks) {
138140
assert.dom('#test-modal').isNotVisible();
139141
});
140142
test('it should not close the modal when `@isDismissDisabled` is `true`', async function (assert) {
143+
this.set('isDismissDisabled', true);
141144
await render(
142-
hbs`<Hds::Modal @isDismissDisabled={{true}} id="test-modal" as |M|><M.Header>Title</M.Header></Hds::Modal>`
145+
hbs`<Hds::Modal @isDismissDisabled={{this.isDismissDisabled}} id="test-modal" as |M|>
146+
<M.Header>Title</M.Header>
147+
<M.Footer as |F|>
148+
<Hds::Button id="cancel-button" type="button" @text="Cancel" @color="secondary" {{on "click" F.close}} />
149+
</M.Footer>
150+
</Hds::Modal>`
143151
);
152+
// top right dismiss button
144153
await click('button.hds-modal__dismiss');
145-
assert.dom('#test-modal').exists();
154+
assert.dom('#test-modal').isVisible();
155+
// cancel button with yielded "close" callback
156+
await click('#cancel-button');
157+
assert.dom('#test-modal').isVisible();
158+
// click on overlay
159+
await click('.hds-modal__overlay');
160+
assert.dom('#test-modal').isVisible();
161+
// "esc" key
162+
await triggerKeyEvent('.hds-modal', 'keydown', 'Escape');
163+
assert.dom('#test-modal').isVisible();
164+
165+
// now let's check that the state is reset and it can be closed
166+
this.set('isDismissDisabled', false);
167+
await rerender();
168+
await click('button.hds-modal__dismiss');
169+
assert.dom('#test-modal').isNotVisible();
146170
});
147171

148172
// ACCESSIBILITY

0 commit comments

Comments
 (0)