Skip to content

Commit

Permalink
perf: avoid effect multiple runs (melt-ui#1089)
Browse files Browse the repository at this point in the history
Co-authored-by: Hunter Johnston <johnstonhuntera@gmail.com>
Co-authored-by: Hunter Johnston <64506580+huntabyte@users.noreply.github.com>
  • Loading branch information
3 people authored and lolcabanon committed Apr 20, 2024
1 parent 5654d84 commit 0e454dc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-eels-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@melt-ui/svelte": patch
---

performance: avoid effect multiple runs
2 changes: 1 addition & 1 deletion src/lib/builders/listbox/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/lib/builders/popover/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
toWritableStores,
portalAttr,
generateIds,
withGet,
} from '$lib/internal/helpers/index.js';

import {
Expand Down Expand Up @@ -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<HTMLElement | null>(null);
const activeTrigger = withGet.writable<HTMLElement | null>(null);

const ids = toWritableStores({ ...generateIds(popoverIdParts), ...withDefaults.ids });

Expand Down Expand Up @@ -184,7 +185,7 @@ export function createPopover(args?: CreatePopoverProps) {
open.update((prev) => {
return !prev;
});
if (triggerEl) {
if (triggerEl && triggerEl !== activeTrigger.get()) {
activeTrigger.set(triggerEl);
}
}
Expand Down

0 comments on commit 0e454dc

Please sign in to comment.