chore(web): migration svelte 5 syntax (#13883)

This commit is contained in:
Alex 2024-11-14 08:43:25 -06:00 committed by GitHub
parent 9203a61709
commit 0b3742cf13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
310 changed files with 6435 additions and 4176 deletions

View file

@ -14,41 +14,52 @@
import { optionClickCallbackStore, selectedIdStore } from '$lib/stores/context-menu.store';
import { clickOutside } from '$lib/actions/click-outside';
import { shortcuts } from '$lib/actions/shortcut';
import type { Snippet } from 'svelte';
export let icon: string;
export let title: string;
/**
* The alignment of the context menu relative to the button.
*/
export let align: Align = 'top-left';
/**
* The direction in which the context menu should open.
*/
export let direction: 'left' | 'right' = 'right';
export let color: Color = 'transparent';
export let size: string | undefined = undefined;
export let padding: Padding | undefined = undefined;
/**
* Additional classes to apply to the button.
*/
export let buttonClass: string | undefined = undefined;
export let hideContent = false;
interface Props {
icon: string;
title: string;
/**
* The alignment of the context menu relative to the button.
*/
align?: Align;
/**
* The direction in which the context menu should open.
*/
direction?: 'left' | 'right';
color?: Color;
size?: string | undefined;
padding?: Padding | undefined;
/**
* Additional classes to apply to the button.
*/
buttonClass?: string | undefined;
hideContent?: boolean;
children?: Snippet;
}
let isOpen = false;
let contextMenuPosition = { x: 0, y: 0 };
let menuContainer: HTMLUListElement;
let buttonContainer: HTMLDivElement;
let {
icon,
title,
align = 'top-left',
direction = 'right',
color = 'transparent',
size = undefined,
padding = undefined,
buttonClass = undefined,
hideContent = false,
children,
}: Props = $props();
let isOpen = $state(false);
let contextMenuPosition = $state({ x: 0, y: 0 });
let menuContainer: HTMLUListElement | undefined = $state();
let buttonContainer: HTMLDivElement | undefined = $state();
const id = generateId();
const buttonId = `context-menu-button-${id}`;
const menuId = `context-menu-${id}`;
$: {
if (isOpen) {
$optionClickCallbackStore = handleOptionClick;
}
}
const openDropdown = (event: KeyboardEvent | MouseEvent) => {
contextMenuPosition = getContextMenuPositionFromEvent(event, align);
isOpen = true;
@ -72,9 +83,10 @@
};
const onResize = () => {
if (!isOpen) {
if (!isOpen || !buttonContainer) {
return;
}
contextMenuPosition = getContextMenuPositionFromBoundingRect(buttonContainer.getBoundingClientRect(), align);
};
@ -92,12 +104,19 @@
};
const focusButton = () => {
const button: HTMLButtonElement | null = buttonContainer.querySelector(`#${buttonId}`);
const button = buttonContainer?.querySelector(`#${buttonId}`) as HTMLButtonElement | null;
button?.focus();
};
$effect(() => {
if (isOpen) {
$optionClickCallbackStore = handleOptionClick;
}
});
</script>
<svelte:window on:resize={onResize} />
<svelte:window onresize={onResize} />
<div
use:contextMenuNavigation={{
closeDropdown,
@ -109,7 +128,7 @@
selectionChanged: (id) => ($selectedIdStore = id),
}}
use:clickOutside={{ onOutclick: closeDropdown }}
on:resize={onResize}
onresize={onResize}
>
<div bind:this={buttonContainer}>
<CircleIconButton
@ -123,7 +142,7 @@
aria-haspopup={true}
class={buttonClass}
id={buttonId}
on:click={handleClick}
onclick={handleClick}
/>
</div>
{#if isOpen || !hideContent}
@ -150,7 +169,7 @@
id={menuId}
isVisible={isOpen}
>
<slot />
{@render children?.()}
</ContextMenu>
</div>
{/if}