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(tooltip): fallback to left/top positioning, add applyPositionStyle #857

Merged
merged 3 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 21 additions & 9 deletions packages/visx-tooltip/src/tooltips/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ import React from 'react';
import cx from 'classnames';

export type TooltipProps = {
children?: React.ReactNode;
Copy link
Collaborator Author

@williaster williaster Oct 8, 2020

Choose a reason for hiding this comment

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

abc ordering

className?: string;
left?: number;
top?: number;
offsetLeft?: number;
offsetTop?: number;
className?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
/** Styles to apply, unless `unstyled=true`. */
style?: React.CSSProperties | null;
top?: number;
/**
* Applies position: 'absolute' for tooltips to correctly position themselves
* when `unstyled=true`. In a future major release this will be the default behavior.
*/
applyPositionStyle?: boolean;
/**
* Whether to omit applying any style, except `left` / `top`.
* In most cases if this is `true` a developer must do one of the following
* for positioning to work correctly:
* - set `applyPositionStyle=true`
* - create a CSS selector like: `.visx-tooltip { position: 'absolute' }`
*/
unstyled?: boolean;
};

Expand All @@ -33,17 +46,16 @@ export default function Tooltip({
style = defaultStyles,
children,
unstyled = false,
applyPositionStyle = false,
...restProps
}: TooltipProps & React.HTMLProps<HTMLDivElement>) {
return (
<div
className={cx('visx-tooltip', className)}
style={{
left: 0,
top: 0,
transform: `translate(${
left == null || offsetLeft == null ? left ?? 0 : left + offsetLeft
}px, ${top == null || offsetTop == null ? top ?? 0 : top + offsetTop}px)`,
top: top == null || offsetTop == null ? top : top + offsetTop,
left: left == null || offsetLeft == null ? left : left + offsetLeft,
...(applyPositionStyle && { position: 'absolute' }),
...(!unstyled && style),
}}
{...restProps}
Expand Down
25 changes: 11 additions & 14 deletions packages/visx-tooltip/src/tooltips/TooltipWithBounds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import { withBoundingRects, WithBoundingRectsProps } from '@visx/bounds';

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

export type TooltipWithBoundsProps = {
offsetLeft?: number;
offsetTop?: number;
} & TooltipProps &
export type TooltipWithBoundsProps = TooltipProps &
React.HTMLProps<HTMLDivElement> &
WithBoundingRectsProps;

function TooltipWithBounds({
children,
Copy link
Collaborator Author

@williaster williaster Oct 8, 2020

Choose a reason for hiding this comment

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

abc ordering

Copy link
Collaborator

Choose a reason for hiding this comment

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

what is abc?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sorry updated

getRects,
left: initialLeft = 0,
top: initialTop = 0,
offsetLeft = 10,
offsetTop = 10,
children,
rect: ownBounds,
parentRect: parentBounds,
getRects,
rect: ownBounds,
style = defaultStyles,
top: initialTop = 0,
unstyled = false,
...otherProps
}: TooltipWithBoundsProps) {
Expand All @@ -44,12 +41,12 @@ function TooltipWithBounds({

return (
<Tooltip
top={top}
left={left}
offsetTop={0}
offsetLeft={0}
style={style}
unstyled={unstyled}
style={{
left: 0,
top: 0,
transform: `translate(${left}px, ${top}px)`,
...(!unstyled && style),
}}
{...otherProps}
>
{children}
Expand Down
7 changes: 2 additions & 5 deletions packages/visx-tooltip/test/TooltipWithBounds.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from 'react';
import { shallow } from 'enzyme';
import { TooltipWithBounds, defaultStyles } from '../src';
Expand All @@ -21,11 +22,7 @@ describe('<TooltipWithBounds />', () => {
const wrapper = shallow(<TooltipWithBounds unstyled>Hello</TooltipWithBounds>, {
disableLifecycleMethods: true,
}).dive();
const styles = wrapper
.find('Tooltip')
.dive()
.find('.visx-tooltip')
.props().style as any;
const styles = wrapper.find('Tooltip').props().style as any;
Object.keys(defaultStyles).forEach(key => {
expect(styles[key]).toBeUndefined();
});
Expand Down