Skip to content

Commit

Permalink
feat(web): better context menu position (immich-app#4271)
Browse files Browse the repository at this point in the history
* feat(web): better context menu position

* fix: album context menu

* fix: add middle variant

* fix: rest of context menus

* fix: linting error
  • Loading branch information
jrasm91 authored Sep 29, 2023
1 parent 3e73cfb commit 68d6d89
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 29 deletions.
9 changes: 3 additions & 6 deletions web/src/lib/components/album-page/album-card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import DotsVertical from 'svelte-material-icons/DotsVertical.svelte';
import IconButton from '../elements/buttons/icon-button.svelte';
import type { OnClick, OnShowContextMenu } from './album-card';
import { getContextMenuPosition } from '../../utils/context-menu';
export let album: AlbumResponseDto;
export let isSharingView = false;
Expand Down Expand Up @@ -41,12 +42,8 @@
}
};
const showAlbumContextMenu = (e: MouseEvent) => {
dispatchShowContextMenu('showalbumcontextmenu', {
x: e.clientX,
y: e.clientY,
});
};
const showAlbumContextMenu = (e: MouseEvent) =>
dispatchShowContextMenu('showalbumcontextmenu', getContextMenuPosition(e));
onMount(async () => {
imageData = (await loadHighQualityThumbnail(album.albumThumbnailAssetId)) || noThumbnailUrl;
Expand Down
15 changes: 4 additions & 11 deletions web/src/lib/components/album-page/share-info-modal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { notificationController, NotificationType } from '../shared-components/notification/notification';
import { handleError } from '../../utils/handle-error';
import ConfirmDialogue from '../shared-components/confirm-dialogue.svelte';
import { getContextMenuPosition } from '../../utils/context-menu';
export let album: AlbumResponseDto;
Expand All @@ -34,16 +35,8 @@
}
});
const showContextMenu = (user: UserResponseDto) => {
const iconButton = document.getElementById('icon-' + user.id);
if (iconButton) {
position = {
x: iconButton.getBoundingClientRect().left,
y: iconButton.getBoundingClientRect().bottom,
};
}
const showContextMenu = (event: MouseEvent, user: UserResponseDto) => {
position = getContextMenuPosition(event);
selectedMenuUser = user;
selectedRemoveUser = null;
};
Expand Down Expand Up @@ -105,7 +98,7 @@
{#if isOwned}
<div>
<CircleIconButton
on:click={() => showContextMenu(user)}
on:click={(event) => showContextMenu(event, user)}
logo={DotsVertical}
backgroundColor="transparent"
hoverColor="#e2e7e9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import CircleIconButton from '../elements/buttons/circle-icon-button.svelte';
import ContextMenu from '../shared-components/context-menu/context-menu.svelte';
import MenuOption from '../shared-components/context-menu/menu-option.svelte';
import { getContextMenuPosition } from '$lib/utils/context-menu';
export let asset: AssetResponseDto;
export let showCopyButton: boolean;
Expand Down Expand Up @@ -52,8 +53,8 @@
let contextMenuPosition = { x: 0, y: 0 };
let isShowAssetOptions = false;
const showOptionsMenu = ({ x, y }: MouseEvent) => {
contextMenuPosition = { x, y };
const showOptionsMenu = (event: MouseEvent) => {
contextMenuPosition = getContextMenuPosition(event, 'top-right');
isShowAssetOptions = !isShowAssetOptions;
};
Expand Down
5 changes: 3 additions & 2 deletions web/src/lib/components/faces-page/people-card.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { PersonResponseDto, api } from '@api';
import { getContextMenuPosition } from '$lib/utils/context-menu';
import ImageThumbnail from '../assets/thumbnail/image-thumbnail.svelte';
import IconButton from '../elements/buttons/icon-button.svelte';
import DotsVertical from 'svelte-material-icons/DotsVertical.svelte';
Expand All @@ -21,8 +22,8 @@
let showVerticalDots = false;
let showContextMenu = false;
let contextMenuPosition = { x: 0, y: 0 };
const showMenu = ({ x, y }: MouseEvent) => {
contextMenuPosition = { x, y };
const showMenu = (event: MouseEvent) => {
contextMenuPosition = getContextMenuPosition(event);
showContextMenu = !showContextMenu;
};
const onMenuExit = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
import CircleIconButton from '$lib/components/elements/buttons/circle-icon-button.svelte';
import ContextMenu from '$lib/components/shared-components/context-menu/context-menu.svelte';
import type Icon from 'svelte-material-icons/AbTesting.svelte';
import { getContextMenuPosition } from '$lib/utils/context-menu';
export let icon: typeof Icon;
export let title: string;
let showContextMenu = false;
let contextMenuPosition = { x: 0, y: 0 };
const handleShowMenu = ({ x }: MouseEvent) => {
const navigationBarHeight = 75;
contextMenuPosition = { x: x, y: navigationBarHeight };
const handleShowMenu = (event: MouseEvent) => {
contextMenuPosition = getContextMenuPosition(event, 'top-left');
showContextMenu = !showContextMenu;
};
Expand Down
5 changes: 3 additions & 2 deletions web/src/lib/components/user-settings-page/library-list.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import Portal from '../shared-components/portal/portal.svelte';
import ContextMenu from '../shared-components/context-menu/context-menu.svelte';
import MenuOption from '../shared-components/context-menu/menu-option.svelte';
import { getContextMenuPosition } from '$lib/utils/context-menu';
let libraries: LibraryResponseDto[] = [];
Expand Down Expand Up @@ -60,8 +61,8 @@
}
};
const showMenu = ({ x, y }: MouseEvent, type: LibraryType) => {
contextMenuPosition = { x, y };
const showMenu = (event: MouseEvent, type: LibraryType) => {
contextMenuPosition = getContextMenuPosition(event);
showContextMenu = !showContextMenu;
libraryType = type;
};
Expand Down
18 changes: 18 additions & 0 deletions web/src/lib/utils/context-menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type Align = 'middle' | 'top-left' | 'top-right';

export const getContextMenuPosition = (event: MouseEvent, align: Align = 'middle') => {
const { x, y, currentTarget, target } = event;
const box = ((currentTarget || target) as HTMLElement)?.getBoundingClientRect();
if (box) {
switch (align) {
case 'middle':
return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
case 'top-left':
return { x: box.x, y: box.y };
case 'top-right':
return { x: box.x + box.width, y: box.y };
}
}

return { x, y };
};
6 changes: 3 additions & 3 deletions web/src/routes/(user)/albums/[albumId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import ShareVariantOutline from 'svelte-material-icons/ShareVariantOutline.svelte';
import type { PageData } from './$types';
import { clickOutside } from '$lib/utils/click-outside';
import { getContextMenuPosition } from '$lib/utils/context-menu';
export let data: PageData;
Expand Down Expand Up @@ -193,9 +194,8 @@
timelineInteractionStore.clearMultiselect();
};
const handleOpenAlbumOptions = ({ x }: MouseEvent) => {
const navigationBarHeight = 75;
contextMenuPosition = { x: x, y: navigationBarHeight };
const handleOpenAlbumOptions = (event: MouseEvent) => {
contextMenuPosition = getContextMenuPosition(event, 'top-left');
viewMode = viewMode === ViewMode.VIEW ? ViewMode.ALBUM_OPTIONS : ViewMode.VIEW;
};
Expand Down

0 comments on commit 68d6d89

Please sign in to comment.