Zero-Dependency React 18/19 Touch Gesture Hooks & Accessible Mobile UI Primitives in TypeScript
A lightweight, zero-runtime-dependency TypeScript library providing native-feeling mobile gesture physics (usePullToRefresh, useSwipeGesture) and WAI-ARIA compliant mobile navigation (BottomNavigation) for React web applications and PWAs.
| Export | Kind | Highlights |
|---|---|---|
usePullToRefresh |
Hook | Logarithmic spring damping (damping: 0.6), configurable activation threshold (80px), normalized pullProgress (0..1), passive touch listener optimization, and scroll-top guard (scrollTop === 0). |
useSwipeGesture |
Hook | 4-directional swipe tracking (left, right, up, down), real-time delta & velocity (px/ms) calculation, axis-lock ratio to prevent diagonal scroll jitter, and preventScrollOnSwipe control. |
BottomNavigation |
Component | Semantic <nav role="navigation"> + aria-current="page", iOS/Android env(safe-area-inset-bottom) support, badge overflow formatting (99+), keyboard navigation, and dark/light CSS custom properties. |
npm install @lbcron/react-touch-primitivesimport { usePullToRefresh } from '@lbcron/react-touch-primitives';
export function FeedList({ onReload }: { onReload: () => Promise<void> }) {
const { ref, isRefreshing, pullProgress } = usePullToRefresh({
onRefresh: onReload,
threshold: 80,
maxPullDistance: 150,
damping: 0.6,
});
return (
<div ref={ref} style={{ overflowY: 'auto', height: '100dvh' }}>
<div
aria-live="polite"
style={{
height: `${Math.round(pullProgress * 56)}px`,
opacity: pullProgress,
transition: isRefreshing ? 'none' : 'height 180ms cubic-bezier(0.22, 1, 0.36, 1)',
}}
>
{isRefreshing ? 'Refreshing…' : 'Pull down to refresh'}
</div>
{/* Feed items */}
</div>
);
}import { useSwipeGesture } from '@lbcron/react-touch-primitives';
export function SwipeableCard({ onDismiss, onArchive }: { onDismiss: () => void; onArchive: () => void }) {
const { ref, swipeState } = useSwipeGesture({
threshold: 64,
velocityThreshold: 0.35,
onSwipeLeft: onDismiss,
onSwipeRight: onArchive,
});
return (
<div
ref={ref}
style={{
transform: swipeState.isSwiping ? `translateX(${swipeState.deltaX}px)` : 'none',
touchAction: 'pan-y',
}}
>
Swipe left to dismiss or right to archive
</div>
);
}import { BottomNavigation } from '@lbcron/react-touch-primitives';
export function AppShell({ activeTab, setActiveTab }: { activeTab: string; setActiveTab: (id: string) => void }) {
return (
<BottomNavigation
activeId={activeTab}
onSelect={setActiveTab}
safeAreaBottom
items={[
{ id: 'home', label: 'Overview', icon: <HomeIcon /> },
{ id: 'activity', label: 'Activity', icon: <PulseIcon />, badge: 4 },
{ id: 'settings', label: 'Settings', icon: <SlidersIcon /> },
]}
/>
);
}- Touch-Action Discipline: Follows WCAG 2.1 SC 2.1.1 / 2.5.1 by scoping gesture containers to
touch-action: pan-yon horizontal swipes rather thantouch-action: none, preserving native vertical page scrolling. - Zero Layout Thrashing: Touch deltas are tracked inside
useRefstate machines before committing threshold transitions. - Dual ESM + CJS Bundle: Built with
tsupgenerating tree-shakeable.mjs,.js, and.d.tsentrypoints (/hooks,/components, and root.).
MIT © Ronan Chen-Lopes