简体   繁体   English

未应用 MaterialUi 自定义断点

[英]MaterialUi custom breakpoints not applied

Based on this question, I tried to have a theme that uses a common config file(that should be used by other themes as well).基于这个问题,我尝试了一个使用通用配置文件的主题(其他主题也应该使用)。 So I figured the breakpoints would be a good starting point since they should be the same across all the themes.所以我认为断点将是一个很好的起点,因为它们在所有主题中应该是相同的。

I have the common part like我有共同的部分

const BREAKPOINTS = {
  xs: 0,
  sm: 768,
  md: 960,
  lg: 1280,
  xl: 1920,
};

const commonConstants = {
  breakpoints: {
    values: BREAKPOINTS,
  },
};

export default commonConstants;

Then I create my theme like然后我创建我的主题

const defaultTheme = responsiveFontSizes(createMuiTheme(myTheme, commonConstants));

If I console.log my theme object, it will display the correct breakpoint values but will not apply them(the default one will be applied).如果我 console.log 我的主题 object,它将显示正确的断点值但不会应用它们(将应用默认值)。 However, if the breakpoint object is added directly inside the theme object myTheme (ie not uside of the theme), the correct breakpoints are applied.但是,如果breakpoint object 直接添加到主题 object myTheme中(即不使用主题),则会应用正确的断点。 What am I missing here?我在这里想念什么? If the final theme object has the same structure, why is it working differently?如果最终主题 object 具有相同的结构,为什么它的工作方式不同?

Several portions of the theme (eg palette, breakpoints , spacing, typography) have additional processing associated with them that creates additional entries in the theme based on the options passed in. This additional processing is only applied to the object passed as the first argument to createMuiTheme .主题的几个部分(例如调色板、 断点、间距、排版)具有与之关联的附加处理,根据传入的选项在主题中创建附加条目。这种附加处理仅适用于作为第一个参数传递给createMuiTheme Any additional arguments are merged in after the additional processing.任何附加的 arguments 在附加处理后合并

For breakpoints, that additional processing is contained in the createBreakpoints function .对于断点,该附加处理包含在createBreakpoints function 中 This creates the various functions that leverage the breakpoint values (eg theme.breakpoints.up , theme.breakpoints.down , etc.).这将创建利用断点值的各种函数(例如theme.breakpoints.uptheme.breakpoints.down等)。 When you pass in custom breakpoint values via a second argument, those values are not being used in the creation of those breakpoint functions.当您通过第二个参数传入自定义断点值时,这些值不会用于创建这些断点函数。

There are two main options for addressing this.有两个主要选项可以解决这个问题。

Option 1: Apply the additional processing yourself选项 1:自行应用附加处理

import { createMuiTheme } from "@material-ui/core/styles";
import createBreakpoints from "@material-ui/core/styles/createBreakpoints";

const BREAKPOINTS = {
  xs: 0,
  sm: 768,
  md: 960,
  lg: 1280,
  xl: 1920
};

const breakpointsFull = {
  breakpoints: createBreakpoints({
    values: BREAKPOINTS
  })
};
const myTheme = { other: "stuff" };
const theme = createMuiTheme(myTheme, breakpointsFull);

Option 2: Merge your custom breakpoint values into the first argument to createMuiTheme选项 2:将自定义断点值合并到createMuiTheme的第一个参数中

import { createMuiTheme } from "@material-ui/core/styles";

const BREAKPOINTS = {
  xs: 0,
  sm: 768,
  md: 960,
  lg: 1280,
  xl: 1920
};

const breakpointsValues = {
  breakpoints: {
    values: BREAKPOINTS
  }
};

const myTheme = { other: "stuff" };
const theme = createMuiTheme({ ...myTheme, ...breakpointsValues });

Here's a working example demonstrating the problem with your current approach as well as the two alternatives:这是一个工作示例,展示了您当前方法的问题以及两种替代方法:

import React from "react";
import { createMuiTheme } from "@material-ui/core/styles";
import createBreakpoints from "@material-ui/core/styles/createBreakpoints";

const BREAKPOINTS = {
  xs: 0,
  sm: 768,
  md: 960,
  lg: 1280,
  xl: 1920
};

const breakpointsValues = {
  breakpoints: {
    values: BREAKPOINTS
  }
};
const breakpointsFull = {
  breakpoints: createBreakpoints({
    values: BREAKPOINTS
  })
};
const myTheme = { other: "stuff" };
const badTheme = createMuiTheme(myTheme, breakpointsValues);
const goodTheme1 = createMuiTheme(myTheme, breakpointsFull);
const goodTheme2 = createMuiTheme({ ...myTheme, ...breakpointsValues });
export default function App() {
  return (
    <ul>
      <li>badTheme.breakpoints.values.sm: {badTheme.breakpoints.values.sm}</li>
      <li>badTheme.breakpoints.up("sm"): {badTheme.breakpoints.up("sm")}</li>
      <li>
        goodTheme1.breakpoints.values.sm: {goodTheme1.breakpoints.values.sm}
      </li>
      <li>
        goodTheme1.breakpoints.up("sm"): {goodTheme1.breakpoints.up("sm")}
      </li>
      <li>
        goodTheme2.breakpoints.values.sm: {goodTheme2.breakpoints.values.sm}
      </li>
      <li>
        goodTheme2.breakpoints.up("sm"): {goodTheme2.breakpoints.up("sm")}
      </li>
    </ul>
  );
}

编辑主题自定义断点

In order to effectively override breakpoints in the default MuiTheme (as shown in Ryan Cogswell answer Option 1) you must use the createBreakpoints function passing in the values and keys elements.为了有效地覆盖默认 MuiTheme 中的断点(如 Ryan Cogswell 回答选项 1 中所示),您必须使用 createBreakpoints function 传递值和键元素。

Based on Ryan's answer, I found the following solution to work.根据 Ryan 的回答,我发现以下解决方案可行。

import { createMuiTheme } from "@material-ui/core/styles";
import createBreakpoints from "@material-ui/core/styles/createBreakpoints";

const BREAKPOINTS = {
  xs: 0,
  sm: 768,
  md: 960,
  lg: 1280,
  xl: 1920
};

const breakpointsFull = createBreakpoints({
    values: {...BREAKPOINTS},
    keys: Object.keys(BREAKPOINTS),
  });

const myTheme = { other: "stuff" };
const theme = createMuiTheme({ 
   default: myTheme, 
   breakpoints: breakpointsFull,
});

This strategy enables easy modification of the BREAKPOINTS variable, and correctly assigns it into the object.此策略可以轻松修改 BREAKPOINTS 变量,并将其正确分配给 object。 Using the createBreakpoints Mui function will generate functions for up and down, which allow you to check against breakpoints in your code for example:使用 createBreakpoints Mui function 将生成 up 和 down 函数,允许您检查代码中的断点,例如:

const isSmallScreen = UseMediaQuery(theme.breakpoints.down('sm'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM