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.
Add it to your project
- 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.
- 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. - 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.