From 56a804b3873c239c6c3eeac09e11d31cfa4f0a78 Mon Sep 17 00:00:00 2001 From: Hugo Korte <63101006+Hugos68@users.noreply.github.com> Date: Wed, 1 Jan 2025 22:00:47 +0100 Subject: [PATCH] Added sync external test aswell as ensure only defined dom nodes are synced with effects (#145) --- .changeset/twelve-tables-press.md | 5 +++++ .../src/hooks/use-floating.svelte.ts | 10 ++++++++-- .../test/hooks/use-floating.svelte.ts | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 .changeset/twelve-tables-press.md diff --git a/.changeset/twelve-tables-press.md b/.changeset/twelve-tables-press.md new file mode 100644 index 0000000..5e5f76d --- /dev/null +++ b/.changeset/twelve-tables-press.md @@ -0,0 +1,5 @@ +--- +"@skeletonlabs/floating-ui-svelte": patch +--- + +Ensures referenced dom elements are synced externally. diff --git a/packages/floating-ui-svelte/src/hooks/use-floating.svelte.ts b/packages/floating-ui-svelte/src/hooks/use-floating.svelte.ts index f958e6b..a267f27 100644 --- a/packages/floating-ui-svelte/src/hooks/use-floating.svelte.ts +++ b/packages/floating-ui-svelte/src/hooks/use-floating.svelte.ts @@ -332,11 +332,17 @@ function useFloating(options: UseFloatingOptions = {}): UseFloatingReturn { }; $effect.pre(() => { - elements.reference = options.elements?.reference; + if (!options.elements || !options.elements.reference) { + return; + } + elements.reference = options.elements.reference; }); $effect.pre(() => { - elements.floating = options.elements?.floating; + if (!options.elements || !options.elements.floating) { + return; + } + elements.floating = options.elements.floating; }); $effect.pre(() => { diff --git a/packages/floating-ui-svelte/test/hooks/use-floating.svelte.ts b/packages/floating-ui-svelte/test/hooks/use-floating.svelte.ts index ed9ff0c..dc6f23f 100644 --- a/packages/floating-ui-svelte/test/hooks/use-floating.svelte.ts +++ b/packages/floating-ui-svelte/test/hooks/use-floating.svelte.ts @@ -85,6 +85,25 @@ describe("useFloating", () => { }); }), ); + it( + "is synced externally", + withRunes(() => { + const elements = $state(createElements()); + + const floating = useFloating({ + get elements() { + return elements; + }, + }); + + const newElements = createElements(); + + floating.elements.reference = newElements.reference; + floating.elements.floating = newElements.floating; + + expect(elements).toEqual(newElements); + }), + ); }); describe("transform", () => {