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

new(vx-tooltip): add Portal + demo #755

Merged
merged 23 commits into from Jun 27, 2020
Merged

new(vx-tooltip): add Portal + demo #755

merged 23 commits into from Jun 27, 2020

Conversation

williaster
Copy link
Collaborator

🚀 Enhancements

📝 Documentation

This PR

  • adds a new Portal component to @vx/tooltip
  • adds ability to set a default tooltip state in useTooltip
  • sets left: 0 by default on TooltipWithBounds which was needed for correct positioning within a Portal
  • adds a new gallery demo /tooltip, which demos the boundary detection + Portal usage
    • required adding react-use-measure dependency in vx-demo)
    • added to Gallery, and to @vx/tooltip examples

Testing

  • tested example on mobile screen size / with touch events
  • tooltip position is correct in demo, in gallery, and in @vx/tooltip docs; note: portal does require additional calculation complexity, but it's not too bad. Hopefully the demo will be good for showing people how to do this.

Without Portal

With Portal

Gif
tooltip-portal

In docs
image

In gallery
image

@hshoff @kristw @techniq


import Tooltip, { TooltipProps, defaultStyles } from './Tooltip';

type RectShape = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

these were written before the @vx/bounds types were added

@williaster williaster mentioned this pull request Jun 18, 2020
33 tasks
@@ -0,0 +1,244 @@
import React, { useState, useCallback } from 'react';
import useMeasure from 'react-use-measure';
import { Tooltip, TooltipWithBounds, useTooltip, defaultStyles, Portal } from '@vx/tooltip/src';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reminder to update src => lib

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is fixed in #756 which set to merge into this branch

Copy link
Collaborator

@kristw kristw left a comment

Choose a reason for hiding this comment

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

small comment. overall lgtm


componentWillUnmount() {
if (this.node && document.body) {
document.body.removeChild(this.node);
Copy link
Collaborator

Choose a reason for hiding this comment

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

can also do this.node.remove()

/>
<TooltipWrapper>
<TooltipComponent
key={Math.random()} // needed for bounds to update correctly
Copy link
Member

Choose a reason for hiding this comment

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

Keys should be stable, predictable, and unique. Unstable keys (like those produced by Math.random()) will cause many component instances and DOM nodes to be unnecessarily recreated, which can cause performance degradation and lost state in child components.
https://reactjs.org/docs/reconciliation.html#trade-offs

Does it have to be recreated all the time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah I think unfortunately for now, we do this in most of our demos. This forces a re-mount (😱) to force-update the bounds the component has, which basically change on every mouse move. Without this, if you start the tooltip on the left of the chart and move it to the right edge, it still thinks it has room because it's using the initial bounds.

This really can't be that great for perf because of the remount and because calling .getBoundingClientRect() forces layout/reflow. I tried to switch to use-measure for the boundary detection, but it only uses scroll listeners + ResizeObserver, so it doesn't re-compute on mouse move.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

IntersectionObserver seems promising. If react-use-measure doesn't add something, we might be able to only update the key when the observer fires... to force an update.

I initially thought we could pre-compute the bounds based on the parent and tooltip size, but sinse the tooltip size can change size based on the contents, this wouldn't work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I initially thought we could pre-compute the bounds based on the parent and tooltip size, but since the tooltip size can change size based on the contents, this wouldn't work.

ah this is interesting, currently we use the bounds.left/top attributes of the tooltip, rather than bounds.width/height (which should be more stable). I took a shot at updating to use the latter and not updating key, but hit issues in the portal where width/height of the parent can be 0 😭

@williaster
Copy link
Collaborator Author

gonna merge this to wrap it up, happy to address any concerns in a followup PR

@williaster williaster merged commit 2c46c47 into master Jun 27, 2020
@williaster williaster deleted the chris--tooltip-portal branch June 27, 2020 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants