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

[Bugfix] Zoomable content within a collapsed parent not showing when expanded (Mobile only) #2185

Merged
Show file tree
Hide file tree
Changes from 9 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/healthy-hairs-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

[Bugfix] Zoomable content within a collapsed parent not showing when expanded (Mobile only)
21 changes: 19 additions & 2 deletions packages/perseus/src/components/zoomable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,26 @@ class Zoomable extends React.Component<Props, State> {
if (!this._isMounted) {
return;
}

// When a zoomable item is contained within a collapsed parent (such as an Explanation widget),
// the parent node has zero width, which causes the scale to be miscalculated.
// Therefore, we check to see if the node has a valid width, and if it doesn't,
// then we traverse up the DOM tree and look for the closest "perseus-renderer"
// (which should have a width).
// To be cautious, we also account for the possibility that we can't find any parents with a valid width
// (not that they don't exist, just that we can't find them).
let parentNode = this._node;
let currentNode: HTMLElement | null = parentNode;
while (currentNode && currentNode.offsetWidth === 0) {
currentNode =
currentNode.parentElement?.closest(".perseus-renderer") ?? null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to access parentElement here before calling .closest()? If we don't need to, then closest() would return and would alleviate us from having to use the bullish coalescing operator on the end also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

closest() also checks the element on which it is called. So, when we find an ancestor with .perseus-renderer, if we need to do another call for closest(), we would end up with the same element, resulting in an infinite loop (speaking from personal experience, unfortunately). Referencing the parent element ensures that we continue traversing up the DOM tree.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ouch! Can you note that in a comment? That was the first thing I wondered and would probably think, "I'll just simplify this code some by refactoring this... " 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks!

}
if (currentNode) {
parentNode = currentNode;
}
const parentBounds = {
width: this._node.offsetWidth,
height: this._node.offsetHeight,
width: parentNode.offsetWidth,
height: parentNode.offsetHeight,
} as const;
const childBounds = this.props.computeChildBounds(
this._node,
Expand Down