Material-UI中使用样式组件的媒体查询

时间:2020-01-26 19:21:53

标签: reactjs material-ui styled-components

材料界面具有一组不错的内置媒体查询:https://material-ui.com/customization/breakpoints/#css-media-queries

材料UI还允许我们将样式组件与材料UI结合使用:https://material-ui.com/guides/interoperability/#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,
}))

3 个答案:

答案 0 :(得分:7)

下面是一个示例,显示了通过样式组件利用Material-UI主题断点的一种方法。这会将Material-UI主题传递给样式化组件ThemeProvider,以使其在样式中作为道具可用。该示例还将StylesProviderinjectFirst道具一起使用,以便Material-UI样式出现在<head>的开始而不是结尾,因此样式化组件的样式出现在{ Material-UI样式,因此在其他方面相等时获胜。

import React from "react";
import styled, { ThemeProvider as SCThemeProvider } from "styled-components";
import { useTheme, StylesProvider } from "@material-ui/core/styles";
import MuiAppBar from "@material-ui/core/AppBar";

const AppBar = styled(MuiAppBar)`
  background-color: red;
  ${props => props.theme.breakpoints.up("sm")} {
    background-color: orange;
  }
  ${props => props.theme.breakpoints.up("md")} {
    background-color: yellow;
    color: black;
  }
  ${props => props.theme.breakpoints.up("lg")} {
    background-color: green;
    color: white;
  }
`;
export default function App() {
  const muiTheme = useTheme();
  return (
    <StylesProvider injectFirst>
      <SCThemeProvider theme={muiTheme}>
        <AppBar>Sample AppBar</AppBar>
      </SCThemeProvider>
    </StylesProvider>
  );
}

Edit MUI theme breakpoints with styled-components

相关文档:

答案 1 :(得分:1)

如果您将"Style Objects" approach(即“ JavaScript”)用于样式化组件,则这是实现相同结果的方法。这是基于Ryan Cogswell先前提到的内容。

如果从另一个CSS-in-JS系统(例如Material-UI的内置JSS)切换过来,某些人可能更喜欢这样做。另外,“样式对象”方法仅要求您一次引入props,而不是在任何行上使用props变量。有选择的好。 ?

样式对象

const AppBar = styled(MuiAppBar)((props) => ({
  backgroundColor: red;

  [props.theme.breakpoints.up("sm")]: {
    backgroundColor: orange,
  },
  [props.theme.breakpoints.up("md")]: {
    backgroundColor: yellow,
    color: black,
  },
  [props.theme.breakpoints.up("lg")]: {
    backgroundColor: green,
    color: white,
  },
}));

样式对象,但更简洁

由于我们只需要使用JavaScript方法访问一次道具,并且只在此样式区域中使用主题,因此我们可以从传入的theme中解构props,从而减​​少代码量。 / p>

const AppBar = styled(MuiAppBar)(({ theme }) => ({
  backgroundColor: red;

  [theme.breakpoints.up("sm")]: {
    backgroundColor: orange,
  },
  [theme.breakpoints.up("md")]: {
    backgroundColor: yellow,
    color: black,
  },
  [theme.breakpoints.up("lg")]: {
    backgroundColor: green,
    color: white,
  },
}));

注意:如果您使用的是TypeScript,并且已设置样式组件主题以与Material-UI主题匹配,那么在CSS或JavaScript方法中,键入安全性仍然可以正常工作。

答案 2 :(得分:0)

breakpoints are provided是默认主题的一部分。

它们是常量,不会改变,因此您可以在组件或样式主题中使用它们:

import React from 'react';
import styled from 'styled-components';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles(theme => {
  console.log('md', theme.breakpoints.up('md'));
  return {};
});

const BP = {
  MD: `@media (min-width:960px) `,
};

const Container = styled.div`
  background-color: green;

  ${({ bp }) => bp} {
    background-color: red;
  }
`;

export default function StyledComponentsButton() {
  useStyles();
  return <Container bp={BP.MD}>Example</Container>;
}

Edit Styled Components