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

[Accordion] Migrate to emotion #24518

Merged
merged 27 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
074f8d3
[Accordion] Migrate to emotion
queengooborg Jan 20, 2021
37fd936
Simplify classes
queengooborg Jan 20, 2021
c31b1ca
Update test
queengooborg Jan 20, 2021
75a1b44
Resolve various issues
queengooborg Jan 20, 2021
44bf6e9
Revert accidental changes
queengooborg Jan 20, 2021
465f9ff
Make sure to pass styleProps
queengooborg Jan 20, 2021
84db65f
Fix expanded value passed into styleProps
queengooborg Jan 20, 2021
ce9c028
Consolidate pseudo classes
queengooborg Jan 20, 2021
a7f48f3
Remove redundant newline
queengooborg Jan 20, 2021
a211ed5
Use first/last-of-type vs. first/last-child
queengooborg Jan 20, 2021
73ba82b
Fix root base element
queengooborg Jan 20, 2021
91ddf6c
Resolve className not passed in
queengooborg Jan 20, 2021
5029053
Skip componentsProp test
queengooborg Jan 20, 2021
d9f5779
Revert "Consolidate pseudo classes"
queengooborg Jan 20, 2021
9399779
Update tests
queengooborg Jan 20, 2021
b197b8a
Place rounded styling in separate callback
queengooborg Jan 20, 2021
d458c31
fix tests
oliviertassinari Jan 20, 2021
ca2c527
fix ci
oliviertassinari Jan 21, 2021
01c9cc7
Remove "region" slot
queengooborg Jan 21, 2021
ba75604
Update packages/material-ui/src/Accordion/Accordion.js
queengooborg Jan 21, 2021
b608ec9
Update packages/material-ui/src/Accordion/Accordion.js
queengooborg Jan 21, 2021
d26fde0
Update packages/material-ui/src/Accordion/Accordion.test.js
mnajdova Jan 21, 2021
9960d84
Merge branch 'next' into migrate/Accordion
mnajdova Jan 21, 2021
1b20c63
Update tests
queengooborg Jan 21, 2021
8fd8b4b
Update packages/material-ui/src/Accordion/Accordion.js
queengooborg Jan 21, 2021
6572655
Update exports
queengooborg Jan 21, 2021
ccf936f
Reserve "region" slot again
queengooborg Jan 21, 2021
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
3 changes: 2 additions & 1 deletion docs/pages/api-docs/accordion.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"expanded": { "type": { "name": "bool" } },
"onChange": { "type": { "name": "func" } },
"square": { "type": { "name": "bool" } },
"sx": { "type": { "name": "object" } },
"TransitionComponent": { "type": { "name": "elementType" }, "default": "Collapse" },
"TransitionProps": { "type": { "name": "object" } }
},
Expand All @@ -21,6 +22,6 @@
"filename": "/packages/material-ui/src/Accordion/Accordion.js",
"inheritance": { "component": "Paper", "pathname": "/api/paper/" },
"demos": "<ul><li><a href=\"/components/accordion/\">Accordion</a></li></ul>",
"styledComponent": false,
"styledComponent": true,
"cssComponent": false
}
1 change: 1 addition & 0 deletions docs/translations/api-docs/accordion/accordion.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"expanded": "If <code>true</code>, expands the accordion, otherwise collapse it. Setting this prop enables control over the accordion.",
"onChange": "Callback fired when the expand/collapse state is changed.<br><br><strong>Signature:</strong><br><code>function(event: object, expanded: boolean) =&gt; void</code><br><em>event:</em> The event source of the callback. <strong>Warning</strong>: This is a generic event not a change event.<br><em>expanded:</em> The <code>expanded</code> state of the accordion.",
"square": "If <code>true</code>, rounded corners are disabled.",
"sx": "The system prop that allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/basics/#the-sx-prop\">`sx` page</a> for more details.",
"TransitionComponent": "The component used for the transition. <a href=\"/components/transitions/#transitioncomponent-prop\">Follow this guide</a> to learn more about the requirements for this component.",
"TransitionProps": "Props applied to the transition element. By default, the element is based on this <a href=\"http://reactcommunity.org/react-transition-group/transition\"><code>Transition</code></a> component."
},
Expand Down
7 changes: 6 additions & 1 deletion packages/material-ui/src/Accordion/Accordion.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { InternalStandardProps as StandardProps } from '..';
import { SxProps } from '@material-ui/system';
import { InternalStandardProps as StandardProps, Theme } from '..';
import { TransitionProps } from '../transitions/transition';
import { PaperProps } from '../Paper';

Expand Down Expand Up @@ -45,6 +46,10 @@ export interface AccordionProps extends StandardProps<PaperProps, 'onChange'> {
* @param {boolean} expanded The `expanded` state of the accordion.
*/
onChange?: (event: React.SyntheticEvent, expanded: boolean) => void;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme>;
/**
* The component used for the transition.
* [Follow this guide](/components/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
Expand Down
129 changes: 82 additions & 47 deletions packages/material-ui/src/Accordion/Accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,52 @@ import * as React from 'react';
import { isFragment } from 'react-is';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { chainPropTypes } from '@material-ui/utils';
import { deepmerge, chainPropTypes } from '@material-ui/utils';
import { unstable_composeClasses as composeClasses } from '@material-ui/unstyled';
import experimentalStyled from '../styles/experimentalStyled';
import useThemeProps from '../styles/useThemeProps';
import Collapse from '../Collapse';
import Paper from '../Paper';
import withStyles from '../styles/withStyles';
import AccordionContext from './AccordionContext';
import useControlled from '../utils/useControlled';
import accordionClasses, { getAccordionUtilityClass } from './accordionClasses';

export const styles = (theme) => {
const transition = {
duration: theme.transitions.duration.shortest,
const overridesResolver = (props, styles) => {
const { styleProps } = props;

return deepmerge(styles.root || {}, {
...(!styleProps.square && styles.rounded),
[`& .${accordionClasses.region}`]: styles.region,
});
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
};

const useUtilityClasses = (styleProps) => {
const { classes, square, expanded, disabled } = styleProps;

const slots = {
root: ['root', !square && 'rounded', expanded && 'expanded', disabled && 'disabled'],
region: ['region'],
};

return {
/* Styles applied to the root element. */
root: {
return composeClasses(slots, getAccordionUtilityClass, classes);
};

const AccordionRoot = experimentalStyled(
Paper,
{},
{
name: 'MuiAccordion',
slot: 'Root',
overridesResolver,
},
)(
({ theme }) => {
const transition = {
duration: theme.transitions.duration.shortest,
};

return {
/* Styles applied to the root element. */
position: 'relative',
transition: theme.transitions.create(['margin'], transition),
overflowAnchor: 'none', // Keep the same scrolling position
Expand All @@ -31,40 +62,44 @@ export const styles = (theme) => {
backgroundColor: theme.palette.divider,
transition: theme.transitions.create(['opacity', 'background-color'], transition),
},
'&:first-child': {
'&:first-of-type': {
'&:before': {
display: 'none',
},
},
'&$expanded': {
/* Styles applied to the root element if `expanded={true}`. */
[`&.${accordionClasses.expanded}`]: {
margin: '16px 0',
'&:first-child': {
'&:before': {
opacity: 0,
},
'&:first-of-type': {
marginTop: 0,
},
'&:last-child': {
'&:last-of-type': {
marginBottom: 0,
},
'&:before': {
opacity: 0,
},
},
'&$expanded + &': {
'&:before': {
display: 'none',
'& + &': {
'&:before': {
display: 'none',
},
},
},
'&$disabled': {
/* Styles applied to the root element if `disabled={true}`. */
[`&.${accordionClasses.disabled}`]: {
backgroundColor: theme.palette.action.disabledBackground,
},
},
};
},
({ theme, styleProps }) => ({
/* Styles applied to the root element unless `square={true}`. */
rounded: {
...(!styleProps.square && {
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
borderRadius: 0,
'&:first-child': {
'&:first-of-type': {
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
borderTopLeftRadius: theme.shape.borderRadius,
borderTopRightRadius: theme.shape.borderRadius,
},
'&:last-child': {
'&:last-of-type': {
borderBottomLeftRadius: theme.shape.borderRadius,
borderBottomRightRadius: theme.shape.borderRadius,
// Fix a rendering issue on Edge
Expand All @@ -73,20 +108,14 @@ export const styles = (theme) => {
borderBottomRightRadius: 0,
},
},
},
/* Pseudo-class applied to the root element if `expanded={true}`. */
expanded: {},
/* Pseudo-class applied to the root element if `disabled={true}`. */
disabled: {},
/* Styles applied to the region element, the container of the children. */
region: {},
};
};
}),
}),
);

const Accordion = React.forwardRef(function Accordion(props, ref) {
const Accordion = React.forwardRef(function Accordion(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiAccordion' });
const {
children: childrenProp,
classes,
className,
defaultExpanded = false,
disabled = false,
Expand Down Expand Up @@ -123,18 +152,20 @@ const Accordion = React.forwardRef(function Accordion(props, ref) {
handleChange,
]);

const styleProps = {
...props,
square,
disabled,
expanded,
};

const classes = useUtilityClasses(styleProps);

return (
<Paper
className={clsx(
classes.root,
{
[classes.expanded]: expanded,
[classes.disabled]: disabled,
[classes.rounded]: !square,
},
className,
)}
<AccordionRoot
className={clsx(classes.root, className)}
ref={ref}
styleProps={styleProps}
square={square}
{...other}
>
Expand All @@ -149,7 +180,7 @@ const Accordion = React.forwardRef(function Accordion(props, ref) {
{children}
</div>
</TransitionComponent>
</Paper>
</AccordionRoot>
);
});

Expand Down Expand Up @@ -211,6 +242,10 @@ Accordion.propTypes = {
* @default false
*/
square: PropTypes.bool,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.object,
/**
* The component used for the transition.
* [Follow this guide](/components/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
Expand All @@ -224,4 +259,4 @@ Accordion.propTypes = {
TransitionProps: PropTypes.object,
};

export default withStyles(styles, { name: 'MuiAccordion' })(Accordion);
export default Accordion;
25 changes: 9 additions & 16 deletions packages/material-ui/src/Accordion/Accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,26 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import { spy } from 'sinon';
import {
createMount,
describeConformance,
getClasses,
createClientRender,
fireEvent,
} from 'test/utils';
import { createMount, describeConformanceV5, createClientRender, fireEvent } from 'test/utils';
import Paper from '../Paper';
import Accordion from './Accordion';
import AccordionSummary from '../AccordionSummary';
import classes from './accordionClasses';

describe('<Accordion />', () => {
const render = createClientRender();
const mount = createMount({ strict: true });
let classes;
const minimalChildren = [<AccordionSummary key="header">Header</AccordionSummary>];

before(() => {
classes = getClasses(<Accordion>{minimalChildren}</Accordion>);
});

describeConformance(<Accordion>{minimalChildren}</Accordion>, () => ({
describeConformanceV5(<Accordion>{minimalChildren}</Accordion>, () => ({
classes,
inheritComponent: Paper,
mount,
refInstanceof: window.HTMLDivElement,
skip: ['componentProp'],
muiName: 'MuiAccordion',
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
testVariantProps: { variant: 'rounded' },
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
testDeepOverrides: { slotName: 'region', slotClassName: classes.region },
skip: ['componentProp', 'componentsProp'],
}));

it('should render and not be controlled', () => {
Expand Down Expand Up @@ -130,7 +123,7 @@ describe('<Accordion />', () => {
it('requires at least one child', () => {
expect(() => {
PropTypes.checkPropTypes(
Accordion.Naked.propTypes,
Accordion.propTypes,
{ classes: {}, children: [] },
'prop',
'MockedName',
Expand All @@ -141,7 +134,7 @@ describe('<Accordion />', () => {
it('needs a valid element as the first child', () => {
expect(() => {
PropTypes.checkPropTypes(
Accordion.Naked.propTypes,
Accordion.propTypes,
{ classes: {}, children: <React.Fragment /> },
'prop',
'MockedName',
Expand Down
13 changes: 13 additions & 0 deletions packages/material-ui/src/Accordion/accordionClasses.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface AccordionClasses {
root: string;
rounded: string;
expanded: string;
disabled: string;
region: string;
}

declare const accordionClasses: AccordionClasses;

export function getAccordionUtilityClass(slot: string): string;

export default accordionClasses;
15 changes: 15 additions & 0 deletions packages/material-ui/src/Accordion/accordionClasses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled';

export function getAccordionUtilityClass(slot) {
return generateUtilityClass('MuiAccordion', slot);
}

const accordionClasses = generateUtilityClasses('MuiAccordion', [
'root',
'rounded',
'expanded',
'disabled',
'region',
]);

export default accordionClasses;
3 changes: 3 additions & 0 deletions packages/material-ui/src/Accordion/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { default } from './Accordion';
export * from './Accordion';

export { default as accordionClasses } from './accordionClasses';
export * from './accordionClasses';
3 changes: 3 additions & 0 deletions packages/material-ui/src/Accordion/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { default } from './Accordion';

export { default as accordionClasses } from './accordionClasses';
export * from './accordionClasses';