Drawer 抽屉
用于在屏幕边缘显示应用导航等内容的面板。
规则
- 是 Android 推荐的导航方式,常见于该平台应用。
代码演示
基本
遮罩层模式
import { Drawer, List, NavBar, Icon } 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 icon={<Icon type="ellipsis" />} onLeftClick={this.onOpenChange}>Basic</NavBar>
<Drawer
className="my-drawer"
style={{ minHeight: document.documentElement.clientHeight }}
enableDragHandle
contentStyle={{ color: '#A6A6A6', textAlign: 'center', paddingTop: 42 }}
sidebar={sidebar}
open={this.state.open}
onOpenChange={this.onOpenChange}
>
Click upper-left corner
</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: 300px;
padding: 0;
}
嵌入文档模式
嵌入到文档流中
import { Drawer, List, NavBar, Icon } 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 icon={<Icon type="ellipsis" />} onLeftClick={() => this.onDock('docked')}>
Docked in document
</NavBar>
<Drawer
className="my-drawer"
style={{ minHeight: document.documentElement.clientHeight }}
contentStyle={{ color: '#A6A6A6', textAlign: 'center', paddingTop: 42 }}
sidebarStyle={{ border: '1px solid #ddd' }}
sidebar={sidebar}
docked={this.state.docked}
>
Click upper-left corner
</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: 300px;
padding: 0;
}
API
属性 | 说明 | 类型 | 默认值 |
---|
sidebar | 抽屉里的内容 | ReactNode | - |
onOpenChange | open 状态切换时调用 | (open: bool): void | - |
open | 开关状态 | Boolean | false |
position | 抽屉所在位置 | String | 'left', enum{'left', 'right', 'top', 'bottom'} |
sidebarStyle | - | Object | {} |
contentStyle | - | Object | {} |
overlayStyle | - | Object | {} |
dragHandleStyle | - | Object | {} |
touch | 是否开启触摸手势 | Boolean | true |
transitions | 是否开启动画 | Boolean | true |
docked | 是否嵌入到正常文档流里 | Boolean | false |
enableDragHandle | 是否禁止 dragHandle | Boolean | false |
dragToggleDistance | 打开关闭抽屉时距 sidebar 的拖动距离 | Number | 30 |