-
Notifications
You must be signed in to change notification settings - Fork 0
/
SavedMessage.tsx
67 lines (58 loc) · 1.59 KB
/
SavedMessage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { faCheck } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import React, { useState, useEffect, useRef } from 'react'
export interface SavedMessageProps {
showSavedMessageRef: React.MutableRefObject<() => void>
className?: string
}
/**
* Shows a "✔ Saved" message next to your "Save changes" button to show that
* user that the change went through. The message fades in and out.
*
* ```
* const showSavedMessageRef = useRef(() => {})
*
* function saveChanges() {
* // call the API
*
* showSavedMessageRef.current()
* }
*
* return <div>
* ...
* <SavedMessage
* showSavedMessageRef={showSavedMessageRef}
* className="saved-message-me"
* />
* ...
* </div>
* ```
*/
export function SavedMessage({
showSavedMessageRef,
className,
}: SavedMessageProps): React.ReactElement {
const [show, setShow] = useState(false)
const timerRef = useRef<number>()
function onSave(): void {
setShow(true)
clearTimeout(timerRef.current)
timerRef.current = window.window.setTimeout(() => setShow(false), 1800)
}
useEffect(() => {
showSavedMessageRef.current = onSave
})
useEffect(
() => (): void => {
clearTimeout(timerRef.current)
},
[]
)
const classes = ['saved-message']
if (className) classes.push(className)
return (
<div className={classes.join(' ')} style={{ opacity: show ? 1 : 0 }}>
<FontAwesomeIcon icon={faCheck} /> Saved
</div>
)
}