@@ -5,8 +5,10 @@ import type {
5
5
FloatingTreeType ,
6
6
MaybeGetter ,
7
7
NarrowedElement ,
8
+ OnOpenChange ,
8
9
OpenChangeReason ,
9
10
ReferenceType ,
11
+ WhileElementsMounted ,
10
12
} from "../types.js" ;
11
13
import {
12
14
type FloatingRootContext ,
@@ -60,11 +62,7 @@ interface UseFloatingOptions<RT extends ReferenceType = ReferenceType> {
60
62
* Callback to handle mounting/unmounting of the elements.
61
63
* @default undefined
62
64
*/
63
- whileElementsMounted ?: (
64
- reference : ReferenceType ,
65
- floating : HTMLElement ,
66
- update : ( ) => void ,
67
- ) => ( ) => void ;
65
+ whileElementsMounted ?: WhileElementsMounted ;
68
66
69
67
rootContext ?: MaybeGetter < FloatingRootContext < RT > > ;
70
68
/**
@@ -97,22 +95,19 @@ interface UseFloatingOptions<RT extends ReferenceType = ReferenceType> {
97
95
}
98
96
99
97
/**
100
- * Reactive options for the `useFloating` hook.
98
+ * An instance of `FloatingOptions` is created for each call to `useFloating`,
99
+ * and is used to store the reactive state of the various options passed to
100
+ * `useFloating`.
101
101
*/
102
- // This enables us to not have to pass around a bunch of getters and setters internally.
102
+ // This enables us to not have to pass around a bunch of getters and setters internally
103
+ // and can instead rely on the reactive state of the `FloatingOptions` instance.
103
104
class FloatingOptions < RT extends ReferenceType = ReferenceType > {
104
105
open : ReadableBox < boolean > ;
105
106
placement : ReadableBox < Placement > ;
106
107
strategy : ReadableBox < Strategy > ;
107
108
middleware : ReadableBox < Array < Middleware | undefined | null | false > > ;
108
109
transform : ReadableBox < boolean > ;
109
- whileElementsMounted :
110
- | ( (
111
- reference : ReferenceType ,
112
- floating : HTMLElement ,
113
- update : ( ) => void ,
114
- ) => ( ) => void )
115
- | undefined ;
110
+ whileElementsMounted : WhileElementsMounted | undefined ;
116
111
rootContext : ReadableBox < FloatingRootContext < RT > | undefined > ;
117
112
onReferenceChange : ( node : Element | null ) => void ;
118
113
onFloatingChange : ( node : HTMLElement | null ) => void ;
@@ -122,13 +117,18 @@ class FloatingOptions<RT extends ReferenceType = ReferenceType> {
122
117
reason ?: OpenChangeReason ,
123
118
) => void ;
124
119
nodeId : ReadableBox < string | undefined > ;
125
- floatingProp = $derived . by ( ( ) => extract ( this . options . floating , null ) ) ;
126
- referenceProp = $derived . by ( ( ) => extract ( this . options . reference , null ) ) ;
120
+
127
121
#stableReference = $state < Element | null > ( null ) ;
128
122
#stableFloating = $state < HTMLElement | null > ( null ) ;
129
123
reference : WritableBox < Element | null > ;
130
124
floating : WritableBox < HTMLElement | null > ;
131
125
constructor ( private readonly options : UseFloatingOptions < RT > ) {
126
+ const floatingProp = $derived . by ( ( ) =>
127
+ extract ( this . options . floating , null ) ,
128
+ ) ;
129
+ const referenceProp = $derived . by ( ( ) =>
130
+ extract ( this . options . reference , null ) ,
131
+ ) ;
132
132
this . open = box . with ( ( ) => extract ( options . open , true ) ) ;
133
133
this . placement = box . with ( ( ) => extract ( options . placement , "bottom" ) ) ;
134
134
this . strategy = box . with ( ( ) => extract ( options . strategy , "absolute" ) ) ;
@@ -160,14 +160,14 @@ class FloatingOptions<RT extends ReferenceType = ReferenceType> {
160
160
this . floating . current = extract ( this . options . floating , null ) ;
161
161
162
162
$effect . pre ( ( ) => {
163
- if ( this . floatingProp ) {
164
- this . floating . current = this . floatingProp ;
163
+ if ( floatingProp ) {
164
+ this . floating . current = floatingProp ;
165
165
}
166
166
} ) ;
167
167
168
168
$effect . pre ( ( ) => {
169
- if ( this . referenceProp ) {
170
- this . reference . current = this . referenceProp ;
169
+ if ( referenceProp ) {
170
+ this . reference . current = referenceProp ;
171
171
}
172
172
} ) ;
173
173
}
@@ -191,11 +191,7 @@ interface FloatingContextData<RT extends ReferenceType = ReferenceType> {
191
191
isPositioned : boolean ;
192
192
update : ( ) => Promise < void > ;
193
193
floatingStyles : string ;
194
- onOpenChange : (
195
- open : boolean ,
196
- event ?: Event ,
197
- reason ?: OpenChangeReason ,
198
- ) => void ;
194
+ onOpenChange : OnOpenChange ;
199
195
open : boolean ;
200
196
data : ContextData < RT > ;
201
197
floatingId : string ;
@@ -206,11 +202,7 @@ interface FloatingContextData<RT extends ReferenceType = ReferenceType> {
206
202
class FloatingContext < RT extends ReferenceType = ReferenceType >
207
203
implements FloatingContextData < RT >
208
204
{
209
- onOpenChange : (
210
- open : boolean ,
211
- event ?: Event ,
212
- reason ?: OpenChangeReason ,
213
- ) => void ;
205
+ onOpenChange : OnOpenChange ;
214
206
update : ( ) => Promise < void > ;
215
207
216
208
constructor ( private readonly opts : FloatingContextOptions < RT > ) {
@@ -296,8 +288,6 @@ class FloatingState<RT extends ReferenceType = ReferenceType> {
296
288
context : FloatingContext < RT > ;
297
289
298
290
constructor ( private readonly options : FloatingOptions < RT > ) {
299
- console . log ( options . reference . current ) ;
300
-
301
291
const internalRootContext = useFloatingRootContext ( {
302
292
open : ( ) => options . open . current ?? true ,
303
293
reference : ( ) => options . reference . current ,
0 commit comments