reactjs - 如何在 MUI 5 的断点属性中访问主题?
问题描述
我需要theme.spacing
在我的sm
断点中使用 my ,但没有任何尝试有效。
sx={{
paddingTop: { sm: 0 },
paddingRight: { sm: "spacing(6)" },
paddingBottom: { sm: "spacing(10)" },
paddingLeft: { sm: "theme.spacing(6)" },
"@media screen and (orientation:landscape)": {
paddingTop: { sm: 0 },
paddingRight: { sm: "spacing(6)" },
paddingBottom: { sm: "spacing(2)" },
paddingLeft: { sm: "theme.spacing(6)" },
},
}}
或这个
sx={{
paddingTop: { sm: 0 },
paddingRight: { sm: (theme) => theme.spacing(6) },
paddingBottom: { sm: (theme) => theme.spacing(10) },
paddingLeft: { sm: (theme) => theme.spacing(6) },
"@media screen and (orientation:landscape)": {
paddingTop: { sm: 0 },
paddingRight: { sm: (theme) => theme.spacing(6) },
paddingBottom: { sm: (theme) => theme.spacing(2) },
paddingLeft: { sm: (theme) => theme.spacing(6) },
},
}}
如何使用带断点的主题值(sm
、、、md
等lg
)
解决方案
CSS 属性支持使用主题的回调语法(甚至嵌套在媒体查询中),但不支持将其作为断点键的值。下面的示例展示了在多个级别使用回调语法——作为顶级属性的值 ( padding
),作为顶级媒体查询的值(横向),以及作为其中指定的属性的值媒体查询(paddingTop
纵向内)。
import * as React from "react";
import Box from "@mui/material/Box";
import { Theme } from "@mui/material/styles";
export default function SxWithOrientation() {
return (
<div>
<Box
sx={{
border: "solid 1px black",
padding: (theme: Theme) => theme.spacing(5),
"@media screen and (orientation: landscape)": (theme: Theme) => ({
color: "black",
paddingTop: {
xs: theme.spacing(2.5),
sm: 3,
md: 4,
lg: 5,
xl: 6
},
backgroundColor: {
xs: "lightgray",
sm: "lightblue",
md: "lightgreen",
lg: "pink",
xl: "orange"
}
}),
"@media screen and (orientation: portrait)": {
color: "white",
paddingTop: (theme: Theme) => ({
xs: theme.spacing(5.5),
sm: 4,
md: 3,
lg: 2
}),
backgroundColor: {
xs: "black",
sm: "blue",
md: "green",
lg: "red"
}
}
}}
>
This box has responsive padding and colors.
</Box>
</div>
);
}
推荐阅读
- python - 多个 if/else 条件来检查字符串列表中的最后一个字符
- html - ViewEncapsulation.None 不适用于 innertHTML
- java - MigLayout,为什么这个单元格使用了额外的空间?
- flutter - 我可以检索流的倒数第二个值吗?
- angular - 无论如何使用带有 express-ws 的 websockets 和带有 Angular Universal 9 应用程序的 WebSocket 客户端 api?
- can-bus - CAN 总线中的帧确认
- python - python - 如何在python中的matplotlib条形图中为某些条形着色?
- android - Android 导航抽屉模板无法工作,除非它在 Emulator AS4.0 上通过制表符和光标键导航
- networking - istio 主机匹配规则重定向问题
- reactjs - react-table 在渲染时重新初始化