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:
MuiButtonBaseCSS class, but this doesn't exist. The correct CSS class name isMuiButtonBase-root.MuiButtonBase-rootis on the root element (i.e. the same element that thestyled-componentsclass name will be applied to) so the appropriate syntax is&.MuiButtonBase-root(without the space after the ampersand). The only effect of.MuiButtonBase-rootin 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.