Skip to content

Commit 48dc6a1

Browse files
authoredJul 16, 2022
feat: add keys option to disable arrow keys drag (#522)
1 parent d73ee4e commit 48dc6a1

File tree

9 files changed

+33
-5
lines changed

9 files changed

+33
-5
lines changed
 

‎.changeset/famous-hats-peel.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@use-gesture/core': patch
3+
---
4+
5+
feat: add option to remove arrow keys listeners for the drag gesture.

‎demo/src/sandboxes/gesture-drag-vanilla/src/App.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ function Draggable() {
2121
gesture: { options: ['offset', 'movement'] },
2222
axis: { options: [undefined, 'x', 'y', 'lock'] },
2323
filterTaps: false,
24-
boundToParent: false
24+
boundToParent: false,
25+
keys: true
2526
})
27+
2628
const pointerOptions = useControls('pointer', { touch: false, capture: true, lock: false })
2729

2830
useEffect(() => {

‎demo/src/sandboxes/gesture-drag/src/App.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function Draggable() {
2222
boundToParent: false,
2323
rubberband: false,
2424
threshold: { value: [3, 3], step: 10 },
25-
delay: { value: 1000, step: 100, min: 0, max: 3000, optional: true, disabled: true }
25+
delay: { value: 1000, step: 100, min: 0, max: 3000, optional: true, disabled: true },
26+
keys: true
2627
})
2728

2829
const pointerOptions = useControls('pointer', { touch: false, capture: true, lock: false, mouse: false })

‎documentation/pages/docs/options.mdx

+9
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Here are all options that can be applied to gestures.
121121
| `pointer.mouse` | **drag** | If `true`, drag will use mouse event listeners instead of pointer listeners when possible. |
122122
| [`pointer.buttons`](#pointerbuttons) | **drag** | Combination of buttons that triggers the drag gesture. [Read more below](#pointerbuttons). |
123123
| [`pointer.lock`](#pointerlock) | **drag** | If `true`, the pointer will enter pointer lock mode when drag starts, and exit pointer lock when drag ends. [Read more below](#pointerlock). |
124+
| [`keys`](#keys) | **drag** | By default, the drag gesture can be triggered by arrow keys when the draggable element is focused. Setting `keys` to `false` won't add keyboard event listeners. |
124125
| [`delay`](#delay) | **drag** | If set, the handler will be delayed for the duration of the delay (in `ms`) — or if the user starts moving. When set to `true`, `delay` is defaulted to `180ms`. |
125126
| [`swipe.distance`](#swipedistance) | **drag** | The minimum distance per axis (in `pixels`) the drag gesture needs to travel to trigger a swipe. |
126127
| [`swipe.velocity`](#swipevelocity) | **drag** | The minimum velocity per axis (in `pixels / ms`) the drag gesture needs to reach before the pointer is released. |
@@ -325,6 +326,14 @@ function InitialExample() {
325326

326327
> Unless your initial position is static or depends on `state`, make sure you use a function rather than a static array.
327328
329+
### keys
330+
331+
<Specs gestures={['drag']} types="boolean" defaultValue="true" />
332+
333+
By default, the drag gesture is accessible, meaning you can control the drag of an element with arrow keys. The ` shiftKey` modifier will make the drag 10x faster, the `altKey` will make the drag 10x slower.
334+
335+
If you set `keys` to `false`, your drag handler won't respond to keyboard events.
336+
328337
### modifierKey
329338

330339
<Specs gestures={['pinch']} types={[`'ctrlKey'`, `'altKey'`, `'metaKey'`, 'null']} defaultValue={`'ctrlKey'`} />

‎packages/core/src/config/dragConfigResolver.ts

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export const dragConfigResolver = {
4141
this.pointerButtons = buttons
4242
return !this.pointerLock && this.device === 'pointer' && capture
4343
},
44+
keys(value = true) {
45+
return value
46+
},
4447
threshold(
4548
this: InternalDragOptions,
4649
value: number | Vector2,

‎packages/core/src/engines/DragEngine.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,10 @@ export class DragEngine extends CoordinatesEngine<'drag'> {
367367
bindFunction('lostPointerCapture', '', this.pointerUp.bind(this))
368368
}
369369

370-
bindFunction('key', 'down', this.keyDown.bind(this))
371-
bindFunction('key', 'up', this.keyUp.bind(this))
372-
370+
if (this.config.keys) {
371+
bindFunction('key', 'down', this.keyDown.bind(this))
372+
bindFunction('key', 'up', this.keyUp.bind(this))
373+
}
373374
if (this.config.filterTaps) {
374375
bindFunction('click', '', this.pointerClick.bind(this), { capture: true, passive: false })
375376
}

‎packages/core/src/types/config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ export type DragConfig = Omit<CoordinatesConfig<'drag'>, 'axisThreshold' | 'boun
168168
* mouse events on non touch devices.
169169
*/
170170
mouse?: boolean
171+
/**
172+
* If false, will disable KeyboardEvents that would otherwise trigger the
173+
* drag gesture when the element is focused. Defaults to true.
174+
*/
175+
keys?: boolean
171176
/**
172177
* Doesn't use setPointerCapture when false and delegate drag handling to
173178
* window

‎packages/core/src/types/internalConfig.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type InternalDragOptions = Omit<InternalCoordinatesOptions<'drag'>, 'axis
3737
preventScrollDelay?: number
3838
preventScrollAxis?: 'x' | 'y' | 'xy'
3939
pointerLock: boolean
40+
keys: boolean
4041
device: 'pointer' | 'touch' | 'mouse'
4142
swipe: {
4243
velocity: Vector2

‎test/config.test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('testing derived config', () => {
6060
transform: identity,
6161
hasCustomTransform: false,
6262
preventDefault: false,
63+
keys: true,
6364
triggerAllEvents: false,
6465
delay: 0,
6566
swipe: {

0 commit comments

Comments
 (0)
Please sign in to comment.