implement long press on list item
This commit is contained in:
@@ -11,6 +11,8 @@ import { LoadingSpinner } from "~/components/loading-spinner"
|
|||||||
import { useMnemonics } from "~/hooks/use-mnemonics"
|
import { useMnemonics } from "~/hooks/use-mnemonics"
|
||||||
import { DialogKind, useBookmarkPageStore } from "./-store"
|
import { DialogKind, useBookmarkPageStore } from "./-store"
|
||||||
|
|
||||||
|
const LONG_PRESS_DELAY_MS = 500
|
||||||
|
|
||||||
enum BookmarkListItemAction {
|
enum BookmarkListItemAction {
|
||||||
Open = "Open",
|
Open = "Open",
|
||||||
Edit = "Edit",
|
Edit = "Edit",
|
||||||
@@ -273,11 +275,40 @@ const BookmarkListItem = memo(
|
|||||||
const setIsItemExpanded = useBookmarkListStore((state) => state.setIsItemExpanded)
|
const setIsItemExpanded = useBookmarkListStore((state) => state.setIsItemExpanded)
|
||||||
const onItemAction = useBookmarkListStore((state) => state.onItemAction)
|
const onItemAction = useBookmarkListStore((state) => state.onItemAction)
|
||||||
|
|
||||||
|
const longPressTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||||
|
const isLongPressActive = useRef(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (selected) {
|
if (selected) {
|
||||||
store.getState().setSelectedIndex(index)
|
store.getState().setSelectedIndex(index)
|
||||||
}
|
}
|
||||||
}, [selected, index, store.getState])
|
}, [selected, index, store])
|
||||||
|
|
||||||
|
function handleTouchStart() {
|
||||||
|
isLongPressActive.current = true
|
||||||
|
longPressTimerRef.current = setTimeout(() => {
|
||||||
|
if (isLongPressActive.current) {
|
||||||
|
setSelectedBookmarkId(bookmark.id)
|
||||||
|
setIsItemExpanded(true)
|
||||||
|
}
|
||||||
|
}, LONG_PRESS_DELAY_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTouchEnd() {
|
||||||
|
isLongPressActive.current = false
|
||||||
|
if (longPressTimerRef.current) {
|
||||||
|
clearTimeout(longPressTimerRef.current)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => () => {
|
||||||
|
if (longPressTimerRef.current) {
|
||||||
|
clearTimeout(longPressTimerRef.current)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
|
||||||
function deleteItem() {
|
function deleteItem() {
|
||||||
onItemAction(bookmark, BookmarkListItemAction.Delete)
|
onItemAction(bookmark, BookmarkListItemAction.Delete)
|
||||||
@@ -304,6 +335,9 @@ const BookmarkListItem = memo(
|
|||||||
"text-teal-600": selected && !isBookmarkItemExpanded,
|
"text-teal-600": selected && !isBookmarkItemExpanded,
|
||||||
})}
|
})}
|
||||||
onMouseEnter={onItemHover}
|
onMouseEnter={onItemHover}
|
||||||
|
onTouchStart={handleTouchStart}
|
||||||
|
onTouchEnd={handleTouchEnd}
|
||||||
|
onTouchCancel={handleTouchEnd}
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
Reference in New Issue
Block a user