Skip to content

Commit 3a15b6a

Browse files
committed
trigger new listeners to support lazy loaded components
1 parent 7b5f3b5 commit 3a15b6a

File tree

4 files changed

+28
-124
lines changed

4 files changed

+28
-124
lines changed

.babelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": [
3-
"@babel/preset-env",
4-
"@babel/preset-react"
3+
"@babel/preset-env"
4+
55
]
66
}

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ npm install -D use-viewport-sizes
1111
```
1212

1313
## Benefits ##
14-
- extremely lightweight and zero dependencies -- adds **1.9kb** after gzip.
14+
- extremely lightweight and zero dependencies -- adds **2kb** after gzip.
1515
- only one `window.onresize` handler used to subscribe to any changes in an unlimited number of components no matter the use-cases.
1616
- 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.
1717
- debouncing does not create new handlers or waste re-renders in your component; the results are also pooled from only one resize result.
1818
- optional hash function to update component subtree only at points you would like to.
19-
- supports SSR (see example under Usage section).
19+
- supports lazy loaded components and SSR out of the box (see example SSR under Usage section).
2020

2121

2222
## Usage ##
@@ -154,7 +154,7 @@ function MySSRComponent (props) {
154154
```
155155

156156
## Support
157-
If you have read the examples and have any issues which you know are glitches,or would like to request something changed, please feel free to [post an issue on Github](https://github.com/rob2d/use-viewport-sizes/issues/new).
157+
If you find any issues or would like to request something changed, please feel free to [post an issue on Github](https://github.com/rob2d/use-viewport-sizes/issues/new).
158158

159159
Otherwise, if this was useful and you'd like to show your support, no donations necessary, but please consider [checking out the repo](https://github.com/rob2d/use-viewport-sizes) and giving it a star (⭐).
160160

package-lock.json

+2-103
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { useState, useMemo, useCallback, useRef, useLayoutEffect } from 'react';
1+
import {
2+
useState,
3+
useMemo,
4+
useCallback,
5+
useRef,
6+
useLayoutEffect
7+
} from 'react';
28

39
function getVpWidth() {
410
return (typeof window != 'undefined') ? Math.max(
@@ -64,9 +70,7 @@ function triggerResizeListener(listener, vpWidth, vpHeight) {
6470
const { options, prevHash=undefined } = resolverMap?.get(listener) || {};
6571
const { hasher } = options;
6672

67-
if(!options?.hasher) {
68-
const dimensionsUpdated = new Set();
69-
73+
if(!hasher) {
7074
switch (options?.dimension) {
7175
case 'w':
7276
hash = `${vpWidth}`;
@@ -89,7 +93,9 @@ function triggerResizeListener(listener, vpWidth, vpHeight) {
8993
if(shouldRun) {
9094
const state = { ...params, options, hash };
9195
resolverMap.set(listener, {
92-
options, prevHash: hash, prevState: state
96+
options,
97+
prevHash: hash,
98+
prevState: state
9399
});
94100
listener(state);
95101
}
@@ -116,20 +122,13 @@ function onResize() {
116122
// =============== //
117123

118124
function getInitialState(options, vpW, vpH) {
119-
let returnValue = {};
120-
if(options.hasher) {
121-
returnValue = options.hasher({ vpW, vpH });
122-
} else {
123-
returnValue = { vpW, vpH };
124-
}
125-
126125
return (!options.hasher ?
127126
{ vpW, vpH } :
128-
hasher && hasher({ vpW: vpWidth, vpH: vpHeight })
127+
options.hasher({ vpW: vpWidth, vpH: vpHeight })
129128
)
130129
}
131130

132-
function useViewportSizes(input) {
131+
export default function useViewportSizes(input) {
133132
const hasher = ((typeof input == 'function') ?
134133
input :
135134
input?.hasher
@@ -213,9 +212,17 @@ function useViewportSizes(input) {
213212

214213
listeners.add(listener);
215214

215+
// if first resize listener, add resize event
216+
216217
if(window && listeners.size == 1) {
217218
window.addEventListener('resize', onResize);
218219
onResize();
220+
}
221+
222+
// if additional resize listener, trigger it on the new listener
223+
224+
else {
225+
triggerResizeListener(listener, vpWidth, vpHeight);
219226
}
220227

221228
// clean up listeners on unmount
@@ -259,5 +266,3 @@ function useViewportSizes(input) {
259266

260267
return returnValue;
261268
}
262-
263-
export default useViewportSizes;

0 commit comments

Comments
 (0)