-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify and document usage of
useInteraction
and `useModifierKeyPr…
…essed`
- Loading branch information
Showing
11 changed files
with
127 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { castArray } from 'lodash'; | ||
import type { ModifierKey } from 'react'; | ||
|
||
import type { InteractionConfig } from './models'; | ||
import { MouseButton } from './models'; | ||
|
||
export class Interaction { | ||
public readonly buttons: MouseButton[]; | ||
public readonly modifierKeys: ModifierKey[]; | ||
public readonly isWheel: boolean; | ||
public readonly isEnabled: boolean; | ||
|
||
public constructor( | ||
public readonly id: string, | ||
config: InteractionConfig, | ||
) { | ||
const { | ||
button = MouseButton.Left, | ||
modifierKey = [], | ||
disabled = false, | ||
} = config; | ||
|
||
if (button === 'Wheel') { | ||
this.buttons = []; | ||
this.isWheel = true; | ||
} else { | ||
this.buttons = castArray(button); | ||
this.isWheel = false; | ||
} | ||
|
||
this.modifierKeys = castArray(modifierKey); | ||
this.isEnabled = !disabled; | ||
} | ||
|
||
public matches(event: MouseEvent): boolean { | ||
return ( | ||
this.isEnabled && | ||
(event instanceof WheelEvent | ||
? this.isWheel | ||
: this.buttons.includes(event.button)) && | ||
this.modifierKeys.every((key) => event.getModifierState(key)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.