Skip to content

Commit 755876e

Browse files
seeniOlabodesimonwep
authored andcommitted
Fix scrolling via keyboard
Closes #215
1 parent bee9f2f commit 755876e

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

packages/vanilla/src/index.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
5858
private _scrollSpeed: Coordinates = {x: 0, y: 0};
5959
private _scrollDelta: Coordinates = {x: 0, y: 0};
6060

61+
// Required for keydown scrolling
62+
private _lastMousePosition = {x: 0, y: 0};
63+
6164
constructor(opt: PartialSelectionOptions) {
6265
super();
6366

@@ -330,7 +333,11 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
330333
if (this._scrollAvailable) {
331334

332335
// Detect mouse scrolling
333-
on(this._targetElement, 'wheel', this._manualScroll, {passive: false});
336+
on(this._targetElement, 'wheel', this._wheelScroll, {passive: false});
337+
338+
// Detect keyboard scrolling
339+
on(this._options.document, 'keydown', this._keyboardScroll, {passive: false});
340+
334341

335342
/**
336343
* The selection-area will also cover another element
@@ -403,6 +410,9 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
403410
_areaLocation.x2 = x;
404411
_areaLocation.y2 = y;
405412

413+
this._lastMousePosition.x = x;
414+
this._lastMousePosition.y = y;
415+
406416
if (this._scrollAvailable && !this._scrollingActive && (_scrollSpeed.y || _scrollSpeed.x)) {
407417

408418
// Continuous scrolling
@@ -484,7 +494,7 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
484494
off(this._targetElement, 'scroll', this._onStartAreaScroll);
485495
}
486496

487-
_manualScroll(evt: ScrollEvent): void {
497+
_wheelScroll(evt: ScrollEvent): void {
488498
const {manualSpeed} = this._options.behaviour.scrolling;
489499

490500
// Consistent scrolling speed on all browsers
@@ -498,6 +508,24 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
498508
evt.preventDefault();
499509
}
500510

511+
_keyboardScroll(evt: KeyboardEvent): void {
512+
const {manualSpeed} = this._options.behaviour.scrolling;
513+
514+
const deltaX = evt.key === 'ArrowLeft' ? -1 : evt.key === 'ArrowRight' ? 1 : 0;
515+
const deltaY = evt.key === 'ArrowUp' ? -1 : evt.key === 'ArrowDown' ? 1 : 0;
516+
517+
this._scrollSpeed.x += Math.sign(deltaX) * manualSpeed;
518+
this._scrollSpeed.y += Math.sign(deltaY) * manualSpeed;
519+
520+
evt.preventDefault();
521+
522+
this._onTapMove({
523+
clientX: this._lastMousePosition.x,
524+
clientY: this._lastMousePosition.y,
525+
preventDefault: () => void 0,
526+
} as ScrollEvent);
527+
}
528+
501529
_recalculateSelectionAreaRect(): void {
502530
const {_scrollSpeed, _areaLocation, _targetElement, _options} = this;
503531
const {scrollTop, scrollHeight, clientHeight, scrollLeft, scrollWidth, clientWidth} = _targetElement as Element;
@@ -572,7 +600,10 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
572600
this._scrollSpeed.y = 0;
573601

574602
// Unbind mouse scrolling listener
575-
off(this._targetElement, 'wheel', this._manualScroll, {passive: true});
603+
off(this._targetElement, 'wheel', this._wheelScroll, {passive: true});
604+
605+
// Unbind keyboard scrolling listener
606+
off(this._options.document, 'keydown', this._keyboardScroll, {passive: true,});
576607

577608
// Remove selection-area from dom
578609
this._clippingElement.remove();

0 commit comments

Comments
 (0)