Tim*_*mmo 9 mobile responsive-design reactjs material-design material-ui
有谁知道如何或知道如何创建一个被动的应用程序栏和抽屉的任何示例?
它需要能够动态地取消对接抽屉并在浏览器较小时隐藏,并在较大时将抽屉停靠,最好动态像材料-ui网站已经做的那样:http://www.material-ui.com/# /组件/抽屉
小智 7
您可以像这样在componentWillMount中侦听屏幕尺寸的变化,我敢肯定有更好的方法,但是这可行。
toggleOpenDrawer = () => {
if (!this.state.mobile) {
return;
}
this.setState({
open: !this.state.open
})
}
setSmall = () => {
this.setState({open: false, docked: false, mobile: true})
}
setLarge = () => {
this.setState({open: true, docked: true, mobile: false})
}
componentWillMount() {
const mediaQuery = window.matchMedia('(min-width: 768px)');
if (mediaQuery.matches) {
this.setLarge()
} else {
this.setSmall()
}
mediaQuery.addListener((mq) => {
if (mq.matches) {
this.setLarge()
} else {
this.setSmall()
}
});
}
Run Code Online (Sandbox Code Playgroud)
对于使用Material-UI V1及更高版本的用户,使用断点是制作响应式布局的最佳方法。
断点:
因此,要更改React渲染树,您应该breakpoints.up()
在JSS中传递不同的断点参数值:
navIconHide: {
[theme.breakpoints.up('md')]: {
display: 'none',
},
},
toolbar: theme.mixins.toolbar,
drawerPaper: {
width: drawerWidth,
[theme.breakpoints.up('md')]: {
position: 'relative',
},
},
Run Code Online (Sandbox Code Playgroud)
整个Drawer源代码在这里:
**import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Drawer from '@material-ui/core/Drawer';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import List from '@material-ui/core/List';
import Typography from '@material-ui/core/Typography';
import IconButton from '@material-ui/core/IconButton';
import Hidden from '@material-ui/core/Hidden';
import Divider from '@material-ui/core/Divider';
import MenuIcon from '@material-ui/icons/Menu';
import { mailFolderListItems, otherMailFolderListItems } from './tileData';
const drawerWidth = 240;
const styles = theme => ({
root: {
flexGrow: 1,
height: 430,
zIndex: 1,
overflow: 'hidden',
position: 'relative',
display: 'flex',
width: '100%',
},
appBar: {
position: 'absolute',
marginLeft: drawerWidth,
[theme.breakpoints.up('md')]: {
width: `calc(100% - ${drawerWidth}px)`,
},
},
navIconHide: {
[theme.breakpoints.up('md')]: {
display: 'none',
},
},
toolbar: theme.mixins.toolbar,
drawerPaper: {
width: drawerWidth,
[theme.breakpoints.up('md')]: {
position: 'relative',
},
},
content: {
flexGrow: 1,
backgroundColor: theme.palette.background.default,
padding: theme.spacing.unit * 3,
},
});
class ResponsiveDrawer extends React.Component {
state = {
mobileOpen: false,
};
handleDrawerToggle = () => {
this.setState(state => ({ mobileOpen: !state.mobileOpen }));
};
render() {
const { classes, theme } = this.props;
const drawer = (
<div>
<div className={classes.toolbar} />
<Divider />
<List>{mailFolderListItems}</List>
<Divider />
<List>{otherMailFolderListItems}</List>
</div>
);
return (
<div className={classes.root}>
<AppBar className={classes.appBar}>
<Toolbar>
<IconButton
color="inherit"
aria-label="Open drawer"
onClick={this.handleDrawerToggle}
className={classes.navIconHide}
>
<MenuIcon />
</IconButton>
<Typography variant="title" color="inherit" noWrap>
Responsive drawer
</Typography>
</Toolbar>
</AppBar>
<Hidden mdUp>
<Drawer
variant="temporary"
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
open={this.state.mobileOpen}
onClose={this.handleDrawerToggle}
classes={{
paper: classes.drawerPaper,
}}
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden smDown implementation="css">
<Drawer
variant="permanent"
open
classes={{
paper: classes.drawerPaper,
}}
>
{drawer}
</Drawer>
</Hidden>
<main className={classes.content}>
<div className={classes.toolbar} />
<Typography noWrap>{'You think water moves fast? You should see ice.'}</Typography>
</main>
</div>
);
}
}
ResponsiveDrawer.propTypes = {
classes: PropTypes.object.isRequired,
theme: PropTypes.object.isRequired,
};
export default withStyles(styles, { withTheme: true })(ResponsiveDrawer);**
Run Code Online (Sandbox Code Playgroud)
在MUI v5Drawers
中,您可以以不同的屏幕尺寸显示 2 个。永久物 Drawer
始终在大屏幕中可见。在小屏幕中,您可以使用 prop中的响应值轻松删除它sx
:
<Drawer
variant="permanent"
open
sx={{ display: { xs: 'none', sm: 'block' } }}
>
Run Code Online (Sandbox Code Playgroud)
并显示一个可以打开或关闭的临时的。 Drawer
临时的不会影响布局,因为它显示在顶部,适合移动视图。
<Drawer
variant="temporary"
open={mobileOpen}
onClose={handleDrawerToggle}
sx={{ display: { xs: 'block', sm: 'none' } }}
>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8058 次 |
最近记录: |