useOverlay

Provides all relevant Overlay behavior and provides a ref for the overlay's container.

useOverlay calls all of the relevant behavior Hooks that all Overlay components and composite components should have and returns a ref to be passed down to the overlay's container.

These behaviors include:

  • Correctly positioning the component based on the values provided to positionSettings and positionDeps.
  • Trapping focus
  • Calling a user provided function when the user presses Escape
  • Calling a user provided function when the user clicks outside of the container
  • Focusing the either a user provided element, or the first focusable element in the container when it is opened.
  • Returning focus to an element when container is closed

Note: useOverlay and Overlay do not manage the open state of the overlay. We leave control of the open state up to the user. All behaviors are built with the assumption that the overlay will not be rendered on the page & fully unmounted when it is not visible. See the examples for details on how to conditionally render a component in JSX.

Examples

Default

Custom first focused element

Prevent overlay from closing when clicking outside the overlay

Prevent focus in the overlay when it is opened

API

Loading data for useOverlay...