Skip to content

Commit

Permalink
ui: a11y modals (#9819)
Browse files Browse the repository at this point in the history
This PR uses the excellent a11y-dialog to implement our modal functionality across the UI.

This package covers all our a11y needs - overlay click and ESC to close, controlling aria-* attributes, focus trap and restore. It's also very small (1.6kb) and has good DOM and JS APIs and also seems to be widely used and well tested.

There is one downside to using this, and that is:

We made use of a very handy characteristic of the relationship between HTML labels and inputs in order to implement our modals previously. Adding a for="id" attribute to a label meant you can control an <input id="id" /> from anywhere else in the page without having to pass javascript objects around. It's just based on using the same string for the for attribute and the id attribute. This allowed us to easily open our login dialog with CSS from anywhere within the UI without having to manage passing around a javascript object/function/method in order to open the dialog.

We've PRed #9813 which includes an approach which would make passing around JS modal object easier to do. But in the meantime we've added a little 'hack' here using an additional <input /> element and a change listener which allows us to keep this label/input characteristic of our old modals. I'd originally thought this would be a temporary amend in order to wait on #9813 but the more I think about it, the more I think its quite a nice thing to keep - so longer term we may/may not keep this.
  • Loading branch information
johncowen authored Mar 9, 2021
1 parent 35daee4 commit 33d0383
Show file tree
Hide file tree
Showing 27 changed files with 192 additions and 263 deletions.
3 changes: 3 additions & 0 deletions .changelog/9819.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: improve accessibility of modal dialogs
```
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<button
data-test-create-permission
type="button"
onclick={{action (mut shouldShowPermissionForm) true}}
{{on "click" (optional this.modal.open)}}
>
Add permission
</button>
Expand All @@ -156,7 +156,7 @@
<Consul::Intention::Notice::Permissions />
<Consul::Intention::Permission::List
@items={{item.Permissions}}
@onclick={{queue (action (mut permission)) (action (mut shouldShowPermissionForm) true)}}
@onclick={{queue (action (mut permission)) (action (optional this.modal.open))}}
@ondelete={{action 'delete' 'Permissions' item}}
/>
{{else}}
Expand Down Expand Up @@ -190,11 +190,11 @@
</label>
</fieldset>

{{#if shouldShowPermissionForm}}
<ModalDialog
class="consul-intention-permission-modal"
@onclose={{queue (action (mut shouldShowPermissionForm) false) (action (mut permission) undefined)}}
@onclose={{action (mut permission) undefined}}
as |modal|>
<Ref @target={{this}} @name="modal" @value={{modal}} />
<BlockSlot @name="header">
<h3>Edit Permission</h3>
</BlockSlot>
Expand Down Expand Up @@ -225,6 +225,5 @@
</button>
</BlockSlot>
</ModalDialog>
{{/if}}

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
float: right;
}
}
.consul-intention-permission-modal [role="dialog"] > div > div {
.consul-intention-permission-modal [role="dialog"] {
width: 100%;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ as |api|>
{{#let api.data as |item|}}
{{#if (can 'write intention' item=item)}}

{{#if this.warn}}
{{#let (changeset-get item 'Action') as |newAction|}}
<ModalDialog
class="consul-intention-action-warn-modal warning"
data-test-action-warning
@onclose={{action (mut this.warn) false}}
>
as |modal|>
<Ref @target={{this}} @name="modal" @value={{modal}} />
<BlockSlot @name="header">
<h2>Set intention to {{newAction}}?</h2>
</BlockSlot>
Expand Down Expand Up @@ -69,7 +68,6 @@ as |api|>
</BlockSlot>
</ModalDialog>
{{/let}}
{{/if}}

<DataSource
@src={{concat '/*/' @dc '/services'}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class ConsulIntentionForm extends Component {

@tracked isManagedByCRDs;

@tracked warn = false;
modal = null; // reference to the warning modal

@service('repository/intention') repo;

Expand Down Expand Up @@ -55,7 +55,7 @@ export default class ConsulIntentionForm extends Component {
// if the action of the intention has changed and its non-empty then warn
// the user
if (typeof item.change.Action !== 'undefined' && typeof item.data.Action === 'undefined') {
this.warn = true;
this.modal.open();
} else {
submit();
}
Expand Down
36 changes: 22 additions & 14 deletions ui/packages/consul-ui/app/components/hashicorp-consul/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -201,39 +201,45 @@
>
{{#let components.AuthForm components.AuthProfile as |AuthForm AuthProfile|}}
<BlockSlot @name="unauthorized">
<label tabindex="0" for="login-toggle" onkeypress={{this.keypressClick}}>
<label
tabindex="0"
{{on 'keypress' this.keypressClick}}
{{on "click" (optional this.modal.open)}}
>
<span>Log in</span>
</label>
<ModalDialog @name="login-toggle" @onclose={{this.close}} @onopen={{this.open}} as |api|>
<Ref @target={{this}} @name="modal" @value={{api}} />
<ModalDialog @name="login-toggle" @onclose={{this.close}} @onopen={{this.open}} as |modal|>
<Ref @target={{this}} @name="modal" @value={{modal}} />
<BlockSlot @name="header">
<h2>Log in to Consul</h2>
</BlockSlot>
<BlockSlot @name="body">
<AuthForm as |api|>
<Ref @target={{this}} @name="authForm" @value={{api}} />
<AuthForm as |authForm|>
<Ref @target={{this}} @name="authForm" @value={{authForm}} />
</AuthForm>
</BlockSlot>
<BlockSlot @name="actions" as |close|>
<button type="button" onclick={{action close}}>
<BlockSlot @name="actions">
<button type="button"
{{on "click" modal.close}}
>
Continue without logging in
</button>
</BlockSlot>
</ModalDialog>
</BlockSlot>
<BlockSlot @name="authorized">
<ModalDialog @name="login-toggle" @onclose={{this.close}} @onopen={{this.open}} as |api|>
<Ref @target={{this}} @name="modal" @value={{api}} />
<ModalDialog @name="login-toggle" @onclose={{this.close}} @onopen={{this.open}} as |modal|>
<Ref @target={{this}} @name="modal" @value={{modal}} />
<BlockSlot @name="header">
<h2>Log in with a different token</h2>
</BlockSlot>
<BlockSlot @name="body">
<AuthForm as |api|>
<Ref @target={{this}} @name="authForm" @value={{api}} />
<AuthForm as |authForm|>
<Ref @target={{this}} @name="authForm" @value={{authForm}} />
</AuthForm>
</BlockSlot>
<BlockSlot @name="actions" as |close|>
<button type="button" onclick={{action close}}>
<BlockSlot @name="actions">
<button type="button" onclick={{action modal.close}}>
Continue without logging in
</button>
</BlockSlot>
Expand Down Expand Up @@ -271,7 +277,9 @@
</:complementary-nav>

<:main>
{{yield}}
{{yield (hash
modal=this.modal
)}}
</:main>

<:content-info>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ export default class HashiCorpConsul extends Component {
this.modal.close();
this.args.onchange(e);
}

@action
keypressClick(e) {
e.target.dispatchEvent(new MouseEvent('click'));
}
}
78 changes: 41 additions & 37 deletions ui/packages/consul-ui/app/components/modal-dialog/index.hbs
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
{{on-window 'resize' (action "resize") }}
<Portal @target="modal">
{{yield}}
<input
id={{@name}}
type="checkbox"
{{on 'change' (action 'change')}}
/>
<div
class="modal-dialog"
{{ref this 'modal'}}
aria-hidden="true"
...attributes
{{did-insert (action "connect")}}
{{will-destroy (action "disconnect")}}
>
<input
class="modal-dialog-control"
id={{name}}
type="radio"
name="modal"
data-checked="{{checked}}"
checked={{checked}}
onchange={{action 'change'}}
/>
<div tabindex="-1" data-a11y-dialog-hide></div>
<div
class="modal-dialog-modal"
role="dialog"
aria-modal="true"
>
<label for="modal_close"></label>
<div>
<div class="modal-dialog-window">
<header class="modal-dialog-header">
<label for="modal_close"></label>
<YieldSlot @name="header">
{{yield (hash
close=(action "close")
)}}
</YieldSlot>
</header>
<div class="modal-dialog-body">
<YieldSlot @name="body">
{{yield (hash
close=(action "close")
)}}
</YieldSlot>
</div>
<footer class="modal-dialog-footer">
<YieldSlot @name="actions" @params={{block-params (action "close")}}>
{{yield (hash
close=(action "close")
)}}
</YieldSlot>
</footer>
<div
role="document"
>
<header class="modal-dialog-header">
<button
type="button"
data-a11y-dialog-hide
aria-label="Close dialog"
>
</button>
<YieldSlot @name="header">
{{yield (hash
open=(action "open")
close=(action "close")
)}}
</YieldSlot>
</header>
<div class="modal-dialog-body">
<YieldSlot @name="body">
{{yield (hash
open=(action "open")
close=(action "close")
)}}
</YieldSlot>
</div>
<footer class="modal-dialog-footer">
<YieldSlot @name="actions" @params={{block-params (action "close")}}>
{{yield (hash
open=(action "open")
close=(action "close")
)}}
</YieldSlot>
</footer>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit 33d0383

Please sign in to comment.