Skip to content

Commit

Permalink
fix: interact outside transition out bug during unmounting melt-ui#1005
Browse files Browse the repository at this point in the history
… (melt-ui#1006)

Co-authored-by: Thomas G. Lopes <26071571+TGlide@users.noreply.github.com>
  • Loading branch information
anatolzak and TGlide authored Feb 26, 2024
1 parent 811f751 commit 4e1ec4a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-rivers-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@melt-ui/svelte': patch
---

Fixed bug where on outside interaction in several components (popover, menu, link preview, tooltip, listbox), on component unmount, the content jumps during out transition if content was mounted in an else if block (closes #1005)
4 changes: 3 additions & 1 deletion src/lib/builders/link-preview/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ export function createLinkPreview(props: CreateLinkPreviewProps = {}) {
floating: $positioning,
clickOutside: $closeOnOutsideClick
? {
handler: (e) => {
handler: async (e) => {
await sleep(0);

onOutsideClick.get()?.(e);
if (e.defaultPrevented) return;

Expand Down
4 changes: 3 additions & 1 deletion src/lib/builders/listbox/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
prev,
removeHighlight,
removeScroll,
sleep,
stripValues,
styleToString,
toWritableStores,
Expand Down Expand Up @@ -226,7 +227,8 @@ export function createListbox<
}

/** Closes the menu & clears the active trigger */
function closeMenu() {
async function closeMenu() {
await sleep(0);
open.set(false);
highlightedItem.set(null);
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/builders/menu/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ export function createMenuBuilder(opts: _MenuBuilderOptions) {
floating: $positioning,
clickOutside: $closeOnOutsideClick
? {
handler: (e) => {
handler: async (e) => {
await sleep(0);

onOutsideClick.get()?.(e);
if (e.defaultPrevented) return;

Expand Down
4 changes: 3 additions & 1 deletion src/lib/builders/popover/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
removeScroll,
styleToString,
toWritableStores,
sleep,
} from '$lib/internal/helpers/index.js';

import { usePopper } from '$lib/internal/actions/index.js';
Expand Down Expand Up @@ -81,7 +82,8 @@ export function createPopover(args?: CreatePopoverProps) {
activeTrigger.set(document.getElementById(ids.trigger.get()));
});

function handleClose() {
async function handleClose() {
await sleep(0);
open.set(false);
const triggerEl = document.getElementById(ids.trigger.get());
handleFocus({ prop: closeFocus.get(), defaultEl: triggerEl });
Expand Down
6 changes: 5 additions & 1 deletion src/lib/builders/tooltip/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
pointInPolygon,
styleToString,
toWritableStores,
sleep,
} from '$lib/internal/helpers/index.js';

import { useFloating, usePortal } from '$lib/internal/actions/index.js';
Expand Down Expand Up @@ -179,7 +180,10 @@ export function createTooltip(props?: CreateTooltipProps) {
if (clickedTrigger) return;
openTooltip('focus');
}),
addMeltEventListener(node, 'blur', () => closeTooltip(true)),
addMeltEventListener(node, 'blur', async () => {
await sleep(0);
closeTooltip(true);
}),
addMeltEventListener(node, 'keydown', keydownHandler),
addEventListener(document, 'keydown', keydownHandler)
);
Expand Down

0 comments on commit 4e1ec4a

Please sign in to comment.