Skip to content

Commit

Permalink
Merge ab4abd4 into e497062
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmax committed Oct 17, 2022
2 parents e497062 + ab4abd4 commit 8df4369
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 31 deletions.
20 changes: 18 additions & 2 deletions packages/visx-bounds/src/enhancers/withBoundingRects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ export default function withBoundingRects<Props extends object = {}>(
) {
return class WrappedComponent extends React.PureComponent<Props> {
static displayName = `withBoundingRects(${BaseComponent.displayName || ''})`;

node: HTMLElement | undefined | null;
nodeRef: React.MutableRefObject<HTMLElement | undefined | null>;

constructor(props: Props) {
super(props);
this.nodeRef = React.createRef<HTMLInputElement | null>();

this.state = {
rect: undefined,
parentRect: undefined,
Expand All @@ -42,7 +47,9 @@ export default function withBoundingRects<Props extends object = {}>(
}

componentDidMount() {
this.node = ReactDOM.findDOMNode(this) as HTMLElement;
this.node = this.nodeRef.current
? this.nodeRef.current
: (ReactDOM.findDOMNode(this) as HTMLElement);
this.setState(() => this.getRects());
}

Expand All @@ -62,7 +69,16 @@ export default function withBoundingRects<Props extends object = {}>(
}

render() {
return <BaseComponent getRects={this.getRects} {...this.state} {...this.props} />;
return (
<BaseComponent
getRects={this.getRects}
{...this.state}
{...this.props}
nodeRef={(params: React.RefObject<HTMLElement> | null) => {
this.nodeRef.current = params?.current;
}}
/>
);
}
};
}
66 changes: 39 additions & 27 deletions packages/visx-tooltip/src/tooltips/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,42 @@ export const defaultStyles: React.CSSProperties = {
pointerEvents: 'none',
};

export default function Tooltip({
className,
top,
left,
offsetLeft = 10,
offsetTop = 10,
style = defaultStyles,
children,
unstyled = false,
applyPositionStyle = false,
...restProps
}: TooltipProps & React.HTMLProps<HTMLDivElement>) {
return (
<div
className={cx('visx-tooltip', className)}
style={{
top: top == null || offsetTop == null ? top : top + offsetTop,
left: left == null || offsetLeft == null ? left : left + offsetLeft,
...(applyPositionStyle && { position: 'absolute' }),
...(!unstyled && style),
}}
{...restProps}
>
{children}
</div>
);
}
const Tooltip = React.forwardRef<
HTMLDivElement,
TooltipProps & React.HTMLAttributes<HTMLDivElement>
>(
(
{
className,
top,
left,
offsetLeft = 10,
offsetTop = 10,
style = defaultStyles,
children,
unstyled = false,
applyPositionStyle = false,
...restProps
},
ref,
) => {
return (
<div
ref={ref}
className={cx('visx-tooltip', className)}
style={{
top: top == null || offsetTop == null ? top : top + offsetTop,
left: left == null || offsetLeft == null ? left : left + offsetLeft,
...(applyPositionStyle && { position: 'absolute' }),
...(!unstyled && style),
}}
{...restProps}
>
{children}
</div>
);
},
);

Tooltip.displayName = 'Tooltip';
export default Tooltip;
7 changes: 5 additions & 2 deletions packages/visx-tooltip/src/tooltips/TooltipWithBounds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Tooltip, { TooltipProps, defaultStyles } from './Tooltip';
import { TooltipPositionProvider } from '../context/TooltipPositionContext';

export type TooltipWithBoundsProps = TooltipProps &
React.HTMLProps<HTMLDivElement> &
WithBoundingRectsProps;
React.HTMLAttributes<HTMLDivElement> &
WithBoundingRectsProps & { nodeRef?: React.Ref<HTMLDivElement> };

function TooltipWithBounds({
children,
Expand All @@ -19,6 +19,8 @@ function TooltipWithBounds({
style = defaultStyles,
top: initialTop = 0,
unstyled = false,
nodeRef,

...otherProps
}: TooltipWithBoundsProps) {
let transform: React.CSSProperties['transform'];
Expand Down Expand Up @@ -61,6 +63,7 @@ function TooltipWithBounds({

return (
<Tooltip
ref={nodeRef}
style={{
left: 0,
top: 0,
Expand Down

0 comments on commit 8df4369

Please sign in to comment.