useDismiss
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.
import { useDismiss } from '@skeletonlabs/floating-ui-svelte';
Usage
<script lang="ts">
import {
useDismiss,
useFloating,
useInteractions,
} from "@skeletonlabs/floating-ui-svelte";
const floating = useFloating();
const dismiss = useDismiss(floating.context);
const interactions = useInteractions([dismiss]);
</script>
<!-- Reference Element -->
<button bind:this={floating.elements.reference} {...interactions.getReferenceProps()}>
Reference
</button>
<!-- Floating Element -->
<div
bind:this={floating.elements.floating}
style={floating.floatingStyles}
{...interactions.getFloatingProps()}
>
Floating
</div>
Options
enabled | Whether the Hook is enabled, including all internal Effects and event handlers. |
escapeKey | Whether to dismiss the floating element upon pressing the `esc` key. |
referencePress | 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 | The type of event to use to determine a “press”. |
outsidePress | Whether to dismiss the floating element upon pressing outside of the floating element. |
outsidePressEvent | The type of event to use to determine an outside “press”. |
ancestorScroll | Whether to dismiss the floating element upon scrolling an overflow ancestor. |
bubbles | Determines whether event listeners bubble upwards through a tree of floating elements. |
capture | 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:
const floating = useFloating({
get open() {
return open;
},
onOpenChange: (value, event, reason) => {
open = value;
if (reason === 'escape-key' || reason === 'outside-press') {
console.log('dismissed');
}
},
});
Compare
Compare to Floating UI React.