Drawer抽屉
Drawer 是从屏幕边缘滑入、覆盖在页面之上的面板。它承载表单、条目详情或一组操作:这类任务对气泡来说太重,又希望用户不离开当前页面完成。它绘制在页面之上,带遮罩时会让背后的页面不可访问。
何时使用
- 适用于用表单创建或编辑一组信息、展示某一行或卡片的详情,或处理对气泡来说太重、又属于页面主任务的子任务。
- 在屏幕中央要求用户作答的对话框是 Modal;无需回应的简短提示是 Message;与页面并排、不妨碍页面使用的面板,则是设置了
mask: false的 Drawer。 - 抽屉内再放抽屉即为嵌套:子抽屉打开时父抽屉会让位。不要叠加超过两层。
代码演示
<script lang="ts">
import { Button, Drawer } from 'svelte-ant-design';
let open = $state(false);
let log = $state('');
// Every opening starts with an empty log, so that the last way to close is the only thing it says.
function show() {
log = '';
open = true;
}
</script>
<!-- bind:open: the close button, the mask and Escape close it by themselves; onclose hears each of them first. -->
<div class="stack">
<Button type="primary" onclick={show}>Open Drawer</Button>
<Drawer
bind:open
title="Basic Drawer"
onclose={() => (log = 'asked to close')}
onafteropenchange={(opened) => {
if (!opened) log = `${log}, then gone`;
}}
>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</Drawer>
<output class="caption">{log}</output>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Drawer, type DrawerPlacement } from 'svelte-ant-design';
const sides: DrawerPlacement[] = ['top', 'right', 'bottom', 'left'];
let open = $state(false);
let placement = $state<DrawerPlacement>('right');
function show(side: DrawerPlacement) {
placement = side;
open = true;
}
</script>
<!-- placement: the side the panel comes from. The sides are physical (left stays on the left in right-to-left); a top or a bottom panel is sized by its height. -->
<div class="stack">
<div class="row">
{#each sides as side (side)}
<Button onclick={() => show(side)}>{side}</Button>
{/each}
</div>
<Drawer bind:open {placement} title="Placement: {placement}">
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, Drawer, type DrawerSize } from 'svelte-ant-design';
const sizes: { label: string; size: DrawerSize }[] = [
{ label: 'default (378 px)', size: 'default' },
{ label: 'large (736 px)', size: 'large' },
{ label: '500', size: 500 },
{ label: "'50%'", size: '50%' },
{ label: "'20vw'", size: '20vw' }
];
let open = $state(false);
let chosen = $state<DrawerSize>('default');
let label = $state('');
function show(entry: (typeof sizes)[number]) {
chosen = entry.size;
label = entry.label;
open = true;
}
</script>
<!-- size: the width of a left or right panel and the height of a top or bottom one. A number is px, a string is any CSS length; the panel is never larger than the page. -->
<div class="stack">
<div class="row">
{#each sizes as entry (entry.label)}
<Button onclick={() => show(entry)}>{entry.label}</Button>
{/each}
</div>
<Drawer bind:open size={chosen} title="size: {label}">
<p>Some contents...</p>
<p>Some contents...</p>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, Drawer } from 'svelte-ant-design';
type Variant = 'extra' | 'end' | 'disabled' | 'none' | 'icon';
const variants: { id: Variant; label: string }[] = [
{ id: 'extra', label: 'extra' },
{ id: 'end', label: "closable.placement: 'end'" },
{ id: 'disabled', label: 'closable.disabled' },
{ id: 'none', label: 'closable: false' },
{ id: 'icon', label: 'closeIcon' }
];
let open = $state(false);
let variant = $state<Variant>('extra');
let log = $state('');
const closable = $derived(variant === 'end' ? { placement: 'end' as const } : variant === 'disabled' ? { disabled: true } : variant === 'none' ? false : true);
function show(id: Variant) {
variant = id;
log = '';
open = true;
}
</script>
{#snippet actions()}
<div class="row">
<Button
size="small"
onclick={() => {
log = 'Cancel pressed';
open = false;
}}>Cancel</Button
>
<Button
size="small"
type="primary"
onclick={() => {
log = 'OK pressed';
open = false;
}}>OK</Button
>
</div>
{/snippet}
{#snippet heart()}
<span aria-hidden="true">♥</span>
{/snippet}
<!-- The header has the close button (at the start by default), the title, and the extra actions at the end. -->
<div class="stack">
<div class="row">
{#each variants as entry (entry.id)}
<Button onclick={() => show(entry.id)}>{entry.label}</Button>
{/each}
</div>
<Drawer
bind:open
title="Drawer with a header"
extra={variant === 'extra' ? actions : undefined}
{closable}
closeIcon={variant === 'icon' ? heart : undefined}
>
<p>Some contents...</p>
<p>{variant === 'disabled' ? 'The close button is shown but does nothing; the mask and Escape still close it.' : 'Some contents...'}</p>
</Drawer>
<output class="caption">{log}</output>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Drawer, Form, FormItem, Input, createForm } from 'svelte-ant-design';
interface Account {
name: string;
email: string;
}
const form = createForm<Account>();
let open = $state(false);
let result = $state('');
// The footer is outside the <Form> in the page, so it asks the form to validate and reads the values from it.
async function save() {
try {
const values = await form.validateFields();
result = `Saved: ${JSON.stringify(values)}`;
open = false;
} catch {
result = '';
}
}
</script>
{#snippet actions()}
<div class="actions">
<Button onclick={() => (open = false)}>Cancel</Button>
<Button type="primary" onclick={save}>Submit</Button>
</div>
{/snippet}
<!-- A form in a Drawer: the task stays in the context of the page. Validation keeps the Drawer open while a field is wrong. -->
<div class="stack">
<Button type="primary" onclick={() => (open = true)}>New account</Button>
<Drawer bind:open title="Create a new account" size="large" footer={actions}>
<Form {form} layout="vertical" initialValues={{ name: '', email: '' }}>
<FormItem name="name" label="Name" rules={[{ required: true, message: 'Please enter a name' }]}>
{#snippet children(field)}
<Input bind:value={field.value} status={field.status} placeholder="Please enter a name" {...field.attrs} />
{/snippet}
</FormItem>
<FormItem name="email" label="Email" rules={[{ required: true, message: 'Please enter an email' }]}>
{#snippet children(field)}
<Input bind:value={field.value} status={field.status} placeholder="Please enter an email" {...field.attrs} />
{/snippet}
</FormItem>
</Form>
</Drawer>
{#if result}<output class="caption">{result}</output>{/if}
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.actions {
display: flex;
justify-content: flex-end;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
a control of the page
<script lang="ts">
import { Button, Drawer } from 'svelte-ant-design';
type Kind = 'dimmed' | 'fixed' | 'none' | 'keys';
const kinds: { id: Kind; label: string }[] = [
{ id: 'dimmed', label: 'Dimmed mask' },
{ id: 'fixed', label: 'mask.closable: false' },
{ id: 'none', label: 'No mask' },
{ id: 'keys', label: 'keyboard: false' }
];
let open = $state(false);
let kind = $state<Kind>('dimmed');
let clicks = $state(0);
const mask = $derived(kind === 'fixed' ? { closable: false } : kind === 'none' ? false : true);
function show(id: Kind) {
kind = id;
open = true;
}
</script>
<!-- A mask dims the page and makes the Drawer modal (the page is locked and out of reach). With no mask it is a panel beside the page: the page works, scrolls, and Escape closes it only while focus is inside. -->
<div class="stack">
<div class="row">
{#each kinds as entry (entry.id)}
<Button onclick={() => show(entry.id)}>{entry.label}</Button>
{/each}
</div>
<div class="field">
<span class="caption">a control of the page</span>
<Button onclick={() => clicks++}>Count a click</Button>
<output class="caption">clicks: {clicks}</output>
</div>
<Drawer bind:open title="Drawer: {kinds.find((entry) => entry.id === kind)?.label}" {mask} keyboard={kind !== 'keys'}>
<p>{kind === 'none' ? 'The page behind is not dimmed: press "Count a click" while this panel is open.' : 'Press the mask, or Escape.'}</p>
<p>{kind === 'fixed' ? 'The mask does not close it; the close button and Escape do.' : ''}</p>
<p>{kind === 'keys' ? 'Escape does nothing here; the mask and the close button close it.' : ''}</p>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
push of the parent
<script lang="ts">
import { Button, Drawer, type DrawerPlacement } from 'svelte-ant-design';
let open = $state(false);
let inner = $state(false);
let side = $state<DrawerPlacement>('right');
let pushed = $state<'default' | 'off' | 'near'>('default');
const push = $derived(pushed === 'off' ? false : pushed === 'near' ? { distance: 60 } : true);
const labels = { default: 'push: true (180 px)', off: 'push: false', near: 'push: { distance: 60 }' };
function show(next: DrawerPlacement) {
side = next;
inner = true;
}
</script>
<!-- A Drawer written inside another one is nested: while it is open the parent moves aside by its own push distance (only for a child of the same side). Only the lowest layer dims the page: the child keeps a clear mask that still takes the press outside it. -->
<div class="stack">
<div class="field">
<span class="caption">push of the parent</span>
<div class="row">
{#each Object.entries(labels) as [id, label] (id)}
<Button type={pushed === id ? 'primary' : 'default'} onclick={() => (pushed = id as typeof pushed)}>{label}</Button>
{/each}
</div>
</div>
<Button type="primary" onclick={() => (open = true)}>Open drawer</Button>
<Drawer bind:open title="Parent Drawer" size={520} {push}>
<div class="stack">
<div class="row">
<Button type="primary" onclick={() => show('right')}>Two-level drawer</Button>
<Button onclick={() => show('left')}>From the other side</Button>
</div>
<p>The child comes from the {side}. The parent moves only when it is of the same side.</p>
</div>
<Drawer bind:open={inner} title="Child Drawer" size={320} placement={side}>
<p>This is the second level.</p>
</Drawer>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
container={false}
Render in current DOM
a control of the page, outside the region
<script lang="ts">
import { Button, Drawer } from 'svelte-ant-design';
let open = $state(false);
let clicks = $state(0);
</script>
<!-- container={false} leaves the Drawer where it is written: it covers its nearest positioned parent only and is a panel of that region, not a modal. The rest of the page works. -->
<div class="stack">
<div class="field">
<span class="caption">container={'{false}'}</span>
<div class="region">
<Button type="primary" onclick={() => (open = true)}>Open in this region</Button>
<p>Render in current DOM</p>
<Drawer bind:open container={false} title="Basic Drawer" size={240}>
<p>Its layer covers the region only.</p>
</Drawer>
</div>
</div>
<div class="field">
<span class="caption">a control of the page, outside the region</span>
<Button onclick={() => clicks++}>Count a click</Button>
<output class="caption">clicks while the panel is open: {clicks}</output>
</div>
</div>
<style>
.stack {
display: grid;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.region {
position: relative;
box-sizing: border-box;
justify-self: stretch;
min-height: 260px;
padding: 16px;
border: 1px dashed var(--ads-color-border);
border-radius: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
default: the content stays after a close
destroyOnHidden
forceRender
<script lang="ts">
import { onMount } from 'svelte';
import { Button, Drawer } from 'svelte-ant-design';
let kept = $state(false);
let destroyed = $state(false);
let forced = $state(false);
let present = $state('');
// forceRender: the content is in the page, hidden, before the first opening.
onMount(() => {
present = document.querySelector('[data-forced-content]') ? 'yes' : 'no';
});
</script>
<!-- destroyOnHidden removes the content on every close (what was typed is gone); forceRender puts it in the page before the first opening. -->
<div class="stack">
<div class="field">
<span class="caption">default: the content stays after a close</span>
<Button onclick={() => (kept = true)}>Keep the content</Button>
</div>
<div class="field">
<span class="caption">destroyOnHidden</span>
<Button onclick={() => (destroyed = true)}>Remove the content</Button>
</div>
<div class="field">
<span class="caption">forceRender</span>
<Button onclick={() => (forced = true)}>Pre-rendered</Button>
<output class="caption">its content is in the page before the first opening: {present}</output>
</div>
<Drawer bind:open={kept} title="Keeps its content">
<input aria-label="Note" placeholder="Type, close, open again" />
</Drawer>
<Drawer bind:open={destroyed} destroyOnHidden title="Removes its content">
<input aria-label="Note" placeholder="Type, close, open again" />
</Drawer>
<Drawer bind:open={forced} forceRender title="Pre-rendered">
<p data-forced-content>This paragraph was in the page from the start.</p>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Drawer } from 'svelte-ant-design';
let open = $state(false);
</script>
<!-- class and style go on the panel, rootClassName and rootStyle on the layer; classNames and styles reach each part (root, mask, wrapper, section, header, title, close, extra, body, footer, dragger). zIndex sets the level of the layer. -->
<div class="stack">
<Button onclick={() => (open = true)}>Open a styled Drawer</Button>
<Drawer
bind:open
title="Custom semantic styling"
footer="Footer"
class="styled-panel"
style="border-radius: 12px 0 0 12px"
rootClassName="styled-root"
rootStyle="--demo-accent: #722ed1"
zIndex={1010}
classNames={{ title: 'styled-title', body: 'styled-body' }}
styles={{
header: { background: 'var(--demo-accent)' },
mask: { 'background-color': 'rgba(114, 46, 209, 0.25)' },
footer: { 'text-align': 'center' }
}}
>
<p>A class on the panel, a style on the layer, a class and a style on three parts.</p>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
:global(.styled-title) {
color: #ffffff;
}
:global(.styled-body) {
font-style: italic;
}
</style>
<script lang="ts">
import type { Snippet } from 'svelte';
import { Button, Drawer } from 'svelte-ant-design';
let open = $state(false);
</script>
{#snippet frame(content: Snippet)}
<div class="frame">{@render content()}</div>
{/snippet}
<!-- drawerRender gets the content of the panel (header, body, footer) as a snippet and may wrap it. -->
<div class="stack">
<Button onclick={() => (open = true)}>Open Drawer</Button>
<Drawer bind:open title="Custom drawer content" footer="Footer" drawerRender={frame}>
<p>The whole content of the panel sits inside a framed box.</p>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.frame {
display: flex;
flex: 1 1 auto;
flex-direction: column;
min-height: 0;
border-inline-start: 4px solid var(--ads-color-primary);
}
</style>
<script lang="ts">
import { Button, Drawer } from 'svelte-ant-design';
type Kind = 'default' | 'free' | 'stay';
let open = $state(false);
let kind = $state<Kind>('default');
const focusable = $derived(kind === 'free' ? { trap: false } : kind === 'stay' ? { focusTriggerAfterClose: false } : undefined);
function show(next: Kind) {
kind = next;
open = true;
}
</script>
<!-- By default focus goes to the panel, Tab stays inside and focus goes back to the button that opened it. focusable turns the trap or the return off. -->
<div class="stack">
<div class="row">
<Button onclick={() => show('default')}>Default</Button>
<Button onclick={() => show('free')}>trap: false</Button>
<Button onclick={() => show('stay')}>focusTriggerAfterClose: false</Button>
</div>
<Drawer bind:open title="focusable: {kind}" {focusable}>
<p>Press Tab: {kind === 'free' ? 'it leaves the Drawer for the page.' : 'it goes around inside.'}</p>
<input aria-label="A field" placeholder="A field" />
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, Drawer, Select, type SelectValue } from 'svelte-ant-design';
let open = $state(false);
let city = $state<SelectValue | undefined>('lisbon');
</script>
<!-- The list of a Select opens above the Drawer and takes typing, Escape closes the list first and the Drawer on the second press. -->
<div class="stack">
<Button onclick={() => (open = true)}>A Drawer with a Select</Button>
<Drawer bind:open title="Where to?">
<div class="field">
<span class="caption">City</span>
<Select aria-label="City" showSearch options={['lisbon', 'porto', 'faro']} bind:value={city} style="width: 200px" />
</div>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, Drawer } from 'svelte-ant-design';
let open = $state(false);
const lines = Array.from({ length: 40 }, (_, index) => index + 1);
</script>
<!-- A long body scrolls on its own: the header and the footer stay in place, and the page behind does not scroll. -->
<div class="stack">
<Button onclick={() => (open = true)}>Open a long Drawer</Button>
<Drawer bind:open title="A long body" footer="The footer stays at the bottom">
{#each lines as line (line)}
<p>Line {line}</p>
{/each}
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
</style>
<script lang="ts">
import { Button, Drawer, type DrawerPlacement } from 'svelte-ant-design';
let open = $state(false);
let placement = $state<DrawerPlacement>('right');
let log = $state('');
function show(side: DrawerPlacement) {
placement = side;
log = '';
open = true;
}
</script>
<!-- resizable draws a strip on the inner edge. Drag it, or focus it and press the arrow keys (10 px, Shift 50 px), Home and End. minSize and maxSize (px) set the stops; the size stays after a close. -->
<div class="stack">
<div class="row">
<Button onclick={() => show('right')}>right</Button>
<Button onclick={() => show('bottom')}>bottom</Button>
</div>
<Drawer
bind:open
{placement}
title="Resizable Drawer"
size={320}
minSize={240}
maxSize={640}
resizable={{
onresizestart: () => (log = 'resizing...'),
onresize: (size) => (log = `size: ${size} px`),
onresizeend: () => (log = `${log} (done)`)
}}
>
<p>Drag the strip on the inner edge, or press the arrow keys when it has focus.</p>
<output class="caption">{log}</output>
</Drawer>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
API
Drawer 的属性
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| open | 抽屉是否显示。支持 bind:open:关闭操作会将其设为 false。 | boolean | false |
| title | 标题,同时也是面板的名称。0 会显示。没有标题时请给抽屉设置 aria-label。 | string | number | Snippet | none |
| extra | 头部末端的操作区。仅设置 extra 也会渲染头部。 | string | number | Snippet | none |
| children | 正文,自行滚动。 | Snippet | none |
| footer | 位于面板底部的页脚。不设置则没有页脚。 | string | number | Snippet | none |
| placement | 面板滑出的方向。方向为物理方向。 | 'top' | 'right' | 'bottom' | 'left' | 'right' |
| size | 左右面板的宽度、上下面板的高度。default 为 378 px,large 为 736 px;数字为 px;字符串为 CSS 长度。取代已弃用的 width 与 height。 | 'default' | 'large' | number | string | 'default' |
| closable | 关闭按钮。false 移除;disabled 显示但不起作用;placement 为逻辑方向,从右到左时会镜像;aria-label 是它的名称(默认 Close)。 | boolean | { closeIcon?: Snippet; disabled?: boolean; placement?: 'start' | 'end'; 'aria-label'?: string } | true |
| closeIcon | 替换叉号。要隐藏按钮请使用 closable: false。 | Snippet | a cross |
| mask | 半透明遮罩。只有已打开图层(抽屉与对话框)中最底层会让页面变暗,其上的图层保留一层透明遮罩,仍可响应点击。closable(默认 true):点击遮罩会请求关闭。没有遮罩时抽屉不是模态的:页面不被锁定、可访问,Tab 不被限制,aria-modal 为 false。 | boolean | { enabled?: boolean; closable?: boolean } | true |
| keyboard | Escape 关闭抽屉。模态抽屉在位于最上层时响应 Escape;非模态抽屉仅在焦点位于其内部时响应。 | boolean | true |
| push | 同一侧的嵌套抽屉打开时,当前抽屉如何让位。false 表示不动;数字为 px,字符串为 CSS 长度。 | boolean | { distance?: string | number } | { distance: 180 } |
| zIndex | 层级的 z-index(令牌 --ads-drawer-z-index):与 Modal 同级,低于弹出层。 | number | 1000 |
| container | 图层放置的位置:元素、函数或选择器。false 保留在书写位置:只覆盖最近的定位父元素,且不是模态。 | HTMLElement | (() => HTMLElement) | string | false | document.body |
| destroyOnHidden | 每次关闭时移除内容。 | boolean | false |
| forceRender | 在首次打开前就把抽屉放入页面(隐藏)。 | boolean | false |
| focusable | 焦点。trap:Tab 与 Shift+Tab 保持在内部(模态抽屉)。focusTriggerAfterClose:关闭后焦点回到原处。打开时焦点移至面板本身。 | { trap?: boolean; focusTriggerAfterClose?: boolean } | { trap: true, focusTriggerAfterClose: true } |
| resizable | 内侧边缘的拖动条,可用指针或方向键移动(可聚焦的 separator)。回调以 px 报告尺寸;一次按键算一次操作。关闭后尺寸保持,size 或 placement 变化时重置。 | boolean | { onresizestart?: () => void; onresize?: (size: number) => void; onresizeend?: () => void; 'aria-label'?: string } | false |
| maxSize | 调整大小时的最大尺寸(px)。不会超过容器。 | number | the container |
| minSize | 调整大小时的最小尺寸(px)。这是我们的补充:React 没有最小值,面板可缩到 7 px。 | number | 100 |
| drawerRender | 以 snippet 形式接收面板内容(头部、正文、页脚),可进行包装。 | Snippet<[Snippet]> | none |
| class | 面板(section)上的类名。 | string | none |
| style | 面板上的样式。 | string | none |
| rootClassName | 包含遮罩与面板的外层上的类名。 | string | none |
| rootStyle | 外层上的样式。 | string | none |
| classNames | 各部分的类名:root, mask, wrapper, section, header, title, close, extra, body, footer, dragger。 | { root?: string; mask?: string; wrapper?: string; section?: string; header?: string; title?: string; close?: string; extra?: string; body?: string; footer?: string; dragger?: string } | none |
| styles | 相同各部分的内联样式。 | { root?: SemanticStyle; mask?: SemanticStyle; wrapper?: SemanticStyle; section?: SemanticStyle; header?: SemanticStyle; title?: SemanticStyle; close?: SemanticStyle; extra?: SemanticStyle; body?: SemanticStyle; footer?: SemanticStyle; dragger?: SemanticStyle } | none |
| onclose | 关闭按钮、遮罩或 Escape 请求关闭。调用之后抽屉会关闭,除非调用了 event.preventDefault()。 | (event: MouseEvent | KeyboardEvent) => void | none |
| onafteropenchange | 进入动画(true)或离开动画(false)结束后触发;在 prefers-reduced-motion 下立即触发。 | (open: boolean) => void | none |
与 Ant Design 的差异:关闭按钮、遮罩与 Escape 会关闭抽屉,除非
onclose 调用了 event.preventDefault()(React 把关闭交给你);焦点移到带有角色与名称的面板上;无遮罩或原位渲染的抽屉不是模态;resizable 支持键盘并有最小值(minSize);size 取代已弃用的 width 与 height。暂未提供:loading 与 mask.blur。不提供:通过代码打开抽屉的服务(只有 Angular 有)。设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-drawer-z-index | 图层层级。 | 1000 |
| --ads-drawer-mask-bg | 遮罩颜色。 | rgba(0, 0, 0, 0.45) |
| --ads-drawer-bg | 面板背景。 | #ffffff |
| --ads-drawer-size | default 尺寸。 | 378px |
| --ads-drawer-size-large | large 尺寸。 | 736px |
| --ads-drawer-shadow-right | 右侧面板的阴影(向左投射)。 | -6px 0 16px 0 rgba(0, 0, 0, 0.08), -3px 0 6px -4px rgba(0, 0, 0, 0.12), -9px 0 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-drawer-shadow-left | 左侧面板的阴影。 | 6px 0 16px 0 rgba(0, 0, 0, 0.08), 3px 0 6px -4px rgba(0, 0, 0, 0.12), 9px 0 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-drawer-shadow-top | 顶部面板的阴影。 | 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-drawer-shadow-bottom | 底部面板的阴影。 | 0 -6px 16px 0 rgba(0, 0, 0, 0.08), 0 -3px 6px -4px rgba(0, 0, 0, 0.12), 0 -9px 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-drawer-header-padding | 头部内边距。 | 16px 24px |
| --ads-drawer-header-border | 头部下方的分隔线。 | rgba(5, 5, 5, 0.06) |
| --ads-drawer-title-color | 标题颜色。 | rgba(0, 0, 0, 0.88) |
| --ads-drawer-title-font-size | 标题字号。 | 16px |
| --ads-drawer-title-line-height | 标题行高。 | 24px |
| --ads-drawer-title-font-weight | 标题字重。 | 600 |
| --ads-drawer-close-size | 关闭按钮尺寸。 | 24px |
| --ads-drawer-close-gap | 关闭按钮与标题的间距。 | 8px |
| --ads-drawer-close-color | 关闭按钮颜色。 | rgba(0, 0, 0, 0.45) |
| --ads-drawer-close-color-hover | 悬停时关闭按钮的颜色。 | rgba(0, 0, 0, 0.88) |
| --ads-drawer-close-bg-hover | 悬停时关闭按钮的填充色。 | rgba(0, 0, 0, 0.06) |
| --ads-drawer-body-color | 文本颜色。 | rgba(0, 0, 0, 0.88) |
| --ads-drawer-body-padding | 正文内边距。 | 24px |
| --ads-drawer-footer-padding | 页脚内边距。 | 8px 16px |
| --ads-drawer-footer-border | 页脚上方的分隔线。 | rgba(5, 5, 5, 0.06) |
| --ads-drawer-motion-duration | 滑动与遮罩淡入淡出的时长。 | 0.3s |
| --ads-drawer-motion-ease | 滑动与淡入淡出的曲线。 | ease |
| --ads-drawer-dragger-size | 调整大小拖动条可见线条的宽度。 | 4px |
| --ads-drawer-dragger-hit | 可响应点击的区域宽度,一半位于面板之外。 | 8px |
| --ads-drawer-dragger-hover-bg | 悬停、聚焦与拖动时拖动条的颜色。 | var(--ads-color-primary) |
无障碍
- 面板为
role="dialog",由标题命名(aria-labelledby)。没有标题时请设置aria-label(开发环境会给出警告)。模态抽屉带有aria-modal="true";无遮罩或原位渲染的抽屉为aria-modal="false"。 - 焦点移入面板本身(而不是关闭按钮,因此会先读出标题)。模态抽屉中 Tab 与 Shift+Tab 保持在内部:最后一个元素上按 Tab 会回到第一个。抽屉关闭后焦点回到打开它的按钮。
- 模态抽屉背后的页面无法通过键盘、指针和辅助技术访问(
inert;在不支持inert的浏览器中使用aria-hidden)。无遮罩或原位渲染的抽屉保持页面可访问,也不限制 Tab。 - Escape 关闭位于最上层的抽屉;内部有展开的 Select 时,Escape 先关闭列表。无遮罩的抽屉仅在焦点位于其内部时响应 Escape。结束输入法组合的按键不会关闭任何内容。
keyboard: false关闭 Escape。 - 模态抽屉在离开动画结束前,会阻止页面滚动,并在滚动条消失时避免页面横向跳动。未处理:iOS 16 之前的 Safari 会在手指下滚动已锁定的页面。
- 调整大小的拖动条是可聚焦的
separator(窗口分隔条模式),带有当前值、范围和名称(Resize);方向键、Home 与 End 可移动它。React 的拖动条没有角色和键盘支持,面板可缩到几个像素;这里minSize默认为 100 px。 - 对比度:标题与文本在面板上约 15:1,关闭图标 3:1(非文本对比度)。拖动条在悬停与聚焦时使用主色。强制颜色模式下面板内侧边缘有边框,关闭按钮与拖动条保持可见。在
prefers-reduced-motion下滑动动画停止。 - 已知限制:从弹出层内部(例如 Select 的列表)打开的抽屉位于该弹出层之外,因此在其中点击会关闭弹出层并连带关闭抽屉。请从页面上打开,或通过
container为其指定位置。 - 未检查:Orca 之外的真实读屏软件(NVDA、JAWS、VoiceOver、手机)、真实触控设备以及真实的 Safari。已检查:Orca 搭配 Firefox(文档从模态抽屉内部开始、Tab 在内部循环、拖动条被读作带数值的分隔条),以及 Chromium、Firefox 与 WebKit 中的按键、焦点和背后页面,模拟 Chromium 中的触摸拖动,以及最低支持的浏览器版本。