Callback for touch start (immediate)
Callback for touch move (coalesced)
Callback for touch end (immediate)
Optimization options
const { isTouching } = useTouchOptimizer(
(x, y) => {
// Immediate visual update (same frame)
buttonRef.current.style.transform = 'scale(0.95)';
// Defer state update
requestIdleCallback(() => {
setPressed(true);
onAction();
});
},
(x, y) => {
// Handle coalesced touch move
updatePosition(x, y);
},
() => {
// Immediate visual reset
buttonRef.current.style.transform = 'scale(1)';
requestIdleCallback(() => {
setPressed(false);
});
}
);
Custom hook for optimized touch handling with <16ms latency
Uses requestAnimationFrame for immediate visual feedback and defers state updates to avoid blocking the main thread