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(demo/barstack): improve Tooltip positioning logic #1018

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 13 additions & 10 deletions packages/visx-demo/src/sandboxes/visx-barstack/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { scaleBand, scaleLinear, scaleOrdinal } from '@visx/scale';
import { timeParse, timeFormat } from 'd3-time-format';
import { useTooltip, useTooltipInPortal, defaultStyles } from '@visx/tooltip';
import { LegendOrdinal } from '@visx/legend';
import { localPoint } from '@visx/event';

type CityName = 'New York' | 'San Francisco' | 'Austin';

Expand Down Expand Up @@ -92,7 +93,12 @@ export default function Example({
showTooltip,
} = useTooltip<TooltipData>();

const { containerRef, TooltipInPortal } = useTooltipInPortal();
const { containerRef, TooltipInPortal } = useTooltipInPortal({
// TooltipInPortal is rendered in a separate child of <body /> and positioned
// with page coordinates which should be updated on scroll. consider using
// Tooltip or TooltipWithBounds if you don't need to render inside a Portal
scroll: true,
});

if (width < 10) return null;
// bounds
Expand All @@ -103,7 +109,6 @@ export default function Example({
temperatureScale.range([yMax, 0]);

return width < 10 ? null : (
// relative position is needed for correct tooltip positioning
<div style={{ position: 'relative' }}>
<svg ref={containerRef} width={width} height={height}>
<rect x={0} y={0} width={width} height={height} fill={background} rx={14} />
Expand Down Expand Up @@ -147,11 +152,14 @@ export default function Example({
}}
onMouseMove={event => {
if (tooltipTimeout) clearTimeout(tooltipTimeout);
const top = event.clientY - margin.top - bar.height;
// TooltipInPortal expects coordinates to be relative to containerRef
// localPoint returns coordinates relative to the nearest SVG, which
// is what containerRef is set to in this example.
const eventSvgCoords = localPoint(event);
const left = bar.x + bar.width / 2;
showTooltip({
tooltipData: bar,
tooltipTop: top,
tooltipTop: eventSvgCoords?.y,
tooltipLeft: left,
});
}}
Expand Down Expand Up @@ -188,12 +196,7 @@ export default function Example({
</div>

{tooltipOpen && tooltipData && (
<TooltipInPortal
key={Math.random()} // update tooltip bounds each render
top={tooltipTop}
left={tooltipLeft}
style={tooltipStyles}
>
<TooltipInPortal top={tooltipTop} left={tooltipLeft} style={tooltipStyles}>
<div style={{ color: colorScale(tooltipData.key) }}>
<strong>{tooltipData.key}</strong>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@types/react": "^16",
"@types/react-dom": "^16",
"@visx/axis": "latest",
"@visx/event": "latest",
"@visx/grid": "latest",
"@visx/group": "latest",
"@visx/legend": "latest",
Expand Down