diff --git a/docs/data/material/components/accordion/AccordionExpandDefault.js b/docs/data/material/components/accordion/AccordionExpandDefault.js new file mode 100644 index 00000000000000..cb49c530d96eea --- /dev/null +++ b/docs/data/material/components/accordion/AccordionExpandDefault.js @@ -0,0 +1,43 @@ +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 AccordionExpandDefault() { + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Expanded by default + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Header + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/AccordionExpandDefault.tsx b/docs/data/material/components/accordion/AccordionExpandDefault.tsx new file mode 100644 index 00000000000000..cb49c530d96eea --- /dev/null +++ b/docs/data/material/components/accordion/AccordionExpandDefault.tsx @@ -0,0 +1,43 @@ +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 AccordionExpandDefault() { + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Expanded by default + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Header + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/AccordionExpandIcon.js b/docs/data/material/components/accordion/AccordionExpandIcon.js new file mode 100644 index 00000000000000..3ec7af1b9a6385 --- /dev/null +++ b/docs/data/material/components/accordion/AccordionExpandIcon.js @@ -0,0 +1,44 @@ +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 ArrowDownwardIcon from '@mui/icons-material/ArrowDownward'; +import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown'; + +export default function AccordionExpandIcon() { + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Accordion 1 + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Accordion 2 + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/AccordionExpandIcon.tsx b/docs/data/material/components/accordion/AccordionExpandIcon.tsx new file mode 100644 index 00000000000000..3ec7af1b9a6385 --- /dev/null +++ b/docs/data/material/components/accordion/AccordionExpandIcon.tsx @@ -0,0 +1,44 @@ +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 ArrowDownwardIcon from '@mui/icons-material/ArrowDownward'; +import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown'; + +export default function AccordionExpandIcon() { + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Accordion 1 + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Accordion 2 + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/AccordionTransition.js b/docs/data/material/components/accordion/AccordionTransition.js new file mode 100644 index 00000000000000..1eab8dc1a5543f --- /dev/null +++ b/docs/data/material/components/accordion/AccordionTransition.js @@ -0,0 +1,59 @@ +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'; +import Fade from '@mui/material/Fade'; + +export default function AccordionTransition() { + const [expanded, setExpanded] = React.useState(false); + + const handleExpansion = () => { + setExpanded((prevExpanded) => !prevExpanded); + }; + + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Custom transition using Fade + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Default transition using Collapse + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/AccordionTransition.tsx b/docs/data/material/components/accordion/AccordionTransition.tsx new file mode 100644 index 00000000000000..62f6870647c6a2 --- /dev/null +++ b/docs/data/material/components/accordion/AccordionTransition.tsx @@ -0,0 +1,59 @@ +import * as React from 'react'; +import Accordion, { AccordionProps } 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'; +import Fade from '@mui/material/Fade'; + +export default function AccordionTransition() { + const [expanded, setExpanded] = React.useState(false); + + const handleExpansion = () => { + setExpanded((prevExpanded) => !prevExpanded); + }; + + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Custom transition using Fade + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Default transition using Collapse + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/AccordionUsage.js b/docs/data/material/components/accordion/AccordionUsage.js new file mode 100644 index 00000000000000..0b425cf4dbc32c --- /dev/null +++ b/docs/data/material/components/accordion/AccordionUsage.js @@ -0,0 +1,57 @@ +import * as React from 'react'; +import Accordion from '@mui/material/Accordion'; +import AccordionActions from '@mui/material/AccordionActions'; +import AccordionSummary from '@mui/material/AccordionSummary'; +import AccordionDetails from '@mui/material/AccordionDetails'; +import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; +import Button from '@mui/material/Button'; + +export default function AccordionUsage() { + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Accordion 1 + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Accordion 2 + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + } + aria-controls="panel3-content" + id="panel3-header" + > + Accordion Actions + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/AccordionUsage.tsx b/docs/data/material/components/accordion/AccordionUsage.tsx new file mode 100644 index 00000000000000..0b425cf4dbc32c --- /dev/null +++ b/docs/data/material/components/accordion/AccordionUsage.tsx @@ -0,0 +1,57 @@ +import * as React from 'react'; +import Accordion from '@mui/material/Accordion'; +import AccordionActions from '@mui/material/AccordionActions'; +import AccordionSummary from '@mui/material/AccordionSummary'; +import AccordionDetails from '@mui/material/AccordionDetails'; +import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; +import Button from '@mui/material/Button'; + +export default function AccordionUsage() { + return ( +
+ + } + aria-controls="panel1-content" + id="panel1-header" + > + Accordion 1 + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + } + aria-controls="panel2-content" + id="panel2-header" + > + Accordion 2 + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + } + aria-controls="panel3-content" + id="panel3-header" + > + Accordion Actions + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + malesuada lacus ex, sit amet blandit leo lobortis eget. + + + + + + +
+ ); +} diff --git a/docs/data/material/components/accordion/BasicAccordion.js b/docs/data/material/components/accordion/DisabledAccordion.js similarity index 84% rename from docs/data/material/components/accordion/BasicAccordion.js rename to docs/data/material/components/accordion/DisabledAccordion.js index c814ec1259ed8d..a5200f7f30e3b8 100644 --- a/docs/data/material/components/accordion/BasicAccordion.js +++ b/docs/data/material/components/accordion/DisabledAccordion.js @@ -5,14 +5,14 @@ import AccordionDetails from '@mui/material/AccordionDetails'; import Typography from '@mui/material/Typography'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; -export default function BasicAccordion() { +export default function DisabledAccordion() { return (
} - aria-controls="panel1a-content" - id="panel1a-header" + aria-controls="panel1-content" + id="panel1-header" > Accordion 1 @@ -26,8 +26,8 @@ export default function BasicAccordion() { } - aria-controls="panel2a-content" - id="panel2a-header" + aria-controls="panel2-content" + id="panel2-header" > Accordion 2 @@ -41,8 +41,8 @@ export default function BasicAccordion() { } - aria-controls="panel3a-content" - id="panel3a-header" + aria-controls="panel3-content" + id="panel3-header" > Disabled Accordion diff --git a/docs/data/material/components/accordion/BasicAccordion.tsx b/docs/data/material/components/accordion/DisabledAccordion.tsx similarity index 84% rename from docs/data/material/components/accordion/BasicAccordion.tsx rename to docs/data/material/components/accordion/DisabledAccordion.tsx index c814ec1259ed8d..a5200f7f30e3b8 100644 --- a/docs/data/material/components/accordion/BasicAccordion.tsx +++ b/docs/data/material/components/accordion/DisabledAccordion.tsx @@ -5,14 +5,14 @@ import AccordionDetails from '@mui/material/AccordionDetails'; import Typography from '@mui/material/Typography'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; -export default function BasicAccordion() { +export default function DisabledAccordion() { return (
} - aria-controls="panel1a-content" - id="panel1a-header" + aria-controls="panel1-content" + id="panel1-header" > Accordion 1 @@ -26,8 +26,8 @@ export default function BasicAccordion() { } - aria-controls="panel2a-content" - id="panel2a-header" + aria-controls="panel2-content" + id="panel2-header" > Accordion 2 @@ -41,8 +41,8 @@ export default function BasicAccordion() { } - aria-controls="panel3a-content" - id="panel3a-header" + aria-controls="panel3-content" + id="panel3-header" > Disabled Accordion diff --git a/docs/data/material/components/accordion/accordion.md b/docs/data/material/components/accordion/accordion.md index 329ddf3f7135ed..df19e8192a0318 100644 --- a/docs/data/material/components/accordion/accordion.md +++ b/docs/data/material/components/accordion/accordion.md @@ -9,24 +9,64 @@ waiAria: https://www.w3.org/WAI/ARIA/apg/patterns/accordion/ # Accordion -

The accordion component allows the user to show and hide sections of related content on a page.

- -An accordion is a lightweight container that may either be used standalone, or be connected to a larger surface, such as a card. +

The Accordion component lets users show and hide sections of related content on a page.

{{"component": "modules/components/ComponentLinkHeader.js"}} +## Introduction + +The Material UI Accordion component includes several complementary utility components to handle various use cases: + +- Accordion: the wrapper for grouping related components. +- Accordion Summary: the wrapper for the Accordion header, which expands or collapses the content when clicked. +- Accordion Details: the wrapper for the Accordion content. +- Accordion Actions: an optional wrapper that groups a set of buttons. + +{{"demo": "AccordionUsage.js", "bg": true}} + :::info This component is no longer documented in the [Material Design guidelines](https://m2.material.io/), but Material UI will continue to support it. ::: -## Basic accordion +## Basics + +```jsx +import Accordion from '@mui/material/Accordion'; +import AccordionDetails from '@mui/material/AccordionDetails'; +import AccordionSummary from '@mui/material/AccordionSummary'; +``` + +### Expand icon + +Use the `expandIcon` prop on the Accordion Summary component to change the expand indicator icon. +The component handles the turning upside-down transition automatically. + +{{"demo": "AccordionExpandIcon.js", "bg": true}} + +### Expanded by default + +Use the `defaultExpanded` prop on the Accordion component to have it opened by default. + +{{"demo": "AccordionExpandDefault.js", "bg": true}} + +### Transition + +Use the `TransitionComponent` and `TransitionProps` props to change the Accordion's default transition. + +{{"demo": "AccordionTransition.js", "bg": true}} -{{"demo": "BasicAccordion.js", "bg": true}} +### Disabled item -## Controlled accordion +Use the `disabled` prop on the Accordion component to disable interaction and focus. + +{{"demo": "DisabledAccordion.js", "bg": true}} + +### Controlled Accordion The Accordion component can be controlled or uncontrolled. +{{"demo": "ControlledAccordions.js", "bg": true}} + :::info - A component is **controlled** when it's managed by its parent using props. @@ -35,34 +75,55 @@ The Accordion component can be controlled or uncontrolled. Learn more about controlled and uncontrolled components in the [React documentation](https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components). ::: -{{"demo": "ControlledAccordions.js", "bg": true}} - ## Customization -Here is an example of customizing the component. -You can learn more about this in the [overrides documentation page](/material-ui/customization/how-to-customize/). +### Only one expanded at a time -{{"demo": "CustomizedAccordions.js"}} +Use the `expanded` prop with React's `useState` hook to allow only one Accordion item to be expanded at a time. +The demo below also shows a bit of visual customziation. + +{{"demo": "CustomizedAccordions.js", "bg": true}} ## Performance -The content of Accordions is mounted by default even if the accordion is not expanded. +The Accordion content is mounted by default even if it's not expanded. This default behavior has server-side rendering and SEO in mind. -If you render expensive component trees inside your accordion details or simply render many -accordions it might be a good idea to change this default behavior by enabling the -`unmountOnExit` in `TransitionProps`: + +If you render the Accordion Details with a big component tree nested inside, or if you have many Accordions, you may want to change this behavior by setting `unmountOnExit` to `true` inside the `TransitionProps` prop to improve performance: ```jsx ``` -As with any performance optimization this is not a silver bullet. -Be sure to identify bottlenecks first and then try out these optimization strategies. - ## Accessibility -(WAI-ARIA: https://www.w3.org/WAI/ARIA/apg/patterns/accordion/) +The [WAI-ARIA guidelines for accordions](https://www.w3.org/WAI/ARIA/apg/patterns/accordion/) recommend setting an `id` and `aria-controls`, which in this case would apply to the Accordion Summary component. +The Accordion component then derives the necessary `aria-labelledby` and `id` from its content. + +```jsx + + + Header + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. + + +``` + +## Anatomy + +The Accordion component is composed of a root `
` that houses interior elements like the Accordion Summary and other optional components (such as buttons or decorators). -For optimal accessibility we recommend setting `id` and `aria-controls` on the -`AccordionSummary`. The `Accordion` will derive the necessary `aria-labelledby` -and `id` for the content region of the accordion. +```jsx +
+
+ +
+
+
+ +
+
+
+```