How to use breakpoints in Theme definition itself

放肆的年华 提交于 2019-12-11 00:48:21

问题


Material-ui allows you to create a nice Theme definition object to override the default Material Design look and feel. However, when you define a Theme, it's sometimes necessary to be able to create a breakpoint based override (mobile, marginTop is 10, desktop, marginTop is 5).

Any idea on how to do that. Obviously, since the Theme is not yet defined, you don't have access to theme reference and through that theme.breakpoints


回答1:


You can create the default theme (createMuiTheme without any arguments), and then use that theme to access the breakpoints for use in your custom theme.

Here's an example:

import React from "react";
import ReactDOM from "react-dom";
import Typography from "@material-ui/core/Typography";
import { createMuiTheme, MuiThemeProvider } from "@material-ui/core/styles";

const defaultTheme = createMuiTheme();
const theme = createMuiTheme({
  typography: {
    h4: {
      fontSize: "0.5rem",
      "&:after": {
        content: '" mobile"'
      },
      [defaultTheme.breakpoints.up("sm")]: {
        fontSize: "1.5rem",
        "&:after": {
          content: '" sm up"'
        }
      },
      [defaultTheme.breakpoints.up("md")]: {
        fontSize: "3rem",
        "&:after": {
          content: '" md up"'
        }
      }
    }
  }
});
function App() {
  return (
    <MuiThemeProvider theme={theme}>
      <div className="App">
        <Typography variant="h4">Here is some h4 text.</Typography>
      </div>
    </MuiThemeProvider>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);



来源:https://stackoverflow.com/questions/56532599/how-to-use-breakpoints-in-theme-definition-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!