From d83f2019c790e06df494f81fd379146f78f00a73 Mon Sep 17 00:00:00 2001 From: lgmarchi Date: Thu, 12 Dec 2024 11:21:00 -0300 Subject: [PATCH] feat(ContextMenu, Input, Chatbar): Enhance keyboard handling for mobile devices to improve user experience --- src/lib/components/ui/ContextMenu.svelte | 42 +++++++++++++++++------- src/lib/elements/Input/Input.svelte | 5 +-- src/lib/layouts/Chatbar.svelte | 16 ++++----- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/lib/components/ui/ContextMenu.svelte b/src/lib/components/ui/ContextMenu.svelte index e0c591f84..42b09ac61 100644 --- a/src/lib/components/ui/ContextMenu.svelte +++ b/src/lib/components/ui/ContextMenu.svelte @@ -1,4 +1,5 @@ @@ -9,8 +10,10 @@ import { clickoutside } from "@svelte-put/clickoutside" import { Appearance } from "$lib/enums" import type { ContextItem } from "$lib/types" - import { createEventDispatcher, tick } from "svelte" + import { createEventDispatcher, onMount, tick } from "svelte" import { log } from "$lib/utils/Logger" + import type { PluginListenerHandle } from "@capacitor/core" + import { isAndroidOriOS } from "$lib/utils/Mobile" let visible: boolean = false let coords: [number, number] = [0, 0] @@ -31,7 +34,7 @@ const { width, height } = context.getBoundingClientRect() const offsetX = evt.pageX - const offsetY = evt.pageY + const offsetY = evt.pageY - keyboardHeight / 2.5 const screenWidth = evt.view!.innerWidth const screenHeight = evt.view!.innerHeight @@ -42,9 +45,7 @@ // Calculate Y position, prioritizing space above the cursor if not enough below const adjustedY = offsetY - height - const topY = screenHeight - offsetY < height + 30 - ? Math.max(5, adjustedY) - : Math.max(5, overFlowY ? offsetY - height : offsetY) + const topY = screenHeight - offsetY < height + 30 ? Math.max(5, adjustedY) : Math.max(5, overFlowY ? offsetY - height : offsetY) return [topX, topY] } @@ -60,6 +61,29 @@ await tick() coords = calculatePos(evt) } + let keyboardHeight = 0 + onMount(() => { + let mobileKeyboardListener01: PluginListenerHandle | undefined + let mobileKeyboardListener02: PluginListenerHandle | undefined + + async function setupListeners() { + mobileKeyboardListener01 = await Keyboard.addListener("keyboardWillShow", info => { + keyboardHeight = info.keyboardHeight + }) + + mobileKeyboardListener02 = await Keyboard.addListener("keyboardWillHide", () => { + keyboardHeight = 0 + }) + } + if (isAndroidOriOS()) { + setupListeners() + } + + return () => { + if (mobileKeyboardListener01) mobileKeyboardListener01.remove() + if (mobileKeyboardListener02) mobileKeyboardListener02.remove() + } + }) function handleItemClick(e: MouseEvent, item: ContextItem) { e.stopPropagation() @@ -74,13 +98,7 @@ {#if visible} -
+
{#each items as item}