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-inapp-back

v0.0.1

Published

React hook for managing browser history in overlays (modals, bottom sheets) to enable back button closing in mobile in-app browsers

Readme

use-inapp-back

A React hook for managing browser history in overlays (modals, bottom sheets, sidebars) to enable native back button behavior in mobile in-app browsers.

Demo Videos

Demo

Problem

In mobile in-app browsers (Instagram, Facebook, KakaoTalk, etc.), when users open a modal or bottom sheet and press the back button, they expect the overlay to close—not the entire page to navigate back. This hook provides a seamless solution by managing browser history states.

Features

  • ✅ Native back button support for overlays
  • ✅ Multiple overlay support with unique IDs
  • ✅ Works with Next.js App Router ('use client' compatible)
  • ✅ TypeScript support with full type definitions
  • ✅ Zero dependencies (only React peer dependency)
  • ✅ Automatic cleanup on unmount
  • ✅ SSR safe

Installation

npm install use-inapp-back
# or
yarn add use-inapp-back
# or
pnpm add use-inapp-back

Live Demo

Try the interactive demos to see use-inapp-back in action:

Run locally:

git clone https://github.com/rami0617/use-inapp-back.git
cd use-inapp-back/demo
npm install
npm run dev

Then open http://localhost:5173 in your browser.

Usage

Basic Example

import { useState } from 'react';
import { useOverlayBack } from 'use-inapp-back';

function MyModal() {
  const [isOpen, setIsOpen] = useState(false);

  const { closeWithBack } = useOverlayBack({
    isOpen,
    onClose: () => setIsOpen(false),
  });

  return (
    <>
      <button onClick={() => setIsOpen(true)}>
        Open Modal
      </button>

      {isOpen && (
        <div className="modal">
          <h2>Modal Title</h2>
          <button onClick={closeWithBack}>
            Close
          </button>
        </div>
      )}
    </>
  );
}

With Multiple Overlays

function MyPage() {
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [isDrawerOpen, setIsDrawerOpen] = useState(false);

  const { closeWithBack: closeModal } = useOverlayBack({
    isOpen: isModalOpen,
    onClose: () => setIsModalOpen(false),
    overlayId: 'main-modal', // Unique ID
  });

  const { closeWithBack: closeDrawer } = useOverlayBack({
    isOpen: isDrawerOpen,
    onClose: () => setIsDrawerOpen(false),
    overlayId: 'side-drawer', // Unique ID
  });

  // ... rest of component
}

With Next.js App Router

'use client'; // Required for Next.js App Router

import { useState } from 'react';
import { useOverlayBack } from 'use-inapp-back';

export default function ModalComponent() {
  // ... implementation
}

API

useOverlayBack(props: UseOverlayBackProps)

Props

| Prop | Type | Required | Description | |------|------|----------|-------------| | isOpen | boolean | ✅ | Whether the overlay is currently open | | onClose | () => void | ✅ | Callback function to close the overlay | | overlayId | string | ❌ | Unique identifier for the overlay (default: 'default-overlay') |

Return Value

| Property | Type | Description | |----------|------|-------------| | closeWithBack | () => void | Function to close the overlay with back navigation |

How It Works

  1. When an overlay opens, the hook pushes a new history entry
  2. If the user presses the back button, the popstate event triggers and closes the overlay
  3. If the user closes via UI (X button), the hook automatically cleans up the history entry
  4. On unmount, any remaining history entries are cleaned up automatically

Browser Compatibility

  • ✅ Modern browsers (Chrome, Firefox, Safari, Edge)
  • ✅ Mobile in-app browsers (Instagram, Facebook, KakaoTalk, Line, etc.)
  • ✅ iOS Safari and Chrome
  • ✅ Android Chrome and Samsung Internet

Use Cases

  • Modal dialogs
  • Bottom sheets
  • Side drawers/sidebars
  • Full-screen overlays
  • Toast notifications with interaction
  • Image lightboxes

License

MIT © rami0617

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues

Found a bug or have a feature request? Please open an issue at GitHub Issues.