[英]Can we use both MUI makeStyles and Theming in _app.js
I'm making a tricky nav component using MUI Persistent Drawers with a React + Next structure.我正在使用具有 React + Next 结构的MUI 持久抽屉制作一个棘手的导航组件。 In order to get the content shrinking thanks to the state change, I had to put the whole navigation system inside of the _app.js file instead of having it in a distinct component.由于 state 更改,为了缩小内容,我不得不将整个导航系统放在 _app.js 文件中,而不是将其放在不同的组件中。
Even though it clearly works, I wonder if it'll not impact rendering and performance once built.尽管它显然有效,但我想知道一旦构建它是否不会影响渲染和性能。 The makeStyles((theme)
seems to work just fine but is not highlighted in VScode, probably because I used both of makeStyles , useTheme and ThemeProvider in the same file, as below: makeStyles((theme)
似乎工作得很好,但在 VScode 中没有突出显示,可能是因为我在同一个文件中同时使用了makeStyles 、 useTheme和ThemeProvider ,如下所示:
import { makeStyles, useTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/core/styles';
const useStyles = makeStyles((theme) => ({ ...
(className example:) appBarShift: {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth,
transition: theme.transitions.create(['margin', 'width'], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
}, ... etc
Was it a good idea to put it all together in _app.js, or do I need to refactor everything into a new component passing props to parent somehow?将所有内容放在 _app.js 中是个好主意,还是我需要将所有内容重构为一个新组件,以某种方式将 props 传递给父级?
Thanks for considering, best regards to all of you感谢您的考虑,向大家致以最诚挚的问候
I recommend you to put your theme inside a folder theme/theme.js and use createMuiTheme then in your app.js wrap your whole component with the theme provider.我建议您将主题放在文件夹 theme/theme.js 中并使用 createMuiTheme 然后在您的 app.js 中使用主题提供程序包装您的整个组件。 That would spread your theme across the app and let you use it where you want !这会将您的主题传播到整个应用程序,并让您在需要的地方使用它! Is the convention we got with material-ui.是我们使用 material-ui 得到的约定。
theme/theme.js:主题/theme.js:
import { createMuiTheme } from "@material-ui/core/styles"
export const themeObject = {
palette: {
primary: { main: "#333333" },
secondary: { main: "#FD8C7C" },
},
backgroundColor: {
primary: { main: "#FEF8F8" },
secondary: { main: "#FFFFFF" },
},
breakpoints: {
values: {
xs: 0,
sm: 600,
md: 960,
lg: 1280,
xl: 1920,
},
},
overrides: {
MuiDivider: {
root: {
backgroundColor: "#FFFFFF",
},
},
},
}
export default createMuiTheme(themeObject)
pages/app,js:页面/应用程序,js:
import React from "react"
import { ThemeProvider } from "@material-ui/styles"
import Box from "@material-ui/core/Box"
import defaultTheme from "../theme/theme"
const App = () => {
return (
<Box>
<ThemeProvider theme={defaultTheme}>
...
</ThemeProvider>
</Box>
)
}
export default App
component/SideBar.js:组件/SideBar.js:
import React from "react"
import { makeStyles } from "@material-ui/core/styles"
const useStyles = makeStyles((theme) => ({
drawer: {
width: theme.sidebar.width,
color: theme.palette.primary
flexShrink: 0,
whiteSpace: "nowrap",
"& .MuiDrawer-paper": {
top: "unset",
},
},
drawerClose: {
transition: theme.transitions.create("width", {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
overflowX: "hidden",
width: theme.spacing(7) + 1,
[theme.breakpoints.up("sm")]: {
width: theme.spacing(7) + 1,
},
},
expandButton: {
[theme.breakpoints.up("sm")]: {
marginLeft: theme.spacing(-2.5),
marginRight: theme.spacing(2.5),
},
},
}))
export default function Sidebar(props) {
const classes = UseStyles()
return (...)
}
In the SideBar you can see that i have use the theme.zindex.drawer who come from directly theme.js.在 SideBar 中,您可以看到我使用了直接来自 theme.js 的 theme.zindex.drawer。 I also recommend you to go check this MUI THEMING that would let you dive into the theming more deeply !我还推荐你到 go 检查这个MUI THEMING ,它会让你更深入地研究主题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.