【发布时间】:2020-05-12 06:46:36
【问题描述】:
Material UI 有一组不错的内置媒体查询:https://material-ui.com/customization/breakpoints/#css-media-queries
Material UI 还允许我们将 Styled-Components 与 Material UI 一起使用:https://material-ui.com/guides/interoperability/#styled-components
我想知道如何将两者结合在一起。也就是说,如何使用 Styled Components 和 Material-UI 的内置断点进行媒体查询?
谢谢。
更新:
这是我正在尝试做的一个示例:
import React, { useState } from 'react'
import styled from 'styled-components'
import {
AppBar as MuiAppBar,
Drawer as MuiDrawer,
Toolbar,
} from '@material-ui/core'
const drawerWidth = 240
const AdminLayout = ({ children }) => {
return (
<BaseLayout>
<AppBar position="static">
<Toolbar>
TOOLBAR
</Toolbar>
</AppBar>
<Drawer>
DRAWER
</Drawer>
{children}
</BaseLayout>
)
}
AdminLayout.propTypes = {
children: PropTypes.node.isRequired,
}
export default AdminLayout
// ------- STYLES -------
const AppBar = styled(MuiAppBar)`
/* Implement appBar styles from useStyles */
`
const Drawer = styled(MuiDrawer)`
/* Implement drawer styles from useStyles */
`
// STYLES THAT I WANT TO CONVERT TO STYLED-COMPONENTS
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
},
drawer: {
[theme.breakpoints.up('sm')]: {
width: drawerWidth,
flexShrink: 0,
},
},
appBar: {
[theme.breakpoints.up('sm')]: {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth,
},
},
toolbar: theme.mixins.toolbar,
}))
【问题讨论】:
-
theme.breakpoints.down('sm')只是一个字符串'(min-width:600px)',在样式组件中使用它 -
这需要我对像素值进行硬编码——如果我想使用
sm变量怎么办? -
什么?使用
theme.breakpoints.down('sm')时无需硬编码 -
也许我没有关注。您介意向我展示一个如何在样式组件中使用
theme.breakpoints.down('sm')的示例吗? -
做一个小例子,说明你在没有样式组件的情况下尝试使用 MU 设置样式,我将向你展示一个示例
标签: reactjs material-ui styled-components