use Dismiss
Closes the floating element when a dismissal is requested — by
default, when the user presses the escape
key or outside of the
floating element with their pointer.
This is useful to ensure the floating element is closed when the user is finished interacting with it, including keyboard support.
Usage
This Hook returns event handler props.
To use it, pass it the context
object returned from
useFloating()
, and then feed its result into the
useInteractions()
array. The returned prop getters are
then spread onto the elements for rendering.
Props
enabled
default: true
Conditionally enable/disable the Hook.
escapeKey
default: true
Whether to dismiss the floating element upon pressing the esc
key.
referencePress
default: false
Whether to dismiss the floating element upon pressing the reference element.
You likely want to ensure the move
option in the
useHover()
hook has been disabled when this is in use.
referencePressEvent
default: 'pointerdown'
The type of event to use to determine a “press”.
outsidePress
default: true
Whether to dismiss the floating element upon pressing outside of both the floating and reference elements.
If you have another element, like a toast, that is rendered outside the floating element’s React tree and don’t want the floating element to close when pressing it, you can guard the check like so:
outsidePressEvent
default: 'pointerdown'
The type of event to use to determine a “press”.
ancestorScroll
default: false
Whether to dismiss the floating element upon scrolling an overflow ancestor.
bubbles
default: undefined
Determines whether event listeners bubble upwards through a tree of floating elements.
escapeKey
determines whether pressing theesc
key bubbles, causing ancestor floating elements to dismiss as well. For instance, if you’re dismissing a tooltip inside a dialog using theesc
key, you likely don’t want the dialog to dismiss as well until a second key press, which is the default behavior.outsidePress
determines whether pressing outside of a child floating element bubbles, causing ancestor floating elements to dismiss as well. Setting this tofalse
requires aFloatingTree
to be set up.
capture
default: undefined
Determines whether to use capture phase event listeners.
Reacting to dismissal
To react to the dismissal event, you can check for the
reason
string in the onOpenChange
callback:
Troubleshooting
Does not close when clicking in an iframe
You can use the
FloatingOverlay
component which will
“cover” iframes to ensure clicks are captured in the same
document as the floating element, as the click occurs on the
overlay backdrop. This guarantees “outside press” detection will
work.