File tree Expand file tree Collapse file tree 2 files changed +39
-25
lines changed
usehooks.com/src/content/hooks Expand file tree Collapse file tree 2 files changed +39
-25
lines changed Original file line number Diff line number Diff line change @@ -98,6 +98,10 @@ export function useClickAway(cb) {
98
98
const ref = React . useRef ( null ) ;
99
99
const refCb = React . useRef ( cb ) ;
100
100
101
+ React . useLayoutEffect ( ( ) => {
102
+ refCb . current = cb ;
103
+ } ) ;
104
+
101
105
React . useEffect ( ( ) => {
102
106
const handler = ( e ) => {
103
107
const element = ref . current ;
@@ -591,6 +595,10 @@ export function useLongPress(
591
595
const timerId = React . useRef ( ) ;
592
596
const cbRef = React . useRef ( callback ) ;
593
597
598
+ React . useLayoutEffect ( ( ) => {
599
+ cbRef . current = callback ;
600
+ } ) ;
601
+
594
602
const start = React . useCallback (
595
603
( ) => ( event ) => {
596
604
if ( isPressed . current ) return ;
@@ -967,6 +975,7 @@ export function useQueue(initialValue = []) {
967
975
first : queue [ 0 ] ,
968
976
last : queue [ queue . length - 1 ] ,
969
977
size : queue . length ,
978
+ queue
970
979
} ;
971
980
}
972
981
@@ -1201,6 +1210,7 @@ export function useVisibilityChange() {
1201
1210
setDocumentVisibility ( true ) ;
1202
1211
}
1203
1212
} ;
1213
+ handleChange ( )
1204
1214
1205
1215
document . addEventListener ( "visibilitychange" , handleChange ) ;
1206
1216
Original file line number Diff line number Diff line change @@ -50,39 +50,43 @@ import StaticCodeContainer from "../../components/StaticCodeContainer.astro";
50
50
51
51
``` jsx
52
52
import * as React from " react" ;
53
- import { useLongPress } from " @uidotdev/usehooks" ;
54
- import { closeIcon } from " ./icons" ;
53
+ import { useMeasure } from " @uidotdev/usehooks" ;
55
54
56
- export default function App () {
57
- const [isOpen , setIsOpen ] = React .useState (false );
58
- const attrs = useLongPress (
59
- () => {
60
- setIsOpen (true );
61
- },
62
- {
63
- onStart : (event ) => console .log (" Press started" ),
64
- onFinish : (event ) => console .log (" Press Finished" ),
65
- onCancel : (event ) => console .log (" Press cancelled" ),
66
- threshold: 500 ,
67
- }
55
+ function Measure ({ type = " horizontal" }) {
56
+ const [ref , { width , height }] = useMeasure ();
57
+
58
+ return (
59
+ < figure>
60
+ < figcaption>
61
+ < span> {type}< / span>
62
+ < / figcaption>
63
+ < article
64
+ ref= {ref}
65
+ className= {type}
66
+ style= {{
67
+ resize: type
68
+ }}
69
+ >
70
+ {type === " horizontal" ? (
71
+ < label> width: {Math .floor (width)}< / label>
72
+ ) : (
73
+ < label> height: {Math .floor (height)}< / label>
74
+ )}
75
+ < / article>
76
+ < / figure>
68
77
);
78
+ }
69
79
80
+ export default function App () {
70
81
return (
71
82
< section>
72
- < h1> useLongPress< / h1>
73
- < button {... attrs} className= " primary" >
74
- Press Me
75
- < / button>
76
- {isOpen && (
77
- < dialog>
78
- < button onClick= {() => setIsOpen (false )}> {closeIcon}< / button>
79
- < h2> Modal< / h2>
80
- < p> This is a modal triggered by a long press.< / p>
81
- < / dialog>
82
- )}
83
+ < h1> useMeasure< / h1>
84
+ < p> (Resize the rulers)< / p>
85
+ < Measure / >
83
86
< / section>
84
87
);
85
88
}
89
+
86
90
```
87
91
88
92
</StaticCodeContainer >
You can’t perform that action at this time.
0 commit comments