DocsHover & overlays
Get started

Search Subtly documentation

Search by a task, helper name or framework.

Hover & overlays

outsidePress()

Close a popover after an outside click.

Try the example

Open the filters panel, change a checkbox, then click elsewhere. Inside interactions keep it open. Escape also closes the panel and returns focus to its trigger.

Filters closed.

Actual npm package · local demonstration

Add it to your project

  1. Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
  2. Connect the behavior. Copy the JavaScript tab and call its exported mount(element) after your component’s elements exist. Replace simulated delays and data operations with your own application code.
  3. Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.

API and lifecycle

Pass inside() returning the trigger, panel and any portalled descendants, plus dismiss(event). Dismissal occurs only when the same primary pointer starts and ends outside those elements.

Interface
inside() array, dismiss; returns cleanup
Lifecycle
The returned cleanup removes captured document listeners. Re-evaluate inside elements through the callback so moved or portalled panels remain included.

Accessibility and safety

Implementation notes

Initialize after the DOM is available. Keep native controls and keyboard interactions intact; release listeners and timers on unmount.

Add Escape separately and let only the topmost overlay handle it. Focus return and focus containment belong to your overlay component.

What to check

Press inside and release outside: the panel should stay open. Then press and release outside to dismiss, including portalled panel content.