npm.io
2.0.0 • Published 18h ago

react-viewport-height

Licence
MIT
Version
2.0.0
Deps
0
Size
9 kB
Vulns
0
Weekly
0
Stars
10

react-viewport-height

npm Version npm downloads per week Unpacked size Open issues Open pull requests GitHub Stars CI status Code scanning

A React hook that returns the size of one vh unit, 1% of the browser's inner window height, and keeps it up to date when the window resizes.

The package is maintained, but for new code, prefer the standard alternatives:

  • To size elements, use the CSS dvh, svh, and lvh units. They follow the actual viewport height on mobile browsers and are widely supported. Read more about the new units.
  • To use the height in JavaScript, use useWindowSize from usehooks-ts or react-use. Both return the window's inner width and height, so useWindowSize().height / 100 is the value that useVH returns.

Usage

npm install react-viewport-height
import useVH from 'react-viewport-height';

const ROW_HEIGHT = 40;

const Rows = ({ items }) => {
  const vh = useVH();
  const rowsThatFit = Math.floor((100 * vh) / ROW_HEIGHT);

  return (
    <ul>
      {items.slice(0, rowsThatFit).map((item) => (
        <li key={item} style={{ height: ROW_HEIGHT }}>
          {item}
        </li>
      ))}
    </ul>
  );
};

Read the documentation for more details. You can also play with a demo.

Keywords