How to customize material-ui Accordion component behavior/style?

Viewed 3104

I have a div container (gray) with an accordion component with just one AccordionSummary component. And another div (blue) below that. Everything looks good (pic #1). When I expand the accordion:

  • it shows AccordionDetails component - GOOD
  • it automatically resize gray container div - GOOD
  • but not enough so the blue div goes outside of container - BAD (pic #2)

Assume it's a standard Accordion component behavior. But has anyone of you met such issue and how did you deal with it?

enter image description here

enter image description here

1 Answers

using flexbox and a general padding for the root element and a margin top for the blue element.

I think your problem is that you set an absolute height to the gray element like 300px which makes the problem use height: "auto" and a general padding

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Accordion from "@material-ui/core/Accordion";
import AccordionSummary from "@material-ui/core/AccordionSummary";
import AccordionDetails from "@material-ui/core/AccordionDetails";
import Typography from "@material-ui/core/Typography";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";

const useStyles = makeStyles((theme) => ({
  root: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    width: "100%",
    backgroundColor: "#eee",
    padding: 20,
  },
  heading: {
    fontSize: theme.typography.pxToRem(15),
    fontWeight: theme.typography.fontWeightRegular
  },
  rectangle: {
    width: "80%",
    height: 200,
    backgroundColor: "blue",
    marginTop:20
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Accordion>
        <AccordionSummary
          expandIcon={<ExpandMoreIcon />}
          aria-controls="panel1a-content"
          id="panel1a-header"
        >
          <Typography className={classes.heading}>Accordion 1</Typography>
        </AccordionSummary>
        <AccordionDetails>
          <Typography>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse
            malesuada lacus ex, sit amet blandit leo lobortis eget.
          </Typography>
        </AccordionDetails>
      </Accordion>
      <div className={classes.rectangle} />
    </div>
  );
}
Related