Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an MUI accordion for the sidebar collapsing sections #3822

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __tests__/src/components/CollapsibleSection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ describe('CollapsibleSection', () => {
expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'expandSection');
});

it('renders children based on the open state', async () => {
expect(screen.getByTestId('child')).toBeInTheDocument();
it('displays children based on the open state', async () => {
expect(screen.getByTestId('child')).toBeVisible();
await userEvent.click(screen.getByRole('button'));
expect(screen.queryByTestId('child')).not.toBeInTheDocument();
expect(screen.queryByTestId('child')).not.toBeVisible();
});
});
53 changes: 16 additions & 37 deletions src/components/CollapsibleSection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { Component } from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import Typography from '@mui/material/Typography';
import KeyboardArrowDown from '@mui/icons-material/KeyboardArrowDownSharp';
import KeyboardArrowUp from '@mui/icons-material/KeyboardArrowUpSharp';
import MiradorMenuButton from '../containers/MiradorMenuButton';

const StyledContainer = styled('div')(() => ({
alignItems: 'flex-start',
cursor: 'pointer', // This style will be applied to Typography
display: 'flex',
justifyContent: 'space-between',
}));
import Accordion from '@mui/material/Accordion';
import AccordionDetails from '@mui/material/AccordionDetails';
import AccordionSummary from '@mui/material/AccordionSummary';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';

/**
* CollapsableSection ~
Expand All @@ -22,14 +15,12 @@ export class CollapsibleSection extends Component {
super(props);

this.state = { open: true };
this.toggleSection = this.toggleSection.bind(this);
this.handleChange = this.handleChange.bind(this);
}

/** */
toggleSection() {
const { open } = this.state;

this.setState({ open: !open });
/** Control the accordion state so we can provide aria labeling */
handleChange(event, isExpanded) {
this.setState({ open: isExpanded });
}

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

return (
<>
<StyledContainer sx={{ padding: 0 }}>
<Typography
sx={{ alignSelf: 'center', cursor: 'pointer' }}
id={id}
onClick={this.toggleSection}
variant="overline"
component="h4"
>
<Accordion id={id} elevation={0} expanded={open} onChange={this.handleChange} disableGutters square variant="compact">
<AccordionSummary id={`${id}-header`} aria-controls={`${id}-content`} aria-label={t(open ? 'collapseSection' : 'expandSection', { section: label })} expandIcon={<ExpandMoreIcon />}>
<Typography variant="overline" component="h4">
{label}
</Typography>
<MiradorMenuButton
aria-label={t(open ? 'collapseSection' : 'expandSection', { section: label })}
aria-expanded={open}
sx={{ padding: 0 }}
onClick={this.toggleSection}
>
{open ? <KeyboardArrowUp /> : <KeyboardArrowDown />}
</MiradorMenuButton>
</StyledContainer>
{open && children}
</>
</AccordionSummary>
<AccordionDetails>
{children}
</AccordionDetails>
</Accordion>
);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/config/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ export default {
},
},
},
MuiAccordion: {
variants: [
{
props: { variant: 'compact' },
style: {
'& .MuiAccordionSummary-root': {
minHeight: 'unset',
padding: 0,
},
'& .MuiAccordionSummary-content': {
margin: 0,
},
'& .MuiAccordionDetails-root': {
padding: 0,
},
},
},
],
},
MuiButtonBase: {
defaultProps: {
disableTouchRipple: true,
Expand Down
Loading