Skip to content

Commit

Permalink
fix(tooltip): fallback to left/top positioning, add applyPositionStyle (
Browse files Browse the repository at this point in the history
#857)

* fix(tooltip): fallback to left/top positioning, add applyPositionStyle

* copy(tooltip): fix prop type annotation

* fix(tooltip): remove optional null style type, add other prop annotations
  • Loading branch information
williaster authored Oct 12, 2020
1 parent 03f33ca commit 72dfcf2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
34 changes: 26 additions & 8 deletions packages/visx-tooltip/src/tooltips/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@ import React from 'react';
import cx from 'classnames';

export type TooltipProps = {
/** Tooltip content. */
children?: React.ReactNode;
/** Optional className to apply to the Tooltip in addition to `visx-tooltip`. */
className?: string;
/** The `left` position of the Tooltip. */
left?: number;
top?: number;
/** Offset the `left` position of the Tooltip by this margin. */
offsetLeft?: number;
/** Offset the `top` position of the Tooltip by this margin. */
offsetTop?: number;
className?: string;
/** Styles to apply, unless `unstyled=true`. */
style?: React.CSSProperties;
children?: React.ReactNode;
/** The `top` position of the Tooltip. */
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 +52,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,
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

0 comments on commit 72dfcf2

Please sign in to comment.