3.3.0 • Published 10 months ago

@visx/bounds v3.3.0

Weekly downloads
17,787
License
MIT
Repository
github
Last release
10 months ago

@visx/bounds

npm install --save @visx/bounds

withBoundingRects HOC

It's often useful to determine whether elements (e.g., tooltips) overflow the bounds of their parent container and adjust positioning accordingly. The withBoundingRects higher-order component is meant to simplify this computation by passing in a component's bounding rect as well as its parent's bounding rect.

Example usage

Example usage with a <Tooltip /> component

import React from 'react';
import PropTypes from 'prop-types';
import { withBoundingRects, withBoundingRectsProps } from '@visx/bounds';

const propTypes = {
  ...withBoundingRectsProps,
  left: PropTypes.number.isRequired,
  top: PropTypes.number.isRequired,
  children: PropTypes.node,
};

const defaultProps = {
  children: null,
};

function Tooltip({ left: initialLeft, top: initialTop, rect, parentRect, children }) {
  let left = initialLeft;
  let top = initialTop;

  if (rect && parentRect) {
    left = rect.right > parentRect.right ? left - rect.width : left;
    top = rect.bottom > parentRect.bottom ? top - rect.height : top;
  }

  return <div style={{ top, left, ...myTheme }}>{children}</div>;
}

Tooltip.propTypes = propTypes;
Tooltip.defaultProps = defaultProps;

export default withBoundingRects(Tooltip);
3.3.0

10 months ago

2.17.0

1 year ago

3.0.0

1 year ago

2.16.0

2 years ago

2.17.0-alpha.0

1 year ago

2.10.0-alpha.0

2 years ago

2.10.0

2 years ago

2.1.2

3 years ago

2.1.0

3 years ago

2.0.1-alpha.0

3 years ago

3.0.0-alpha.0

3 years ago

1.7.0

3 years ago

1.6.1

3 years ago

1.0.0

4 years ago

0.0.200-alpha.0

4 years ago