Skip to content

Commit

Permalink
Merge pull request #721 from dennisja/fix-tooltip-with-bounds
Browse files Browse the repository at this point in the history
Fix tooltip with bounds
  • Loading branch information
williaster authored May 29, 2020
2 parents 074d5ff + 619a003 commit 5fb35e1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/vx-tooltip/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ accepts the following props, and will spread any additional props on the tooltip
| className | string | -- | Adds a class (in addition to `vx-tooltip-portal`) to the tooltip container |
| style | object | -- | Sets / overrides any styles on the tooltip container (including top and left) |
| children | node | -- | Sets the children of the tooltip, i.e., the actual content |
| unstyled | bool | true | Whether the tooltip use styles from the style prop or not |
| unstyled | bool | true | Whether the tooltip should use styles from the style prop or not |

#### TooltipWithBounds

Expand All @@ -92,6 +92,7 @@ component (i.e., ...restProps):
| offsetRight | number | 10 | Vertical offset of the tooltip from the passed `top` value, functions as a vertical padding. |
| style | object | -- | Sets / overrides any styles on the tooltip container (including top and left) |
| children | node | -- | Sets the children of the tooltip, i.e., the actual content |
| unstyled | bool | true | Whether the tooltip should use styles from the style prop or not |

Note that this component is positioned using a `transform`, so overriding `left` and `top` via
styles may have no effect.
Expand Down
20 changes: 9 additions & 11 deletions packages/vx-tooltip/src/tooltips/TooltipWithBounds.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { withBoundingRects } from '@vx/bounds';

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

type RectShape = {
top: number;
Expand All @@ -18,14 +18,6 @@ type WithBoundingRectsProps = {
parentRect?: RectShape;
};

type TooltipProps = {
left?: number;
top?: number;
className?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
};

type Props = {
offsetLeft?: number;
offsetTop?: number;
Expand All @@ -41,7 +33,8 @@ function TooltipWithBounds({
parentRect,
getRects,
children,
style,
style = defaultStyles,
unstyled = false,
...otherProps
}: Props) {
let left = initialLeft;
Expand All @@ -64,7 +57,12 @@ function TooltipWithBounds({

return (
<Tooltip
style={{ top: 0, transform: `translate(${left}px, ${top}px)`, ...style }}
style={{
top: 0,
transform: `translate(${left}px, ${top}px)`,
...(!unstyled && style),
}}
unstyled={unstyled}
{...otherProps}
>
{children}
Expand Down
24 changes: 23 additions & 1 deletion packages/vx-tooltip/test/TooltipWithBounds.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import { TooltipWithBounds } from '../src';
import React from 'react';
import { shallow } from 'enzyme';
import { TooltipWithBounds, defaultStyles } from '../src';

describe('<TooltipWithBounds />', () => {
test('it should be defined', () => {
expect(TooltipWithBounds).toBeDefined();
});

it('should render the Tooltip with default styles by default', () => {
const wrapper = shallow(<TooltipWithBounds>Hello</TooltipWithBounds>, {
disableLifecycleMethods: true,
}).dive();
const styles = wrapper.find('Tooltip').props().style as any;
Object.entries(defaultStyles).forEach(([key, value]) => {
expect(styles[key]).toBe(value);
});
});

it('should render the tooltip without default styles if unstyled is set to true', () => {
const wrapper = shallow(<TooltipWithBounds unstyled>Hello</TooltipWithBounds>, {
disableLifecycleMethods: true,
}).dive();
const styles = wrapper.find('Tooltip').props().style as any;
Object.keys(defaultStyles).forEach(key => {
expect(styles[key]).toBeUndefined();
});
});
});

0 comments on commit 5fb35e1

Please sign in to comment.