-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create our own wrapper around Mafs'
useMovable
(#1367)
## Summary: We need the `useMovable` hook to do a couple things that Mafs doesn't yet enable: - let us handle keyboard events explicitly (needed for angle graphs, and maybe polygons if we decide to continue supporting side-length snapping). Currently, Mafs uses heuristics and search to convert keyboard input into movement vectors based on the provided `constrain` function, but this only works for grid-based snapping. - get the movement vector in *pixel coordinates*. This is needed to implement the protractor rotation handle. The protractor is a fixed-size image that doesn't scale with the graph, so we really want to be working in pixels - otherwise, we have to do a bunch of confusing coordinate transformations. We'll likely want to iterate on these features a bit before contributing them back to Mafs. Therefore, this commit creates a wrapper around `useMovable` where we can add our new functionality. I have named the wrapper `useDraggable` instead of `useMovable` so people don't mistake it for the Mafs hook. Issue: none Test plan: - Run `yarn dev` and view the graphs at `http://localhost:5173/`. - Turn on the Mafs flags for all graph types. - Verify that you can move all the interactive graph elements with mouse and keyboard. Author: benchristel Reviewers: SonicScrewdriver, handeyeco, mark-fitzgerald, Myranae, nishasy Required Reviewers: Approved By: SonicScrewdriver Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1367
- Loading branch information
1 parent
86f94e1
commit e5a54d8
Showing
8 changed files
with
51 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": patch | ||
--- | ||
|
||
Internal: wrap the Mafs `useMovable` hook, creating a seam where we can add new functionality. |
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
19 changes: 19 additions & 0 deletions
19
packages/perseus/src/widgets/interactive-graphs/graphs/use-draggable.ts
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,19 @@ | ||
import {useMovable} from "mafs"; | ||
|
||
import type {vec} from "mafs"; | ||
import type {RefObject} from "react"; | ||
|
||
type Params = { | ||
gestureTarget: RefObject<Element>; | ||
onMove: (point: vec.Vector2) => unknown; | ||
point: vec.Vector2; | ||
constrain: (point: vec.Vector2) => vec.Vector2; | ||
}; | ||
|
||
type DragState = { | ||
dragging: boolean; | ||
}; | ||
|
||
export function useDraggable(params: Params): DragState { | ||
return useMovable(params); | ||
} |