Reputation: 33
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.
(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!*/}
}
`;
Upvotes: 3
Views: 4348
Reputation: 80986
There are two problems in your code:
MuiButtonBase
CSS class, but this doesn't exist. The correct CSS class name is MuiButtonBase-root
.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>
);
}
Upvotes: 4