Skip to content

Commit

Permalink
refactor: asset viewer context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
alextran1502 committed Nov 4, 2024
1 parent c4c2a36 commit a782b63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
8 changes: 6 additions & 2 deletions web/src/lib/actions/context-menu-navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Options {
/**
* The container element that with direct children that should be navigated.
*/
container: HTMLElement;
container?: HTMLElement;
/**
* Indicates if the dropdown is open.
*/
Expand Down Expand Up @@ -52,7 +52,11 @@ export const contextMenuNavigation: Action<HTMLElement, Options> = (node, option
await tick();
}

const children = Array.from(container?.children).filter((child) => child.tagName !== 'HR') as HTMLElement[];
if (!container) {
return;
}

const children = Array.from(container.children).filter((child) => child.tagName !== 'HR') as HTMLElement[];
if (children.length === 0) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
let isOpen = $state(false);
let contextMenuPosition = $state({ x: 0, y: 0 });
let menuContainer: HTMLUListElement = $state();
let buttonContainer: HTMLDivElement = $state();
let menuContainer: HTMLUListElement | undefined = $state();
let buttonContainer: HTMLDivElement | undefined = $state();
const id = generateId();
const buttonId = `context-menu-button-${id}`;
Expand Down Expand Up @@ -84,9 +84,10 @@
};
const onResize = () => {
if (!isOpen) {
if (!isOpen || !buttonContainer) {
return;
}
contextMenuPosition = getContextMenuPositionFromBoundingRect(buttonContainer.getBoundingClientRect(), align);
};
Expand All @@ -104,17 +105,19 @@
};
const focusButton = () => {
const button: HTMLButtonElement | null = buttonContainer.querySelector(`#${buttonId}`);
const button = buttonContainer?.querySelector(`#${buttonId}`) as HTMLButtonElement | null;
button?.focus();
};
run(() => {
$effect(() => {
if (isOpen) {
$optionClickCallbackStore = handleOptionClick;
}
});
</script>

<svelte:window onresize={onResize} />

<div
use:contextMenuNavigation={{
closeDropdown,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<script lang="ts">
import { run } from 'svelte/legacy';
import { quintOut } from 'svelte/easing';
import { slide } from 'svelte/transition';
import { clickOutside } from '$lib/actions/click-outside';
Expand Down Expand Up @@ -28,19 +26,19 @@
ariaLabel = undefined,
ariaLabelledBy = undefined,
ariaActiveDescendant = undefined,
menuElement = $bindable(undefined),
menuElement = $bindable(),
onClose = undefined,
children,
}: Props = $props();
let left: number = $state();
let top: number = $state();
let left: number = $state(0);
let top: number = $state(0);
// We need to bind clientHeight since the bounding box may return a height
// of zero when starting the 'slide' animation.
let height: number = $state();
let height: number = $state(0);
run(() => {
$effect(() => {
if (menuElement) {
const rect = menuElement.getBoundingClientRect();
const directionWidth = direction === 'left' ? rect.width : 0;
Expand Down

0 comments on commit a782b63

Please sign in to comment.