Skip to content

Commit 5979b1a

Browse files
authoredMar 31, 2022
feat: add modifierKey for wheel option (#477)
Defaults to `'ctrlKey'`
1 parent 5db61b5 commit 5979b1a

File tree

7 files changed

+33
-3
lines changed

7 files changed

+33
-3
lines changed
 

‎.changeset/grumpy-beds-hang.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 modifierKey for wheel option. Defaults to `'ctrlKey'`.

‎documentation/pages/docs/options.mdx

+7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Here are all options that can be applied to gestures.
107107
| [`bounds`](#bounds) | **xy** | Limits the gesture `offset` to the specified bounds. |
108108
| [`scaleBounds`](#scalebounds) | **pinch** | Limits the scale `offset` to the specified bounds. |
109109
| [`angleBounds`](#anglebounds) | **pinch** | Limits the angle `offset` to the specified bounds. |
110+
| [`modifierKey`](#modifierKey) | **pinch** | The modifier key that triggers a scale when wheeling. Defaults to `'ctrlKey'`. Can be null. |
110111
| [`rubberband`](#rubberband) | **all** | The elasticity coefficient of the gesture when going out of bounds. When set to `true`, the elasticity coefficient will be defaulted to `0.15` |
111112
| [`transform`](#transform) | **all** | A function that you can use to transform pointer values. Useful to map your screen coordinates to custom space coordinates such as a canvas. |
112113
| [`filterTaps`](#filtertaps) | **drag** | If `true`, the component won't trigger your drag logic if the user just clicked on the component. |
@@ -306,6 +307,12 @@ function InitialExample() {
306307

307308
> Unless your initial position is static or depends on `state`, make sure you use a function rather than a static array.
308309
310+
### modifierKey
311+
312+
<Specs gestures={['pinch']} types={[`'ctrlKey'`, `'altKey'`, `'metaKey'`, 'null']} defaultValue={`'ctrlKey'`} />
313+
314+
This option lets you set the modifier key that triggers a scale gesture when using wheel inside the `onPinch` handler.
315+
309316
### pointer.buttons
310317

311318
<Specs gestures={['drag']} types={['number', 'number[]']} defaultValue="1" />

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ModifierKey } from '../types'
12
import { PinchConfig, GenericOptions, InternalPinchOptions, State, Vector2 } from '../types'
23
import { call, assignDefault } from '../utils/fn'
34
import { V } from '../utils/maths'
@@ -42,5 +43,9 @@ export const pinchConfigResolver = {
4243
this.lockDirection = config.axis === 'lock'
4344
const threshold = V.toVector(value, this.lockDirection ? [0.1, 3] : 0)
4445
return threshold
46+
},
47+
modifierKey(value: ModifierKey) {
48+
if (value === undefined) return 'ctrlKey'
49+
return value
4550
}
4651
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ export class PinchEngine extends Engine<'pinch'> {
233233
}
234234

235235
wheel(event: WheelEvent) {
236-
if (!event.ctrlKey) return
236+
const modifierKey = this.config.modifierKey
237+
if (modifierKey && !event[modifierKey]) return
237238
if (!this.state._active) this.wheelStart(event)
238239
else this.wheelChange(event)
239240
this.timeoutStore.add('wheelEnd', this.wheelEnd.bind(this))

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

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export type CoordinatesConfig<Key extends CoordinatesKey = CoordinatesKey> = Ges
8787
}
8888

8989
export type PinchBounds = { min?: number; max?: number }
90+
export type ModifierKey = 'ctrlKey' | 'altKey' | 'metaKey' | null
9091

9192
export type PinchConfig = GestureOptions<'pinch'> & {
9293
pointer?: {
@@ -107,6 +108,10 @@ export type PinchConfig = GestureOptions<'pinch'> & {
107108
* Scales OR rotates when set to 'lock'.
108109
*/
109110
axis?: 'lock' | undefined
111+
/**
112+
* Key that triggers scale when using the wheel. Defaults to `'ctrlKey'`.
113+
*/
114+
modifierKey?: ModifierKey
110115
}
111116

112117
export type DragBounds = Bounds | HTMLElement | React.RefObject<HTMLElement>

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GestureKey, CoordinatesKey } from './config'
1+
import { GestureKey, CoordinatesKey, ModifierKey } from './config'
22
import { State } from './state'
33
import { Vector2 } from './utils'
44

@@ -50,6 +50,7 @@ export type InternalPinchOptions = InternalGestureOptions<'pinch'> & {
5050
*/
5151
device: 'gesture' | 'pointer' | 'touch' | undefined
5252
lockDirection: boolean
53+
modifierKey: ModifierKey
5354
}
5455

5556
type MoveAndHoverMouseOnly = {

‎test/config.test.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ describe('testing derived config', () => {
161161
lockDirection: false,
162162
from: undefined,
163163
threshold: [0, 0],
164-
rubberband: [0, 0]
164+
rubberband: [0, 0],
165+
modifierKey: 'ctrlKey'
165166
})
166167
})
167168

@@ -173,5 +174,10 @@ describe('testing derived config', () => {
173174
[0, 270]
174175
])
175176
})
177+
178+
test(`setting modifierKey to null should result in modifierKey being null`, () => {
179+
config = { modifierKey: null }
180+
expect(parse(config, 'pinch').pinch).toHaveProperty('modifierKey', null)
181+
})
176182
})
177183
})

0 commit comments

Comments
 (0)
Please sign in to comment.