Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy the implementation of useMovable into useDraggable #1370

Merged
merged 7 commits into from
Jun 26, 2024

Conversation

benchristel
Copy link
Member

@benchristel benchristel commented Jun 21, 2024

Summary:

This copies Mafs' implementation of the useMovable hook into our own useDraggable
hook with no substantive changes. In a future PR, I'll add the new features I need.

Context: 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.

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.

@khan-actions-bot
Copy link
Contributor

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/olive-rivers-eat.md, packages/perseus/package.json, packages/perseus/src/widgets/interactive-graphs/graphs/use-draggable.test.tsx, packages/perseus/src/widgets/interactive-graphs/graphs/use-draggable.ts

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

@khan-actions-bot khan-actions-bot requested a review from a team June 21, 2024 19:23
Copy link
Contributor

github-actions bot commented Jun 21, 2024

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (652c5b8) and published it to npm. You
can install it using the tag PR1370.

Example:

yarn add @khanacademy/perseus@PR1370

If you are working in Khan Academy's webapp, you can run:

./dev/tools/bump_perseus_version.sh -t PR1370

@benchristel benchristel changed the title Create our own wrapper around Mafs' useMovable Copy the implementation of useMovable into useDraggable Jun 21, 2024
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@use-gesture/react is a dependency of Mafs that we are now importing directly. I verified that adding it here doesn't cause multiple copies/versions of @use-gesture/react to get installed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are unfortunately incomplete. I couldn't figure out how to get @use-gesture/react to register a drag gesture, either with userEvent or fireEvent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Is this Moving a Pointer section of the Testing Library docs useful? It seems like it is showing how to user userEvent.pointer to drag, though I could be reading it wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tried what those docs suggest. @use-gesture/react doesn't see it as a drag, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I looked at the tests for @use-gesture/react itself and found that they're using fireEvent, so I came up with a testing solution based on that.

Copy link
Contributor

github-actions bot commented Jun 21, 2024

Size Change: +496 B (+0.06%)

Total Size: 847 kB

Filename Size Change
packages/perseus-editor/dist/es/index.js 272 kB -1 B (0%)
packages/perseus/dist/es/index.js 408 kB +497 B (+0.12%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38.2 kB
packages/kmath/dist/es/index.js 4.26 kB
packages/math-input/dist/es/index.js 80.1 kB
packages/math-input/dist/es/strings.js 1.73 kB
packages/perseus-core/dist/es/index.js 906 B
packages/perseus-error/dist/es/index.js 866 B
packages/perseus-linter/dist/es/index.js 21.8 kB
packages/perseus/dist/es/strings.js 3.21 kB
packages/pure-markdown/dist/es/index.js 3.67 kB
packages/simple-markdown/dist/es/index.js 12.4 kB

compressed-size-action

Copy link

codecov bot commented Jun 21, 2024

Codecov Report

Attention: Patch coverage is 96.20253% with 6 lines in your changes missing coverage. Please review.

Project coverage is 71.35%. Comparing base (e5a54d8) to head (652c5b8).
Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1370      +/-   ##
==========================================
+ Coverage   69.63%   71.35%   +1.71%     
==========================================
  Files         492      495       +3     
  Lines      103871   104092     +221     
  Branches     7451    11277    +3826     
==========================================
+ Hits        72333    74270    +1937     
+ Misses      31356    29822    -1534     
+ Partials      182        0     -182     

Impacted file tree graph

Files Coverage Δ
...widgets/interactive-graphs/graphs/use-draggable.ts 96.51% <96.20%> (-3.49%) ⬇️

... and 146 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5a54d8...652c5b8. Read the comment docs.

Copy link
Contributor

@Myranae Myranae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Was able to test that all the Mafs graphs (or everything I could tab to) moved with keyboard and also with mouse. I was surprised to see the points on a polygon with angle snapping moving with keyboard, but the points on the polygon with sides snapping still don't move with the keyboard. Added some non-blocking comments.

Comment on lines 22 to 24
<p role="button" ref={gestureTarget}>
dragging: {String(dragging)}
</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Is it okay to set the role of a p element to "button" like this? I know this is only a test, so maybe it's not super important.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's valid, but potentially confusing. I should probably just make this a button element.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Is this Moving a Pointer section of the Testing Library docs useful? It seems like it is showing how to user userEvent.pointer to drag, though I could be reading it wrong.

Copy link
Member

@catandthemachines catandthemachines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved! Though I am curious why it is necessary to move the logic in Mafs into a useDraggable function for our internal use, instead of utilizing useMovable.

@benchristel
Copy link
Member Author

@catandthemachines I've updated the PR description explaining the reasoning behind this change. Copied from here: #1367

@benchristel benchristel merged commit 48e879a into main Jun 26, 2024
21 checks passed
@benchristel benchristel deleted the benc/copy-in-useMovable branch June 26, 2024 20:31
benchristel pushed a commit that referenced this pull request Jul 1, 2024
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.


# Releases
## @khanacademy/perseus@24.1.0

### Minor Changes

-   [#1376](#1376) [`3ee100add`](3ee100a) Thanks [@benchristel](https://github.com/benchristel)! - Implement the protractor for Mafs interactive graphs


-   [#1381](#1381) [`26dceb8d7`](26dceb8) Thanks [@benchristel](https://github.com/benchristel)! - Make the `mafs.point` flag control whether point graphs with fixed numbers of points should use Mafs. Previously, turning on the `mafs.point` flag would enable Mafs for point graphs with unlimited points as well.

### Patch Changes

-   [#1358](#1358) [`93eeda1e2`](93eeda1) Thanks [@benchristel](https://github.com/benchristel)! - Add TODO comment


-   [#1379](#1379) [`685fa9048`](685fa90) Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph Editor] Moving around/renaming components so they make more sense for the upcoming hint mode work


-   [#1370](#1370) [`48e879ace`](48e879a) Thanks [@benchristel](https://github.com/benchristel)! - Internal: copy Mafs' implementation of useMovable into our own useDraggable hook.

## @khanacademy/perseus-editor@7.0.1

### Patch Changes

-   [#1375](#1375) [`a8b3aa9c0`](a8b3aa9) Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph Editor] Fix the broken storybook preview for segments, points, and polygons


-   [#1379](#1379) [`685fa9048`](685fa90) Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph Editor] Moving around/renaming components so they make more sense for the upcoming hint mode work

-   Updated dependencies \[[`3ee100add`](3ee100a), [`93eeda1e2`](93eeda1), [`685fa9048`](685fa90), [`48e879ace`](48e879a), [`26dceb8d7`](26dceb8)]:
    -   @khanacademy/perseus@24.1.0

Author: khan-actions-bot

Reviewers: benchristel

Required Reviewers:

Approved By: benchristel

Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ⏭️  Publish npm snapshot, ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1377
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants