抽屉
从视口边缘展示导航或次级任务。
本指南覆盖默认 Emotion 环境的 Material UI Core;扩展包与额外示例按清单单独标注。
使用场景与业务边界
临时与常驻抽屉的模态行为不同;滑动手势不能替代键盘关闭控件。
原生组成与范围
Drawer, SwipeableDrawer
保持 Material UI 原生组合与类型,不替换为 JGuang/Base UI/Radix 同名 API。示例条目不是额外组件。
原生安装与初始化
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1
采用原生 npm 包与 Emotion,不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内,按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置;不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8;React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。
关键原生参数
仅解释本指南选出的关键参数,不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。
| 所属组件.属性 | 用途说明 | 声明来源 |
|---|---|---|
Drawer.open | 受控原生可见性,不获取数据或执行操作。 | Drawer |
Drawer.onClose | 原生关闭请求,reason/event 签名因组件不同,宿主须更新 open。 | Drawer |
Drawer.variant | 所属组件的原生视觉枚举,不可替换为通用 variant 属性。 | Drawer |
Drawer.anchor | 原生 Drawer 出现的视口边缘。 | Drawer |
SwipeableDrawer.onOpen | 原生打开请求,同步所属打开状态。 | SwipeableDrawer |
可运行原生示例
下列机器键只选择示例入口,不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文;JGuang 编写的验收示例明确标注。
- 临时抽屉 ·
TemporaryDrawer· 上游原生示例 - 锚定方向 ·
AnchorTemporaryDrawer· 上游原生示例 - 可滑动抽屉 ·
SwipeableTemporaryDrawer· 上游原生示例 - 边缘滑动 ·
SwipeableEdgeDrawer· 上游原生示例 - 响应式抽屉 ·
ResponsiveDrawer· 上游原生示例 - 常驻抽屉 ·
PersistentDrawerLeft· 上游原生示例 - 常驻抽屉 ·
PersistentDrawerRight· 上游原生示例 - 紧凑变体抽屉 ·
MiniDrawer· 上游原生示例 - 全高度导航 ·
PermanentDrawerLeft· 上游原生示例 - 全高度导航 ·
PermanentDrawerRight· 上游原生示例 - 应用栏下方的抽屉 ·
ClippedDrawer· 上游原生示例
真实使用示例
固定版本的未修改原生示例;复制时同时保留所需的局部辅助文件。
import * as React from 'react';
import Box from '@mui/material/Box';
import Drawer from '@mui/material/Drawer';
import Button from '@mui/material/Button';
import List from '@mui/material/List';
import Divider from '@mui/material/Divider';
import ListItem from '@mui/material/ListItem';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import InboxIcon from '@mui/icons-material/MoveToInbox';
import MailIcon from '@mui/icons-material/Mail';
export default function TemporaryDrawer() {
const [open, setOpen] = React.useState(false);
const toggleDrawer = (newOpen: boolean) => () => {
setOpen(newOpen);
};
const DrawerList = (
<Box sx={{ width: 250 }} role="presentation" onClick={toggleDrawer(false)}>
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem key={text} disablePadding>
<ListItemButton>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItemButton>
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem key={text} disablePadding>
<ListItemButton>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItemButton>
</ListItem>
))}
</List>
</Box>
);
return (
<div>
<Button onClick={toggleDrawer(true)}>Open drawer</Button>
<Drawer open={open} onClose={toggleDrawer(false)}>
{DrawerList}
</Drawer>
</div>
);
}
示例依赖边界
react, @mui/material/Box, @mui/material/Drawer, @mui/material/Button, @mui/material/List, @mui/material/Divider, @mui/material/ListItem, @mui/material/ListItemButton, @mui/material/ListItemIcon, @mui/material/ListItemText, @mui/icons-material/MoveToInbox, @mui/icons-material/Mail, @mui/material/SwipeableDrawer, @emotion/react, @mui/material/styles, @mui/material/CssBaseline, @mui/material/colors, @mui/material/Skeleton, @mui/material/Typography, @mui/material/AppBar, @mui/material/IconButton, @mui/icons-material/Menu, @mui/material/Toolbar, @mui/icons-material/ChevronLeft, @mui/icons-material/ChevronRight
直接导入 Icons/System/Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包,不是额外组件。远程图片和模拟操作不提供后端。
固定上游 API 元数据参考
以下表格保留上游元数据,不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。
Drawer
import Drawer from '@mui/material/Drawer';
import { Drawer } from '@mui/material';
| 属性 | 必需 | 上游元数据类型/约束 | 元数据默认值 |
|---|---|---|---|
anchor | 否 | 'bottom' | 'left' | 'right' | 'top' | 'left' |
children | 否 | node | — |
classes | 否 | object | — |
elevation | 否 | integer | 16 |
hideBackdrop | 否 | bool | false |
ModalProps | 否 | object | {} |
onClose | 否 | func | — |
open | 否 | bool | false |
slotProps | 否 | { backdrop?: func | object, docked?: func | object, paper?: func | object, root?: func | object, transition?: func | object } | {} |
slots | 否 | { backdrop?: elementType, docked?: elementType, paper?: elementType, root?: elementType, transition?: elementType } | {} |
sx | 否 | Array<func | object | bool> | func | object | — |
transitionDuration | 否 | number | { appear?: number, enter?: number, exit?: number } | { enter: theme.transitions.duration.enteringScreen, exit: theme.transitions.duration.leavingScreen, } |
variant | 否 | 'permanent' | 'persistent' | 'temporary' | 'temporary' |
SwipeableDrawer
import SwipeableDrawer from '@mui/material/SwipeableDrawer';
import { SwipeableDrawer } from '@mui/material';
原生继承: Drawer. 父级与 DOM 属性未被伪造为本组件新属性。
| 属性 | 必需 | 上游元数据类型/约束 | 元数据默认值 |
|---|---|---|---|
onClose | 是 | func | — |
onOpen | 是 | func | — |
allowSwipeInChildren | 否 | func | bool | false |
children | 否 | node | — |
disableBackdropTransition | 否 | bool | false |
disableDiscovery | 否 | bool | false |
disableSwipeToOpen | 否 | bool | typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent) |
hysteresis | 否 | number | 0.52 |
minFlingVelocity | 否 | number | 450 |
open | 否 | bool | false |
slotProps | 否 | { backdrop?: func | object, docked?: func | object, paper?: func | object, root?: func | object, swipeArea?: func | object, transition?: func | object } | {} |
slots | 否 | { backdrop?: elementType, docked?: elementType, paper?: elementType, root?: elementType, swipeArea?: elementType, transition?: elementType } | {} |
swipeAreaWidth | 否 | number | 20 |
transitionDuration | 否 | number | { appear?: number, enter?: number, exit?: number } | { enter: theme.transitions.duration.enteringScreen, exit: theme.transitions.duration.leavingScreen, } |
原生源码声明
声明与 import 从固定源码提取,不平铺联合类型或外部继承。英文注释/类型属于原始源码参考,不代表上游正文已全文翻译。共享类型仍位于原生包中。
vendor/src/Drawer/Drawer.d.ts
import * as React from 'react';
import { SxProps } from '@mui/system';
import { Theme } from '../styles';
import { InternalStandardProps as StandardProps } from '../internal';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
import { ModalProps } from '../Modal';
import { BackdropProps } from '../Backdrop';
import { PaperProps } from '../Paper';
import { TransitionProps } from '../transitions/types';
import { DrawerClasses } from './drawerClasses';
export interface DrawerRootSlotPropsOverrides {}
export interface DrawerDockedSlotPropsOverrides {}
export interface DrawerPaperSlotPropsOverrides {}
export interface DrawerTransitionSlotPropsOverrides {}
export interface DrawerBackdropSlotPropsOverrides {}
export interface DrawerSlots {
/**
* The component used for the root when the variant is `temporary`.
* @default Modal
*/
root: React.ElementType;
/**
* The component used for the Modal backdrop.
* @default Backdrop
*/
backdrop: React.ElementType;
/**
* The component used for the root element when the variant is `permanent` or `persistent`.
* @default div
*/
docked: React.ElementType;
/**
* The component used for the paper.
* @default Paper
*/
paper: React.ElementType;
/**
* The component used for the transition.
* [Follow this guide](/material-ui/transitions/#transition-slots) to learn more about the requirements for this component.
* @default Slide
*/
transition: React.ElementType;
}
export type DrawerSlotsAndSlotProps = CreateSlotsAndSlotProps<
DrawerSlots,
{
/**
* Props forwarded to the root slot.
* By default, the available props are based on the [Modal](https://mui.com/material-ui/api/modal/#props) component.
*/
root: SlotProps<React.ElementType<ModalProps>, DrawerRootSlotPropsOverrides, DrawerOwnerState>;
/**
* Props forwarded to the backdrop slot.
* By default, the available props are based on the [Backdrop](https://mui.com/material-ui/api/backdrop/#props) component.
*/
backdrop: SlotProps<
React.ElementType<BackdropProps>,
DrawerBackdropSlotPropsOverrides,
DrawerOwnerState
>;
/**
* Props forwarded to the docked slot.
* By default, the available props are based on a div element.
*/
docked: SlotProps<'div', DrawerDockedSlotPropsOverrides, DrawerOwnerState>;
/**
* Props forwarded to the paper slot.
* By default, the available props are based on the [Paper](https://mui.com/material-ui/api/paper/#props) component.
*/
paper: SlotProps<
React.ElementType<PaperProps>,
DrawerPaperSlotPropsOverrides,
DrawerOwnerState
>;
/**
* Props forwarded to the transition slot.
* By default, the available props are based on the [Slide](https://mui.com/material-ui/api/slide/#props) component.
*/
transition: SlotProps<
React.ElementType<TransitionProps>,
TransitionProps & DrawerTransitionSlotPropsOverrides,
DrawerOwnerState
>;
}
>;
export interface DrawerProps
extends
StandardProps<ModalProps, 'open' | 'children' | 'slots' | 'slotProps'>,
DrawerSlotsAndSlotProps {
/**
* Side from which the drawer will appear.
* @default 'left'
*/
anchor?: 'left' | 'top' | 'right' | 'bottom' | undefined;
/**
* The content of the component.
*/
children?: React.ReactNode;
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<DrawerClasses> | undefined;
/**
* The elevation of the drawer.
* @default 16
*/
elevation?: number | undefined;
/**
* Props applied to the [`Modal`](https://mui.com/material-ui/api/modal/) element.
* @default {}
*/
ModalProps?: Partial<ModalProps> | undefined;
/**
* Callback fired when the component requests to be closed.
* The `reason` parameter can optionally be used to control the response to `onClose`.
*
* @param {object} event The event source of the callback.
* @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`.
*/
onClose?: ModalProps['onClose'] | undefined;
/**
* If `true`, the component is shown.
* @default false
*/
open?: boolean | undefined;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme> | undefined;
/**
* The duration for the transition, in milliseconds.
* You may specify a single timeout for all transitions, or individually with an object.
* @default {
* enter: theme.transitions.duration.enteringScreen,
* exit: theme.transitions.duration.leavingScreen,
* }
*/
transitionDuration?: TransitionProps['timeout'] | undefined;
/**
* The variant to use.
* @default 'temporary'
*/
variant?: 'permanent' | 'persistent' | 'temporary' | undefined;
}
export interface DrawerOwnerState extends Omit<DrawerProps, 'slots' | 'slotProps'> {}
export default function Drawer(props: DrawerProps): React.JSX.Element;
vendor/src/SwipeableDrawer/SwipeableDrawer.d.ts
import * as React from 'react';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
import { DrawerProps, DrawerOwnerState, DrawerSlotsAndSlotProps } from '../Drawer';
export interface SwipeableDrawerSwipeAreaSlotPropsOverrides {}
export interface SwipeableDrawerSlots {
/**
* The component used for the swipeArea slot.
* @default div
*/
swipeArea?: React.ElementType | undefined;
}
type SwipeableDrawerSlotsAndSlotProps = DrawerSlotsAndSlotProps &
CreateSlotsAndSlotProps<
SwipeableDrawerSlots,
{
/**
* Props forwarded to the docked slot.
* By default, the available props are based on a div element.
*/
swipeArea: SlotProps<'div', SwipeableDrawerSwipeAreaSlotPropsOverrides, DrawerOwnerState>;
}
>;
export interface SwipeableDrawerProps
extends
Omit<DrawerProps, 'onClose' | 'open' | 'slots' | 'slotProps'>,
SwipeableDrawerSlotsAndSlotProps {
/**
* If set to true, the swipe event will open the drawer even if the user begins the swipe on one of the drawer's children.
* This can be useful in scenarios where the drawer is partially visible.
* You can customize it further with a callback that determines which children the user can drag over to open the drawer
* (for example, to ignore other elements that handle touch move events, like sliders).
*
* @param {TouchEvent} event The 'touchstart' event
* @param {HTMLDivElement} swipeArea The swipe area element
* @param {HTMLDivElement} paper The drawer's paper element
*
* @default false
*/
allowSwipeInChildren?:
| boolean
| ((event: TouchEvent, swipeArea: HTMLDivElement, paper: HTMLDivElement) => boolean)
| undefined;
/**
* Disable the backdrop transition.
* This can improve the FPS on low-end devices.
* @default false
*/
disableBackdropTransition?: boolean | undefined;
/**
* If `true`, touching the screen near the edge of the drawer will not slide in the drawer a bit
* to promote accidental discovery of the swipe gesture.
* @default false
*/
disableDiscovery?: boolean | undefined;
/**
* If `true`, swipe to open is disabled. This is useful in browsers where swiping triggers
* navigation actions. Swipe to open is disabled on iOS browsers by default.
* @default typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent)
*/
disableSwipeToOpen?: boolean | undefined;
/**
* Affects how far the drawer must be opened/closed to change its state.
* Specified as percent (0-1) of the width of the drawer
* @default 0.52
*/
hysteresis?: number | undefined;
/**
* Defines, from which (average) velocity on, the swipe is
* defined as complete although hysteresis isn't reached.
* Good threshold is between 250 - 1000 px/s
* @default 450
*/
minFlingVelocity?: number | undefined;
/**
* Callback fired when the component requests to be closed.
*
* @param {React.SyntheticEvent<{}>} event The event source of the callback.
*/
onClose: React.ReactEventHandler<{}>;
/**
* Callback fired when the component requests to be opened.
*
* @param {React.SyntheticEvent<{}>} event The event source of the callback.
*/
onOpen: React.ReactEventHandler<{}>;
/**
* If `true`, the component is shown.
* @default false
*/
open?: boolean | undefined;
/**
* The width of the left most (or right most) area in `px` that
* the drawer can be swiped open from.
* @default 20
*/
swipeAreaWidth?: number | undefined;
}
declare const SwipeableDrawer: React.JSXElementConstructor<SwipeableDrawerProps>;
安装后验收
执行严格类型检查、生产构建和实际交互。检查键盘标签/焦点、原生支持的禁用/错误/只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调,不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。
来源与许可
https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e
MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs,不标为全文中文化。