Skip to content

Commit

Permalink
[Dialog] Fix animated dialog example to remove memory leak warning (#786
Browse files Browse the repository at this point in the history
)

* dialog: fix warning in animated example docs
* dialog: add animated example
  • Loading branch information
mbellagamba authored Apr 21, 2021
1 parent 1ee681c commit 54764b1
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 37 deletions.
58 changes: 58 additions & 0 deletions packages/dialog/examples/animated.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from "react";
import { DialogOverlay, DialogContent } from "@reach/dialog";
import { animated, useTransition } from "react-spring";
import "@reach/dialog/styles.css";

let name = "Animated";

const AnimatedDialogOverlay = animated(DialogOverlay);
const AnimatedDialogContent = animated(DialogContent);

function Example() {
const [showDialog, setShowDialog] = React.useState(false);
const transitions = useTransition(showDialog, null, {
from: { opacity: 0, y: -10 },
enter: { opacity: 1, y: 0 },
leave: { opacity: 0, y: 10 },
});

return (
<div>
<button onClick={() => setShowDialog(true)}>Show Dialog</button>
{transitions.map(
({ item, key, props: styles }) =>
item && (
<AnimatedDialogOverlay
key={key}
style={{ opacity: styles.opacity }}
>
<AnimatedDialogContent
aria-labelledby="dialog-title"
style={{
transform: styles.y.interpolate(
(value) => `translate3d(0px, ${value}px, 0px)`
),
border: "4px solid hsla(0, 0%, 0%, 0.5)",
borderRadius: 10,
}}
>
<button onClick={() => setShowDialog(false)}>
Close Dialog
</button>
<h2 id="dialog-title">Animated Dialog</h2>
<p>React Spring makes it too easy!</p>
<input type="text" />
<br />
<input type="text" />
<button>Ayyyyyy</button>
</AnimatedDialogContent>
</AnimatedDialogOverlay>
)
)}
</div>
);
}

Example.story = { name };
export const Comp = Example;
export default { title: "Dialog" };
83 changes: 46 additions & 37 deletions website/src/pages/dialog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -518,46 +518,55 @@ If you'd like to animate the content, give [React Spring](https://github.com/drc
```jsx
// jsx-demo
function Example(props) {
(() => {
const AnimatedDialogOverlay = animated(DialogOverlay);
const AnimatedDialogContent = animated(DialogContent);
const [showDialog, setShowDialog] = React.useState(false);
const transitions = useTransition(showDialog, null, {
from: { opacity: 0, y: -10 },
enter: { opacity: 1, y: 0 },
leave: { opacity: 0, y: 10 },
});
return (
<div>
<button onClick={() => setShowDialog(true)}>Show Dialog</button>
{transitions.map(
({ item, key, props: styles }) =>
item && (
<AnimatedDialogOverlay style={{ opacity: styles.opacity }}>
<AnimatedDialogContent
style={{
transform: styles.y.interpolate(
(value) => `translate3d(0px, ${value}px, 0px)`
),
border: "4px solid hsla(0, 0%, 0%, 0.5)",
borderRadius: 10,
}}

function Example(props) {
const [showDialog, setShowDialog] = React.useState(false);
const transitions = useTransition(showDialog, null, {
from: { opacity: 0, y: -10 },
enter: { opacity: 1, y: 0 },
leave: { opacity: 0, y: 10 },
});
return (
<div>
<button onClick={() => setShowDialog(true)}>Show Dialog</button>
{transitions.map(
({ item, key, props: styles }) =>
item && (
<AnimatedDialogOverlay
key={key}
style={{ opacity: styles.opacity }}
>
<button onClick={() => setShowDialog(false)}>
Close Dialog
</button>
<p>React Spring makes it too easy!</p>
<input type="text" />
<br />
<input type="text" />
<button>Ayyyyyy</button>
</AnimatedDialogContent>
</AnimatedDialogOverlay>
)
)}
</div>
);
}
<AnimatedDialogContent
aria-labelledby="dialog-title"
style={{
transform: styles.y.interpolate(
(value) => `translate3d(0px, ${value}px, 0px)`
),
border: "4px solid hsla(0, 0%, 0%, 0.5)",
borderRadius: 10,
}}
>
<button onClick={() => setShowDialog(false)}>
Close Dialog
</button>
<h2 id="dialog-title">Animated Dialog</h2>
<p>React Spring makes it too easy!</p>
<input type="text" />
<br />
<input type="text" />
<button>Ayyyyyy</button>
</AnimatedDialogContent>
</AnimatedDialogOverlay>
)
)}
</div>
);
}
return <Example />;
})();
```
## Accessibility
Expand Down

0 comments on commit 54764b1

Please sign in to comment.