Skip to content

Commit

Permalink
Fix arrow keys not working in the Create New Playlist prompt (#5243)
Browse files Browse the repository at this point in the history
* Fix arrow keys not working in the Create New Playlist prompt

* Allow multiple prompts to be open at the same time

* Make add video prompt inert when create playlist one is open too
  • Loading branch information
absidue authored Jun 19, 2024
1 parent ff016c9 commit 877a644
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/renderer/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
>
<portal-target
name="promptPortal"
multiple
@change="handlePromptPortalUpdate"
/>
<ft-prompt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ft-prompt
theme="flex-column"
:label="title"
:inert="showingCreatePlaylistPrompt"
@click="hide"
>
<h2 class="heading">
Expand Down
34 changes: 19 additions & 15 deletions src/renderer/components/ft-prompt/ft-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export default defineComponent({
theme: {
type: String,
default: 'base'
},
inert: {
type: Boolean,
default: false
}
},
emits: ['click'],
Expand All @@ -56,14 +60,9 @@ export default defineComponent({
},
mounted: function () {
this.lastActiveElement = document.activeElement
this.$nextTick(() => {
nextTick(() => {
document.addEventListener('keydown', this.closeEventFunction, true)
document.querySelector('.prompt').addEventListener('keydown', this.arrowKeys, true)
this.promptButtons = Array.from(
document.querySelector('.prompt .promptCard .ft-flex-box').childNodes
).filter((e) => {
return e.id && e.id.startsWith('prompt')
})
this.promptButtons = Array.from(this.$refs.promptCard.$el.querySelectorAll('.btn.ripple'))
this.focusItem(0)
})
},
Expand Down Expand Up @@ -115,20 +114,25 @@ export default defineComponent({
},
// close on escape key and unfocus
closeEventFunction: function(event) {
if (event.type === 'keydown' && event.key === 'Escape') {
if (event.type === 'keydown' && event.key === 'Escape' && !this.inert) {
event.preventDefault()
this.hide()
}
},
arrowKeys: function(e) {
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
e.preventDefault()
const currentIndex = this.promptButtons.findIndex((cur) => {
return cur === e.target
})
const direction = (e.key === 'ArrowLeft') ? -1 : 1
this.focusItem(parseInt(currentIndex) + direction)
const currentIndex = this.promptButtons.findIndex((cur) => {
return cur === e.target
})

// Only react if a button was focused when the arrow key was pressed
if (currentIndex === -1) {
return
}

e.preventDefault()

const direction = (e.key === 'ArrowLeft') ? -1 : 1
this.focusItem(parseInt(currentIndex) + direction)
},

...mapActions([
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/components/ft-prompt/ft-prompt.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
ref="openPrompt"
class="prompt"
tabindex="-1"
:inert="inert"
@click="handleHide"
@keydown.enter="handleHide"
@keydown.left.right.capture="arrowKeys"
>
<ft-card
ref="promptCard"
class="promptCard"
:class="{ autosize, [theme]: true }"
role="dialog"
Expand All @@ -33,7 +36,6 @@
<ft-flex-box>
<ft-button
v-for="(option, index) in optionNames"
:id="'prompt-' + sanitizedLabel + '-' + index"
:key="index"
:label="option"
:text-color="optionButtonTextColor(index)"
Expand Down

0 comments on commit 877a644

Please sign in to comment.