Skip to content

Commit 3b647ee

Browse files
committed
some housekeeping
1 parent ddb75cf commit 3b647ee

File tree

5 files changed

+36
-71
lines changed

5 files changed

+36
-71
lines changed

packages/floating-ui-svelte/src/hooks/use-click.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ interface UseClickOptions {
5151
* reference element for the first time.
5252
* @default true
5353
*/
54-
stickIfOpen?: boolean;
54+
stickIfOpen?: MaybeGetter<boolean>;
5555
}
5656

5757
function isButtonTarget(event: KeyboardEvent) {

packages/floating-ui-svelte/src/hooks/use-floating-root-context.svelte.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ReferenceElement } from "@floating-ui/dom";
22
import type {
33
ContextData,
44
MaybeGetter,
5+
OnOpenChange,
56
OpenChangeReason,
67
ReferenceType,
78
} from "../types.js";
@@ -21,11 +22,7 @@ import { noop } from "../internal/noop.js";
2122

2223
interface UseFloatingRootContextOptions {
2324
open?: MaybeGetter<boolean>;
24-
onOpenChange?: (
25-
open: boolean,
26-
event?: Event,
27-
reason?: OpenChangeReason,
28-
) => void;
25+
onOpenChange?: OnOpenChange;
2926
reference: MaybeGetter<Element | null>;
3027
floating: MaybeGetter<HTMLElement | null>;
3128
onReferenceChange?: (node: Element | null) => void;
@@ -34,11 +31,7 @@ interface UseFloatingRootContextOptions {
3431

3532
class FloatingRootContextOptions {
3633
open: ReadableBox<boolean>;
37-
onOpenChange: (
38-
open: boolean,
39-
event?: Event,
40-
reason?: OpenChangeReason,
41-
) => void;
34+
onOpenChange: OnOpenChange;
4235
onReferenceChange: (node: Element | null) => void;
4336
onFloatingChange: (node: HTMLElement | null) => void;
4437
#stableReference = $state<Element | null>(null);
@@ -113,6 +106,7 @@ class FloatingRootContext<RT extends ReferenceType = ReferenceType> {
113106
}
114107
this.#positionReference = this.options.reference.current;
115108
this.onOpenChange = this.onOpenChange.bind(this);
109+
this.setPositionReference = this.setPositionReference.bind(this);
116110
}
117111

118112
onOpenChange(open: boolean, event?: Event, reason?: OpenChangeReason) {

packages/floating-ui-svelte/src/hooks/use-floating.svelte.ts

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import type {
55
FloatingTreeType,
66
MaybeGetter,
77
NarrowedElement,
8+
OnOpenChange,
89
OpenChangeReason,
910
ReferenceType,
11+
WhileElementsMounted,
1012
} from "../types.js";
1113
import {
1214
type FloatingRootContext,
@@ -60,11 +62,7 @@ interface UseFloatingOptions<RT extends ReferenceType = ReferenceType> {
6062
* Callback to handle mounting/unmounting of the elements.
6163
* @default undefined
6264
*/
63-
whileElementsMounted?: (
64-
reference: ReferenceType,
65-
floating: HTMLElement,
66-
update: () => void,
67-
) => () => void;
65+
whileElementsMounted?: WhileElementsMounted;
6866

6967
rootContext?: MaybeGetter<FloatingRootContext<RT>>;
7068
/**
@@ -97,22 +95,19 @@ interface UseFloatingOptions<RT extends ReferenceType = ReferenceType> {
9795
}
9896

9997
/**
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`.
101101
*/
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.
103104
class FloatingOptions<RT extends ReferenceType = ReferenceType> {
104105
open: ReadableBox<boolean>;
105106
placement: ReadableBox<Placement>;
106107
strategy: ReadableBox<Strategy>;
107108
middleware: ReadableBox<Array<Middleware | undefined | null | false>>;
108109
transform: ReadableBox<boolean>;
109-
whileElementsMounted:
110-
| ((
111-
reference: ReferenceType,
112-
floating: HTMLElement,
113-
update: () => void,
114-
) => () => void)
115-
| undefined;
110+
whileElementsMounted: WhileElementsMounted | undefined;
116111
rootContext: ReadableBox<FloatingRootContext<RT> | undefined>;
117112
onReferenceChange: (node: Element | null) => void;
118113
onFloatingChange: (node: HTMLElement | null) => void;
@@ -122,13 +117,18 @@ class FloatingOptions<RT extends ReferenceType = ReferenceType> {
122117
reason?: OpenChangeReason,
123118
) => void;
124119
nodeId: ReadableBox<string | undefined>;
125-
floatingProp = $derived.by(() => extract(this.options.floating, null));
126-
referenceProp = $derived.by(() => extract(this.options.reference, null));
120+
127121
#stableReference = $state<Element | null>(null);
128122
#stableFloating = $state<HTMLElement | null>(null);
129123
reference: WritableBox<Element | null>;
130124
floating: WritableBox<HTMLElement | null>;
131125
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+
);
132132
this.open = box.with(() => extract(options.open, true));
133133
this.placement = box.with(() => extract(options.placement, "bottom"));
134134
this.strategy = box.with(() => extract(options.strategy, "absolute"));
@@ -160,14 +160,14 @@ class FloatingOptions<RT extends ReferenceType = ReferenceType> {
160160
this.floating.current = extract(this.options.floating, null);
161161

162162
$effect.pre(() => {
163-
if (this.floatingProp) {
164-
this.floating.current = this.floatingProp;
163+
if (floatingProp) {
164+
this.floating.current = floatingProp;
165165
}
166166
});
167167

168168
$effect.pre(() => {
169-
if (this.referenceProp) {
170-
this.reference.current = this.referenceProp;
169+
if (referenceProp) {
170+
this.reference.current = referenceProp;
171171
}
172172
});
173173
}
@@ -191,11 +191,7 @@ interface FloatingContextData<RT extends ReferenceType = ReferenceType> {
191191
isPositioned: boolean;
192192
update: () => Promise<void>;
193193
floatingStyles: string;
194-
onOpenChange: (
195-
open: boolean,
196-
event?: Event,
197-
reason?: OpenChangeReason,
198-
) => void;
194+
onOpenChange: OnOpenChange;
199195
open: boolean;
200196
data: ContextData<RT>;
201197
floatingId: string;
@@ -206,11 +202,7 @@ interface FloatingContextData<RT extends ReferenceType = ReferenceType> {
206202
class FloatingContext<RT extends ReferenceType = ReferenceType>
207203
implements FloatingContextData<RT>
208204
{
209-
onOpenChange: (
210-
open: boolean,
211-
event?: Event,
212-
reason?: OpenChangeReason,
213-
) => void;
205+
onOpenChange: OnOpenChange;
214206
update: () => Promise<void>;
215207

216208
constructor(private readonly opts: FloatingContextOptions<RT>) {
@@ -296,8 +288,6 @@ class FloatingState<RT extends ReferenceType = ReferenceType> {
296288
context: FloatingContext<RT>;
297289

298290
constructor(private readonly options: FloatingOptions<RT>) {
299-
console.log(options.reference.current);
300-
301291
const internalRootContext = useFloatingRootContext({
302292
open: () => options.open.current ?? true,
303293
reference: () => options.reference.current,

packages/floating-ui-svelte/src/hooks/use-position.svelte.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "@floating-ui/dom";
99
import { getDPR, roundByDPR } from "../internal/dpr.js";
1010
import { styleObjectToString } from "../internal/style-object-to-string.js";
11-
import type { ReferenceType } from "../types.js";
11+
import type { ReferenceType, WhileElementsMounted } from "../types.js";
1212
import type { FloatingOptions } from "./use-floating.svelte.js";
1313
import type { FloatingRootContext } from "./use-floating-root-context.svelte.js";
1414

@@ -66,11 +66,7 @@ interface UsePositionOptions<RT extends ReferenceType = ReferenceType> {
6666
* Callback to handle mounting/unmounting of the elements.
6767
* @default undefined
6868
*/
69-
whileElementsMounted?: (
70-
reference: RT,
71-
floating: HTMLElement,
72-
update: () => void,
73-
) => () => void;
69+
whileElementsMounted?: WhileElementsMounted<RT>;
7470
}
7571

7672
interface UsePositionData {

packages/floating-ui-svelte/src/types.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,6 @@ interface FloatingEvents {
2525
off(event: string, handler: (data: any) => void): void;
2626
}
2727

28-
interface FloatingElements {
29-
/**
30-
* The reference element.
31-
*/
32-
reference?: ReferenceType | null;
33-
34-
/**
35-
* The floating element.
36-
*/
37-
floating?: FloatingElement | null;
38-
}
39-
40-
interface ExtendedElements<RT> extends Required<FloatingElements> {
41-
/**
42-
* Some hooks require the reference element to be a DOM element,
43-
* not a VirtualElement.
44-
*/
45-
domReference: NarrowedElement<RT> | null;
46-
}
47-
4828
interface ContextData<RT extends ReferenceType = ReferenceType> {
4929
/**
5030
* The latest even that caused the open state to change.
@@ -89,20 +69,25 @@ interface FloatingTreeType<RT extends ReferenceType = ReferenceType> {
8969
removeNode(node: FloatingNodeType): void;
9070
}
9171

72+
type WhileElementsMounted<RT extends ReferenceType = ReferenceType> = (
73+
reference: RT,
74+
floating: HTMLElement,
75+
update: () => void,
76+
) => () => void;
77+
9278
type Getter<T> = () => T;
9379
type MaybeGetter<T> = T | Getter<T>;
9480

9581
export type {
9682
OpenChangeReason,
9783
FloatingEvents,
98-
FloatingElements,
9984
ContextData,
100-
ExtendedElements,
10185
FloatingNodeType,
10286
FloatingTreeType,
10387
ReferenceType,
10488
NarrowedElement,
10589
OnOpenChange,
10690
Getter,
10791
MaybeGetter,
92+
WhileElementsMounted,
10893
};

0 commit comments

Comments
 (0)