Skip to content

Commit

Permalink
fix(overflow-menu): support Svelte 5
Browse files Browse the repository at this point in the history
Fixes #2092
  • Loading branch information
metonym committed Jan 30, 2025
1 parent f61e316 commit 88f4304
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
21 changes: 6 additions & 15 deletions src/OverflowMenu/OverflowMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
setContext("OverflowMenu", {
focusedId,
items,
add: ({ id, text, primaryFocus, disabled }) => {
items.update((_) => {
if (primaryFocus) {
Expand All @@ -90,8 +91,11 @@
return [..._, { id, text, primaryFocus, disabled, index: _.length }];
});
},
update: (id) => {
update: (id, item) => {
currentId.set(id);
dispatch("close", { index: item.index, text: item.text });
open = false;
},
change: (direction) => {
let index = $currentIndex + direction;
Expand Down Expand Up @@ -121,12 +125,6 @@
});
afterUpdate(() => {
if ($currentId) {
const { index, text } = $items.filter((_) => _.id === $currentId)[0];
dispatch("close", { index, text });
open = false;
}
if (open) {
const width = buttonRef.offsetWidth;
const height = buttonRef.offsetHeight;
Expand Down Expand Up @@ -183,6 +181,7 @@
on:click={({ target }) => {
if (buttonRef && buttonRef.contains(target)) return;
if (menuRef && !menuRef.contains(target)) {
dispatch("close");
open = false;
}
}}
Expand Down Expand Up @@ -225,14 +224,6 @@
}
}
}}
on:focusout={(e) => {
if (open) {
if (!buttonRef.contains(e.relatedTarget)) {
dispatch("close");
open = false;
}
}
}}
>
<slot name="menu">
<svelte:component
Expand Down
14 changes: 9 additions & 5 deletions src/OverflowMenu/OverflowMenuItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import { getContext, afterUpdate } from "svelte";
const { focusedId, add, update, change } = getContext("OverflowMenu");
const { focusedId, add, update, change, items } = getContext("OverflowMenu");
$: item = $items.find((_) => _.id === id);
add({ id, text, primaryFocus, disabled });
Expand Down Expand Up @@ -68,8 +70,9 @@
bind:this={ref}
{...buttonProps}
on:click
on:click={() => {
update(id);
on:click={(e) => {
e.stopPropagation();
update(id, item);
}}
on:keydown
on:keydown={({ key }) => {
Expand All @@ -91,8 +94,9 @@
bind:this={ref}
{...buttonProps}
on:click
on:click={() => {
update(id);
on:click={(e) => {
e.stopPropagation();
update(id, item);
}}
on:keydown
on:keydown={({ key }) => {
Expand Down

0 comments on commit 88f4304

Please sign in to comment.