Skip to content

Commit 9008771

Browse files
authored
Merge pull request #520 from NicolasGn/master
feat: add isOpen flag to useIntercom() (re-open)
2 parents cb3ca5b + b27baac commit 9008771

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
115115

116116
Make sure `IntercomProvider` is wrapped around your component when calling `useIntercom()`.
117117

118-
**Remark** - You can't use `useIntercom()` in the same component where `IntercomProvider` is initialized.
118+
**Remark** - You can't use `useIntercom()` in the same component where `IntercomProvider` is initialized.
119+
120+
#### State
121+
| name | type | description |
122+
|---------------------|------------------|-----------------------------------------------------------------------------------------|
123+
| isOpen | boolean | the visibility status of the messenger |
119124

120125
#### Methods
121126
| name | type | description |

src/provider.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,24 @@ export const IntercomProvider: React.FC<React.PropsWithChildren<
7575
[apiBase, appId, shouldInitialize],
7676
);
7777

78+
const [isOpen, setIsOpen] = React.useState(false);
79+
80+
const onHideWrapper = React.useCallback(() => {
81+
setIsOpen(false);
82+
if (onHide) onHide();
83+
}, [onHide, setIsOpen]);
84+
85+
const onShowWrapper = React.useCallback(() => {
86+
setIsOpen(true);
87+
if (onShow) onShow();
88+
}, [onShow, setIsOpen]);
89+
7890
if (!isSSR && shouldInitialize && !isInitialized.current) {
7991
initialize(appId, initializeDelay);
8092

8193
// attach listeners
82-
if (onHide) IntercomAPI('onHide', onHide);
83-
if (onShow) IntercomAPI('onShow', onShow);
94+
IntercomAPI('onHide', onHideWrapper);
95+
IntercomAPI('onShow', onShowWrapper);
8496
if (onUnreadCountChange)
8597
IntercomAPI('onUnreadCountChange', onUnreadCountChange);
8698

@@ -231,6 +243,7 @@ export const IntercomProvider: React.FC<React.PropsWithChildren<
231243
update,
232244
hide,
233245
show,
246+
isOpen,
234247
showMessages,
235248
showNewMessages,
236249
getVisitorId,
@@ -245,6 +258,7 @@ export const IntercomProvider: React.FC<React.PropsWithChildren<
245258
update,
246259
hide,
247260
show,
261+
isOpen,
248262
showMessages,
249263
showNewMessages,
250264
getVisitorId,

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ export type IntercomContextValues = {
314314
* @see {@link https://developers.intercom.com/installing-intercom/docs/intercom-javascript#section-intercomshow}
315315
*/
316316
show: () => void;
317+
/**
318+
* The visibility status of the messenger.
319+
*/
320+
isOpen: boolean;
317321
/**
318322
* Opens the Messenger with the message list.
319323
*/

0 commit comments

Comments
 (0)