You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a tiny React hook which allows you to track visible window viewport size in your components w/ an optional debounce or custom memo function for updates for optimal rendering.
5
+
a tiny React hook which allows you to track visible window viewport size in your components w/ an optional debounce and
- extremely lightweight and dependency-free -- **3.75kb**without/before gzipping.
15
-
- only one `window.onresize` handler used to subscribe to any changes in an unlimited number of components as well despite several different paradigms that pull from this.
15
+
- extremely lightweight and zero dependencies -- adds **1.6kb**after gzip.
16
+
- only one `window.onresize` handler used to subscribe to any changes in an unlimited number of components no matter the use-cases.
16
17
- optional debounce to delay updates until user stops dragging their window for a moment; this can make expensive components with size-dependent calculations run much faster and your app feel smoother.
17
18
- debouncing does not create new handlers or waste re-renders in your component; the results are also pooled from only one resize result.
18
19
- optional hash function to update component subtree only at points you would like to.
If passed a number as the first argument, it registers dimension changes only when a user stops dragging/resizing the window for a specified number of miliseconds. This is useful for listening to expensive components such as data grids which may be too
69
+
If passed `options.debounceTimeout`, or options are entered as a `Number`, it registers dimension changes only when a user stops dragging/resizing the window for a specified number of miliseconds. This is useful for listening to expensive components such as data grids which may be too
52
70
expensive to re-render during window resize dragging.
### **Only update vpW/vpH passed on specific conditions**
65
-
If passed a function as the first argument, it will use this 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.
84
+
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.
85
+
66
86
```js
67
87
importReactfrom'react';
68
88
importuseViewportSizesfrom'use-viewport-sizes';
69
89
70
-
functiongetBreakpoint({ vpW, vpH }) {
90
+
functiongetBreakpointHash({ vpW, vpH }) {
71
91
if(vpW <640) {
72
92
return'md';
73
93
}
@@ -83,7 +103,7 @@ function getBreakpoint({ vpW, vpH }) {
// and add new update for vpW, vpH in this component's
@@ -94,24 +114,24 @@ function MyBreakpointBehaviorComponent() {
94
114
95
115
### **Server Side Rendering**
96
116
97
-
*While serverside rendering is supported, it requires an explicit update via `useEffect` since viewport does not actually exist on the server before rendering to client. The following has been tested with [NextJS](https://nextjs.org/).*
117
+
*Note: While serverside rendering is supported, it requires an explicit update via `useEffect` since viewport does not actually exist on the server before rendering to client. The following has been tested with [NextJS](https://nextjs.org/).*
98
118
99
-
*Sidenote that you will see a `useLayoutEffect` warning from React. This is perfectly normal as there is no viewport/context to paint to when pre-rendering in SSR and will not interfere with your app once served to the client*
119
+
*Sidenote that you will see a `useLayoutEffect` warning from React. This is perfectly expected as there is no viewport/context to paint to when pre-rendering in SSR and will not interfere with your app once served to the client*
Copy file name to clipboardExpand all lines: package.json
+4-3Lines changed: 4 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,16 @@
1
1
{
2
2
"name": "use-viewport-sizes",
3
-
"version": "0.2.12",
3
+
"version": "0.3.0",
4
4
"description": "a tiny React hook which allows you to track visible window viewport size in your components w/ an optional debounce or custom memo function for updates for optimal rendering.",
5
5
"main": "./build/index.js",
6
6
"types": "./build/index.d.ts",
7
7
"scripts": {
8
8
"start": "webpack --watch",
9
9
"build": "webpack",
10
-
"dev": "webpack-dev-server --env testServer --mode development --open"
10
+
"dev": "webpack-dev-server --env testServer --mode development --open",
0 commit comments