Gathering detailed insights and metrics for use-viewport-sizes
Gathering detailed insights and metrics for use-viewport-sizes
Gathering detailed insights and metrics for use-viewport-sizes
Gathering detailed insights and metrics for use-viewport-sizes
get-viewport-sizes
Get correct viewport size and use it in CSS
react-device-sizes
Create or use device breakpoints in react
@austral-ui/use-viewport-size
A tiny React hook which allows you to track visible window viewport size in your components.
controller-controller
Controller-controller is a package to manage different controllers based on viewport. With controller-controller you can use multiple controllers for different viewport sizes. This package will initialise and dispose controllers on viewport width change.
tiny React hook which allows you to track visible window viewport size in your components w/ an optional debounce for updates for optimal rendering.
npm install use-viewport-sizes
Typescript
Module System
Node Version
NPM Version
64.3
Supply Chain
95.8
Quality
79.5
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
98,128
Last Day
41
Last Week
304
Last Month
1,371
Last Year
9,956
MIT License
41 Stars
107 Commits
3 Forks
1 Watchers
1 Branches
3 Contributors
Updated on Apr 01, 2025
Minified
Minified + Gzipped
Latest Version
0.8.1
Package Id
use-viewport-sizes@0.8.1
Unpacked Size
12.22 kB
Size
4.94 kB
File Count
5
NPM Version
10.9.0
Node Version
22.12.0
Published on
Mar 30, 2025
Cumulative downloads
Total Downloads
Last Day
-47.4%
41
Compared to previous day
Last Week
-5%
304
Compared to previous week
Last Month
6.3%
1,371
Compared to previous month
Last Year
-21.6%
9,956
Compared to previous year
A lightweight, TypeScript-compatible React hook for tracking viewport sizes in your components. Includes optional debounce, throttle, and custom memoization for optimized rendering.
npm install -D use-viewport-sizes
window.onresize
handler used to subscribe to any changes in an unlimited number of components no matter the use-cases.registers dimension changes on every resize event immediately
1import useViewportSizes from 'use-viewport-sizes' 2 3function MyComponent(props) { 4 const [vpWidth, vpHeight] = useViewportSizes(); 5 6 // ...renderLogic 7}
If passed options.dimension
as w
or h
, only the viewport width or height will be
measured and observed for updates.
The only dimension returned in the return array value will be the width or height, according
to what was passed.
1import useViewportSizes from 'use-viewport-sizes'; 2 3function MyComponent(props) { 4 const [vpHeight] = useViewportSizes({ dimension: 'h' }); 5 6 // ...renderLogic 7}
If passed options.throttleTimeout
, or options are entered as a Number
, dimension changes
are registered on a throttled basis e.g. with a maximum frequency.
This is useful for listening to expensive components such as data grids which may be too expensive to re-render during window resize dragging.
1import useViewportSizes from 'use-viewport-sizes'; 2 3function MyExpensivelyRenderedComponent(props) { 4 // throttled by 1s between updates 5 const [vpWidth, vpHeight] = useViewportSizes({ throttleTimeout: 1000 }); 6 7 // ...renderLogic 8}
If passed options.debounceTimeout
, dimension changes are registered only when a user stops dragging/resizing the window for a specified number of miliseconds. This is an alternative behavior to throttleTimeout
where it may be less
important to update viewport the entire way that a user is resizing.
1import useViewportSizes from 'use-viewport-sizes'; 2 3function MyExpensivelyRenderedComponent(props) { 4 // debounced by 1s between updates 5 const [vpWidth, vpHeight] = useViewportSizes({ debounceTimeout: 1000 }); 6 7 // ...renderLogic 8}
If passed an options.hasher
function, this will be used to calculate a hash that only updates the viewport when the calculation changes. In the example here, we are using it to detect when we have a breakpoint change which may change how a component is rendered if this is not fully possible or inconvenient via CSS @media
queries. The hash will also be available as the 3rd value returned from the hook for convenience.
1import useViewportSizes from 'use-viewport-sizes'; 2 3function getBreakpointHash({ vpW, vpH }) { 4 if(vpW <= 240) { return 'xs' } 5 if(vpW <= 320) { return 'sm' } 6 else if(vpW <= 640) { return 'md' } 7 return 'lg'; 8} 9 10function MyBreakpointBehaviorComponent() { 11 const [vpW, vpH] = useViewportSizes({ hasher: getBreakpointHash }); 12 13 // do-something in render and add new update for vpW, 14 // vpH in this component's subtree only when a breakpoint 15 // hash updates 16}
If you find any issues or would like to request something changed, please feel free to post an issue on Github.
Otherwise, if this was useful and you'd like to show your support, no donations necessary, but please consider checking out the repo and giving it a star (⭐).
Open Source MIT license
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
16 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 0/8 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
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
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-04-28
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