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

[Timeline] Migrate Timeline to emotion #25838

Merged
merged 9 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 4 additions & 2 deletions docs/pages/api-docs/timeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"default": "'left'"
},
"children": { "type": { "name": "node" } },
"classes": { "type": { "name": "object" } }
"classes": { "type": { "name": "object" } },
"className": { "type": { "name": "string" } },
"sx": { "type": { "name": "object" } }
},
"name": "Timeline",
"styles": {
Expand All @@ -21,6 +23,6 @@
"filename": "/packages/material-ui-lab/src/Timeline/Timeline.tsx",
"inheritance": null,
"demos": "<ul><li><a href=\"/components/timeline/\">Timeline</a></li></ul>",
"styledComponent": false,
"styledComponent": true,
"cssComponent": false
}
4 changes: 3 additions & 1 deletion docs/translations/api-docs/timeline/timeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"propDescriptions": {
"align": "The position where the timeline&#39;s content should appear.",
"children": "The content of the component.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details."
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"className": "className applied to the root element.",
"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."
},
"classDescriptions": {
"root": { "description": "Styles applied to the root element." },
Expand Down
16 changes: 7 additions & 9 deletions packages/material-ui-lab/src/Timeline/Timeline.test.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import * as React from 'react';
import { getClasses, createMount, describeConformance } from 'test/utils';
import Timeline from './Timeline';
import { createClientRender, createMount, describeConformanceV5 } from 'test/utils';
import Timeline, { timelineClasses as classes } from './index';
mnajdova marked this conversation as resolved.
Show resolved Hide resolved

describe('<Timeline />', () => {
const mount = createMount();
let classes: Record<string, string>;
const render = createClientRender();

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

describeConformance(<Timeline />, () => ({
describeConformanceV5(<Timeline />, () => ({
classes,
inheritComponent: 'ul',
mount,
render,
muiName: 'MuiTimeline',
refInstanceof: window.HTMLUListElement,
skip: ['componentProp'],
skip: ['componentProp', 'componentsProp', 'themeVariants'],
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
}));
});
107 changes: 79 additions & 28 deletions packages/material-ui-lab/src/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { SxProps } from '@material-ui/system';
// eslint-disable-next-line no-restricted-imports -- importing types
import { InternalStandardProps as StandardProps } from '@material-ui/core';
import { capitalize } from '@material-ui/core/utils';
import { MuiStyles, withStyles } from '@material-ui/core/styles';
import { deepmerge } from '@material-ui/utils';
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
import { unstable_composeClasses as composeClasses } from '@material-ui/unstyled';
import {
experimentalStyled,
unstable_useThemeProps as useThemeProps,
Theme,
} from '@material-ui/core/styles';
import TimelineContext from './TimelineContext';
import { getTimelineUtilityClass } from './timelineClasses';

export type TimelineClassKey = 'root' | 'alignLeft' | 'alignRight' | 'alignAlternate';

export const styles: MuiStyles<TimelineClassKey> = {
/* Styles applied to the root element. */
root: {
display: 'flex',
flexDirection: 'column',
padding: '6px 16px',
flexGrow: 1,
},
/* Styles applied to the root element if `align="left"`. */
alignLeft: {},
/* Styles applied to the root element if `align="right"`. */
alignRight: {},
/* Styles applied to the root element if `align="alternate"`. */
alignAlternate: {},
};

export interface TimelineProps extends StandardProps<React.HTMLAttributes<HTMLUListElement>> {
/**
* The position where the timeline's content should appear.
Expand All @@ -48,20 +40,75 @@ export interface TimelineProps extends StandardProps<React.HTMLAttributes<HTMLUL
/** Styles applied to the root element if `align="alternate"`. */
alignAlternate?: string;
};

siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
/**
* className applied to the root element.
*/
className?: string;
Comment on lines +42 to +46
Copy link
Member

@oliviertassinari oliviertassinari Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird, I was expecting it to already come for free with React.HTMLAttributes<HTMLUListElement

/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme>;
}

const Timeline = React.forwardRef<HTMLUListElement, TimelineProps>(function Timeline(props, ref) {
const { align = 'left', classes, className, ...other } = props;
type Styles = Record<TimelineClassKey, any>;
type StyleProps = TimelineProps;

const useUtilityClasses = (styleProps: StyleProps) => {
const { align, classes } = styleProps;

const slots = {
root: ['root', align && `align${capitalize(align)}`],
};

return composeClasses(slots, getTimelineUtilityClass, classes);
};

const TimelineRoot = experimentalStyled(
'ul' as const,
{},
{
name: 'MuiTimeline',
slot: 'Root',
// @ts-ignore TODO should fix the type in experimentalStyled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great that we can spot issues like this by actually using it in TS. Let me know if you need some help with this. Probably these can be inferred from the props' props, but also allow generic, as I think sometimes we have additional or calculated props here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnajdova I fix as workaround by changing from string | object to Record<string, any>

overridesResolver: (props: { styleProps: StyleProps }, styles: Styles) => {
const { styleProps } = props;
return deepmerge(
{
...(styleProps.align &&
styles[`align${capitalize(styleProps.align)}` as TimelineClassKey]),
},
styles.root || {},
);
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
},
},
)({
display: 'flex',
flexDirection: 'column',
padding: '6px 16px',
flexGrow: 1,
});

/**
*
* Demos:
*
* - [Timeline](https://material-ui.com/components/timeline/)
*
* API:
*
* - [Timeline API](https://material-ui.com/api/timeline/)
*/
const Timeline = React.forwardRef<HTMLUListElement, TimelineProps>(function Timeline(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiTimeline' });
const { align = 'left', className, ...other } = props;
const styleProps = { ...props, align };
const classes = useUtilityClasses(styleProps);
return (
<TimelineContext.Provider value={{ align }}>
<ul
className={clsx(
classes!.root,
// @ts-expect-error unsafe string concat
classes[`align${capitalize(align)}`],
className,
)}
<TimelineRoot
className={clsx(classes.root, className)}
styleProps={styleProps}
// @ts-expect-error TypeScript bug, need to keep unknown for DX
ref={ref}
{...other}
Expand Down Expand Up @@ -89,9 +136,13 @@ Timeline.propTypes /* remove-proptypes */ = {
*/
classes: PropTypes.object,
/**
* @ignore
* className applied to the root element.
*/
className: PropTypes.string,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.object,
} as any;

/**
Expand All @@ -104,4 +155,4 @@ Timeline.propTypes /* remove-proptypes */ = {
*
* - [Timeline API](https://material-ui.com/api/timeline/)
*/
export default withStyles(styles, { name: 'MuiTimeline' })(Timeline);
export default Timeline;
3 changes: 3 additions & 0 deletions packages/material-ui-lab/src/Timeline/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export { default } from './Timeline';

export type TimelineClassKey = import('./Timeline').TimelineClassKey;
export type TimelineProps = import('./Timeline').TimelineProps;

export { default as timelineClasses } from './timelineClasses';
export * from './timelineClasses';
14 changes: 14 additions & 0 deletions packages/material-ui-lab/src/Timeline/timelineClasses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled';

export function getTimelineUtilityClass(slot: string) {
return generateUtilityClass('MuiTimeline', slot);
}

const timelineClasses = generateUtilityClasses('MuiTimeline', [
'root',
'alignLeft',
'alignRight',
'alignAlternate',
]);

export default timelineClasses;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function composeClasses<ClassKey extends string>(
slots: Record<ClassKey, ReadonlyArray<string | false>>,
slots: Record<ClassKey, ReadonlyArray<string | false | undefined>>,
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
getUtilityClass: (slot: string) => string,
classes: Record<string, string> | undefined,
): Record<ClassKey, string> {
Expand Down