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

Style movable points and segments on Mafs graphs #1068

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-buckets-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Adjust styling of the movable points and line segments on the Mafs segment graph
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe("segment graph", () => {

// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const movablePoints = container.querySelectorAll(
"circle.mafs-movable-point-hitbox",
"circle.movable-point-hitbox",
);

// Act
Expand All @@ -166,7 +166,7 @@ describe("segment graph", () => {

// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const movablePoints = container.querySelectorAll(
"circle.mafs-movable-point-hitbox",
"circle.movable-point-hitbox",
);

// Act
Expand Down
20 changes: 9 additions & 11 deletions packages/perseus/src/widgets/interactive-graphs/graphs/segment.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Color from "@khanacademy/wonder-blocks-color";
import {MovablePoint, vec, useMovable, useTransformContext} from "mafs";
import {vec, useMovable, useTransformContext} from "mafs";
import * as React from "react";
import {useRef} from "react";

import {moveControlPoint, moveSegment} from "../interactive-graph-action";
import {MovablePoint} from "../movable-point";

import type {Segment, SegmentGraphState} from "../interactive-graph-state";
import type {MafsGraphProps} from "../types";
Expand Down Expand Up @@ -64,10 +64,10 @@ const SegmentView = (props: {
});

const {viewTransform, userTransform} = useTransformContext();
const transform = vec.matrixMult(viewTransform, userTransform);
const transformToPx = vec.matrixMult(viewTransform, userTransform);

const scaledPoint1 = vec.transform(pt1, transform);
const scaledPoint2 = vec.transform(pt2, transform);
const pt1Px = vec.transform(pt1, transformToPx);
const pt2Px = vec.transform(pt2, transformToPx);

return (
<>
Expand All @@ -79,13 +79,13 @@ const SegmentView = (props: {
>
{/* This transparent line creates a nice big click target. */}
<SVGLine
start={scaledPoint1}
end={scaledPoint2}
start={pt1Px}
end={pt2Px}
style={{stroke: "transparent", strokeWidth: 30}}
/>
<SVGLine
start={scaledPoint1}
end={scaledPoint2}
start={pt1Px}
end={pt2Px}
style={{
stroke: "var(--mafs-segment-stroke-color)",
strokeWidth: "var(--mafs-segment-stroke-weight)",
Expand All @@ -94,14 +94,12 @@ const SegmentView = (props: {
</g>
<MovablePoint
point={pt1}
color={Color.blue}
onMove={(newPoint) => {
props.onMovePoint(0, newPoint);
}}
/>
<MovablePoint
point={pt2}
color={Color.blue}
onMove={(newPoint) => {
props.onMovePoint(1, newPoint);
}}
Expand Down
38 changes: 38 additions & 0 deletions packages/perseus/src/widgets/interactive-graphs/mafs-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
--mafs-green: #00a60e; /* WB color.green */
--mafs-violet: #9059ff; /* WB color.purple */
--mafs-yellow: #ffb100; /* WB color.gold */

/* overridden on a per-point basis */
--movable-point-color: var(--mafs-blue);
}

.MafsView .movable-segment {
Expand All @@ -30,3 +33,38 @@
.MafsView .movable-segment:focus {
outline: none;
}

.MafsView .movable-point-hitbox {
fill: transparent;
}

.MafsView :is(.movable-point-center, .movable-point-ring, .movable-point-halo) {
transition: r 0.2s ease;
}

.MafsView .movable-point-halo {
fill: var(--movable-point-color);
opacity: 0.25;
filter: drop-shadow(0 5px 5px #0008);
}

.MafsView .movable-point-ring {
fill: #fff;
}

.MafsView .movable-point:hover .movable-point-center {
r: 8px;
}

.MafsView .movable-point:hover .movable-point-ring {
r: 10px;
}

.MafsView .movable-point:hover .movable-point-halo {
r: 13px;
}

.MafsView .movable-point:focus-visible .movable-point-halo {
outline: 2px solid blue;
border-radius: 99px;
}
52 changes: 52 additions & 0 deletions packages/perseus/src/widgets/interactive-graphs/movable-point.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Color from "@khanacademy/wonder-blocks-color";
import {vec, useMovable, useTransformContext} from "mafs";
import * as React from "react";
import {useRef} from "react";

type Props = {
point: vec.Vector2;
onMove: (newPoint: vec.Vector2) => unknown;
};

export const MovablePoint = (props: Props) => {
const hitboxRef = useRef<SVGCircleElement>(null);
const {point, onMove} = props;

const {dragging} = useMovable({
gestureTarget: hitboxRef,
point,
onMove,
constrain: (p) => p,
});

const {viewTransform, userTransform} = useTransformContext();
const transformToPx = vec.matrixMult(viewTransform, userTransform);
const [x, y] = vec.transform(point, transformToPx);

return (
<g
ref={hitboxRef}
className="movable-point"
tabIndex={0}
style={
{
cursor: dragging ? "grabbing" : "grab",
touchAction: "none",
outline: "none",
"--movable-point-color": Color.blue,
} as any
}
>
<circle className="movable-point-hitbox" r={30} cx={x} cy={y} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional suggestion: Maybe also move the hitbox radius to the styles for consistency?

<circle className="movable-point-halo" r={11} cx={x} cy={y} />
<circle className="movable-point-ring" r={8} cx={x} cy={y} />
<circle
className="movable-point-center"
r={6}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to break out these scalar numbers into a constants file? I'm not entirely sure since it doesn't look like there are a lot of reused ones (between this file and the CSS file), but I think naming them could make it easier to figure out why the sizes are what they are in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call-out. Probably all of the radii should be in CSS, at least. I'll move these.

cx={x}
cy={y}
style={{fill: Color.blue}}
/>
</g>
);
};
Loading