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

[docs] Animate component's mounting and unmounting #24049

Merged
merged 8 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"react-spring": "^8.0.27",
"react-swipeable-views": "^0.13.9",
"react-text-mask": "^5.0.2",
"react-transition-group": "^4.4.1",
"react-virtualized": "^9.21.1",
"react-window": "^1.8.5",
"recast": "^0.20.2",
Expand Down
72 changes: 72 additions & 0 deletions docs/src/pages/components/transitions/TransitionGroupExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import {
Button,
Collapse,
IconButton,
List,
ListItem,
ListItemSecondaryAction,
ListItemText,
} from '@material-ui/core';
import DeleteIcon from '@material-ui/icons/Delete';
import { TransitionGroup } from 'react-transition-group';

const useStyles = makeStyles((theme) => ({
root: {
width: '100%',
backgroundColor: theme.palette.background.paper,
},
button: {
marginBottom: theme.spacing(2),
},
}));

const FRUITS = ['Apple', 'Banana', 'Mango', 'Papaya', 'Watermelon', 'Coconut'];

export default function TransitionGroupExample() {
const classes = useStyles();
const [fruitsInBasket, setFruitsInBasket] = React.useState(FRUITS.slice(0, 3));

const handleAddFruit = () => {
const nextHiddenItem = FRUITS.find((i) => !fruitsInBasket.includes(i));
if (nextHiddenItem) setFruitsInBasket((prev) => [nextHiddenItem, ...prev]);
};

const handleRemoveFruit = (item) => {
setFruitsInBasket((prev) => [...prev.filter((i) => i !== item)]);
};

return (
<div>
<Button
variant="contained"
className={classes.button}
disabled={fruitsInBasket.length >= FRUITS.length}
onClick={handleAddFruit}
>
Add fruit to basket
</Button>
<List className={classes.root}>
<TransitionGroup>
{fruitsInBasket.map((item) => (
<Collapse key={item} in>
<ListItem>
<ListItemText primary={item} />
<ListItemSecondaryAction>
<IconButton
edge={'end'}
aria-label="delete"
onClick={() => handleRemoveFruit(item)}
>
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
</Collapse>
))}
</TransitionGroup>
</List>
</div>
);
}
74 changes: 74 additions & 0 deletions docs/src/pages/components/transitions/TransitionGroupExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as React from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import {
Button,
Collapse,
IconButton,
List,
ListItem,
ListItemSecondaryAction,
ListItemText,
} from '@material-ui/core';
import DeleteIcon from '@material-ui/icons/Delete';
import { TransitionGroup } from 'react-transition-group';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
width: '100%',
backgroundColor: theme.palette.background.paper,
},
button: {
marginBottom: theme.spacing(2),
},
}),
);

const FRUITS = ['Apple', 'Banana', 'Mango', 'Papaya', 'Watermelon', 'Coconut'];

export default function TransitionGroupExample() {
const classes = useStyles();
const [fruitsInBasket, setFruitsInBasket] = React.useState(FRUITS.slice(0, 3));

const handleAddFruit = () => {
const nextHiddenItem = FRUITS.find((i) => !fruitsInBasket.includes(i));
if (nextHiddenItem) setFruitsInBasket((prev) => [nextHiddenItem, ...prev]);
};

const handleRemoveFruit = (item: string) => {
setFruitsInBasket((prev) => [...prev.filter((i) => i !== item)]);
};

return (
<div>
<Button
variant="contained"
className={classes.button}
disabled={fruitsInBasket.length >= FRUITS.length}
onClick={handleAddFruit}
>
Add fruit to basket
</Button>
<List className={classes.root}>
<TransitionGroup>
{fruitsInBasket.map((item) => (
<Collapse key={item} in>
<ListItem>
<ListItemText primary={item} />
<ListItemSecondaryAction>
<IconButton
edge={'end'}
aria-label="delete"
onClick={() => handleRemoveFruit(item)}
>
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
</Collapse>
))}
</TransitionGroup>
</List>
</div>
);
}
9 changes: 9 additions & 0 deletions docs/src/pages/components/transitions/transitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ This example also demonstrates how to delay the enter transition.

{{"demo": "pages/components/transitions/SimpleZoom.js", "bg": true}}

## Animate component's mounting and unmounting

To animate a component when it is mounted or unmounted, you can use the `TransitionGroup` component from `react-transition-group`.
As components are added or removed, the `in` prop is toggled automatically by `TransitionGroup`.

More information about the `TransitionGroup` component can be found [here](https://reactcommunity.org/react-transition-group/transition-group).

{{"demo": "pages/components/transitions/TransitionGroupExample.js", "bg": true}}

## TransitionComponent prop

Some Material-UI components use these transitions internally. These accept a `TransitionComponent` prop to customize the default transition.
Expand Down