Skip to content

About

Zero-dependency React 18/19 touch gesture hooks & mobile UI primitives in TypeScript — usePullToRefresh (spring damping physics), useSwipeGesture & accessible BottomNavigation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

@lbcron/react-touch-primitives

Zero-Dependency React 18/19 Touch Gesture Hooks & Accessible Mobile UI Primitives in TypeScript

TypeScript React 18 / 19 Zero Dependencies License: MIT

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.


Primitives Overview

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.

Installation

npm install @lbcron/react-touch-primitives

Usage

1. usePullToRefresh — Spring-Damped Pull-to-Refresh

import { 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>
  );
}

2. useSwipeGesture — Velocity & Axis-Locked Swipe Detection

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>
  );
}

3. BottomNavigation — Accessible Mobile Tab Bar

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 /> },
      ]}
    />
  );
}

Architecture & Accessibility Notes

  • Touch-Action Discipline: Follows WCAG 2.1 SC 2.1.1 / 2.5.1 by scoping gesture containers to touch-action: pan-y on horizontal swipes rather than touch-action: none, preserving native vertical page scrolling.
  • Zero Layout Thrashing: Touch deltas are tracked inside useRef state machines before committing threshold transitions.
  • Dual ESM + CJS Bundle: Built with tsup generating tree-shakeable .mjs, .js, and .d.ts entrypoints (/hooks, /components, and root .).

License

MIT © Ronan Chen-Lopes

About

Zero-dependency React 18/19 touch gesture hooks & mobile UI primitives in TypeScript — usePullToRefresh (spring damping physics), useSwipeGesture & accessible BottomNavigation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages