Files
bambuddy/frontend/src/hooks/useLongPress.ts
T
maziggy 42cdfdbd74 * Add comprehensive mobile support with responsive navigation
- Add hamburger drawer navigation for mobile (< 768px)
  - Create useIsMobile hook for responsive breakpoint detection
  - Create useLongPress hook for touch gesture context menus
  - Update Button component with 44px min-height for WCAG touch compliance
  - Update Toggle component with larger mobile size (44x28px)
  - Add responsive padding (p-4 md:p-8) to all pages
  - Update ArchivesPage with mobile menu button and horizontal scroll filters
  - Update SettingsPage with stacked column layout on mobile
  - Increase drag handle sizes for touch-friendly reordering
  - Add CSS utilities for touch-manipulation and safe-area insets
2025-12-09 12:55:59 +00:00

47 lines
1.3 KiB
TypeScript

import { useCallback, useRef } from 'react';
interface LongPressOptions {
onLongPress: (e: React.TouchEvent | React.MouseEvent) => void;
onClick?: () => void;
delay?: number;
}
export function useLongPress({ onLongPress, onClick, delay = 500 }: LongPressOptions) {
const timeoutRef = useRef<number | null>(null);
const targetRef = useRef<EventTarget | null>(null);
const longPressTriggered = useRef(false);
const start = useCallback(
(e: React.TouchEvent | React.MouseEvent) => {
longPressTriggered.current = false;
targetRef.current = e.target;
timeoutRef.current = window.setTimeout(() => {
longPressTriggered.current = true;
onLongPress(e);
}, delay);
},
[onLongPress, delay]
);
const clear = useCallback(
(e: React.TouchEvent | React.MouseEvent, shouldTriggerClick = true) => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
if (shouldTriggerClick && !longPressTriggered.current && onClick && targetRef.current === e.target) {
onClick();
}
},
[onClick]
);
return {
onMouseDown: start,
onMouseUp: (e: React.MouseEvent) => clear(e, true),
onMouseLeave: (e: React.MouseEvent) => clear(e, false),
onTouchStart: start,
onTouchEnd: (e: React.TouchEvent) => clear(e, true),
};
}