Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scrollThreshold option to KeyboardSensor #1301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/core/src/sensors/keyboard/KeyboardSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ import type {
SensorOptions,
} from '../types';

import {KeyboardCoordinateGetter, KeyboardCode, KeyboardCodes} from './types';
import {
KeyboardCoordinateGetter,
KeyboardCode,
KeyboardCodes,
type ScrollThreshold,
} from './types';
import {
defaultKeyboardCodes,
defaultKeyboardCoordinateGetter,
defaultScrollThreshold,
} from './defaults';

export interface KeyboardSensorOptions extends SensorOptions {
keyboardCodes?: KeyboardCodes;
coordinateGetter?: KeyboardCoordinateGetter;
scrollBehavior?: ScrollBehavior;
scrollThreshold?: ScrollThreshold;
onActivation?({event}: {event: KeyboardEvent}): void;
}

Expand Down Expand Up @@ -84,6 +91,7 @@ export class KeyboardSensor implements SensorInstance {
keyboardCodes = defaultKeyboardCodes,
coordinateGetter = defaultKeyboardCoordinateGetter,
scrollBehavior = 'smooth',
scrollThreshold = defaultScrollThreshold,
} = options;
const {code} = event;

Expand Down Expand Up @@ -132,23 +140,29 @@ export class KeyboardSensor implements SensorInstance {
const clampedCoordinates = {
x: Math.min(
direction === KeyboardCode.Right
? scrollElementRect.right - scrollElementRect.width / 2
? scrollElementRect.right -
scrollElementRect.width * scrollThreshold.x -
coordinatesDelta.x
: scrollElementRect.right,
Math.max(
direction === KeyboardCode.Right
? scrollElementRect.left
: scrollElementRect.left + scrollElementRect.width / 2,
: scrollElementRect.left +
scrollElementRect.width * scrollThreshold.x,
newCoordinates.x
)
),
y: Math.min(
direction === KeyboardCode.Down
? scrollElementRect.bottom - scrollElementRect.height / 2
? scrollElementRect.bottom -
scrollElementRect.height * scrollThreshold.y -
coordinatesDelta.y
: scrollElementRect.bottom,
Math.max(
direction === KeyboardCode.Down
? scrollElementRect.top
: scrollElementRect.top + scrollElementRect.height / 2,
: scrollElementRect.top +
scrollElementRect.height * scrollThreshold.y,
newCoordinates.y
)
),
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/sensors/keyboard/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {KeyboardCoordinateGetter, KeyboardCode, KeyboardCodes} from './types';
import {
KeyboardCoordinateGetter,
KeyboardCode,
KeyboardCodes,
type ScrollThreshold,
} from './types';

export const defaultKeyboardCodes: KeyboardCodes = {
start: [KeyboardCode.Space, KeyboardCode.Enter],
Expand Down Expand Up @@ -35,3 +40,5 @@ export const defaultKeyboardCoordinateGetter: KeyboardCoordinateGetter = (

return undefined;
};

export const defaultScrollThreshold: ScrollThreshold = {x: 0.5, y: 0.5};
5 changes: 5 additions & 0 deletions packages/core/src/sensors/keyboard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ export type KeyboardCoordinateGetter = (
context: SensorContext;
}
) => Coordinates | void;

export type ScrollThreshold = {
x: number;
y: number;
};