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

fix(interaction): 🐛 create conditional Xplacement based on container width #1268

Merged
merged 1 commit into from
Nov 15, 2023

Conversation

lloydrichards
Copy link
Collaborator

@lloydrichards lloydrichards commented Nov 14, 2023

The Mouse Over Box should appear on the right Side so as not to be cut off. The Problem appears only by embedding in AEM or iFrame. The URL to share is ok : https://visualize.admin.ch/en/v/uCDwLYAzMW5c

image

#1266

Goals/Scope

After investigating it seems the bug is cased on small containers where the tooltip size is larger then the allotted space. Since larger screens still work, I looked to create a solution that could conditionally place the tooltip based on the charts size rather then measuring the window.

Description

Created a fold function that accepts the chartWidth and returns a function that can be used to define the behaviour of the xPlacement depending on that size. Here we can define the xs (smallest) and md (@breakpoint 600) to define the mobile-first behaviours.

Comments

I'm not sure how to test the embedded links without deploying this to INT, any suggestions are welcome in the comments 👇

Copy link

vercel bot commented Nov 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
visualization-tool ✅ Ready (Inspect) Visit Preview Nov 14, 2023 1:29pm

Copy link
Collaborator

@bprusinowski bprusinowski left a comment

Choose a reason for hiding this comment

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

LGTM!

I think there's no easy way to preview embedded charts from deployment previews, but – there are two small-ish charts on the homepage that seem to work correctly (50% of chart width as a breakpoint) 🎉

app/charts/shared/interaction/tooltip-box.tsx Show resolved Hide resolved
x: foldContainerSize<Xplacement>(chartWidth)({
xs: (w) => (xAnchor < w * 0.5 ? "right" : "left"),
md: (w) => (xAnchor < w * 0.25 ? "right" : "left"),
}),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like the logic is a bit brittle, we are never sure that this will work correctly right & we must rely on the > 0.5 & 0.25 heuristics. For me it would be better the Tooltip from MUI here.

  • It is already included in the bundle (see datatable file)
  • It will correctly check (through Popper) the boundary of the container and position the Tooltip accordingly
  • It would allow us to remove a lof the code here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

my resistance to replacing the Tooltip with the MUI version is that the two APIs work very differently with the MUI wrapping the target, and the current one taking the position from the state of useInteraction(). I think this is mostly in order to be able to follow the cursor in some instances, and in others to lock to something like a bar or point in a chart.

Seeing as the Tooltip is used in all 13 chart types with multiple variants and conditional logic, I think the effort to replace all the behaviours with the MUI version will take considerable effort (especially from someone new to the project)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair point, you're right 👍

In the future, I would guess but not sure that we could make either Popper or Tooltip work directly maybe without wrapping.

Nit: For what it's worth, I had trouble understanding the fold code at first glance. I find that we could simplify the logic a bit.

const isSmall = containerWidth < 600;
const leftThreshold = (isSmall ? 0.5 : 0.25) * containerWidth
const placement = xAnchor < leftThreshold ? 'right' : 'left'

Copy link
Collaborator Author

@lloydrichards lloydrichards Nov 15, 2023

Choose a reason for hiding this comment

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

i agree that when its a binary option like at the moment with small and larger screens then the fold seems excessive, but I want to make sure the solution is robust, so in the future (or if there needs to be edge cases) then its possible to add more options without the need for a switch:

x: foldContainerSize<Xplacement>(chartWidth)({
  xs: (w) => (xAnchor < w * 0.5 ? "right" : "left"), // places tooltip on opposite side of screen
  md: (w) => (xAnchor < w * 0.25 ? "right" : "left"),  // on medium size charts, places based on 1/4 ratio
  lg: (w) => "right", // on large size charts, places always on right
  }),

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, fair and my point is a nit. I think that shaping the code so that breakpoints (especially more than 2) are used in the future should not be a goal, as usually I find breakpoints make things more brittle than sturdy.

@lloydrichards lloydrichards merged commit d4947ff into main Nov 15, 2023
4 of 6 checks passed
@lloydrichards lloydrichards deleted the fix/tooltip-small-screens branch November 15, 2023 21:00
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.

3 participants