Drawer 抽屉
用于在屏幕边缘显示应用导航等内容的面板。
规则
- 是 Android 推荐的导航方式,常见于该平台应用。
代码演示
基本
遮罩层模式
import { Drawer, List, NavBar } from 'antd-mobile';
class App1 extends React.Component {
state = {
open: true,
}
onOpenChange = (...args) => {
console.log(args);
this.setState({ open: !this.state.open });
}
render() {
// fix in codepen
const sidebar = (<List>
{[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].map((i, index) => {
if (index === 0) {
return (<List.Item key={index}
thumb="https://zos.alipayobjects.com/rmsportal/eOZidTabPoEbPeU.png"
multipleLine
>Category</List.Item>);
}
return (<List.Item key={index}
thumb="https://zos.alipayobjects.com/rmsportal/eOZidTabPoEbPeU.png"
>Category{index}</List.Item>);
})}
</List>);
return (<div>
<NavBar iconName="ellipsis" onLeftClick={this.onOpenChange}>Basic</NavBar>
<Drawer
className="my-drawer"
style={{ minHeight: document.documentElement.clientHeight - 200 }}
enableDragHandle
contentStyle={{ color: '#A6A6A6', textAlign: 'center', paddingTop: 42 }}
sidebar={sidebar}
open={this.state.open}
onOpenChange={this.onOpenChange}
>
Click upper-left corner icon
</Drawer>
</div>);
}
}
ReactDOM.render(<App1 />, mountNode);
.my-drawer {
position: relative;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
.my-drawer .am-drawer-sidebar {
background-color: #fff;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
.my-drawer .am-drawer-sidebar .am-list {
width: 6rem;
padding: 0;
}
嵌入文档模式
嵌入到文档流中
import { Drawer, List, NavBar } from 'antd-mobile';
class App extends React.Component {
state = {
docked: false,
}
onDock = (d) => {
this.setState({
[d]: !this.state[d],
});
}
render() {
const sidebar = (<List>
{[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].map((i, index) => {
if (index === 0) {
return (<List.Item key={index}
thumb="https://zos.alipayobjects.com/rmsportal/eOZidTabPoEbPeU.png"
multipleLine
>Category</List.Item>);
}
return (<List.Item key={index}
thumb="https://zos.alipayobjects.com/rmsportal/eOZidTabPoEbPeU.png"
>Category{index}</List.Item>);
})}
</List>);
return (<div style={{ height: '100%' }}>
<NavBar iconName="ellipsis" onLeftClick={() => this.onDock('docked')}>Docked in document</NavBar>
<Drawer
className="my-drawer"
style={{ minHeight: document.documentElement.clientHeight - 200 }}
contentStyle={{ color: '#A6A6A6', textAlign: 'center', paddingTop: 42 }}
sidebarStyle={{ border: '1px solid #ddd' }}
sidebar={sidebar}
docked={this.state.docked}
>
Click upper-left corner icon
</Drawer>
</div>);
}
}
ReactDOM.render(<App />, mountNode);
.my-drawer {
position: relative;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
.my-drawer .am-drawer-sidebar {
background-color: #fff;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
.my-drawer .am-drawer-sidebar .am-list {
width: 6rem;
padding: 0;
}
API
适用平台:WEB、React-Native
属性 | 说明 | 类型 | 默认值 |
---|
sidebar | 抽屉里的内容 | ReactNode | - |
onOpenChange | open 状态切换时调用 | (open: bool): void | - |
open | 开关状态 | Boolean | false |
position | 抽屉所在位置 | String | 'left', enum{'left', 'right', 'top'(web only ), 'bottom'(web only )} |
sidebarStyle (web only ) | - | Object | {} |
contentStyle (web only ) | - | Object | {} |
overlayStyle(web only ) | - | Object | {} |
dragHandleStyle(web only ) | - | Object | {} |
touch(web only ) | 是否开启触摸手势 | Boolean | true |
transitions(web only ) | 是否开启动画 | Boolean | true |
docked(web only ) | 是否嵌入到正常文档流里 | Boolean | false |
enableDragHandle(web only ) | 是否禁止 dragHandle | Boolean | false |
dragToggleDistance(web only ) | 打开关闭抽屉时距 sidebar 的拖动距离 | Number | 30 |
drawerWidth (rn only ) | 抽屉宽度 | Number | 300 |
drawerBackgroundColor (rn only ) | 指定抽屉背景色 | String | - |
openDrawer (rn only ) | 打开函数 | (): void | - |
closeDrawer (rn only ) | 关闭函数 | (): void | - |