Use use-long-press instead

This commit is contained in:
Lim Chee Aun 2023-03-08 00:01:51 +08:00
parent 497f5334e4
commit 6e487ad848
4 changed files with 45 additions and 24 deletions

30
package-lock.json generated
View file

@ -18,7 +18,6 @@
"fast-deep-equal": "~3.1.3", "fast-deep-equal": "~3.1.3",
"idb-keyval": "~6.2.0", "idb-keyval": "~6.2.0",
"just-debounce-it": "~3.2.0", "just-debounce-it": "~3.2.0",
"long-press-event": "~2.4.6",
"masto": "~5.10.0", "masto": "~5.10.0",
"mem": "~9.0.2", "mem": "~9.0.2",
"p-retry": "~5.1.2", "p-retry": "~5.1.2",
@ -32,6 +31,7 @@
"toastify-js": "~1.12.0", "toastify-js": "~1.12.0",
"uid": "~2.0.1", "uid": "~2.0.1",
"use-debounce": "~9.0.3", "use-debounce": "~9.0.3",
"use-long-press": "~2.0.3",
"use-resize-observer": "~9.1.0", "use-resize-observer": "~9.1.0",
"valtio": "1.9.0" "valtio": "1.9.0"
}, },
@ -4665,11 +4665,6 @@
"integrity": "sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==", "integrity": "sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==",
"dev": true "dev": true
}, },
"node_modules/long-press-event": {
"version": "2.4.6",
"resolved": "https://registry.npmjs.org/long-press-event/-/long-press-event-2.4.6.tgz",
"integrity": "sha512-59zL3M+uD7Q2DTuxJ2UkbVV3+0D9PrcI7zgem1AXRinH6g8mb7iN9vOKCqiVriW15S4L9OmKOr/d8q9qAaeCGQ=="
},
"node_modules/loose-envify": { "node_modules/loose-envify": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
@ -6570,6 +6565,18 @@
"react": ">=16.8.0" "react": ">=16.8.0"
} }
}, },
"node_modules/use-long-press": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/use-long-press/-/use-long-press-2.0.3.tgz",
"integrity": "sha512-n3cfv90Y1ldNt+hhXzxnxuLZmgLOOC/+qfLGoeEBgOxmnokPPt39MPF3KmvKriq5VMoJ7uQdVjHejCdHBt9anw==",
"engines": {
"node": ">=10",
"npm": ">=5"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
}
},
"node_modules/use-resize-observer": { "node_modules/use-resize-observer": {
"version": "9.1.0", "version": "9.1.0",
"resolved": "https://registry.npmjs.org/use-resize-observer/-/use-resize-observer-9.1.0.tgz", "resolved": "https://registry.npmjs.org/use-resize-observer/-/use-resize-observer-9.1.0.tgz",
@ -10341,11 +10348,6 @@
"integrity": "sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==", "integrity": "sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==",
"dev": true "dev": true
}, },
"long-press-event": {
"version": "2.4.6",
"resolved": "https://registry.npmjs.org/long-press-event/-/long-press-event-2.4.6.tgz",
"integrity": "sha512-59zL3M+uD7Q2DTuxJ2UkbVV3+0D9PrcI7zgem1AXRinH6g8mb7iN9vOKCqiVriW15S4L9OmKOr/d8q9qAaeCGQ=="
},
"loose-envify": { "loose-envify": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
@ -11620,6 +11622,12 @@
"integrity": "sha512-FhtlbDtDXILJV7Lix5OZj5yX/fW1tzq+VrvK1fnT2bUrPOGruU9Rw8NCEn+UI9wopfERBEZAOQ8lfeCJPllgnw==", "integrity": "sha512-FhtlbDtDXILJV7Lix5OZj5yX/fW1tzq+VrvK1fnT2bUrPOGruU9Rw8NCEn+UI9wopfERBEZAOQ8lfeCJPllgnw==",
"requires": {} "requires": {}
}, },
"use-long-press": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/use-long-press/-/use-long-press-2.0.3.tgz",
"integrity": "sha512-n3cfv90Y1ldNt+hhXzxnxuLZmgLOOC/+qfLGoeEBgOxmnokPPt39MPF3KmvKriq5VMoJ7uQdVjHejCdHBt9anw==",
"requires": {}
},
"use-resize-observer": { "use-resize-observer": {
"version": "9.1.0", "version": "9.1.0",
"resolved": "https://registry.npmjs.org/use-resize-observer/-/use-resize-observer-9.1.0.tgz", "resolved": "https://registry.npmjs.org/use-resize-observer/-/use-resize-observer-9.1.0.tgz",

View file

@ -20,7 +20,6 @@
"fast-deep-equal": "~3.1.3", "fast-deep-equal": "~3.1.3",
"idb-keyval": "~6.2.0", "idb-keyval": "~6.2.0",
"just-debounce-it": "~3.2.0", "just-debounce-it": "~3.2.0",
"long-press-event": "~2.4.6",
"masto": "~5.10.0", "masto": "~5.10.0",
"mem": "~9.0.2", "mem": "~9.0.2",
"p-retry": "~5.1.2", "p-retry": "~5.1.2",
@ -34,6 +33,7 @@
"toastify-js": "~1.12.0", "toastify-js": "~1.12.0",
"uid": "~2.0.1", "uid": "~2.0.1",
"use-debounce": "~9.0.3", "use-debounce": "~9.0.3",
"use-long-press": "~2.0.3",
"use-resize-observer": "~9.1.0", "use-resize-observer": "~9.1.0",
"valtio": "1.9.0" "valtio": "1.9.0"
}, },

View file

@ -537,6 +537,7 @@ a[href^='http'][rel*='nofollow']:visited:not(:has(div)) {
transition: background-color 0.2s ease-out; transition: background-color 0.2s ease-out;
-webkit-tap-highlight-color: transparent; -webkit-tap-highlight-color: transparent;
animation: appear 0.2s ease-out; animation: appear 0.2s ease-out;
-webkit-touch-callout: none;
} }
:is(.status-link, .status-focus):is(:focus, .is-active) { :is(.status-link, .status-focus):is(:focus, .is-active) {
background-color: var(--link-bg-hover-color); background-color: var(--link-bg-hover-color);
@ -1051,6 +1052,11 @@ body:has(.status-deck) .media-post-link {
/* MENU POPUP */ /* MENU POPUP */
.szh-menu-container {
user-select: none;
-webkit-touch-callout: none;
-webkit-user-drag: none;
}
.szh-menu-container:has(.szh-menu--state-open) { .szh-menu-container:has(.szh-menu--state-open) {
inset: 0; inset: 0;
inset: env(safe-area-inset-top) env(safe-area-inset-right) inset: env(safe-area-inset-top) env(safe-area-inset-right)

View file

@ -7,12 +7,12 @@ import {
MenuHeader, MenuHeader,
MenuItem, MenuItem,
} from '@szhsin/react-menu'; } from '@szhsin/react-menu';
import 'long-press-event';
import mem from 'mem'; import mem from 'mem';
import pThrottle from 'p-throttle'; import pThrottle from 'p-throttle';
import { memo } from 'preact/compat'; import { memo } from 'preact/compat';
import { useEffect, useMemo, useRef, useState } from 'preact/hooks'; import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
import 'swiped-events'; import 'swiped-events';
import { useLongPress } from 'use-long-press';
import useResizeObserver from 'use-resize-observer'; import useResizeObserver from 'use-resize-observer';
import { useSnapshot } from 'valtio'; import { useSnapshot } from 'valtio';
@ -507,25 +507,27 @@ function Status({
</> </>
); );
const contextMenuRef = useRef();
const [isContextMenuOpen, setIsContextMenuOpen] = useState(false); const [isContextMenuOpen, setIsContextMenuOpen] = useState(false);
const [contextMenuAnchorPoint, setContextMenuAnchorPoint] = useState({ const [contextMenuAnchorPoint, setContextMenuAnchorPoint] = useState({
x: 0, x: 0,
y: 0, y: 0,
}); });
useEffect(() => { const bindLongPress = useLongPress(
function openContextMenu(e) { (e) => {
e.preventDefault(); const { clientX, clientY } = e.touches?.[0] || e;
setContextMenuAnchorPoint({ setContextMenuAnchorPoint({
x: e.clientX, x: clientX,
y: e.clientY, y: clientY,
}); });
setIsContextMenuOpen(true); setIsContextMenuOpen(true);
} },
statusRef.current?.addEventListener?.('long-press', openContextMenu); {
return () => { captureEvent: true,
statusRef.current?.removeEventListener?.('long-press', openContextMenu); detect: 'touch',
}; cancelOnMovement: true,
}, []); },
);
return ( return (
<article <article
@ -551,9 +553,11 @@ function Status({
}); });
setIsContextMenuOpen(true); setIsContextMenuOpen(true);
}} }}
{...bindLongPress()}
> >
{size !== 'l' && ( {size !== 'l' && (
<ControlledMenu <ControlledMenu
ref={contextMenuRef}
state={isContextMenuOpen ? 'open' : undefined} state={isContextMenuOpen ? 'open' : undefined}
anchorPoint={contextMenuAnchorPoint} anchorPoint={contextMenuAnchorPoint}
direction="right" direction="right"
@ -566,6 +570,9 @@ function Status({
// Higher than the backdrop // Higher than the backdrop
zIndex: 1001, zIndex: 1001,
}, },
onClick: () => {
contextMenuRef.current?.closeMenu?.();
},
}} }}
overflow="auto" overflow="auto"
boundingBoxPadding="8 8 8 8" boundingBoxPadding="8 8 8 8"