-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingWhatsApp.tsx
299 lines (250 loc) · 9.35 KB
/
FloatingWhatsApp.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import React, { useReducer, useEffect, useCallback, useRef, useMemo } from 'react'
import { reducer } from '../reducer'
import { WhatsappSVG, CloseSVG, CheckSVG, SendSVG } from './Icons'
import css from '../styles.module.css'
import darkBG from './assets/bg-chat-tile-light.png'
import lightBG from './assets/bg-chat-tile-dark.png'
import dummyAvatar from './assets/uifaces-avatar.jpg'
import SoundBeep from './assets/whatsapp-notification.mp3'
export interface FloatingWhatsAppProps {
/** Callback function fires on click */
onClick?: (event: React.MouseEvent<HTMLDivElement>) => void
/** Callback function fires on submit with event and form input value passed */
onSubmit?: (event: React.FormEvent<HTMLFormElement>, formValue: string) => void
/** Callback function fires on close */
onClose?: () => void
/** Callback function fired when notification runs */
onNotification?: () => void
/** Callback function called when notification loop done */
onLoopDone?: () => void
/** Phone number in [intenational format](https://faq.whatsapp.com/general/contacts/how-to-add-an-international-phone-number) */
phoneNumber: string
/** Account Name */
accountName: string
/** Set chat box height */
chatboxHeight?: number
/** Inline style applied to chat box */
chatboxStyle?: React.CSSProperties
/** CSS className applied to chat box */
chatboxClassName?: string
/** Change user avatar using [static assets](https://create-react-app.dev/docs/adding-images-fonts-and-files/) */
avatar?: string
/** Text below the account username */
statusMessage?: string
/** Text inside the chat box */
chatMessage?: string
/** Input placeholder */
placeholder?: string
/** Time delay after which the chatMessage is displayed (in seconds) */
messageDelay?: number
/** Allow notifications (Disabled after user opens the chat box) */
notification?: boolean
/** Time delay between notifications in seconds */
notificationDelay?: number
/** Repeat notifications loop */
notificationLoop?: number
/** Enable notification sound */
notificationSound?: boolean
/** Notification sound custom src */
notificationSoundSrc?: string
/** Inline style applied to notification */
notificationStyle?: React.CSSProperties
/** CSS className applied to notification */
notificationClassName?: string
/** Allow default submit action */
allowDefaultSubmit?: boolean
/** open the chat box if clicked on wp icon */
openChatOnClick?: boolean
/** Closes the chat box if click outside the chat box */
allowClickAway?: boolean
/** Closes the chat box if `Escape` key is clicked */
allowEsc?: boolean
/** Enable / Disable dark mode */
darkMode?: boolean
/** Inline style applied to the main wrapping `Div` */
style?: React.CSSProperties
/** CSS className applied to the main wrapping `Div` */
className?: string
/** Inline style applied to button */
buttonStyle?: React.CSSProperties
/** CSS className applied to button */
buttonClassName?: string
}
export function FloatingWhatsApp({
onClick,
onSubmit,
onClose,
onNotification,
onLoopDone,
phoneNumber = '1234567890',
accountName = 'Account Name',
avatar = dummyAvatar,
statusMessage = 'Typically replies within 1 hour',
chatMessage = 'Hello there! 🤝 \nHow can we help?',
placeholder = 'Type a message..',
messageDelay = 2,
allowDefaultSubmit = true,
openChatOnClick = true,
allowClickAway = false,
allowEsc = false,
notification = true,
notificationDelay = 60,
notificationLoop = 0,
notificationSound = false,
notificationSoundSrc = SoundBeep,
notificationStyle,
notificationClassName = 'floating-whatsapp-notification',
buttonStyle,
buttonClassName = 'floating-whatsapp-button',
chatboxHeight = 320,
chatboxStyle,
chatboxClassName = 'floating-whatsapp-chatbox',
darkMode = false,
style,
className = 'floating-whatsapp'
}: FloatingWhatsAppProps) {
const [{ isOpen, isDelay, isNotification }, dispatch] = useReducer(reducer, {
isOpen: false,
isDelay: true,
isNotification: false
})
const timeNow = useMemo(() => new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }), [])
const inputRef = useRef<HTMLInputElement | null>(null)
const soundRef = useRef<HTMLAudioElement | null>(null)
const loops = useRef(0)
const notificationInterval = useRef(0)
const handleNotification = useCallback(() => {
if (!notification) return
dispatch({ type: 'notification' })
if (onNotification) onNotification()
if (notificationLoop > 0) {
loops.current += 1
if (notificationSound) {
if (soundRef.current) {
soundRef.current.currentTime = 0
soundRef.current.play()
}
}
if (loops.current === notificationLoop) {
clearInterval(notificationInterval.current)
if (onLoopDone) onLoopDone()
}
}
}, [notification, notificationLoop, notificationSound, onNotification, onLoopDone])
useEffect(() => {
const delayInSecond = notificationDelay * 1000
if (delayInSecond < 10) return console.error('notificationDelay prop value must be at least 10 seconds.')
notificationInterval.current = window.setInterval(handleNotification, delayInSecond)
return () => clearInterval(notificationInterval.current)
}, [handleNotification, notificationDelay])
const handleOpen = useCallback(
(event: React.MouseEvent<HTMLDivElement>) => {
event.stopPropagation()
if (isOpen) return
if(openChatOnClick){
clearInterval(notificationInterval.current)
dispatch({ type: 'open' })
setTimeout(() => dispatch({ type: 'delay' }), messageDelay * 1000)
}
if (onClick) onClick(event)
},
[isOpen, onClick, messageDelay]
)
const handleClose = useCallback(() => {
dispatch({ type: 'close' })
if (onClose) onClose()
}, [onClose])
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
if (!inputRef.current?.value) return
if (allowDefaultSubmit) {
window.open(`https://api.whatsapp.com/send/?phone=${phoneNumber}&text=${inputRef.current.value}`)
}
if (onSubmit) onSubmit(event, inputRef.current.value)
inputRef.current.value = ''
}
useEffect(() => {
const onClickOutside = () => {
if (!allowClickAway || !isOpen) return
handleClose()
}
document.addEventListener('click', onClickOutside, false)
return () => document.removeEventListener('click', onClickOutside)
}, [allowClickAway, isOpen, handleClose])
useEffect(() => {
const onEscKey = (event: KeyboardEvent) => {
if (!allowEsc || !isOpen) return
if (event.key === 'Escape') handleClose()
}
document.addEventListener('keydown', onEscKey, false)
return () => document.removeEventListener('keydown', onEscKey)
}, [allowEsc, isOpen, handleClose])
return (
<div className={`${css.floatingWhatsapp} ${darkMode ? `${css.dark} ` : ''} ${className}`} style={style}>
<div
className={`${css.whatsappButton} ${buttonClassName}`}
onClick={handleOpen}
style={buttonStyle}
aria-hidden='true'
>
<WhatsappSVG />
{isNotification && (
<span className={`${css.notificationIndicator} ${notificationClassName}`} style={notificationStyle}>
1
</span>
)}
</div>
<div
className={`${css.whatsappChatBox} ${isOpen ? css.open : css.close} ${chatboxClassName}`}
onClick={(event) => event.stopPropagation()}
aria-hidden='true'
style={{ height: isOpen ? chatboxHeight : 0, ...chatboxStyle }}
>
<header className={css.chatHeader}>
<div className={css.avatar}>
<img src={avatar} width='60' height='60' alt='whatsapp-avatar' />
</div>
<div className={css.status}>
<span className={css.statusTitle}>{accountName}</span>
<span className={css.statusSubtitle}>{statusMessage}</span>
</div>
<div className={css.close} onClick={handleClose} aria-hidden='true'>
<CloseSVG />
</div>
</header>
<div className={css.chatBody} style={{ backgroundImage: `url(${darkMode ? darkBG : lightBG})` }}>
{isDelay ? (
<div className={css.chatBubble}>
<div className={css.typing}>
<div className={css.dot} />
<div className={css.dot} />
<div className={css.dot} />
</div>
</div>
) : (
<div className={css.message}>
<span className={css.triangle} />
<span className={css.accountName}>{accountName}</span>
<p className={css.messageBody}>{chatMessage}</p>
<span className={css.messageTime}>
{timeNow}
<span style={{ marginLeft: 5 }}>
<CheckSVG />
</span>
</span>
</div>
)}
</div>
<footer className={css.chatFooter}>
<form onSubmit={handleSubmit}>
<input className={css.input} placeholder={placeholder} ref={inputRef} dir='auto' />
<button type='submit' className={css.buttonSend}>
<SendSVG />
</button>
</form>
</footer>
</div>
{notificationSound && <audio ref={soundRef} hidden src={notificationSoundSrc} />}
</div>
)
}