Skip to content

Commit

Permalink
refactor: catch keydown.escape from child component
Browse files Browse the repository at this point in the history
  • Loading branch information
rwd committed Feb 19, 2024
1 parent fa2a8a1 commit 2a5ab5c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
16 changes: 6 additions & 10 deletions packages/portal/src/components/generic/AttributionToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
:set-focus="keyboardNav"
extended
data-qa="attribution"
@keydown.escape.native="handleCiteAttributionKeydownEscape"
/>
</figcaption>
</template>
Expand Down Expand Up @@ -64,31 +65,26 @@
watch: {
showCite(newVal) {
if (newVal) {
window.addEventListener('keydown', this.handleWindowKeydown);
window.addEventListener('focusin', this.handleWindowFocusin);
} else {
window.removeEventListener('keydown', this.handleWindowKeydown);
window.removeEventListener('focusin', this.handleWindowFocusin);
}
}
},
beforeDestroy() {
window.removeEventListener('keydown', this.handleWindowKeydown);
window.removeEventListener('focusin', this.handleWindowFocusin);
},
methods: {
toggleCite() {
this.showCite = !this.showCite;
},
handleWindowKeydown(event) {
if (event.key === 'Escape') {
this.toggleCite();
this.$nextTick(() => {
this.$refs.toggle.focus();
});
}
handleCiteAttributionKeydownEscape() {
this.toggleCite();
this.$nextTick(() => {
this.$refs.toggle.focus();
});
},
handleWindowFocusin(event) {
// focus has changed, toggle the citation if not to a child element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('components/generic/AttributionToggle', () => {

wrapper.find('[data-qa="toggle"]').trigger('click');
await wrapper.vm.$nextTick();
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
wrapper.find('[data-qa="attribution"]').trigger('keydown.escape.native');
await wrapper.vm.$nextTick();

const attribution = wrapper.find('[data-qa="attribution"]');
Expand Down

0 comments on commit 2a5ab5c

Please sign in to comment.