From 15ef366c075e1e7c67bcbb7cbbd2f1ab36de7547 Mon Sep 17 00:00:00 2001 From: Theodorus Yoga Date: Tue, 17 Sep 2019 17:19:30 +0700 Subject: [PATCH 1/3] Added alert component --- snippets/Alert.md | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 snippets/Alert.md diff --git a/snippets/Alert.md b/snippets/Alert.md new file mode 100644 index 000000000..e89f5946a --- /dev/null +++ b/snippets/Alert.md @@ -0,0 +1,107 @@ +--- +title: Alert +tags: visual,beginner,state,effect +--- + +Creates an alert component with `type` prop. + +- Define appropriate CSS styles and animations for the component's elements. +- Use the `React.setState()` hook to create the `isShown` and `isLeaving` state variables and set their values to `false`. +- Define `timeoutId` to keep the timer instance for clearing on component unmount. +- Use the `React.setEffect()` hook to update the value of `isShown` to `true` and clear interval by using `timeoutId` when component is unmounted. +- Define `closeNotification` function to set the component is removed from DOM by displaying fading out animation and set `isShown` to `false` via `setTimeout()`. +- Define the component, which renders alert component with user defined `message` and a close button to remove the component from DOM. + +```css +@keyframes leave { + 0% { opacity: 1 } + 100% { opacity: 0 } +} + +.alert { + padding: 0.75rem 0.5rem; + margin-bottom: 0.5rem; + text-align: left; + padding-right: 40px; + border-radius: 4px; + font-size: 16px; + position: relative; +} + +.alert.success { + color: #155724; + background-color: #d4edda; + border-color: #c3e6cb; +} + +.alert.info{ + color: #004085; + background-color: #cce5ff; + border-color: #b8daff; +} + +.alert.warning{ + color: #856404; + background-color: #fff3cd; + border-color: #ffeeba; +} + +.alert.danger{ + color: #721c24; + background-color: #f8d7da; + border-color: #f5c6cb; +} + +.alert.leaving{ + animation: leave 0.5s forwards; +} + +.alert .close { + position: absolute; + top: 0; + right: 0; + padding: 0 0.75rem; + color: #333; + border: 0; + height: 100%; + cursor: pointer; + background: none; + font-weight: 600; + font-size: 16px; +} +``` + +```jsx +function Notification(props) { + const [isShown, setIsShown] = React.useState(false); + const [isLeaving, setIsLeaving] = React.useState(false); + + let timeoutId = null; + + React.useEffect(() => { + setIsShown(true); + return () => { + clearTimeout(timeoutId); + } + }, [props.isShown, props.timeout, timeoutId]); + + const closeNotification = () => { + setIsLeaving(true); + timeoutId = setTimeout(() => { + setIsLeaving(false); + setIsShown(false); + }, 250) + } + + return isShown && ( +
+ + {props.message} +
+ ) +} +``` + +```jsx +ReactDOM.render(, document.getElementById('root')); +``` From 284c5b936eac8216cdcc816957b38bb4acd21abd Mon Sep 17 00:00:00 2001 From: Theodorus Yoga Date: Thu, 19 Sep 2019 19:15:56 +0700 Subject: [PATCH 2/3] Revision after review 1 --- snippets/Alert.md | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/snippets/Alert.md b/snippets/Alert.md index e89f5946a..5238d3ef3 100644 --- a/snippets/Alert.md +++ b/snippets/Alert.md @@ -10,7 +10,7 @@ Creates an alert component with `type` prop. - Define `timeoutId` to keep the timer instance for clearing on component unmount. - Use the `React.setEffect()` hook to update the value of `isShown` to `true` and clear interval by using `timeoutId` when component is unmounted. - Define `closeNotification` function to set the component is removed from DOM by displaying fading out animation and set `isShown` to `false` via `setTimeout()`. -- Define the component, which renders alert component with user defined `message` and a close button to remove the component from DOM. +- Define the component, which renders the alert component with user defined `message` and a close button to remove the component from DOM. ```css @keyframes leave { @@ -28,25 +28,13 @@ Creates an alert component with `type` prop. position: relative; } -.alert.success { - color: #155724; - background-color: #d4edda; - border-color: #c3e6cb; -} - -.alert.info{ - color: #004085; - background-color: #cce5ff; - border-color: #b8daff; -} - .alert.warning{ color: #856404; background-color: #fff3cd; border-color: #ffeeba; } -.alert.danger{ +.alert.error{ color: #721c24; background-color: #f8d7da; border-color: #f5c6cb; @@ -69,6 +57,10 @@ Creates an alert component with `type` prop. font-weight: 600; font-size: 16px; } + +.alert .close:after{ + content: 'x'; +} ``` ```jsx @@ -95,7 +87,7 @@ function Notification(props) { return isShown && (
- +
) From 94f82f6666676e5384c68799995615c506c9fc7d Mon Sep 17 00:00:00 2001 From: Theodorus Yoga Mahendraputra Date: Fri, 20 Sep 2019 10:56:11 +0700 Subject: [PATCH 3/3] Update snippets/Alert.md Co-Authored-By: Angelos Chalaris --- snippets/Alert.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/Alert.md b/snippets/Alert.md index 5238d3ef3..bb4631d51 100644 --- a/snippets/Alert.md +++ b/snippets/Alert.md @@ -86,7 +86,7 @@ function Notification(props) { } return isShown && ( -
+