@@ -8,6 +8,7 @@ export const NODES: Node[] = [null, 'html', 'body', 'offsetParent'];
8
8
9
9
export function Relative ( ) {
10
10
const [ node , setNode ] = useState < Node > ( null ) ;
11
+ const [ offset , setOffset ] = useState ( 0 ) ;
11
12
const { x, y, refs, strategy, update} = useFloating ( ) ;
12
13
13
14
useLayoutEffect ( ( ) => {
@@ -21,20 +22,23 @@ export function Relative() {
21
22
element = document . body ;
22
23
break ;
23
24
default :
25
+ element = document . querySelector ( '.container' ) ;
24
26
}
25
27
26
28
if ( element ) {
27
29
element . style . position = 'relative' ;
30
+ element . style . top = `${ - offset } px` ;
28
31
}
29
32
30
33
update ( ) ;
31
34
32
35
return ( ) => {
33
36
if ( element ) {
34
37
element . style . position = '' ;
38
+ element . style . top = '' ;
35
39
}
36
40
} ;
37
- } , [ node , update ] ) ;
41
+ } , [ node , offset , update ] ) ;
38
42
39
43
return (
40
44
< >
@@ -63,6 +67,7 @@ export function Relative() {
63
67
</ div >
64
68
</ div >
65
69
70
+ < h2 > Node</ h2 >
66
71
< Controls >
67
72
{ NODES . map ( ( localNode ) => (
68
73
< button
@@ -77,6 +82,22 @@ export function Relative() {
77
82
</ button >
78
83
) ) }
79
84
</ Controls >
85
+
86
+ < h2 > Offset</ h2 >
87
+ < Controls >
88
+ { [ 0 , 100 ] . map ( ( localOffset ) => (
89
+ < button
90
+ key = { localOffset }
91
+ data-testid = { `offset-${ localOffset } ` }
92
+ onClick = { ( ) => setOffset ( localOffset ) }
93
+ style = { {
94
+ backgroundColor : offset === localOffset ? 'black' : '' ,
95
+ } }
96
+ >
97
+ { localOffset }
98
+ </ button >
99
+ ) ) }
100
+ </ Controls >
80
101
</ >
81
102
) ;
82
103
}
0 commit comments