Skip to content

Commit

Permalink
feat(modal): close on 'Escape' key (#4433)
Browse files Browse the repository at this point in the history
* feat(modal): close on 'Escape' key

* fix(modal): implement typescript

* fix(modal): listen to keydown

* fix(keybindinput): listen/focus on child element

* fix(modal): fix background glitch

Co-authored-by: Jason Woodland <me@jasonwoodland.com>
  • Loading branch information
nathan-power and jasonwoodland committed Aug 26, 2022
1 parent 1c337dc commit fe15bd2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
7 changes: 6 additions & 1 deletion components/interactables/KeybindInput/KeybindInput.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<div class="keybind-input" v-click-outside="cancel">
<div
class="keybind-input"
v-click-outside="cancel"
ref="container"
tabindex="-1"
>
<TypographyLabel :text="name" />
<div class="control">
<TypographyText v-if="!isListening && value === ''" class="info-label">
Expand Down
15 changes: 10 additions & 5 deletions components/interactables/KeybindInput/KeybindInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export default Vue.extend({
newKeybindString(): string {
return this.newKeybind.join(separator).toLowerCase()
},
container(): HTMLElement {
return this.$refs.container as HTMLElement
},
},
beforeDestroy() {
this.removeKeybindListener()
Expand Down Expand Up @@ -121,13 +124,14 @@ export default Vue.extend({
this.removeKeybindListener()
},
addKeybindListener() {
window.addEventListener('keydown', this.handleKeyEvent)
window.addEventListener('keyup', this.handleKeyEvent)
this.container.addEventListener('keydown', this.handleKeyEvent)
this.container.addEventListener('keyup', this.handleKeyEvent)
this.container.focus()
this.isListening = true
},
removeKeybindListener() {
window.removeEventListener('keydown', this.handleKeyEvent)
window.removeEventListener('keyup', this.handleKeyEvent)
this.container.removeEventListener('keydown', this.handleKeyEvent)
this.container.removeEventListener('keyup', this.handleKeyEvent)
this.isListening = false
Object.assign(this.modifiers, initialModifiersState)
},
Expand All @@ -144,7 +148,8 @@ export default Vue.extend({
}
if (this.activeModifiers.length === 0) {
const editButton = this.$refs.editButton as HTMLButtonElement
const editButton = (this.$refs.editButton as Vue)
?.$el as HTMLButtonElement
if (event.keyCode === 13) {
this.save()
editButton.focus()
Expand Down
6 changes: 1 addition & 5 deletions components/ui/Modal/Modal.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<div class="container">
<div
class="modal"
:class="{small: small}"
v-click-outside="() => $emit('close')"
>
<div class="modal" :class="{small: small}" v-click-outside="close">
<TypographyTitle v-if="title" :text="title" :size="6" />
<slot />
</div>
Expand Down
24 changes: 23 additions & 1 deletion components/ui/Modal/Modal.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template src="./Modal.html"></template>

<script>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
Expand All @@ -15,6 +15,28 @@ export default Vue.extend({
default: false,
},
},
created() {
this.addEventListener()
},
beforeDestroy() {
this.removeEventListener()
},
methods: {
close() {
this.$emit('close')
},
handleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape') {
this.close()
}
},
addEventListener() {
document.addEventListener('keydown', this.handleKeydown)
},
removeEventListener() {
document.removeEventListener('keydown', this.handleKeydown)
},
},
})
</script>

Expand Down
2 changes: 2 additions & 0 deletions layouts/desktop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export default Vue.extend({
inset: 0;
position: absolute;
transition: left @animation-speed-long ease;
/* background required for modal backdrop-filter to work properly */
background: @background;
&.hide-sidebars {
left: calc(calc(@sidebar-width + @slimbar-width) * -1);
Expand Down

0 comments on commit fe15bd2

Please sign in to comment.