npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-swipe-to-close

v1.0.2

Published

React hook for native-like swipe-to-close bottom sheets and modals

Readme

use-swipe-to-close

npm version License: MIT

A React hook for building native-like, swipe-to-close bottom sheets and modals with smooth gestures and smart scroll handling.

✨ Features

  • 📱 Native Feel: Spring animations and physics that match iOS and Android behavior.
  • 🔗 Smart Scroll Chaining: If the content is scrolled, swiping down scrolls it to the top first, then pulls the sheet. No accidental closes!
  • High Performance: Direct transform manipulation using the Web Animations API. No heavy third-party animation libraries.
  • 🛡️ Robust Touch Handling: Prevents text selection, ignores multi-touch conflicts, and allows seamlessly interrupting animations mid-flight.
  • 🪶 Zero Dependencies: Pure React and browser APIs.

📦 Installation

npm install use-swipe-to-close
# or
yarn add use-swipe-to-close
# or
pnpm add use-swipe-to-close

🚀 Usage

Here is a complete, minimal example of how to use the hook.

import React, { useState, useEffect } from 'react';
import { useSwipeToClose } from 'use-swipe-to-close';

export default function BottomSheetDemo() {
  const [isOpen, setIsOpen] = useState(false);

  const { sheetRef, contentRef, animateOpen, animateClose } = useSwipeToClose({
    isOpen,
    onClose: () => setIsOpen(false),
    threshold: 0.25, // Closes if dragged down more than 25% of the sheet's height
    isEnabled: true,
  });

  // Trigger open animation when state changes
  useEffect(() => {
    if (isOpen) {
      // Double RAF ensures the DOM is fully painted before animating
      requestAnimationFrame(() => {
        requestAnimationFrame(() => animateOpen());
      });
    }
  }, [isOpen, animateOpen]);

  if (!isOpen) {
    return <button onClick={() => setIsOpen(true)}>Open Bottom Sheet</button>;
  }

  return (
    <div className="overlay" onClick={() => animateClose()}>
      <div 
        ref={sheetRef} 
        className="sheet"
        onClick={(e) => e.stopPropagation()} // Prevent closing when clicking inside the sheet
      >
        <div className="handle" />
        
        {/* Attach contentRef to the scrollable container! */}
        <div ref={contentRef} className="content">
          <h2>Sheet Title</h2>
          <p>Swipe down to close. If you scroll down first, swiping down will scroll the content back to the top before closing the sheet.</p>
          {Array.from({ length: 20 }).map((_, i) => (
            <div key={i} className="card">
              <h3>Item {i + 1}</h3>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

🎨 Required CSS

For the hook to work perfectly and prevent page bounce/scrolling issues, apply these essential styles to your components:

/* The main sheet container */
.sheet {
  touch-action: none; /* We control gestures via JS */
  user-select: none; /* Prevent text selection on rapid taps */
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  will-change: transform;
}

/* The scrollable content inside the sheet */
.content {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* Smooth inertial scrolling on iOS */
  overscroll-behavior: contain; /* Prevents the body behind the sheet from scrolling */
}

/* Basic overlay styling (optional) */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  align-items: flex-end; /* Bottom sheet alignment */
}