Skip to content

Commit 0448407

Browse files
EXE901r4pt0s
andauthored
feat: add keyboard arrow navigation for interaction choices (#267)
Co-authored-by: Wolfgang Kreminger <r4pt0s@protonmail.com>
1 parent 29885d4 commit 0448407

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

src/utils.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,29 @@ export const showCustomPrompt = (
394394
optionsContainer.appendChild(button);
395395
});
396396

397+
// Add arrow key navigation
398+
const buttons = optionsContainer.querySelectorAll('.option-btn');
399+
let currentIndex = 0;
400+
401+
const keyHandler = (e) => {
402+
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
403+
e.preventDefault();
404+
currentIndex = (currentIndex + 1) % buttons.length;
405+
buttons[currentIndex].focus();
406+
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
407+
e.preventDefault();
408+
currentIndex = (currentIndex - 1 + buttons.length) % buttons.length;
409+
buttons[currentIndex].focus();
410+
}
411+
};
412+
413+
document.addEventListener('keydown', keyHandler);
414+
415+
// Clean up event listener when prompt closes
416+
window.cleanupPromptKeyHandler = () => {
417+
document.removeEventListener('keydown', keyHandler);
418+
};
419+
397420
// Display the custom prompt
398421
document.getElementById('custom-prompt').style.display = 'flex';
399422

@@ -405,6 +428,12 @@ export const showCustomPrompt = (
405428

406429
// Function to close the custom prompt
407430
export const closeCustomPrompt = (player, k, abort) => {
431+
// Clean up keyboard event listener
432+
if (window.cleanupPromptKeyHandler) {
433+
window.cleanupPromptKeyHandler();
434+
delete window.cleanupPromptKeyHandler;
435+
}
436+
408437
// Hide the custom prompt
409438
document.getElementById('custom-prompt').style.display = 'none';
410439

@@ -413,6 +442,5 @@ export const closeCustomPrompt = (player, k, abort) => {
413442

414443
time.paused = false;
415444
abort.abort();
416-
player.state.isInDialog = false;
417-
k.canvas.focus();
418445
};
446+

0 commit comments

Comments
 (0)