Skip to content

Commit

Permalink
[Typography] Migrate styles to Emotion (#23841)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanailH committed Dec 18, 2020
1 parent 7afb764 commit 259253d
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 175 deletions.
2 changes: 1 addition & 1 deletion docs/pages/api-docs/typography.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@
"filename": "/packages/material-ui/src/Typography/Typography.js",
"inheritance": null,
"demos": "<ul><li><a href=\"/components/breadcrumbs/\">Breadcrumbs</a></li>\n<li><a href=\"/components/typography/\">Typography</a></li></ul>",
"styledComponent": false
"styledComponent": true
}
44 changes: 44 additions & 0 deletions docs/scripts/buildApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,50 @@ async function updateStylesDefinition(context: {
// Do nothing as not every components has an unstyled version
}

// If there is no unstyledFile we need to extract this info from the component's definition file
if (typesFilename !== unstyledFileName) {
try {
const typesSource = readFileSync(typesFilename, { encoding: 'utf8' });
const typesAST = await babel.parseAsync(typesSource, {
configFile: false,
filename: typesFilename,
presets: [require.resolve('@babel/preset-typescript')],
});
if (typesAST === null) {
throw new Error('No AST returned from babel.');
}

traverse(typesAST, {
TSPropertySignature(babelPath) {
const { node } = babelPath;
const possiblyPropName = (node.key as babel.types.Identifier).name;
if (possiblyPropName === 'classes' && node.typeAnnotation !== null) {
const members = (node.typeAnnotation.typeAnnotation as babel.types.TSTypeLiteral)
.members;

if (members) {
styles.descriptions = styles.descriptions || {};
members.forEach((member) => {
const className = ((member as babel.types.TSPropertySignature)
.key as babel.types.Identifier).name;
styles.classes.push(className);
if (member.leadingComments) {
styles.descriptions[className] = trimComment(member.leadingComments[0].value);
}
});
}
}
},
});

if (styles.classes.length > 0) {
styles.name = generateMuiName(path.parse(component.filename).name);
}
} catch (e) {
// Do nothing as not every components has an unstyled version
}
}

styles.classes = Array.from(new Set(styles.classes));
}

Expand Down
10 changes: 5 additions & 5 deletions docs/src/modules/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import MuiLink from '@material-ui/core/Link';
import { useUserLanguage } from 'docs/src/modules/utils/i18n';

const NextComposed = React.forwardRef(function NextComposed(props, ref) {
const { as, href, ...other } = props;
const { linkAs, href, ...other } = props;

return (
<NextLink href={href} as={as}>
<NextLink href={href} as={linkAs}>
<a ref={ref} {...other} />
</NextLink>
);
});

NextComposed.propTypes = {
as: PropTypes.string,
href: PropTypes.string,
linkAs: PropTypes.string,
};

// A styled version of the Next.js Link component:
Expand Down Expand Up @@ -64,7 +64,7 @@ function Link(props) {
if (naked) {
return (
<NextComposed
as={linkAs}
linkAs={linkAs}
className={className}
href={href}
ref={innerRef}
Expand All @@ -76,7 +76,7 @@ function Link(props) {

return (
<MuiLink
as={linkAs}
linkAs={linkAs}
component={NextComposed}
className={className}
href={href}
Expand Down
6 changes: 6 additions & 0 deletions docs/src/pages/guides/migration-v4/migration-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ const classes = makeStyles(theme => ({
}));
```

### Core components

As the core components use emotion as a styled engine, the props used by emotion are not intercepted. The prop `as` in the following codesnippet will not be propagated to the `SomeOtherComponent`.

`<MuiComponent component={SomeOtherComponent} as="button" />`

### AppBar

- [AppBar] Remove z-index when position static and relative
Expand Down
6 changes: 3 additions & 3 deletions examples/nextjs-with-typescript/src/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import NextLink, { LinkProps as NextLinkProps } from 'next/link';
import MuiLink, { LinkProps as MuiLinkProps } from '@material-ui/core/Link';

type NextComposedProps = Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> &
NextLinkProps;
Omit<NextLinkProps, 'as'> & { linkAs?: NextLinkProps['as'] };

const NextComposed = React.forwardRef<HTMLAnchorElement, NextComposedProps>((props, ref) => {
const { as, href, replace, scroll, passHref, shallow, prefetch, ...other } = props;
const { href, linkAs, replace, scroll, passHref, shallow, prefetch, ...other } = props;

return (
<NextLink
href={href}
prefetch={prefetch}
as={as}
as={linkAs}
replace={replace}
scroll={scroll}
shallow={shallow}
Expand Down
6 changes: 3 additions & 3 deletions examples/nextjs/src/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import MuiLink from '@material-ui/core/Link';

const NextComposed = React.forwardRef(function NextComposed(props, ref) {
// eslint-disable-next-line react/prop-types
const { as, href, replace, scroll, passHref, shallow, prefetch, ...other } = props;
const { href, linkAs, replace, scroll, passHref, shallow, prefetch, ...other } = props;

return (
<NextLink
href={href}
prefetch={prefetch}
as={as}
as={linkAs}
replace={replace}
scroll={scroll}
shallow={shallow}
Expand Down Expand Up @@ -54,10 +54,10 @@ function Link(props) {

Link.propTypes = {
activeClassName: PropTypes.string,
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
className: PropTypes.string,
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
linkAs: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
naked: PropTypes.bool,
onClick: PropTypes.func,
prefetch: PropTypes.bool,
Expand Down
4 changes: 1 addition & 3 deletions packages/material-ui/src/CardHeader/CardHeader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import * as React from 'react';
import { expect } from 'chai';
import { getClasses, createMount, createClientRender, describeConformance } from 'test/utils';
import CardHeader from './CardHeader';
import Typography from '../Typography';
import { typographyClasses } from '../Typography';

describe('<CardHeader />', () => {
const mount = createMount();
let classes;
let typographyClasses;
const render = createClientRender();

before(() => {
typographyClasses = getClasses(<Typography />);
classes = getClasses(<CardHeader />);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { expect } from 'chai';
import { getClasses, createMount, createClientRender, describeConformance } from 'test/utils';
import Typography from '../Typography';
import { typographyClasses } from '../Typography';
import InputAdornment from './InputAdornment';
import TextField from '../TextField';
import FormControl from '../FormControl';
Expand All @@ -26,7 +26,6 @@ describe('<InputAdornment />', () => {

it('should wrap text children in a Typography', () => {
const { container } = render(<InputAdornment position="start">foo</InputAdornment>);
const typographyClasses = getClasses(<Typography />);
const typography = container.querySelector(`.${typographyClasses.root}`);

expect(typography).not.to.equal(null);
Expand Down Expand Up @@ -161,7 +160,6 @@ describe('<InputAdornment />', () => {
foo
</InputAdornment>,
);
const typographyClasses = getClasses(<Typography />);

expect(container.querySelector(`.${typographyClasses.root}`)).to.equal(null);
});
Expand Down
4 changes: 1 addition & 3 deletions packages/material-ui/src/Link/Link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
fireEvent,
} from 'test/utils';
import Link from './Link';
import Typography from '../Typography';
import Typography, { typographyClasses } from '../Typography';

function focusVisible(element) {
act(() => {
Expand All @@ -24,11 +24,9 @@ describe('<Link />', () => {
const mount = createMount();
const render = createClientRender();
let classes;
let typographyClasses;

before(() => {
classes = getClasses(<Link href="/">Home</Link>);
typographyClasses = getClasses(<Typography />);
});

describeConformance(<Link href="/">Home</Link>, () => ({
Expand Down
4 changes: 1 addition & 3 deletions packages/material-ui/src/ListItemText/ListItemText.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import * as React from 'react';
import { expect } from 'chai';
import { getClasses, createMount, createClientRender, describeConformance } from 'test/utils';
import Typography from '../Typography';
import Typography, { typographyClasses } from '../Typography';
import ListItemText from './ListItemText';

describe('<ListItemText />', () => {
const mount = createMount();
const render = createClientRender();
let classes;
let typographyClasses;

before(() => {
classes = getClasses(<ListItemText />);
typographyClasses = getClasses(<Typography />);
});

describeConformance(<ListItemText />, () => ({
Expand Down
4 changes: 1 addition & 3 deletions packages/material-ui/src/StepLabel/StepLabel.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { expect } from 'chai';
import { getClasses, createClientRender, createMount, describeConformance } from 'test/utils';
import Typography from '../Typography';
import Typography, { typographyClasses } from '../Typography';
import Stepper from '../Stepper';
import Step from '../Step';
import StepIcon from '../StepIcon';
Expand All @@ -10,14 +10,12 @@ import StepLabel from './StepLabel';
describe('<StepLabel />', () => {
let classes;
let iconClasses;
let typographyClasses;
const mount = createMount({ strict: true });
const render = createClientRender();

before(() => {
classes = getClasses(<StepLabel />);
iconClasses = getClasses(<StepIcon />);
typographyClasses = getClasses(<Typography />);
});

describeConformance(<StepLabel />, () => ({
Expand Down

0 comments on commit 259253d

Please sign in to comment.