From 1154b5dcd4d3ac76d98fe0cc90515f54ae04fc78 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 10 Sep 2019 09:07:29 +0300 Subject: [PATCH] Add RippleButton A button component with a ripple animation on click. --- snippets/RippleButton.md | 120 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 snippets/RippleButton.md diff --git a/snippets/RippleButton.md b/snippets/RippleButton.md new file mode 100644 index 000000000..cd888d89a --- /dev/null +++ b/snippets/RippleButton.md @@ -0,0 +1,120 @@ +--- +title: RippleButton +tags: visual,state,effect,intermediate +--- + +### RippleButton + +Renders a button that animates a ripple effect when clicked. + +- Define some appropriate CSS styles and an animation for the ripple effect. +- Use the `React.useState()` hook to create appropriate variables and setters for the pointer's coordinates and for the animation state of the button. +- Use the `React.useEffect()` hook to change the state every time the `coords` state variable changes, starting the animation. +- Use `setTimeout()` in the previous hook to clear the animation after it's done playing. +- Use the `React.useEffect()` hook a second time to reset `coords` whenever the `isRippling` state variable is `false.` +- Handle the `onClick` event by updating the `coords` state variable and calling the passed callback. +- Finally, render a ` + ); +} +``` + +```jsx +ReactDOM.render( + console.log(e)}>Click me, + document.getElementById('root') +); +```