Skip to content

Commit

Permalink
Merge pull request #4294 from marmelab/delayed-skeleton
Browse files Browse the repository at this point in the history
Delay Skeleton Display a la Suspense
  • Loading branch information
fzaninotto authored Jan 15, 2020
2 parents d6050a2 + 5e1b7ce commit da78414
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
109 changes: 57 additions & 52 deletions packages/ra-ui-materialui/src/list/DatagridLoading.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import IconButton from '@material-ui/core/IconButton';
import Checkbox from '@material-ui/core/Checkbox';
import classnames from 'classnames';
import Placeholder from './Placeholder';
import { useTimeout } from 'ra-core';

const times = (nbChildren, fn) =>
Array.from({ length: nbChildren }, (_, key) => fn(key));
Expand All @@ -22,55 +23,18 @@ const DatagridLoading = ({
nbChildren,
nbFakeLines = 5,
size,
}) => (
<Table className={classnames(classes.table, className)} size={size}>
<TableHead>
<TableRow className={classes.row}>
{expand && (
<TableCell
padding="none"
className={classes.expandHeader}
/>
)}
{hasBulkActions && (
<TableCell
padding="checkbox"
className={classes.expandIconCell}
>
<Checkbox
className="select-all"
color="primary"
checked={false}
/>
</TableCell>
)}
{times(nbChildren, key => (
<TableCell
variant="head"
className={classes.headerCell}
key={key}
>
<Placeholder />
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{times(nbFakeLines, key1 => (
<TableRow key={key1} style={{ opacity: 1 / (key1 + 1) }}>
}) => {
const oneSecondHasPassed = useTimeout(1000);

return oneSecondHasPassed ? (
<Table className={classnames(classes.table, className)} size={size}>
<TableHead>
<TableRow className={classes.row}>
{expand && (
<TableCell
padding="none"
className={classes.expandIconCell}
>
<IconButton
className={classes.expandIcon}
component="div"
aria-hidden="true"
>
<ExpandMoreIcon />
</IconButton>
</TableCell>
className={classes.expandHeader}
/>
)}
{hasBulkActions && (
<TableCell
Expand All @@ -84,16 +48,57 @@ const DatagridLoading = ({
/>
</TableCell>
)}
{times(nbChildren, key2 => (
<TableCell className={classes.rowCell} key={key2}>
{times(nbChildren, key => (
<TableCell
variant="head"
className={classes.headerCell}
key={key}
>
<Placeholder />
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
);
</TableHead>
<TableBody>
{times(nbFakeLines, key1 => (
<TableRow key={key1} style={{ opacity: 1 / (key1 + 1) }}>
{expand && (
<TableCell
padding="none"
className={classes.expandIconCell}
>
<IconButton
className={classes.expandIcon}
component="div"
aria-hidden="true"
>
<ExpandMoreIcon />
</IconButton>
</TableCell>
)}
{hasBulkActions && (
<TableCell
padding="checkbox"
className={classes.expandIconCell}
>
<Checkbox
className="select-all"
color="primary"
checked={false}
/>
</TableCell>
)}
{times(nbChildren, key2 => (
<TableCell className={classes.rowCell} key={key2}>
<Placeholder />
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
) : null;
};

DatagridLoading.propTypes = {
classes: PropTypes.object,
Expand Down
1 change: 1 addition & 0 deletions packages/ra-ui-materialui/src/list/SimpleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const SimpleList = ({
/>
);
}

return (
(loading || total > 0) && (
<List className={className} {...sanitizeListRestProps(rest)}>
Expand Down
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/list/SimpleListLoading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ListItemText from '@material-ui/core/ListItemText';
import { makeStyles } from '@material-ui/core/styles';

import Placeholder from './Placeholder';
import { useTimeout } from 'ra-core';

const useStyles = makeStyles(
theme => ({
Expand Down Expand Up @@ -45,8 +46,9 @@ const SimpleListLoading: FC<Props & ListProps> = ({
...rest
}) => {
const classes = useStyles({ classes: classesOverride });
const oneSecondHasPassed = useTimeout(1000);

return (
return oneSecondHasPassed ? (
<List className={className} {...rest}>
{times(nbFakeLines, key => (
<ListItem>
Expand Down Expand Up @@ -78,7 +80,7 @@ const SimpleListLoading: FC<Props & ListProps> = ({
</ListItem>
))}
</List>
);
) : null;
};

SimpleListLoading.propTypes = {
Expand Down

0 comments on commit da78414

Please sign in to comment.