Skip to content
Nacre
Color palette
中文

Drawer

experimentalDrawer · DrawerHeaderOpen in playground
Drawer
Runs the real component in your browser.

A detail surface floating at the right of the page: no dimming, and the page keeps its width; the left edge drags to resize. For more levels the caller stacks the next one on the same surface, and DrawerHeader shows “Back to” the previous one.

Status: experimental · Components: Drawer, DrawerHeader

import { Drawer, DrawerHeader } from 'nacre'

Put Drawer inside the area it covers; it is absolutely positioned on the right and takes only its own part, so the uncovered page stays clickable. Pass that area’s width as room.

import { Text, View, signal } from 'nsh'
import type { JSX } from 'nsh'
import { Button, Drawer, DrawerHeader, Presence, TEXT_SIZE } from 'nacre'
function Frame(props: { testID?: string; height?: number; children?: (room: () => number) => JSX.Element }) {
const room = signal(0)
return (
<View testID={props.testID} class="w-full rounded-lg border border-border bg-background overflow-hidden" style={{ height: props.height ?? 320 }} onLayout={(e) => room.set(Math.floor(e.bounds.size.width))}>
{props.children?.(room)}
</View>
)
}
function FakeTable(props: { onPress?: () => void }) {
return (
<View class="p-3 gap-2">
{['陈志远 · 港股开户', '林嘉怡 · 出金未到账', 'Michael Wong · 二线支持', '周可欣 · 资料补充'].map((row) => (
<Text class={`${TEXT_SIZE.body} text-foreground`}>{row}</Text>
))}
<If when={props.onPress}>
{(press) => (
<Button testID="demo-under" size="sm" variant="secondary" class="self-start" onPress={() => press()()}>
页面上的按钮
</Button>
)}
</If>
</View>
)
}
export function DrawerUsage() {
const open = signal(true)
const n = signal(1)
const hit = signal(false)
return (
<Frame>
{(room) => (
<>
<FakeTable onPress={() => hit.set(true)} />
<If when={hit()}>
<Text class={`px-3 ${TEXT_SIZE.small} text-muted-foreground`}>点到了页面</Text>
</If>
<Presence when={open()}>
{(_, leave) => (
<Drawer width={360} minWidth={280} room={room()} leave={leave}>
<DrawerHeader
testID="demo-drawer"
title={`第 ${n()} 条`}
stepper={{ prevLabel: '上一条', nextLabel: '下一条', onPrev: n() > 1 ? () => n.set(n() - 1) : undefined, onNext: () => n.set(n() + 1) }}
closeLabel="关闭"
onClose={() => open.set(false)}
/>
<Text class={`p-3 ${TEXT_SIZE.body} text-muted-foreground`}>详情内容</Text>
</Drawer>
)}
</Presence>
</>
)}
</Frame>
)
}

onDismiss(reason, at): Escape reports cancel; a press outside the drawer reports pressOutside with where it landed (the caller can filter further, e.g. keep it open for a press on a table row). Presses on the drawer, or while a popover is open, are not reported; without onDismiss nothing listens.

import { Text, View, signal } from 'nsh'
import type { JSX } from 'nsh'
import { Button, Drawer, DrawerHeader, Presence, TEXT_SIZE } from 'nacre'
function Frame(props: { testID?: string; height?: number; children?: (room: () => number) => JSX.Element }) {
const room = signal(0)
return (
<View testID={props.testID} class="w-full rounded-lg border border-border bg-background overflow-hidden" style={{ height: props.height ?? 320 }} onLayout={(e) => room.set(Math.floor(e.bounds.size.width))}>
{props.children?.(room)}
</View>
)
}
export function DrawerDismiss() {
const open = signal(true)
const reason = signal('')
return (
<Frame>
{(room) => (
<>
<View class="p-3 gap-2 items-start">
<Button testID="demo-dismiss-open" size="sm" variant="secondary" onPress={() => open.set(true)}>
Open details
</Button>
<Text testID="demo-dismiss-reason" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$Last dismissed by: ${reason() || '—'}`}</Text>
</View>
<Presence when={open()}>
{(_, leave) => (
<Drawer
width={360}
minWidth={280}
room={room()}
leave={leave}
onDismiss={(why) => {
reason.set(why)
open.set(false)
}}
>
<DrawerHeader testID="demo-dismiss" title="Chat details" closeLabel="Close" onClose={() => open.set(false)} />
<Text class={`p-3 ${TEXT_SIZE.body} text-muted-foreground`}>Escape or a press outside dismisses it</Text>
</Drawer>
)}
</Presence>
</>
)}
</Frame>
)
}

Level 2 covers level 1 at the same width; Back returns to the previous level, Close closes the whole stack.

import { Text, View, signal } from 'nsh'
import type { JSX } from 'nsh'
import { Button, Drawer, DrawerHeader, TEXT_SIZE } from 'nacre'
function Frame(props: { testID?: string; height?: number; children?: (room: () => number) => JSX.Element }) {
const room = signal(0)
return (
<View testID={props.testID} class="w-full rounded-lg border border-border bg-background overflow-hidden" style={{ height: props.height ?? 320 }} onLayout={(e) => room.set(Math.floor(e.bounds.size.width))}>
{props.children?.(room)}
</View>
)
}
function FakeTable(props: { onPress?: () => void }) {
return (
<View class="p-3 gap-2">
{['陈志远 · 港股开户', '林嘉怡 · 出金未到账', 'Michael Wong · 二线支持', '周可欣 · 资料补充'].map((row) => (
<Text class={`${TEXT_SIZE.body} text-foreground`}>{row}</Text>
))}
<If when={props.onPress}>
{(press) => (
<Button testID="demo-under" size="sm" variant="secondary" class="self-start" onPress={() => press()()}>
页面上的按钮
</Button>
)}
</If>
</View>
)
}
export function DrawerStack() {
const sub = signal(false)
return (
<Frame>
{(room) => (
<>
<FakeTable />
<Drawer width={360} minWidth={280} room={room()}>
<If
when={sub()}
else={
<View class="flex-1">
<DrawerHeader testID="demo-stack" title="通话详情" closeLabel="关闭" onClose={() => {}} />
<Button testID="demo-push" size="sm" variant="secondary" class="m-3 self-start" onPress={() => sub.set(true)}>
打开客诉工单
</Button>
</View>
}
>
<View class="flex-1">
<DrawerHeader testID="demo-stack" title="客诉工单" back={{ label: '返回 通话详情', onPress: () => sub.set(false) }} closeLabel="关闭" onClose={() => sub.set(false)} />
<Text class={`p-3 ${TEXT_SIZE.body} text-muted-foreground`}>第 2 层盖住第 1 层,同宽</Text>
</View>
</If>
</Drawer>
</>
)}
</Frame>
)
}

When it doesn’t fit, the drawer narrows first, down to 480; narrower still, it covers more of the page.

import { Text, View, signal } from 'nsh'
import type { JSX } from 'nsh'
import { Button, Drawer, DrawerHeader, TEXT_SIZE } from 'nacre'
function Frame(props: { testID?: string; height?: number; children?: (room: () => number) => JSX.Element }) {
const room = signal(0)
return (
<View testID={props.testID} class="w-full rounded-lg border border-border bg-background overflow-hidden" style={{ height: props.height ?? 320 }} onLayout={(e) => room.set(Math.floor(e.bounds.size.width))}>
{props.children?.(room)}
</View>
)
}
function FakeTable(props: { onPress?: () => void }) {
return (
<View class="p-3 gap-2">
{['陈志远 · 港股开户', '林嘉怡 · 出金未到账', 'Michael Wong · 二线支持', '周可欣 · 资料补充'].map((row) => (
<Text class={`${TEXT_SIZE.body} text-foreground`}>{row}</Text>
))}
<If when={props.onPress}>
{(press) => (
<Button testID="demo-under" size="sm" variant="secondary" class="self-start" onPress={() => press()()}>
页面上的按钮
</Button>
)}
</If>
</View>
)
}
export function DrawerNarrow() {
return (
<Frame testID="demo-narrow-frame">
{(room) => (
<>
<FakeTable />
<Drawer width={855} room={room()}>
<View testID="demo-narrow-surface" class="flex-1">
<DrawerHeader title="容器放不下时最小 480" closeLabel="关闭" onClose={() => {}} />
</View>
</Drawer>
</>
)}
</Frame>
)
}

Dragging the left edge shows a guide line under the pointer; on release the width is set and handed to onWidthChange for the caller to remember.

import { Text, View, signal } from 'nsh'
import type { JSX } from 'nsh'
import { Button, Drawer, DrawerHeader, TEXT_SIZE } from 'nacre'
function Frame(props: { testID?: string; height?: number; children?: (room: () => number) => JSX.Element }) {
const room = signal(0)
return (
<View testID={props.testID} class="w-full rounded-lg border border-border bg-background overflow-hidden" style={{ height: props.height ?? 320 }} onLayout={(e) => room.set(Math.floor(e.bounds.size.width))}>
{props.children?.(room)}
</View>
)
}
function FakeTable(props: { onPress?: () => void }) {
return (
<View class="p-3 gap-2">
{['陈志远 · 港股开户', '林嘉怡 · 出金未到账', 'Michael Wong · 二线支持', '周可欣 · 资料补充'].map((row) => (
<Text class={`${TEXT_SIZE.body} text-foreground`}>{row}</Text>
))}
<If when={props.onPress}>
{(press) => (
<Button testID="demo-under" size="sm" variant="secondary" class="self-start" onPress={() => press()()}>
页面上的按钮
</Button>
)}
</If>
</View>
)
}
export function DrawerWidth() {
const width = signal(480)
return (
<Frame>
{(room) => (
<>
<FakeTable />
<Drawer testID="demo-width" width={width()} minWidth={280} pageMin={160} room={room()} handleAccessibilityLabel="调整宽度" onWidthChange={(w) => width.set(w)}>
<DrawerHeader title={`宽 ${width()}`} closeLabel="关闭" onClose={() => {}} />
</Drawer>
</>
)}
</Frame>
)
}
Key Action
Esc onDismiss hears cancel: the caller goes back one level, or closes the last one
⌥↑ / ⌥↓ Previous / next item when there is a stepper; the caller binds them
  • Back, Previous, Next and Close are buttons with screen reader names and tooltips.
  • The resize handle’s screen reader name comes from handleAccessibilityLabel.

Do

  • Use it for details and inspectors read alongside a list
  • Keep every level of a stack the same width
  • Put Close at the far right of the header
  • Leave Escape and presses outside to onDismiss; with several in one window, give it only to the current one

Don’t

  • Use it for tasks the user must finish first (use Dialog)
  • Dim the page behind it
  • Give each level a different width
  • Mount your own zero-size Overlay for Escape and presses outside

Property descriptions follow the Chinese source comments.

Prop Type Default Description
width * number — 想要的宽度;放不下时先压到 room - pageMin,最小 minWidth,任何时候不超过 room
room number 0 所在区域的宽度(调用方量好传进来);0 表示还没量到
onWidthChange (width: number) => void —
minWidth number —
pageMin number — 左侧至少给页面留的宽度
handleAccessibilityLabel string —
leave Leave —
surfaceRef (node: ViewRef) => void —
onDismiss (reason: 'cancel' | 'pressOutside', at?: Point) => void — 要收起:Esc(cancel)或在抽屉外按下(pressOutside,带落点的窗口坐标,调用方可再按区域筛)。 落在抽屉上、库里有浮层开着(这一下只关浮层)、落点恰为 (0, 0)(headless 的 press 不带坐标,FINDINGS 83)的按下不报。 不给就不监听:同一窗口叠着几块时只给当前那块
testID string —
children JSX.Element —

* required

Prop Type Default Description
title * string —
detail string — 标题后一段次要信息(客户抽屉身份区滚出视野后的账户号),muted、等宽数字,空间不够先截断它
back { label: string; onPress: () => void } — 第 2 层及以上是「返回 上一层」,第 1 层可以是跨页的「返回 来源页」
actions JSX.Element —
stepper DrawerStepper —
closeLabel * string —
onClose * () => void —
testID string — 前缀:生成 -return、-title、-prev、-next、-close

* required