Skip to content

Commit 7fd586e

Browse files
authored
[Interactive Graph Editor] Add white as a fill option for locked ellipses and polygons (#1478)
## Summary: We got a request from content authors to add a "white" fill type for locked ellipses and polygons. Adding that here. Issue: https://khanacademy.atlassian.net/browse/LEMS-2119 ## Test plan: Storybook - http://localhost:6006/?path=/story/perseuseditor-widgets-interactive-graph--mafs-with-locked-figures-m-2-b-flag - Go through all the fill options for locked ellipses and locked polygons - Confirm that the swatch in the summary also updates corrently ## Demo: https://github.com/user-attachments/assets/2e12e38e-5b47-4a11-8ee5-55252efff78e Author: nishasy Reviewers: Myranae, jeremywiebe, mark-fitzgerald Required Reviewers: Approved By: Myranae Checks: ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1478
1 parent a8aaac3 commit 7fd586e

File tree

8 files changed

+139
-4
lines changed

8 files changed

+139
-4
lines changed

.changeset/gentle-roses-pump.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@khanacademy/perseus": patch
3+
"@khanacademy/perseus-editor": patch
4+
---
5+
6+
[Interactive Graph Editor] Add "white" as a fill option for locked ellipses and polygons

packages/perseus-editor/src/components/graph-locked-figures/ellipse-swatch.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ const EllipseSwatch = (props: Props) => {
3333
styles.innerCircle,
3434
{
3535
backgroundColor: lockedFigureColors[color],
36-
opacity: lockedFigureFillStyles[fillStyle],
36+
opacity:
37+
fillStyle === "white"
38+
? 0
39+
: lockedFigureFillStyles[fillStyle],
3740
},
3841
]}
3942
/>

packages/perseus-editor/src/components/graph-locked-figures/polygon-swatch.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ const PolygonSwatch = (props: Props) => {
3333
styles.innerSquare,
3434
{
3535
backgroundColor: lockedFigureColors[color],
36-
opacity: lockedFigureFillStyles[fillStyle],
36+
opacity:
37+
fillStyle === "white"
38+
? 0
39+
: lockedFigureFillStyles[fillStyle],
3740
},
3841
]}
3942
/>

packages/perseus/src/perseus-types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,12 @@ export type LockedVectorType = {
722722
color: LockedFigureColor;
723723
};
724724

725-
export type LockedFigureFillType = "none" | "solid" | "translucent";
725+
export type LockedFigureFillType = "none" | "white" | "translucent" | "solid";
726726
export const lockedFigureFillStyles: Record<LockedFigureFillType, number> = {
727727
none: 0,
728-
solid: 1,
728+
white: 1,
729729
translucent: 0.4,
730+
solid: 1,
730731
} as const;
731732

732733
export type LockedEllipseType = {

packages/perseus/src/widgets/__testdata__/interactive-graph.testdata.ts

+38
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,18 @@ export const segmentWithLockedEllipses: PerseusRenderer =
665665
})
666666
.build();
667667

668+
export const segmentWithLockedEllipseWhite: PerseusRenderer =
669+
interactiveGraphQuestionBuilder()
670+
.addLockedEllipse([0, 0], [5, 5], {
671+
color: "green",
672+
fillStyle: "white",
673+
})
674+
.addLockedEllipse([-5, 5], [2, 3], {
675+
color: "pink",
676+
fillStyle: "translucent",
677+
})
678+
.build();
679+
668680
export const segmentWithLockedPolygons: PerseusRenderer =
669681
interactiveGraphQuestionBuilder()
670682
.addLockedPolygon([
@@ -703,6 +715,32 @@ export const segmentWithLockedPolygons: PerseusRenderer =
703715
)
704716
.build();
705717

718+
export const segmentWithLockedPolygonWhite: PerseusRenderer =
719+
interactiveGraphQuestionBuilder()
720+
.addLockedPolygon(
721+
[
722+
[0, 3],
723+
[-3, 0],
724+
[3, 0],
725+
],
726+
{
727+
color: "green",
728+
fillStyle: "white",
729+
},
730+
)
731+
.addLockedPolygon(
732+
[
733+
[-5, 0],
734+
[-3, -1],
735+
[3, -1],
736+
],
737+
{
738+
color: "pink",
739+
fillStyle: "translucent",
740+
},
741+
)
742+
.build();
743+
706744
export const segmentWithLockedFunction = (
707745
equation: string = "x^2",
708746
options?: LockedFunctionOptions,

packages/perseus/src/widgets/__tests__/interactive-graph.test.tsx

+60
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ import {
3434
segmentQuestion,
3535
segmentQuestionDefaultCorrect,
3636
segmentWithLockedEllipses,
37+
segmentWithLockedEllipseWhite,
3738
segmentWithLockedFunction,
3839
segmentWithLockedLineQuestion,
3940
segmentWithLockedPointsQuestion,
4041
segmentWithLockedPointsWithColorQuestion,
4142
segmentWithLockedPolygons,
43+
segmentWithLockedPolygonWhite,
4244
segmentWithLockedVectors,
4345
sinusoidQuestionWithDefaultCorrect,
4446
} from "../__testdata__/interactive-graph.testdata";
@@ -647,6 +649,35 @@ describe("locked layer", () => {
647649
});
648650
});
649651

652+
it("should render locked ellipses with white fill", async () => {
653+
// Arrange
654+
const {container} = renderQuestion(segmentWithLockedEllipseWhite, {
655+
flags: {
656+
mafs: {
657+
segment: true,
658+
},
659+
},
660+
});
661+
662+
// Act
663+
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
664+
const circles = container.querySelectorAll("ellipse");
665+
const whiteCircle = circles[0];
666+
const translucentCircle = circles[1];
667+
668+
// Assert
669+
expect(whiteCircle).toHaveStyle({
670+
"fill-opacity": 1,
671+
fill: wbColor.white,
672+
stroke: lockedFigureColors["green"],
673+
});
674+
expect(translucentCircle).toHaveStyle({
675+
"fill-opacity": "0.4",
676+
fill: lockedFigureColors["pink"],
677+
stroke: lockedFigureColors["pink"],
678+
});
679+
});
680+
650681
it("should render locked polygons with style", async () => {
651682
// Arrange
652683
const {container} = renderQuestion(segmentWithLockedPolygons, {
@@ -677,6 +708,35 @@ describe("locked layer", () => {
677708
});
678709
});
679710

711+
it("should render locked polygons with white fill", async () => {
712+
// Arrange
713+
const {container} = renderQuestion(segmentWithLockedPolygonWhite, {
714+
flags: {
715+
mafs: {
716+
segment: true,
717+
},
718+
},
719+
});
720+
721+
// Act
722+
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
723+
const polygons = container.querySelectorAll(".locked-polygon polygon");
724+
const whitePolygon = polygons[0];
725+
const translucentPolygon = polygons[1];
726+
727+
// Assert
728+
expect(whitePolygon).toHaveStyle({
729+
"fill-opacity": 1,
730+
fill: wbColor.white,
731+
stroke: lockedFigureColors["green"],
732+
});
733+
expect(translucentPolygon).toHaveStyle({
734+
"fill-opacity": "0.4",
735+
fill: lockedFigureColors["pink"],
736+
stroke: lockedFigureColors["pink"],
737+
});
738+
});
739+
680740
it("should render vertices of locked polygons with showVertices", async () => {
681741
// Arrange
682742
const {container} = renderQuestion(segmentWithLockedPolygons, {

packages/perseus/src/widgets/interactive-graphs/locked-figures/locked-ellipse.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {color as wbColor} from "@khanacademy/wonder-blocks-tokens";
12
import {Ellipse} from "mafs";
23
import * as React from "react";
34

@@ -18,6 +19,17 @@ const LockedEllipse = (props: LockedEllipseType) => {
1819
fillOpacity={lockedFigureFillStyles[fillStyle]}
1920
strokeStyle={strokeStyle}
2021
color={lockedFigureColors[color]}
22+
// We need to override the svg props if we want to have a
23+
// different fill color than the stroke color (specifically,
24+
// in the case where the fillStyle is "white").
25+
svgEllipseProps={{
26+
style: {
27+
fill:
28+
fillStyle === "white"
29+
? wbColor.white
30+
: lockedFigureColors[color],
31+
},
32+
}}
2133
/>
2234
);
2335
};

packages/perseus/src/widgets/interactive-graphs/locked-figures/locked-polygon.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {color as wbColor} from "@khanacademy/wonder-blocks-tokens";
12
import {Point, Polygon} from "mafs";
23
import * as React from "react";
34

@@ -19,6 +20,17 @@ const LockedPolygon = (props: LockedPolygonType) => {
1920
fillOpacity={lockedFigureFillStyles[fillStyle]}
2021
strokeStyle={strokeStyle}
2122
color={lockedFigureColors[color]}
23+
// We need to override the svg props if we want to have a
24+
// different fill color than the stroke color (specifically,
25+
// in the case where the fillStyle is "white").
26+
svgPolygonProps={{
27+
style: {
28+
fill:
29+
fillStyle === "white"
30+
? wbColor.white
31+
: lockedFigureColors[color],
32+
},
33+
}}
2234
/>
2335
{showVertices &&
2436
points.map((point, index) => (

0 commit comments

Comments
 (0)