vael-ui v0.5.7

Used with a component · Dialog

useDialog

The headless modal behavior behind <Dialog>: a cancelable close, Escape through the shared layer stack, a Tab focus trap, scroll lock, initial focus and focus return, and optional back-button dismiss. It doesn't render an overlay or listen for outside clicks: call requestClose('outside', event) from your own overlay. <Dialog> adds the teleport, overlay, panel, title ids and animations. <Drawer> and <BottomSheet> are built on <Dialog>.

Install

import { useDialog } from 'vael-ui'

Examples

<script setup lang="ts">
import { shallowRef, useTemplateRef } from 'vue'
import { useDialog } from 'vael-ui'

const open = shallowRef(false)
const panelEl = useTemplateRef<HTMLElement>('panel')
const wrapperEl = useTemplateRef<HTMLElement>('wrapper')
const { close, requestClose } = useDialog(open, { panelEl, wrapperEl })
</script>

<template>
  <button @click="open = true">Edit profile</button>
  <Teleport to="body">
    <div v-if="open" ref="wrapper" class="wrapper">
      <div class="overlay" @click="requestClose('outside', $event)" />
      <div ref="panel" role="dialog" aria-modal="true" aria-label="Edit profile" tabindex="-1">
        <input placeholder="Name" />
        <button @click="close">Save</button>
      </div>
    </div>
  </Teleport>
</template>

Parameters

NameTypeDescription
openRef<boolean>Whether the dialog is open (first argument). You set it to true; the dialog sets it to false when it closes.
panelElRef<HTMLElement | null>The dialog panel. Focus is trapped inside it, and it takes focus itself when it holds nothing focusable.
wrapperElRef<HTMLElement | null>The outermost teleported node, holding the overlay and the panel. The layer stack and inert treat it as the dialog, so the overlay stays clickable. Falls back to panelEl.
containerMaybeRefOrGetter<HTMLElement | string | null>Scopes the dialog to one element or selector: the scroll lock, modality and inert apply only inside it, and the rest of the page stays usable. A static container gets position: relative. Omit it for a page-level dialog.
scrollTargetMaybeRefOrGetter<HTMLElement | string | null>The element whose scrolling locks while open. Default: container, then document.body.
closeOnEscMaybeRefOrGetter<boolean>Closes on Escape. Only the topmost layer closes. Default true.
onOpenChange(value: boolean, details: DialogOpenChangeDetails) => voidFires before every close, with value: false. details.reason is 'trigger', 'escape', 'outside', 'history' or 'programmatic'. Call details.cancel() to keep it open. It doesn't fire on open.
beforeClose() => BeforeCloseA getter for the close guard. Return true to close, false or nothing to stay open, or a promise of either. isClosing is true while a promise is pending.
initialFocus() => HTMLElement | nullThe element to focus on open. Return nothing to fall back to an [autofocus] element, then the panel's first focusable element (Dialog tries its body and footer before its header buttons), then the panel.
modalMaybeRefOrGetter<boolean>Locks scroll, makes the page inert and traps Tab. false skips all three; Escape, layer stacking, focus on open and focus return still work. Default true. Read once per open.
closeOnHistoryBackMaybeRefOrGetter<boolean>Closes the dialog on the back button or back gesture, with the 'history' reason, instead of leaving the page. It pushes a history entry on open, and any other close removes it. Default false.
holdScrollUntilExitbooleanKeeps the scroll lock after the close until you call releaseScroll(), so a returning scrollbar doesn't shift the panel during its exit. It releases on its own after 1000 ms. Default false.

Returns

NameTypeDescription
isClosingRef<boolean>true while the beforeClose guard decides.
close() => voidCloses with the 'programmatic' reason, through onOpenChange and beforeClose.
requestClose(reason: DialogCloseReason, event?: Event) => voidCloses with your own reason: 'outside' from an overlay click, 'trigger' from a close button.
cancelClose() => voidKeeps it open and ignores the pending beforeClose answer.
container / scrollTargetRef<HTMLElement | null>The resolved elements, or null. Teleport into container when it is set.
containedComputedRef<boolean>true when container resolved to an element. Position the dialog absolute instead of fixed then.
layerIndex() => numberThis dialog's position in the shared layer stack, or -1 while closed. Add it to your z-index so a dialog opened later stacks on top.
releaseScroll() => voidEnds the lock that holdScrollUntilExit keeps. Call it when your exit animation ends. It does nothing while open.