Skip to content

Commit

Permalink
fix(ContextMenu): Fix context menu on iOS with long press (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgmarchi authored Dec 17, 2024
1 parent 047471f commit dba53eb
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions src/lib/components/ui/ContextMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
import { clickoutside } from "@svelte-put/clickoutside"
import { Appearance } from "$lib/enums"
import type { ContextItem } from "$lib/types"
import { createEventDispatcher, onMount, tick } from "svelte"
import { createEventDispatcher, onDestroy, 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]
let context: HTMLElement
let slotContainer: HTMLElement
export let items: ContextItem[] = []
export let hook: string = ""
const dispatch = createEventDispatcher()
function onClose(event: CustomEvent<MouseEvent> | MouseEvent) {
if (isLongPress) {
return
}
visible = false
dispatch("close", event)
close_context = undefined
Expand All @@ -34,9 +38,9 @@
const { width, height } = context.getBoundingClientRect()
const offsetX = evt.pageX
const offsetY = evt.pageY - keyboardHeight / 2.5
const offsetY = evt.pageY
const screenWidth = evt.view!.innerWidth
const screenHeight = evt.view!.innerHeight
const screenHeight = evt.view!.innerHeight - keyboardHeight
const overFlowX = screenWidth < width + offsetX
const overFlowY = screenHeight < height + offsetY
Expand All @@ -54,13 +58,18 @@
if (close_context !== undefined) {
close_context()
}
close_context = () => (visible = false)
close_context = () => {
if (!isLongPress) {
visible = false
}
}
evt.preventDefault()
visible = true
coords = [evt.clientX, evt.clientY]
visible = true
await tick()
coords = calculatePos(evt)
}
let keyboardHeight = 0
onMount(() => {
let mobileKeyboardListener01: PluginListenerHandle | undefined
Expand All @@ -85,6 +94,46 @@
}
})
let touchTimer: number | undefined
let isLongPress: boolean = false
function handleTouchStart(evt: TouchEvent) {
if (evt.touches.length === 1) {
isLongPress = false
let longPressElement = evt.target as HTMLElement
longPressElement.style.pointerEvents = "none"
touchTimer = window.setTimeout(() => {
const touch = evt.touches[0]
const mouseEvent = new MouseEvent("contextmenu", {
bubbles: true,
cancelable: true,
view: window,
clientX: touch.clientX,
clientY: touch.clientY,
})
isLongPress = true
openContext(mouseEvent)
}, 500)
}
}
function handleTouchEnd(evt: TouchEvent) {
clearTimeout(touchTimer)
let longPressElement = evt.target as HTMLElement
longPressElement.style.pointerEvents = ""
if (isLongPress) {
evt.preventDefault()
}
setTimeout(() => {
isLongPress = false
}, 100)
}
function handleTouchMove(evt: TouchEvent) {
clearTimeout(touchTimer)
isLongPress = false
}
function handleItemClick(e: MouseEvent, item: ContextItem) {
e.stopPropagation()
log.info(`Clicked ${item.text}`)
Expand All @@ -94,9 +143,23 @@
})
onClose(customEvent)
}
onMount(() => {
slotContainer.addEventListener("touchstart", handleTouchStart)
slotContainer.addEventListener("touchend", handleTouchEnd)
slotContainer.addEventListener("touchmove", handleTouchMove)
})
onDestroy(() => {
slotContainer.removeEventListener("touchstart", handleTouchStart)
slotContainer.removeEventListener("touchend", handleTouchEnd)
slotContainer.removeEventListener("touchmove", handleTouchMove)
})
</script>

<slot name="content" open={openContext} />
<div class="long-press-div-container" bind:this={slotContainer}>
<slot name="content" open={openContext} />
</div>
{#if visible}
<div id="context-menu" data-cy={hook} bind:this={context} use:clickoutside on:clickoutside={onClose} style={`position: fixed; left: ${coords[0]}px; top: ${coords[1]}px;`}>
<slot name="items" close={onClose}></slot>
Expand All @@ -115,6 +178,15 @@
{/if}

<style lang="scss">
.long-press-div-container {
all: unset;
display: contents;
-webkit-touch-callout: none !important;
-webkit-user-select: none !important;
user-select: none;
touch-action: none;
}
#context-menu {
z-index: 1000;
position: fixed;
Expand Down

0 comments on commit dba53eb

Please sign in to comment.