Skip to content

Commit

Permalink
Refactor arrow key handling in toolbar and picker components for impr…
Browse files Browse the repository at this point in the history
…oved readability
  • Loading branch information
colfin-96 committed Jan 10, 2025
1 parent 06661f7 commit d3fb27d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
11 changes: 4 additions & 7 deletions packages/quill/src/modules/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,27 +153,24 @@ class Toolbar extends Module<ToolbarProps> {
var target = e.currentTarget;
if (!target) return;

switch (e.key) {
case 'ArrowLeft':
case 'ArrowRight':
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
this.updateTabIndexes(target, e.key);
break;
}
}
}

updateTabIndexes(target: EventTarget, key: string) {
const currentIndex = this.controls.findIndex(control => control[1] === target);
const currentItem = this.controls[currentIndex][1];
currentItem.tabIndex = -1;

let nextIndex;
let nextIndex: number | null = null;
if (key === 'ArrowLeft') {
nextIndex = currentIndex === 0 ? this.controls.length - 1 : currentIndex - 1;
} else if (key === 'ArrowRight') {
nextIndex = currentIndex === this.controls.length - 1 ? 0 : currentIndex + 1;
}

if (nextIndex === undefined) return;
if (nextIndex === null) return;
const nextItem = this.controls[nextIndex][1];
if (nextItem.tagName === 'SELECT') {
const qlPickerLabel = nextItem.previousElementSibling?.querySelectorAll('.ql-picker-label')[0];
Expand Down
7 changes: 2 additions & 5 deletions packages/quill/src/ui/picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,9 @@ class Picker {
var target = e.currentTarget;
if (!target) return;

switch (e.key) {
case 'ArrowLeft':
case 'ArrowRight':
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
this.updateTabIndexes(target, e.key);
break;
}
}
}

updateTabIndexes(target: EventTarget, key: string) {
Expand Down

0 comments on commit d3fb27d

Please sign in to comment.