Skip to content

Commit d82eaaf

Browse files
committed
v0.2.8, add support for changing on custom-hash function output changes
1 parent 857f6c1 commit d82eaaf

File tree

5 files changed

+5343
-47
lines changed

5 files changed

+5343
-47
lines changed

README.md

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ npm install -D use-viewport-sizes
1111
```
1212

1313
## Benefits ##
14-
- extremely lightweight and dependency-free -- **2.25kb** before gzipping.
15-
- only one `window.onresize` handler used to subscribe to any changes in an unlimited number of components.
14+
- extremely lightweight and dependency-free -- **3.7kb** 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.
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
- supports SSR.
1919

20+
2021
## Usage ##
2122

2223
### **See it in Action** ###
@@ -36,17 +37,18 @@ npm install -D use-viewport-sizes
3637
import React from 'react'
3738
import useViewportSizes from 'use-viewport-sizes'
3839

39-
function MyComponent (props) {
40+
function MyComponent(props) {
4041
const [vpWidth, vpHeight] = useViewportSizes();
4142

4243
...renderLogic
4344
}
4445
```
4546

4647

47-
### **With Debouncing**
48-
*registers dimension changes only when a user stops dragging/resizing the window for a specified number of miliseconds; for expensive components such as data grids which may be too
49-
expensive to re-render during window resize dragging.*
48+
### **With Debouncing**
49+
50+
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
51+
expensive to re-render during window resize dragging.
5052
```js
5153
import React from 'react'
5254
import useViewportSizes from 'use-viewport-sizes'
@@ -58,7 +60,38 @@ function MyExpensivelyRenderedComponent (props) {
5860
}
5961
```
6062

61-
### **Server Side Rendering**
63+
### **Only update vpW/vpH passed on specific conditions**
64+
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.
65+
```js
66+
import React from 'react';
67+
import useViewportSizes from 'use-viewport-sizes';
68+
69+
function getBreakpoint({ vpW, vpH }) {
70+
if(vpW < 640) {
71+
return 'md';
72+
}
73+
if(vpW < 320) {
74+
return 'sm';
75+
}
76+
else if(vpW < 240) {
77+
return 'xs';
78+
}
79+
else {
80+
return 'lg';
81+
}
82+
}
83+
84+
function MyBreakpointBehaviorComponent() {
85+
const [vpW, vpH, bp] = useViewportSizes(getBreakpoint);
86+
87+
// do-something-with-breakpoints in render
88+
// and add new update for vpW, vpH in this component's
89+
// subtree only when a named breakpoint changes
90+
}
91+
```
92+
93+
94+
### **Server Side Rendering**
6295

6396
*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/).*
6497

@@ -82,11 +115,10 @@ function MySSRComponent (props) {
82115
```
83116

84117
## Support
85-
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).
118+
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).
86119

87-
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 a star.
120+
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 (⭐).
88121

89122
## License ##
90123

91-
- Open Source **[MIT license](http://opensource.org/licenses/mit-license.php)**
92-
- 2019 © <a href="http://robertconcepcion.com" target="_blank">Robert Concepción III</a>
124+
- Open Source **[MIT license](http://opensource.org/licenses/mit-license.php)**

build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)