import { Accordion, AccordionSummary, AccordionDetails } from '@mui/material';
How to use Material UI's accordion component
Key takeaways:
Material UI is an open-source React library providing pre-designed, user-friendly components based on Google's Material Design.
The Accordion component simplifies content organization by displaying expandable sections with summaries and collapsible details.
Uncontrolled Accordion manages its open/closed state internally without external control.
Controlled Accordion uses React state to manage its expanded state which offer more control over which panel is open.
Material UI is a React open-source library that focuses on providing pre-designed components and styles for creating modern, user-friendly, and visually appealing web applications. It follows the guidelines provided by Google's Material Design, resulting in intuitive and well-designed components.
In this Answer, we will look into what Material UI's Accordion component is, and how we can integrate it into our React application.
Note: Please make sure to install Material UI version 5 in your systems as we will be using it in the Answer.
The Accordion component
When we create a complex web application with a lot of content, a major task is to organize it in a manageable and visually appealing way. The Accordion component plays a major role in this aspect as it simplifies content organization and enhances user experience.
The primary feature of the Accordion component is to display a list of items where each list has a summary and a collapsible content section. Below, we can see an example of a simple Accordion component.
import * as React from 'react';
import Accordion from '@mui/material/Accordion';
import AccordionSummary from '@mui/material/AccordionSummary';
import AccordionDetails from '@mui/material/AccordionDetails';
import Typography from '@mui/material/Typography';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
export default function SimpleAccordion() {
return (
<div>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon/>}
>
<Typography variant='h4'>Item 1</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 1
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
>
<Typography variant='h4'>Item 2</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 2
</Typography>
</AccordionDetails>
</Accordion>
<Accordion >
<AccordionSummary
expandIcon={<ExpandMoreIcon/>}
>
<Typography variant='h4' >Item 3</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 3
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
>
<Typography variant='h4'>Item 4</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 4
</Typography>
</AccordionDetails>
</Accordion>
</div>
);
}Accordion types
The Accordion component creates expandable content sections. It has two types depending on the control of the expanding sections:
Uncontrolled accordion
Controlled accordion
Uncontrolled Accordion
The uncontrolled accordion operates without any explicit control over its open and closed states. To integrate the above shown Accordion as an uncontrolled component in our application, we need to run the following code:
import * as React from 'react';
import Accordion from '@mui/material/Accordion';
import AccordionSummary from '@mui/material/AccordionSummary';
import AccordionDetails from '@mui/material/AccordionDetails';
import Typography from '@mui/material/Typography';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
export default function SimpleAccordion() {
return (
<div>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon/>}
>
<Typography variant='h4'>Item 1</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 1
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
>
<Typography variant='h4'>Item 2</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 2
</Typography>
</AccordionDetails>
</Accordion>
<Accordion >
<AccordionSummary
expandIcon={<ExpandMoreIcon/>}
>
<Typography variant='h4' >Item 3</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 3
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
>
<Typography variant='h4'>Item 4</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 4
</Typography>
</AccordionDetails>
</Accordion>
</div>
);
}Code explanation
Line 2: We import the
Accordioncomponent from@mui/material/Accordion. This is the main component that wraps the andAccordianSummaryThe content that is displayed when the component is collapsed. component.AccordianDetailsThe component that displays content once we click on the accordion component. Line 3: We import the
AccordionSummarycomponent that displays the summary content.Line 4: We import the
AccordionDetailscomponent that displays the detailed content of theAccordiancomponent when theAccordioncomponent is expanded.
Note: The above code shows the
uncontrolledaccordion as it is managed by its own local state.
Controlled Accordion
The Accordion component is controlled when it is managed using the props passed by the parent component. The example code is shown below:
import * as React from 'react';
import Accordion from '@mui/material/Accordion';
import AccordionSummary from '@mui/material/AccordionSummary';
import AccordionDetails from '@mui/material/AccordionDetails';
import Typography from '@mui/material/Typography';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
export default function ControlledAccordion() {
const [expanded, setExpanded] = React.useState(false);
const handleChange = (acc) => (event, isExpanded) => {
setExpanded(isExpanded ? acc : false);
};
return (
<div>
<Accordion expanded={expanded === 'acc1'} onChange={handleChange('acc1')}>
<AccordionSummary
expandIcon={<ExpandMoreIcon/>}
>
<Typography variant='h4'>Item 1</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 1
</Typography>
</AccordionDetails>
</Accordion>
<Accordion expanded={expanded === 'acc2'} onChange={handleChange('acc2')}>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
>
<Typography variant='h4'>Item 2</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 2
</Typography>
</AccordionDetails>
</Accordion>
<Accordion expanded={expanded === 'acc3'} onChange={handleChange('acc3')}>
<AccordionSummary
expandIcon={<ExpandMoreIcon/>}
>
<Typography variant='h4' >Item 3</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 3
</Typography>
</AccordionDetails>
</Accordion>
</div>
);
}Code explanation
Line 9: We create a state variable
expandedthat keeps track of whether an accordion is expanded or not.Line 10–12: We create a function that takes
acc(in our case a string) and theisExpandedparameter to set the state of theexpandedvariable using the conditionisExpanded ? acc : false.Line 16: We pass in the
expandedattribute that checks if theexpandedstate variable equalsacc1. If it's true, then the accordion expands. TheonChangeattribute calls thehandleChangefunction and pass inacc1as a string.Lines 28 and 40: Similarly, for the second and third accordions, we pass in the
expandedattribute that checks if theexpandedstate variable equalsacc2andacc3respectively. If it is true, then the accordion expands.
Color customization
We can change the color of an accordion by using sx (styling attribute) and setting the bgcolor (background color) to a specific color. Let's see an example of adding color to the accordion:
import * as React from 'react';
import Accordion from '@mui/material/Accordion';
import AccordionSummary from '@mui/material/AccordionSummary';
import AccordionDetails from '@mui/material/AccordionDetails';
import Typography from '@mui/material/Typography';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
export default function SimpleAccordion() {
return (
<div>
<Accordion sx={{bgcolor:"blueviolet"}}>
<AccordionSummary
expandIcon={<ExpandMoreIcon sx = {{color:'white'}}/>}
>
<Typography variant='h4' color={'white'}>Item 1</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5' color={'white'}>
This section contains the collapsible content of item 1
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
>
<Typography variant='h4'>Item 2</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 2
</Typography>
</AccordionDetails>
</Accordion>
<Accordion sx={{bgcolor:"blueviolet"}}>
<AccordionSummary
expandIcon={<ExpandMoreIcon sx = {{color:'white'}}/>}
>
<Typography variant='h4' color={'white'}>Item 3</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5' color={'white'}>
This section contains the collapsible content of item 3
</Typography>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
>
<Typography variant='h4'>Item 4</Typography>
</AccordionSummary>
<AccordionDetails>
<Typography variant='h5'>
This section contains the collapsible content of item 4
</Typography>
</AccordionDetails>
</Accordion>
</div>
);
}Code explanation
Lines 11 and 35: We use the
sxattribute in theAccordiancomponent and set the background color toblueviolet.
Explore the following projects for hands-on practice in creating real-world applications with React. You can enhance these projects by incorporating Material-UI for a polished and visually appealing design.
Conclusion
Material UI's Accordian component is valuable to improve a web page's content presentation and user experience. Using it, developers can manage how their applications show complex information on the screen.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
How do I import Accordion in Material UI?
How can we keep accordion open by default in Material UI?
How can I make sure only one Accordion panel is expanded at a time in Material UI?
How do I disable the Accordion?
Free Resources