如何使用 material-ui 和 styled-components 定位按钮库

IT技术 css reactjs material-ui gatsby styled-components
2022-07-26 00:41:46

从material-ui 的gatsby 模板开始使用ToggleButtonToggleButtonGroup组件为了避免尝试使用的生产版本的常见错误material-uimaterial-ui styled-components

我有以下react代码无法正确定位材料 ui 基本按钮。

  • 如何使用样式组件正确执行此操作
  • 有没有我错过的最佳实践?

(我知道材料 ui 可能对布局有意见,但我说我想要基本元素的悬停或文本颜色)。

// Also imports React, gatsby packages etc

import ToggleButton from '@material-ui/lab/ToggleButton';
import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';
import styled from 'styled-components';

const StyledToggleButton = styled(ToggleButton)`
  color: black;
  ${'' /* Do not care as much in this section */}

  & .MuiButtonBase{
    color: pink;
    border-radius: 10em;
     ${'' /* THIS IS WHERE I WANT TO EFFECT THE BASE BUTTONS! DO NOT THINK .MuiButtonBase is correct!*/}
  }
`;

1个回答

您的代码中有两个问题:

  • 您正在使用MuiButtonBaseCSS 类,但这不存在。正确的CSS 类名MuiButtonBase-root.
  • 您正在使用后代选择器引用 CSS 类,但它位于根元素(即将应用类名称MuiButtonBase-root的同一元素)上,因此适当的语法是(在 & 号后面没有空格)。在这种情况下,唯一的效果是增加特异性,使其胜过默认样式。可以通过使用来达到同样的效果(即只需将生成的类名加倍)。styled-components&.MuiButtonBase-root.MuiButtonBase-root&&

下面是一个示例,显示了几种指定样式的不同方法,所有这些都具有相同的特异性。

import React from "react";
import { makeStyles, StylesProvider } from "@material-ui/core/styles";
import FormatAlignLeftIcon from "@material-ui/icons/FormatAlignLeft";
import FormatAlignCenterIcon from "@material-ui/icons/FormatAlignCenter";
import FormatAlignRightIcon from "@material-ui/icons/FormatAlignRight";
import FormatAlignJustifyIcon from "@material-ui/icons/FormatAlignJustify";
import ToggleButton from "@material-ui/lab/ToggleButton";
import ToggleButtonGroup from "@material-ui/lab/ToggleButtonGroup";
import styled from "styled-components";

const useStyles = makeStyles(theme => ({
  toggleContainer: {
    margin: theme.spacing(2, 0)
  }
}));

const StyledToggleButton1 = styled(ToggleButton)`
  && {
    color: pink;
    border-radius: 10em;
  }
`;
const StyledToggleButton2 = styled(ToggleButton)`
  &.MuiToggleButton-root {
    color: blue;
    border-radius: 1em;
  }
`;
const StyledToggleButton3 = styled(ToggleButton)`
  &.MuiButtonBase-root {
    color: purple;
    border-color: blue;
  }
`;

export default function ToggleButtons() {
  const [alignment, setAlignment] = React.useState("left");

  const handleAlignment = (event, newAlignment) => {
    setAlignment(newAlignment);
  };

  const classes = useStyles();

  return (
    <StylesProvider injectFirst>
      <div className={classes.toggleContainer}>
        <ToggleButtonGroup
          value={alignment}
          exclusive
          onChange={handleAlignment}
          aria-label="text alignment"
        >
          <StyledToggleButton1 value="left" aria-label="left aligned">
            <FormatAlignLeftIcon />
          </StyledToggleButton1>
          <StyledToggleButton2 value="center" aria-label="centered">
            <FormatAlignCenterIcon />
          </StyledToggleButton2>
          <StyledToggleButton3 value="right" aria-label="right aligned">
            <FormatAlignRightIcon />
          </StyledToggleButton3>
          <ToggleButton value="justify" aria-label="justified">
            <FormatAlignJustifyIcon />
          </ToggleButton>
        </ToggleButtonGroup>
      </div>
    </StylesProvider>
  );
}

编辑 ToggleButton 样式组件