Skip to content

Commit 5a09f17

Browse files
committed
feat(add: allowesc prop): add new allowEsc props
add prop that closes the chat box when escape key is pressed BREAKING CHANGE: new prop
1 parent 543ff9f commit 5a09f17

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/Components/FloatingWhatsapp.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface FloatingWhatsAppProps {
1010
chatMessage?: string
1111
darkMode?: boolean
1212
allowClickAway?: boolean
13-
13+
allowEsc?: boolean
1414
styles?: React.CSSProperties
1515
className?: string
1616
placeholder?: string
@@ -28,6 +28,7 @@ function FloatingWhatsApp({
2828
chatMessage = 'Hello there! 🤝 \nHow can we help?',
2929
darkMode = false,
3030
allowClickAway = false,
31+
allowEsc = false,
3132
styles = {},
3233
className = '',
3334
placeholder = 'Type a message..'
@@ -52,12 +53,29 @@ function FloatingWhatsApp({
5253
if (isOpen) setOpen(false)
5354
}, [allowClickAway, isOpen])
5455

56+
const onEscKey = useCallback(
57+
(event: KeyboardEvent) => {
58+
if (!allowEsc) return
59+
60+
if (event.key === 'Escape') {
61+
if (isOpen) setOpen(false)
62+
}
63+
},
64+
[allowEsc, isOpen]
65+
)
66+
5567
useEffect(() => {
5668
document.addEventListener('click', onClickOutside, false)
5769

5870
return () => document.removeEventListener('click', onClickOutside)
5971
}, [onClickOutside])
6072

73+
useEffect(() => {
74+
document.addEventListener('keydown', onEscKey, false)
75+
76+
return () => document.removeEventListener('keydown', onEscKey)
77+
}, [onEscKey])
78+
6179
return (
6280
<div className={`${css.floatingWhatsapp} ${darkMode ? css.dark : ''} ${className}`}>
6381
<div className={css.whatsappButton} onClick={handleClick} style={styles} aria-hidden='true'>

0 commit comments

Comments
 (0)