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

Implementation of new nested SVGs and Tick Labels for Interactive Graph to solve Safari rendering issues. #1452

Merged
merged 27 commits into from
Sep 11, 2024

Conversation

SonicScrewdriver
Copy link
Contributor

@SonicScrewdriver SonicScrewdriver commented Jul 25, 2024

Screenshot:

Screenshot 2024-09-05 at 3 29 59 PM

Summary:

This PR introduces a new approach to solving the issues related to re-rendering Axis Ticks Labels in Safari, along with several other related issues related to SVG rendering in Safari.

We are now overriding the overflow rules from Mafs in order to allow our Axis Tick Labels to render outside the bounds of the cartesian grid. This required the addition of two nested SVG elements in order to contain the interactive figures, locked figures, and protractor within the new graph bounds. The elements are bound to the graph edges by adjusting the viewBox attribute on the nested SVGs according to the graph's range.

While researching this issue, we were able to discover that there's a key issue with the MathJax font itself, which has required us to change the Axis Tick Labels back to using the original KaTeX font.

Several stories were updated or added in the Interactive Graph Regression Tests in order to help protect against regressions for key issues related to off-center ranges with locked figures.

This approach also allowed us to reintroduce the text stroke around the axis tick labels in order to improve general legibility.

History:

This nested SVG approach has become necessary in order to solve numerous reported issues related to the rendering of the axis tick labels when using earlier versions of Safari (14-16). Unfortunately, Safari has many long standing bugs related to re-rendering SVGs, particularly when dealing with absolutely positioned elements or more modern features.

Issue: LEMS-2035

Test plan:

  • Significant manual testing using new Interactive Graph Regression Tests in Storybook
  • Several rounds of Play testing
  • Manually testing in Webapp on a ZND

@SonicScrewdriver SonicScrewdriver self-assigned this Jul 25, 2024
@SonicScrewdriver SonicScrewdriver changed the title Testing prototype of new SVG axis tick labels + clipping mask [Prototype] Testing new SVG labels + clipping mask in Safari Jul 25, 2024
@SonicScrewdriver SonicScrewdriver force-pushed the the-grid-behind-the-mask branch from 28e4835 to d021668 Compare July 25, 2024 23:09
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Jul 25, 2024

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/wet-students-float.md, packages/perseus/src/widgets/interactive-graphs/interactive-graph-regression.stories.tsx, packages/perseus/src/widgets/interactive-graphs/mafs-graph.test.tsx, packages/perseus/src/widgets/interactive-graphs/mafs-graph.tsx, packages/perseus/src/widgets/interactive-graphs/mafs-styles.css, packages/perseus/src/widgets/interactive-graphs/backgrounds/axis-arrows.tsx, packages/perseus/src/widgets/interactive-graphs/backgrounds/axis-ticks.test.ts, packages/perseus/src/widgets/interactive-graphs/backgrounds/axis-ticks.tsx, packages/perseus/src/widgets/interactive-graphs/backgrounds/grid.tsx

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 July 25, 2024 23:09
Copy link
Contributor

github-actions bot commented Jul 25, 2024

npm Snapshot: Published

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

Example:

yarn add @khanacademy/perseus@PR1452

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

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

Copy link
Contributor

github-actions bot commented Jul 25, 2024

Size Change: -1.29 kB (-0.15%)

Total Size: 859 kB

Filename Size Change
packages/perseus-editor/dist/es/index.js 277 kB -375 B (-0.14%)
packages/perseus/dist/es/index.js 416 kB -911 B (-0.22%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38.3 kB
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/index.js 78 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 1.48 kB
packages/perseus-linter/dist/es/index.js 22.2 kB
packages/perseus/dist/es/strings.js 3.36 kB
packages/pure-markdown/dist/es/index.js 3.66 kB
packages/simple-markdown/dist/es/index.js 12.4 kB

compressed-size-action

@SonicScrewdriver SonicScrewdriver force-pushed the the-grid-behind-the-mask branch from a82a5a6 to b04aca0 Compare August 15, 2024 20:12
@SonicScrewdriver SonicScrewdriver force-pushed the the-grid-behind-the-mask branch from f9cec28 to bc29621 Compare August 23, 2024 19:33
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file is no longer needed, as the labels were able to be reintegrated with the axis-ticks themselves.

@SonicScrewdriver SonicScrewdriver force-pushed the the-grid-behind-the-mask branch from b283d18 to 8a8d3ce Compare August 27, 2024 23:05
Copy link

codecov bot commented Aug 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.04%. Comparing base (bdb382f) to head (2b2e503).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1452      +/-   ##
==========================================
- Coverage   70.52%   70.04%   -0.48%     
==========================================
  Files         548      572      +24     
  Lines      107317   111560    +4243     
  Branches     7827    11819    +3992     
==========================================
+ Hits        75687    78146    +2459     
- Misses      31514    33414    +1900     
+ Partials      116        0     -116     

Impacted file tree graph

see 1119 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 bdb382f...2b2e503. Read the comment docs.

@SonicScrewdriver SonicScrewdriver force-pushed the the-grid-behind-the-mask branch from 8d1eb80 to cde544d Compare August 28, 2024 21:35
@@ -214,53 +216,9 @@
.MafsView pattern g {
stroke: rgba(33, 36, 44, 0.32);
}
.axis-tick-labels {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of these styles are no longer needed as we're using attributes to ensure proper compatibility with older versions of Safari.

const isMax = n === max;
const shouldRender = isOnStep && !isNegativeOne && !isMin && !isMax;
return shouldRender ? `${n}` : "";
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was already unused prior to these changes, and is safe to delete.

@@ -1,72 +0,0 @@
import {lineLabelText} from "./grid";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is testing a function that was not being used, and has been deleted as part of this PR.

@SonicScrewdriver SonicScrewdriver changed the title [Prototype] Testing new SVG labels + clipping mask in Safari Implementation of new nested SVGs and labels for Interactive Graph to solve Safari rendering issues. Sep 5, 2024
@SonicScrewdriver SonicScrewdriver changed the title Implementation of new nested SVGs and labels for Interactive Graph to solve Safari rendering issues. Implementation of new nested SVGs and Tick Labels for Interactive Graph to solve Safari rendering issues. Sep 5, 2024
<>
<AxisTicks />
<AxisArrows />
</>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both the Axis Ticks and Axis Arrows need to be allowed to render outside of the bounds of the graph, but ALSO need to render below the protractor/interactive/locked figures AND above the gridlines.

Unfortunately this requires the use of two separate nested SVGs so that we can render these elements in the correct order.

const tickSize = 10;
const tickLabelSize = 14;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The font size is important for calculating positioning, so we're handling it within the component.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file is no longer necessary, as we're pairing the ticks and labels back together within the SVG.

if (range[X][MIN] < -1 && range[X][MAX] > 0 && number === -1) {
showLabel = false;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I split this into a separate function as I had a feeling that we may come up with more scenarios in the future for when we may want to conditionally hide labels. It also helps keep the code a little more organized.

@@ -84,7 +102,7 @@ export const MafsGraph = (props: MafsGraphProps) => {
padding: "25px 25px 0 0",
boxSizing: "content-box",
marginLeft: "20px",
marginBottom: "20px",
marginBottom: "30px",
pointerEvents: props.static ? "none" : "auto",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We needed a little more breathing room for the labels now that they're within the SVG

// whether the x-axis is above, within, or below the graph
const xAdjustment =
range[X][MAX] <= 0 ? tickLabelSize * 1.5 : -tickLabelSize * 1.1;
const xPositionText = xPosition + xAdjustment;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not a big fan of using 1.1 here, but I found it was the best value to visually line up the labels for our most used gridStep/ranges

// Adjust the X position of the x-axis labels based on
// whether the label is positive or negative, in order to
// account for the width of the negative sign
const xAdjustment = x < 0 ? -2 : 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Magic number fix?


.MafsView .axis-tick-label {
/* There is a bug in the MathJax font for Safari, so we use KaTeX */
font-family: "KaTeX_Main";
Copy link
Contributor

Choose a reason for hiding this comment

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

Specify a fallback font


.MafsView .axis-tick-label {
/* There is a bug in the MathJax font for Safari, so we use KaTeX */
font-family: "KaTeX_Main";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we use mathjax for all fonts EXCEPT the one media query with safari to use KaTeX

@SonicScrewdriver SonicScrewdriver merged commit 3980a36 into main Sep 11, 2024
9 of 10 checks passed
@SonicScrewdriver SonicScrewdriver deleted the the-grid-behind-the-mask branch September 11, 2024 17:44
SonicScrewdriver added a commit that referenced this pull request Sep 11, 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/[email protected]

### Minor Changes

- [#1452](#1452)
[`3980a36fa`](3980a36)
Thanks [@SonicScrewdriver](https://github.com/SonicScrewdriver)! -
Implementation of SVG-based Axis Tick Labels for Interactive Graph

### Patch Changes

- [#1609](#1609)
[`981047211`](9810472)
Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph]
Remove the start-coords-ui flags


- [#1610](#1610)
[`e9b317ca0`](e9b317c)
Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph
Editor] Remove the start coords UI if the graph is static


- [#1608](#1608)
[`737fe30b4`](737fe30)
Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph]
Remove the interactive-graph-locked-feature-m2b flag

## @khanacademy/[email protected]

### Patch Changes

- [#1609](#1609)
[`981047211`](9810472)
Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph]
Remove the start-coords-ui flags


- [#1607](#1607)
[`1b340b197`](1b340b1)
Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph
Editor] Use Wonder Blocks TextArea in the graph description settings UI


- [#1610](#1610)
[`e9b317ca0`](e9b317c)
Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph
Editor] Remove the start coords UI if the graph is static


- [#1608](#1608)
[`737fe30b4`](737fe30)
Thanks [@nishasy](https://github.com/nishasy)! - [Interactive Graph]
Remove the interactive-graph-locked-feature-m2b flag

- Updated dependencies
\[[`981047211`](9810472),
[`e9b317ca0`](e9b317c),
[`737fe30b4`](737fe30),
[`3980a36fa`](3980a36)]:
    -   @khanacademy/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants