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

@patch-kit/popover

v1.2.0

Published

Popover Engine for React

Readme

@patch-kit/popover

A Popover Engine built with Zustand and React.

This popover focuses on being a purely technical engine — its only job is to handle the mechanics of managing floating content so the application never has to. The application remains fully in control of every visual decision.

The popover automatically handles stacking, z-indexing, viewport boundary correction, safe zone tracking, auto-close on mouse leave, outside-click close, and parent-child cascade closing.

Interactive demo: Default · Tooltip · Dropdown · Context Menu

Folder contents

  • usePopover.tsx — Zustand store, usePopover hook, and shared popover types.
  • Popover.tsx — Single popover instance renderer (positioning + event handling).
  • PopoverRenderer.tsx — Root renderer that portals the popover stack.
  • index.ts — Public exports.

Exports

  • usePopover — Hook that exposes showPopover, closePopover, closeAllPopovers.
  • PopoverRenderer — Component that renders all active popovers to document.body.

How it works

  • Popovers are stored in a stack (PopoverConfig[]) in Zustand.
  • showPopover(anchor, content, options?) pushes a new popover, or replaces an existing one by id.
  • closePopover() closes the most recently opened; closePopover(id) closes a specific one.
  • PopoverRenderer maps the stack to Popover instances and portals them outside the React tree.

Usage

// 1) Render the engine once, near the app root
<PopoverRenderer />

// 2) Open/close popovers from anywhere
const { showPopover, closePopover } = usePopover();

// Dropdown anchored below a button
showPopover(buttonRef.current.getBoundingClientRect(), <MyDropdown options={[...]} />, {
  placement: "bottom",
  safeZone: { trigger: buttonRef.current },
});

// Tooltip on hover
showPopover(buttonRef.current.getBoundingClientRect(), <MyTooltip text="Align left" />, {
  placement: "top",
  safeZone: { trigger: buttonRef.current },
  autoCloseDelay: 150,
});

// Submenu opening to the right of a menu item
showPopover(itemRef.current.getBoundingClientRect(), <MySubmenu />, {
  id: "submenu",
  parentId: "my-dropdown",
  placement: "right",
  safeZone: { trigger: itemRef.current },
  autoCloseDelay: 150,
});

showPopover options

  • id: string — If provided and already open, the popover is replaced in-place.
  • parentId: string — If the parent popover closes, this one closes too (cascade).
  • placement: "top" | "bottom" | "left" | "right" — Preferred render direction. The engine centers the popover on the relevant edge of the anchor rect and flips to the opposite side if the viewport clips. Default: "bottom".
  • gap: number — Pixel distance between the trigger and the popover. Default: 4.
  • safeZone.trigger: HTMLElement — Element that opened the popover. Cursor entering it cancels auto-close.
  • safeZone.container: HTMLElement — Larger parent region. Cursor entering it cancels auto-close.
  • autoCloseDelay: number — ms after cursor leaves all safe zones before closing. 0 closes immediately on leave. undefined disables auto-close entirely.
  • closeOnOutsideClick: boolean — Closes on mousedown outside the popover. Default: false.
  • toggable: boolean — Calling showPopover() with the same id while the popover is already open will close it instead of reopening it. To make it work correctly with closeOnOutsideClick, safeZone.trigger must be set to the element that calls showPopover(). Default: false.
  • onOpen: function — Callback fired when the popover opens.
  • onClose: function — Callback fired when the popover closes.

Placement and viewport correction

The engine uses the DOMRect passed as anchor to compute the correct position:

  • "bottom" / "top" — horizontally centered on the trigger, placed below/above
  • "left" / "right" — vertically centered on the trigger, placed to the left/right

If the computed position clips the viewport, the engine flips to the opposite side. After flipping, the position is hard-clamped with 10px padding from all viewport edges.

Safe zones

Safe zones are HTML elements where the cursor is considered "still engaged" with a popover. They prevent premature close when the cursor briefly passes through whitespace between the trigger and the popover.

[Button]          ← trigger (safe zone 1)
    ↕ gap         ← cursor passes through here — do NOT close
┌─────────┐
│  Menu   │       ← popover content (implicitly safe — cursor is inside)
└─────────┘
┌──────────────────────┐
│  container (panel)   │  ← safe zone 2: anywhere inside = don't close
│                      │
│  [Button]            │  ← safe zone 1
│      ↕ gap           │
│  ┌─────────┐         │
│  │  Menu   │         │
│  └─────────┘         │
└──────────────────────┘

Nested popovers and cascade closing

Each popover is independent. A submenu is opened from inside the parent's content by calling showPopover again with parentId set to the parent's id:

// Inside <MyDropdown /> — consumer code
function MyDropdown() {
  const { showPopover } = usePopover();

  return (
    <button
      onMouseEnter={(e) => {
        showPopover(e.currentTarget.getBoundingClientRect(), <MySubmenu />, {
          id: "submenu",
          parentId: "my-dropdown",
          placement: "right",
          safeZone: { trigger: e.currentTarget },
          autoCloseDelay: 150,
        });
      }}
    >
      More options →
    </button>
  );
}

When "my-dropdown" closes, the engine automatically closes "submenu" and any further descendants.

Behavior details

  • Outside click — Uses mousedown (not click) for reliable capture before other handlers. Checks all rendered [data-popover] elements so clicking inside a sibling popover (e.g. a submenu) does not close the parent.
  • Auto-closemouseleave on the popover div checks relatedTarget against safe zones before starting the timer. Re-entering the popover or any safe zone cancels the timer.
  • Container leave — If a safeZone.container is provided and the cursor leaves it entirely, the popover closes after 100ms unless the cursor moved into another popover.
  • Viewport correction — Runs after mount via useEffect + getBoundingClientRect(). No external positioning library.
  • Cascade closingclosePopover(id) recursively closes all popovers with parentId matching that id before closing itself, firing each onClose callback in order.