Gathering detailed insights and metrics for react-use-dimensions
Gathering detailed insights and metrics for react-use-dimensions
Gathering detailed insights and metrics for react-use-dimensions
Gathering detailed insights and metrics for react-use-dimensions
react-cool-dimensions
React hook to measure an element's size and handle responsive components.
ink-use-stdout-dimensions
React hook for subscribing to stdout dimensions in Ink
react-native-use-dimensions
This Node.js package is a collection of React hooks for using the dimensions of the screen, window, or both.
use-element-dimensions
React Hook to figure out DOM Element dimensions with updates
npm install react-use-dimensions
Typescript
Module System
Node Version
NPM Version
TypeScript (83.63%)
JavaScript (16.37%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
5,844,494
Last Day
2,365
Last Week
12,338
Last Month
52,912
Last Year
1,010,072
594 Stars
32 Commits
43 Forks
7 Watchers
19 Branches
2 Contributors
Updated on Nov 27, 2024
Minified
Minified + Gzipped
Latest Version
1.2.1
Package Id
react-use-dimensions@1.2.1
Size
2.97 kB
NPM Version
6.4.1
Node Version
10.12.0
Published on
Jun 16, 2019
Cumulative downloads
Total Downloads
Last Day
8.8%
2,365
Compared to previous day
Last Week
-1%
12,338
Compared to previous week
Last Month
53.4%
52,912
Compared to previous month
Last Year
-20%
1,010,072
Compared to previous year
3
???? check out demo page to see useDimensions in action
The other day I wanted to measure some DOM nodes. This is useful when you have to align items, or respond to browser width, or ... lots of reasons okay.
I had to align a curvy line with elements that aren't under my control. This little stepper component uses flexbox to evenly space circles, CSS layouting aligns the title, and you see where this is going.
SVG in the background detects position of itself, positions of the title and circle, and uses those to define the start
and end
line of my curve. ????
Many ways you can do this.
@lavrton linked to a list of existing NPM packages that sort of do it. @mcalus shared how he uses react-sizeme
to get it done.
All great, but I wanted something even simpler. I also didn't know about them and kind of just wanted to make my own.
Here's an approach I found works great
Yep that's it. It really is that simple.
???? GitHub link
useRef
creates a React.ref, lets you access the DOMuseState
gives you place to store/read the resultuseLayoutEffect
runs before browser paint but after all is knowngetClientBoundingRect()
measures a DOM node. Width, height, x, y, etctoJSON
turns a DOMRect object into a plain object so you can destructureHere's how to use it in your project ????
First, add useDimensions
to your project
$ yarn add react-use-dimensions
or
$ npm install --save react-use-dimensions
Using it in a component looks like this
1import React from "react"; 2import useDimensions from "react-use-dimensions"; 3 4const MyComponent = () => { 5 const [ref, { x, y, width }] = useDimensions(); 6 7 return <div ref={ref}>This is the element you'll measure</div>; 8};
useDimensions
returns a 2-element array. First the ref
, second the dimensions.
This is so multiple useDimensions
hooks in the same component don't step on each others' toes. Create as many refs and measurement objects as you'd like.
1const MyComponent = () => { 2 const [stepRef, stepSize] = useDimensions(); 3 const [titleRef, titleSize] = useDimensions(); 4 5 console.log("Step is at X: ", stepSize.x); 6 console.log("Title is", titleSize.width, "wide"); 7 8 return ( 9 <div> 10 <div ref={stepRef}>This is a step</div> 11 <h1 ref={titleRef}>The title</h1> 12 </div> 13 ); 14};
By default useDimensions
live updates its measurements on every scroll and resize event. This can lead to a lot of re-rendering, if you aren't careful.
I recommend feeding the dimensions you care about into your list of useEffect
triggers. That is the best way to ensure good performance.
If however, you don't want that and know you just need to measure once, useDimensions
supports an optional liveMeasure
argument.
1const MyComponent = () => { 2 const [stepRef, stepSize] = useDimensions({ liveMeasure: false }); 3 4 // useDimensions never updates and won't trigger re-renders 5 console.log("Step is at X: ", stepSize.x); 6 7 return ( 8 <div> 9 <div ref={stepRef}>This is a step</div> 10 </div> 11 ); 12};
MIT License of course.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
159 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More