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) {
9898 const ref = React . useRef ( null ) ;
9999 const refCb = React . useRef ( cb ) ;
100100
101+ React . useLayoutEffect ( ( ) => {
102+ refCb . current = cb ;
103+ } ) ;
104+
101105 React . useEffect ( ( ) => {
102106 const handler = ( e ) => {
103107 const element = ref . current ;
@@ -591,6 +595,10 @@ export function useLongPress(
591595 const timerId = React . useRef ( ) ;
592596 const cbRef = React . useRef ( callback ) ;
593597
598+ React . useLayoutEffect ( ( ) => {
599+ cbRef . current = callback ;
600+ } ) ;
601+
594602 const start = React . useCallback (
595603 ( ) => ( event ) => {
596604 if ( isPressed . current ) return ;
@@ -967,6 +975,7 @@ export function useQueue(initialValue = []) {
967975 first : queue [ 0 ] ,
968976 last : queue [ queue . length - 1 ] ,
969977 size : queue . length ,
978+ queue
970979 } ;
971980}
972981
@@ -1201,6 +1210,7 @@ export function useVisibilityChange() {
12011210 setDocumentVisibility ( true ) ;
12021211 }
12031212 } ;
1213+ handleChange ( )
12041214
12051215 document . addEventListener ( "visibilitychange" , handleChange ) ;
12061216
Original file line number Diff line number Diff line change @@ -50,39 +50,43 @@ import StaticCodeContainer from "../../components/StaticCodeContainer.astro";
5050
5151``` jsx
5252import * as React from " react" ;
53- import { useLongPress } from " @uidotdev/usehooks" ;
54- import { closeIcon } from " ./icons" ;
53+ import { useMeasure } from " @uidotdev/usehooks" ;
5554
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>
6877 );
78+ }
6979
80+ export default function App () {
7081 return (
7182 < 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 / >
8386 < / section>
8487 );
8588}
89+
8690```
8791
8892</StaticCodeContainer >
You can’t perform that action at this time.
0 commit comments