Skip to content

Commit

Permalink
Update aria-menu modifier for new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wenincode authored and chris-hut committed Oct 16, 2023
1 parent 13e3d1c commit b7295ee
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions ui/packages/consul-ui/app/modifiers/aria-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Modifier from 'ember-modifier';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { registerDestructor } from '@ember/destroyable';

const TAB = 9;
const ESC = 27;
Expand Down Expand Up @@ -38,6 +39,12 @@ const keys = {

const MENU_ITEMS = '[role^="menuitem"]';

function cleanup(instance) {
if (instance) {
instance?.doc?.removeEventListener('keydown', instance?.keydown);
}
}

export default class AriaMenuModifier extends Modifier {
@service('-document') doc;
orientation = 'vertical';
Expand Down Expand Up @@ -85,28 +92,19 @@ export default class AriaMenuModifier extends Modifier {
}
}

connect(params, named) {
this.$trigger = this.doc.getElementById(this.element.getAttribute('aria-labelledby'));
if (typeof named.openEvent !== 'undefined') {
this.focus(named.openEvent);
}
this.doc.addEventListener('keydown', this.keydown);
}

disconnect() {
this.doc.removeEventListener('keydown', this.keydown);
}

didReceiveArguments() {
this.params = this.args.positional;
this.options = this.args.named;
}
modify(element, positional, named) {
this.params = positional;
this.options = named;

didInstall() {
this.connect(this.args.positional, this.args.named);
}
if (!this.$trigger) {
this.element = element;
this.$trigger = this.doc.getElementById(element.getAttribute('aria-labelledby'));
if (typeof named.openEvent !== 'undefined') {
this.focus(named.openEvent);
}

willRemove() {
this.disconnect();
this.doc.addEventListener('keydown', this.keydown);
}
registerDestructor(this, cleanup);
}
}

0 comments on commit b7295ee

Please sign in to comment.