Components定位按钮库

Components定位按钮库

本文介绍了如何使用Material-UI和Styled-Components定位按钮库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 material-ui 中的 ToggleButton ToggleButtonGroup 组件,从material-ui的

Using the ToggleButton and ToggleButtonGroup components from material-ui starting with material-ui's gatsby template. To avoid common material-ui errors with production builds trying to use styled-components as well.

I have the following react code which is unable to correctly target the material ui base button.

  • How do I correctly do this using styled-components
  • Is there a best practice I am missing?

(I know material ui may be opinionated on the layout but say I wanted a hover or text colour of base element instead).

// 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!*/}
  }
`;

解决方案

There are two problems in your code:

  • You are using a MuiButtonBase CSS class, but this doesn't exist. The correct CSS class name is MuiButtonBase-root.
  • You are referencing the CSS class using a descendant selector, but MuiButtonBase-root is on the root element (i.e. the same element that the styled-components class name will be applied to) so the appropriate syntax is &.MuiButtonBase-root (without the space after the ampersand). The only effect of .MuiButtonBase-root in this case is to increase the specificity so that it wins over the default styling. This same effect can be achieved by using && (i.e. just doubling the generated class name).

Below is an example showing a few different ways of specifying the styles all of which have equivalent specificity.

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>
  );
}

这篇关于如何使用Material-UI和Styled-Components定位按钮库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 02:57