Skip to content

Commit 785ba3b

Browse files
committed
Use an MUI accordion for the sidebar collapsing sections
1 parent ea70a21 commit 785ba3b

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

__tests__/src/components/CollapsibleSection.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ describe('CollapsibleSection', () => {
3434
expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'expandSection');
3535
});
3636

37-
it('renders children based on the open state', async () => {
38-
expect(screen.getByTestId('child')).toBeInTheDocument();
37+
it('displays children based on the open state', async () => {
38+
expect(screen.getByTestId('child')).toBeVisible();
3939
await userEvent.click(screen.getByRole('button'));
40-
expect(screen.queryByTestId('child')).not.toBeInTheDocument();
40+
expect(screen.queryByTestId('child')).not.toBeVisible();
4141
});
4242
});

src/components/CollapsibleSection.js

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import { Component } from 'react';
22
import PropTypes from 'prop-types';
3-
import { styled } from '@mui/material/styles';
43
import Typography from '@mui/material/Typography';
5-
import KeyboardArrowDown from '@mui/icons-material/KeyboardArrowDownSharp';
6-
import KeyboardArrowUp from '@mui/icons-material/KeyboardArrowUpSharp';
7-
import MiradorMenuButton from '../containers/MiradorMenuButton';
8-
9-
const StyledContainer = styled('div')(() => ({
10-
alignItems: 'flex-start',
11-
cursor: 'pointer', // This style will be applied to Typography
12-
display: 'flex',
13-
justifyContent: 'space-between',
14-
}));
4+
import Accordion from '@mui/material/Accordion';
5+
import AccordionDetails from '@mui/material/AccordionDetails';
6+
import AccordionSummary from '@mui/material/AccordionSummary';
7+
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
158

169
/**
1710
* CollapsableSection ~
@@ -22,14 +15,12 @@ export class CollapsibleSection extends Component {
2215
super(props);
2316

2417
this.state = { open: true };
25-
this.toggleSection = this.toggleSection.bind(this);
18+
this.handleChange = this.handleChange.bind(this);
2619
}
2720

28-
/** */
29-
toggleSection() {
30-
const { open } = this.state;
31-
32-
this.setState({ open: !open });
21+
/** Control the accordion state so we can provide aria labeling */
22+
handleChange(event, isExpanded) {
23+
this.setState({ open: isExpanded });
3324
}
3425

3526
/**
@@ -42,28 +33,16 @@ export class CollapsibleSection extends Component {
4233
const { open } = this.state;
4334

4435
return (
45-
<>
46-
<StyledContainer sx={{ padding: 0 }}>
47-
<Typography
48-
sx={{ alignSelf: 'center', cursor: 'pointer' }}
49-
id={id}
50-
onClick={this.toggleSection}
51-
variant="overline"
52-
component="h4"
53-
>
36+
<Accordion id={id} elevation={0} expanded={open} onChange={this.handleChange} disableGutters square variant="compact">
37+
<AccordionSummary id={`${id}-header`} aria-controls={`${id}-content`} aria-label={t(open ? 'collapseSection' : 'expandSection', { section: label })} expandIcon={<ExpandMoreIcon />}>
38+
<Typography variant="overline" component="h4">
5439
{label}
5540
</Typography>
56-
<MiradorMenuButton
57-
aria-label={t(open ? 'collapseSection' : 'expandSection', { section: label })}
58-
aria-expanded={open}
59-
sx={{ padding: 0 }}
60-
onClick={this.toggleSection}
61-
>
62-
{open ? <KeyboardArrowUp /> : <KeyboardArrowDown />}
63-
</MiradorMenuButton>
64-
</StyledContainer>
65-
{open && children}
66-
</>
41+
</AccordionSummary>
42+
<AccordionDetails>
43+
{children}
44+
</AccordionDetails>
45+
</Accordion>
6746
);
6847
}
6948
}

src/config/settings.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ export default {
176176
},
177177
},
178178
},
179+
MuiAccordion: {
180+
variants: [
181+
{
182+
props: { variant: 'compact' },
183+
style: {
184+
'& .MuiAccordionSummary-root': {
185+
minHeight: 'unset',
186+
padding: 0,
187+
},
188+
'& .MuiAccordionSummary-content': {
189+
margin: 0,
190+
},
191+
'& .MuiAccordionDetails-root': {
192+
padding: 0,
193+
},
194+
},
195+
},
196+
],
197+
},
179198
MuiButtonBase: {
180199
defaultProps: {
181200
disableTouchRipple: true,

0 commit comments

Comments
 (0)