跳转到内容
Nacre
组件配色
EN

抽屉 Drawer

experimentalDrawer · DrawerHeader在画廊运行
Drawer
在浏览器里运行真实组件。

浮在页面右侧的详情面:不压暗、不改页面宽度,左缘可拖宽。多级时由调用方把新的一层叠在同一块面里,DrawerHeader 显示「返回 上一层」。

状态:experimental · 组件:Drawer、DrawerHeader

import { Drawer, DrawerHeader } from 'nacre'

Drawer 放在要被覆盖的区域里,靠右绝对定位,只占自己那一块;页面露出的部分仍可点。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):Esc 报 cancel,在抽屉外按下报 pressOutside 并带落点(调用方可再按区域筛,比如点在表格行上不关)。落在抽屉上、有浮层开着时的按下不报;不给 onDismiss 就不监听。

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)}>
打开详情
</Button>
<Text testID="demo-dismiss-reason" class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$上次收起:${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="会话详情" closeLabel="关闭" onClose={() => open.set(false)} />
<Text class={`p-3 ${TEXT_SIZE.body} text-muted-foreground`}>Esc 或点抽屉外面收起</Text>
</Drawer>
)}
</Presence>
</>
)}
</Frame>
)
}

第 2 层盖住第 1 层,同宽;返回回到上一层,关闭整栈。

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>
)
}

放不下时先压 drawer,最小 480,再窄就盖住更多页面。

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>
)
}

拖左缘时一条参考线跟手,松手后定宽,onWidthChange 交给调用方记忆。

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>
)
}
键 行为
Esc onDismiss 收到 cancel:调用方退一层,只剩一层时关闭
⌥↑ / ⌥↓ 有 stepper 时上一条 / 下一条,由调用方绑定
  • 返回、上一条、下一条、关闭都是带读屏名的按钮,悬停有提示。
  • 拖宽手柄的读屏名由 handleAccessibilityLabel 给。

该用

  • 详情、检查器这类边看列表边看的内容用它
  • 多级时整栈同宽
  • 关闭按钮放在头部最右
  • Esc 与点外面收起交给 onDismiss;同一窗口叠着几块时只给当前那块

不该用

  • 需要用户先完成的任务用它(用 Dialog)
  • 加遮罩压暗页面
  • 多级时每层不同宽
  • 自己挂 0 尺寸的 Overlay 接 Esc 与点外面
属性 类型 默认 说明
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 —

* 必填

属性 类型 默认 说明
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

* 必填