John Glabb
John Glabb

Reputation: 1621

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

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:

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

Upvotes: 7

Views: 12817

Answers (2)

Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15935

import Accordion from '@mui/material/Accordion';
import AccordionDetails from '@mui/material/AccordionDetails';
import AccordionSummary from '@mui/material/AccordionSummary';
import { styled } from '@mui/material/styles';

const CustomAccordion = styled(Accordion)(({ theme }) => {
  return {
    boxShadow: 'none', // this styles directly apply to accordion
    border: `1px solid gray`,
    '.MuiAccordionDetails-root': {},
    '.MuiAccordionSummary-root': {}, // this apply to Summary
  };
});

Now you can use in place of Accordion

Upvotes: 4

Daphaz
Daphaz

Reputation: 506

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

Upvotes: 0

Related Questions