Skip to content

Commit

Permalink
feat(pinch): allow modifierkey to be an array (#613)
Browse files Browse the repository at this point in the history
* feat(pinch): allow modifierkey to be an array

* test: add test for modifierKey as an array

* demo: update pinch demo with modifier keys

* ci: changeset
  • Loading branch information
BJvdA authored Sep 16, 2023
1 parent 97765d6 commit c19ff0b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-laws-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@use-gesture/core': patch
---

feat: modifierKey can be an array via @BJvdA
20 changes: 18 additions & 2 deletions demo/src/sandboxes/gesture-pinch/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from 'react'
import { usePinch } from '@use-gesture/react'
import { a, useSpring } from '@react-spring/web'
import { useControls } from 'leva'
import { folder, useControls } from 'leva'

import styles from './styles.module.css'

Expand All @@ -14,6 +14,16 @@ export default function App() {
axis: { options: [undefined, 'lock'] }
})

const _modifierKey = useControls('modifierKey', {
ctrlKey: false,
metaKey: false,
altKey: false
})

const modifierKey = Object.entries(_modifierKey)
.filter(([, v]) => !!v)
.map((e) => e[0])

useEffect(() => {
const handler = (e) => e.preventDefault()
document.addEventListener('gesturestart', handler)
Expand All @@ -34,7 +44,13 @@ export default function App() {
rotate: active || gesture === 'offset' ? angle : 0
})
},
{ target, eventOptions: { passive: false }, pointer: { touch }, ...rest }
{
target,
eventOptions: { passive: false },
pointer: { touch },
modifierKey: modifierKey.length ? modifierKey : undefined,
...rest
}
)

return (
Expand Down
6 changes: 5 additions & 1 deletion documentation/pages/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ function InitialExample() {
### modifierKey

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

This option lets you set the modifier key that triggers a scale gesture when using wheel inside the `onPinch` handler.

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config/pinchConfigResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const pinchConfigResolver = {
const threshold = V.toVector(value, this.lockDirection ? [0.1, 3] : 0)
return threshold
},
modifierKey(value: ModifierKey) {
modifierKey(value: ModifierKey | ModifierKey[]) {
if (value === undefined) return 'ctrlKey'
return value
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/engines/PinchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class PinchEngine extends Engine<'pinch'> {

wheel(event: WheelEvent) {
const modifierKey = this.config.modifierKey
if (modifierKey && !event[modifierKey]) return
if (modifierKey && (Array.isArray(modifierKey) ? !modifierKey.find((k) => event[k]) : !event[modifierKey])) return
if (!this.state._active) this.wheelStart(event)
else this.wheelChange(event)
this.timeoutStore.add('wheelEnd', this.wheelEnd.bind(this))
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export type PinchConfig = GestureOptions<'pinch'> & {
/**
* Key that triggers scale when using the wheel. Defaults to `'ctrlKey'`.
*/
modifierKey?: ModifierKey
modifierKey?: ModifierKey | NonNullable<ModifierKey>[]
/**
* Whether wheel should trigger a pinch at all.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/internalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type InternalPinchOptions = InternalGestureOptions<'pinch'> & {
*/
device: 'gesture' | 'pointer' | 'touch' | undefined
lockDirection: boolean
modifierKey: ModifierKey
modifierKey: ModifierKey | NonNullable<ModifierKey>[]
pinchOnWheel: boolean
}

Expand Down
5 changes: 5 additions & 0 deletions test/config.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,10 @@ describe('testing derived config', () => {
config = { modifierKey: null }
expect(parse(config, 'pinch').pinch).toHaveProperty('modifierKey', null)
})

test(`setting modifierKey to array should result in modifierKey being an array`, () => {
config = { modifierKey: ['altKey', 'ctrlKey'] }
expect(parse(config, 'pinch').pinch).toHaveProperty('modifierKey', ['altKey', 'ctrlKey'])
})
})
})

0 comments on commit c19ff0b

Please sign in to comment.