Drawer
Slide a panel in from an edge of the viewport. Use it for editing or details while preserving the context of the page behind it.
Import
import { HuiDrawer } from '@nscode/handy-ui'
import type {
DrawerProps,
DrawerDirection,
DrawerEmits,
DrawerFooterSlotProps
} from '@nscode/handy-ui'
Examples
Basic drawer and custom footer
Control visibility with v-model:visible. The default slot is the body; header replaces the title and footer supplies actions. Content remains mounted after the first opening unless destroyOnClose is enabled.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDrawer, HuiInput, HuiSpace } from '@nscode/handy-ui'
const visible = ref(false)
const value = ref('')
</script>
<template>
<HuiButton @click="visible = true">Open drawer</HuiButton>
<HuiDrawer v-model:visible="visible" title="Edit details" :size="360">
<HuiInput v-model="value" placeholder="This value remains after closing" />
<template #footer
><HuiSpace><HuiButton @click="visible = false">Close</HuiButton></HuiSpace></template
>
</HuiDrawer>
</template>
Four directions and size
rtl opens from the right, ltr from the left, ttb from the top and btt from the bottom. size is width for horizontal directions and height for vertical directions. Numbers use px; strings accept CSS lengths.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDrawer, HuiSpace } from '@nscode/handy-ui'
import type { DrawerDirection } from '@nscode/handy-ui'
const visible = ref(false)
const direction = ref<DrawerDirection>('rtl')
const directions: DrawerDirection[] = ['rtl', 'ltr', 'ttb', 'btt']
const open = (value: DrawerDirection) => {
direction.value = value
visible.value = true
}
</script>
<template>
<HuiSpace wrap
><HuiButton v-for="value in directions" :key="value" @click="open(value)">{{
value
}}</HuiButton></HuiSpace
>
<HuiDrawer
v-model:visible="visible"
:direction="direction"
:title="`Direction: ${direction}`"
size="40%"
><p>The selected edge determines the meaning of size.</p></HuiDrawer
>
</template>
Built-in footer and asynchronous confirmation
Enable showFooter for Cancel and Confirm. Returning false, throwing or rejecting from beforeConfirm keeps the drawer open. A pending Promise automatically displays loading and blocks competing actions. User cancellation and other close actions use beforeClose.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDrawer, HuiInput, HuiSpace } from '@nscode/handy-ui'
const visible = ref(false)
const name = ref('')
const state = ref('Not saved')
const save = async () => {
if (!name.value.trim()) {
state.value = 'Enter a name'
return false
}
await new Promise((resolve) => setTimeout(resolve, 500))
state.value = `Saved ${name.value}`
}
</script>
<template>
<HuiSpace direction="vertical" align="start"
><HuiButton @click="visible = true">Edit and save</HuiButton><span>{{ state }}</span></HuiSpace
>
<HuiDrawer v-model:visible="visible" title="Save details" show-footer :before-confirm="save"
><HuiInput v-model="name" placeholder="Name"
/></HuiDrawer>
</template>
Close guards and close reasons
beforeClose receives one reason: close-button, mask, escape or cancel. Only false, a thrown error or a rejected Promise blocks closing; undefined also allows closing. Return a Promise to wait for asynchronous work. A direct parent change to v-model:visible bypasses this guard and emits close without a reason.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDrawer, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
import type { ModalCloseTrigger } from '@nscode/handy-ui'
const visible = ref(false)
const allowed = ref(false)
const state = ref('No close attempt')
const beforeClose = (trigger: ModalCloseTrigger) => {
state.value = trigger
return allowed.value
}
</script>
<template>
<HuiSpace direction="vertical" align="start"
><HuiButton @click="visible = true">Open guarded drawer</HuiButton
><span>{{ state }}</span></HuiSpace
>
<HuiDrawer v-model:visible="visible" title="Close guard" show-footer :before-close="beforeClose">
<HuiSpace align="center"><HuiSwitch v-model="allowed" /><span>Allow closing</span></HuiSpace>
</HuiDrawer>
</template>
Without a mask or header
modal=false keeps the page interactive and disables focus trapping. withHeader=false removes both the title area and close button, so provide another close action. lockScroll defaults to the value of modal and can be set independently.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDrawer, HuiSpace } from '@nscode/handy-ui'
const noMask = ref(false)
const noHeader = ref(false)
const count = ref(0)
</script>
<template>
<HuiSpace wrap
><HuiButton @click="noMask = true">No mask</HuiButton
><HuiButton @click="noHeader = true">No header</HuiButton
><HuiButton @click="count++">Page action: {{ count }}</HuiButton></HuiSpace
>
<HuiDrawer v-model:visible="noMask" title="Page remains interactive" :modal="false" :size="320"
><p>Use the close button or Esc. Clicking the page does not close this drawer.</p></HuiDrawer
>
<HuiDrawer v-model:visible="noHeader" :with-header="false" :size="320"
><HuiButton @click="noHeader = false">Close drawer</HuiButton></HuiDrawer
>
</template>
Mount targets and legacy compatibility
The default target is body, avoiding ancestor transform, filter and overflow restrictions. appendTo accepts a selector or connected HTMLElement. Every opening and target change resolves the target again. A missing target, invalid selector or disconnected element produces a development warning and falls back to body; closing and unmounting still clean up normally.
An initially open drawer resolves an unavailable target after the template's other nodes mount. A target can therefore be placed after the drawer in the same template. Changing between targets moves existing content rather than remounting it.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDrawer, HuiSpace } from '@nscode/handy-ui'
const custom = ref(false)
const inline = ref(false)
</script>
<template>
<HuiSpace wrap
><HuiButton @click="custom = true">Custom target</HuiButton
><HuiButton @click="inline = true">Legacy in-place</HuiButton></HuiSpace
>
<HuiDrawer
v-model:visible="custom"
append-to="#drawer-en-target"
title="Custom target"
:size="340"
><p>The root mounts into the target below.</p></HuiDrawer
>
<div id="drawer-en-target" style="min-height: 80px; border: 1px dashed var(--hui-border-color)">
Mount target
</div>
<HuiDrawer v-model:visible="inline" :append-to-body="false" title="In-place" :size="340"
><p>This root stays at the template position.</p></HuiDrawer
>
</template>
The deprecated appendToBody remains for one version. Explicit false keeps in-place rendering; true mounts to body. An explicit appendTo takes priority, so omit it when using the legacy false setting.
Body mounting changes DOM ancestry. Ancestor selectors, parent scoped :deep() rules, ancestor CSS variable overrides and local data-palette containers no longer reach the moved root through the former ancestors. Native fields moved out of a <form> also lose automatic association with that form. Temporarily preserve those relationships with :append-to-body="false" and no appendTo, or mount into the appropriate container explicitly. Vue provide/inject, slots and component events retain their component relationships.
Reopening and stacking order
Visible overlays receive levels in opening order. The first uses the closest ConfigProvider or application zIndex (2000 by default); later automatic levels sit above existing visible layers. Automatic levels are compacted when layers close, while explicit levels are preserved. Reopening keeps the existing root and its content in place.
Teleported dropdowns, popovers and fixed ActionBars belong to their parent overlay through the component tree. They are hidden while that parent is closed, and still-requested child layers receive a higher level when it reopens.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiDrawer, HuiInput, HuiSpace } from '@nscode/handy-ui'
const first = ref(false)
const second = ref(false)
const value = ref('')
</script>
<template>
<HuiSpace wrap
><HuiButton @click="second = true">Open B alone first</HuiButton
><HuiButton @click="first = true">Open A</HuiButton></HuiSpace
>
<HuiDrawer v-model:visible="first" title="A" :size="520"
><HuiButton @click="second = true">Open B above A</HuiButton></HuiDrawer
>
<HuiDrawer v-model:visible="second" title="B" :size="320"
><HuiInput v-model="value" placeholder="Kept on reopen"
/></HuiDrawer>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| visible | boolean | false | Open state, supports v-model:visible |
| title | string | '' | Title when no header slot is supplied |
| direction | 'rtl' | 'ltr' | 'ttb' | 'btt' | 'rtl' | Edge from which the drawer opens |
| size | number | string | '30%' | Width or height according to direction; numbers use px |
| modal | boolean | true | Display a mask and trap focus |
| appendTo | string | HTMLElement | 'body' | Resolved target; missing or invalid targets fall back to body |
| appendToBody | boolean | — | Deprecated; false keeps in-place rendering, appendTo takes priority |
| lockScroll | boolean | Follows modal | Lock body scrolling |
| closeOnClickModal | boolean | true | Close when clicking the mask |
| closeOnPressEscape | boolean | true | Close the top overlay with Esc |
| showClose | boolean | true | Display the close button |
| withHeader | boolean | true | Display title area and close button |
| beforeClose | (trigger) => boolean | void | Promise<boolean | void> | — | Guard user-initiated closing |
| showFooter | boolean | false | Show built-in actions when footer slot is absent |
| confirmButtonText / cancelButtonText | string | Locale | Visible button text; empty strings are preserved |
| confirmButtonType | ButtonType | 'primary' | Confirm button type |
| showConfirmButton / showCancelButton | boolean | true | Display the corresponding button |
| confirmButtonLoading | boolean | false | Controlled confirmation loading |
| beforeConfirm | () => boolean | void | Promise<boolean | void> | — | Guard built-in confirmation |
| destroyOnClose | boolean | false | Destroy content after leaving |
Events
| Event | Parameters | Description |
|---|---|---|
| update:visible | boolean | Request changing the open state |
| open / opened | — | Opening starts / enter animation finishes |
| close | trigger?: ModalCloseTrigger | 'confirm' | Closing starts; external updates have no trigger |
| closed | — | Leave finishes; also completes closing before delayed initial mounting |
| confirm | — | Confirmation passed, before requesting close |
| cancel | — | Built-in Cancel clicked, before the close guard |
Slots
| Slot | Scope | Description |
|---|---|---|
| default | — | Drawer content |
| header | — | Replace title content |
| footer | { confirm, cancel, confirmLoading } | Replace built-in actions while retaining their flows |
Methods
No instance methods. Change v-model:visible to open or close.
Types
type DrawerDirection = 'rtl' | 'ltr' | 'ttb' | 'btt'
type ModalCloseTrigger = 'close-button' | 'mask' | 'escape' | 'cancel'
interface DrawerFooterSlotProps {
confirm: () => void
cancel: () => void
confirmLoading: boolean
}