From 0e454dc6b6bda2a19df60feb8e9b25fb6249de20 Mon Sep 17 00:00:00 2001 From: Anatol Zakrividoroga <53095479+anatolzak@users.noreply.github.com> Date: Thu, 14 Mar 2024 17:54:23 +0200 Subject: [PATCH] perf: avoid effect multiple runs (#1089) Co-authored-by: Hunter Johnston Co-authored-by: Hunter Johnston <64506580+huntabyte@users.noreply.github.com> --- .changeset/silly-eels-search.md | 5 +++++ src/lib/builders/listbox/create.ts | 2 +- src/lib/builders/popover/create.ts | 5 +++-- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .changeset/silly-eels-search.md diff --git a/.changeset/silly-eels-search.md b/.changeset/silly-eels-search.md new file mode 100644 index 0000000000..01c56cb631 --- /dev/null +++ b/.changeset/silly-eels-search.md @@ -0,0 +1,5 @@ +--- +"@melt-ui/svelte": patch +--- + +performance: avoid effect multiple runs diff --git a/src/lib/builders/listbox/create.ts b/src/lib/builders/listbox/create.ts index f50a8be1a1..6a14a33bf9 100644 --- a/src/lib/builders/listbox/create.ts +++ b/src/lib/builders/listbox/create.ts @@ -212,7 +212,7 @@ export function createListbox< if (!triggerEl) return; // The active trigger is used to anchor the menu to the input element. - activeTrigger.set(triggerEl); + if (triggerEl !== activeTrigger.get()) activeTrigger.set(triggerEl); // Wait a tick for the menu to open then highlight the selected item. await tick(); diff --git a/src/lib/builders/popover/create.ts b/src/lib/builders/popover/create.ts index dec6a850b1..c7b32f7927 100644 --- a/src/lib/builders/popover/create.ts +++ b/src/lib/builders/popover/create.ts @@ -19,6 +19,7 @@ import { toWritableStores, portalAttr, generateIds, + withGet, } from '$lib/internal/helpers/index.js'; import { @@ -79,7 +80,7 @@ export function createPopover(args?: CreatePopoverProps) { const openWritable = withDefaults.open ?? writable(withDefaults.defaultOpen); const open = overridable(openWritable, withDefaults?.onOpenChange); - const activeTrigger = writable(null); + const activeTrigger = withGet.writable(null); const ids = toWritableStores({ ...generateIds(popoverIdParts), ...withDefaults.ids }); @@ -184,7 +185,7 @@ export function createPopover(args?: CreatePopoverProps) { open.update((prev) => { return !prev; }); - if (triggerEl) { + if (triggerEl && triggerEl !== activeTrigger.get()) { activeTrigger.set(triggerEl); } }