Skip to content

Commit d6a540d

Browse files
committed
Duplicate README in package
1 parent d77815c commit d6a540d

1 file changed

Lines changed: 263 additions & 0 deletions

File tree

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
<p align="center"><img src="https://raw.githubusercontent.com/devrnt/react-use-intercom/main/assets/logo.png" alt="Logo" height="120px" style="margin-top: 20px;"/></p>
2+
<h1 align="center">react-use-intercom</h1>
3+
<p align="center">A React <a href="https://www.intercom.com" alt="Intercom">Intercom </a> integration powered by hooks.</p>
4+
5+
<p align="center">
6+
<img alt="ci" src="https://github.com/devrnt/react-use-intercom/workflows/CI/badge.svg?branch=main">
7+
<img alt="version" src="https://img.shields.io/npm/v/react-use-intercom.svg" />
8+
<img alt="downloads" src="https://badgen.net/npm/dw/react-use-intercom" />
9+
<img alt="minzipped size" src="https://badgen.net/bundlephobia/minzip/react-use-intercom">
10+
<img alt="known vulnerabilities" src="https://snyk.io/test/github/devrnt/react-use-intercom/badge.svg">
11+
</p>
12+
13+
## Features
14+
* Hooks
15+
* Written in TypeScript
16+
* Documented, self explaining methods
17+
* [Tiny size](https://bundlephobia.com/result?p=react-use-intercom@latest) without any external libraries
18+
* Safeguard for SSR environments (NextJS, Gatsby)
19+
* Compatible to hook into existing Intercom intance (loaded by [Segment](https://segment.com/))
20+
21+
## Installation
22+
23+
```
24+
yarn add react-use-intercom
25+
```
26+
27+
## Quickstart
28+
29+
```js
30+
import * as React from 'react';
31+
32+
import { IntercomProvider, useIntercom } from 'react-use-intercom';
33+
34+
const INTERCOM_APP_ID = 'your-intercom-app-id';
35+
36+
const App = () => (
37+
<IntercomProvider appId={INTERCOM_APP_ID}>
38+
<HomePage />
39+
</IntercomProvider>
40+
);
41+
42+
// Anywhere in your app
43+
const HomePage = () => {
44+
const { boot, shutdown, hide, show, update } = useIntercom();
45+
46+
return <button onClick={boot}>Boot intercom! ☎️</button>;
47+
};
48+
```
49+
50+
## Context
51+
This library is a React abstraction of [IntercomJS](https://developers.intercom.com/installing-intercom/docs/intercom-for-web). `react-use-intercom` tries to keep as close as a one-on-one abstraction of the "vanilla" Intercom functionality.
52+
53+
Note that a lot of issues could be related to the vanilla IntercomJS. Please see https://forum.intercom.com/s/ before reporting an issue here.
54+
55+
## Links
56+
* [API](#api)
57+
* [Playground](#playground)
58+
* [Examples](#examples)
59+
* [TypeScript](#typescript)
60+
* [Troubleshoot](#troubleshoot)
61+
* [Advanced](#advanced)
62+
63+
## API
64+
* [IntercomProvider](#intercomprovider)
65+
* [useIntercom](#useintercom)
66+
* [IntercomProps](#intercomprops)
67+
68+
### IntercomProvider
69+
`IntercomProvider` is used to initialize the `window.Intercom` instance. It makes sure the initialization is only done once. If any listeners are passed, the `IntercomProvider` will make sure these are attached.
70+
71+
Place the `IntercomProvider` as high as possible in your application. This will make sure you can call `useIntercom` anywhere.
72+
73+
#### Props
74+
| name | type | description | required | default |
75+
|---------------------|------------------|-----------------------------------------------------------------------------------------|----------|---------|
76+
| appId | string | app ID of your Intercom instance | true | |
77+
| children | React.ReactNode | React children | true | |
78+
| autoBoot | boolean | indicates if Intercom should be automatically booted. If `true` no need to call `boot`, the `IntercomProvider` will call it for you | false | false |
79+
| onHide | () => void | triggered when the Messenger hides | false | |
80+
| onShow | () => void | triggered when the Messenger shows | false | |
81+
| onUnreadCountChange | (number) => void | triggered when the current number of unread messages changes | false | |
82+
| shouldInitialize | boolean | indicates if the Intercom should be initialized. Can be used in multistaged environment | false | true |
83+
| apiBase | string | If you need to route your Messenger requests through a different endpoint than the default. Generally speaking, this is not needed.<br/> Format: `https://${INTERCOM_APP_ID}.intercom-messenger.com` (See: [https://github.com/devrnt/react-use-intercom/pull/96](https://github.com/devrnt/react-use-intercom/pull/96)) | false | |
84+
| initializeDelay | number | Indicates if the intercom initialization should be delayed, delay is in ms, defaults to 0. See https://github.com/devrnt/react-use-intercom/pull/236 | false | |
85+
| autoBootProps | IntercomProps | Pass properties to `boot` method when `autoBoot` is `true` | false | |
86+
87+
#### Example
88+
```javascript
89+
const App = () => {
90+
const [unreadMessagesCount, setUnreadMessagesCount] = React.useState(0);
91+
92+
const onHide = () => console.log('Intercom did hide the Messenger');
93+
const onShow = () => console.log('Intercom did show the Messenger');
94+
const onUnreadCountChange = (amount: number) => {
95+
console.log('Intercom has a new unread message');
96+
setUnreadMessagesCount(amount);
97+
};
98+
99+
return (
100+
<IntercomProvider
101+
appId={INTERCOM_APP_ID}
102+
onHide={onHide}
103+
onShow={onShow}
104+
onUnreadCountChange={onUnreadCountChange}
105+
autoBoot
106+
>
107+
<p>Hi there, I am a child of the IntercomProvider</p>
108+
</IntercomProvider>
109+
);
110+
};
111+
```
112+
113+
### useIntercom
114+
Used to retrieve all methods bundled with Intercom. These are based on the official [Intercom docs](https://developers.intercom.com/installing-intercom/docs/javascript-api-attributes-objects). Some extra methods were added to improve convenience.
115+
116+
Make sure `IntercomProvider` is wrapped around your component when calling `useIntercom()`.
117+
118+
**Remark** - You can't use `useIntercom()` in the same component where `IntercomProvider` is initialized.
119+
120+
#### API
121+
122+
| name | type | description |
123+
|-----------------|--------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|
124+
| isOpen | boolean | the visibility status of the messenger |
125+
| boot | (props?: IntercomProps) => void | boots the Intercom instance, not needed if `autoBoot` in `IntercomProvider` is `true` |
126+
| shutdown | () => void | shuts down the Intercom instance |
127+
| hardShutdown | () => void | same functionality as `shutdown`, but makes sure the Intercom cookies, `window.Intercom` and `window.intercomSettings` are removed. |
128+
| update | (props?: IntercomProps) => void | updates the Intercom instance with the supplied props. To initiate a 'ping', call `update` without props |
129+
| hide | () => void | hides the Messenger, will call `onHide` if supplied to `IntercomProvider` |
130+
| show | () => void | shows the Messenger, will call `onShow` if supplied to `IntercomProvider` |
131+
| showMessages | () => void | shows the Messenger with the message list |
132+
| showNewMessage | (content?: string) => void | shows the Messenger as if a new conversation was just created. If `content` is passed, it will fill in the message composer |
133+
| getVisitorId | () => string | gets the visitor id |
134+
| startTour | (tourId: number) => void | starts a tour based on the `tourId` |
135+
| trackEvent | (event: string, metaData?: object) => void | submits an `event` with optional `metaData`
136+
| showArticle | (articleId: string) => void | opens the Messenger with the specified article by `articleId`
137+
138+
#### Example
139+
```javascript
140+
import * as React from 'react';
141+
142+
import { IntercomProvider, useIntercom } from 'react-use-intercom';
143+
144+
const INTERCOM_APP_ID = 'your-intercom-app-id';
145+
146+
const App = () => (
147+
<IntercomProvider appId={INTERCOM_APP_ID}>
148+
<HomePage />
149+
</IntercomProvider>
150+
);
151+
152+
const HomePage = () => {
153+
const {
154+
boot,
155+
shutdown,
156+
hardShutdown,
157+
update,
158+
hide,
159+
show,
160+
showMessages,
161+
showNewMessage,
162+
getVisitorId,
163+
startTour,
164+
trackEvent,
165+
showArticle
166+
} = useIntercom();
167+
168+
const bootWithProps = () => boot({ name: 'Russo' });
169+
const updateWithProps = () => update({ name: 'Ossur' });
170+
const handleNewMessages = () => showNewMessage();
171+
const handleNewMessagesWithContent = () => showNewMessage('content');
172+
const handleGetVisitorId = () => console.log(getVisitorId());
173+
const handleStartTour = () => startTour(123);
174+
const handleTrackEvent = () => trackEvent('invited-friend');
175+
const handleTrackEventWithMetaData = () =>
176+
trackEvent('invited-frind', {
177+
name: 'Russo',
178+
});
179+
const handleShowArticle = () => showArticle(123456);
180+
181+
return (
182+
<>
183+
<button onClick={boot}>Boot intercom</button>
184+
<button onClick={bootWithProps}>Boot with props</button>
185+
<button onClick={shutdown}>Shutdown</button>
186+
<button onClick={hardShutdown}>Hard shutdown</button>
187+
<button onClick={update}>Update clean session</button>
188+
<button onClick={updateWithProps}>Update session with props</button>
189+
<button onClick={show}>Show messages</button>
190+
<button onClick={hide}>Hide messages</button>
191+
<button onClick={showMessages}>Show message list</button>
192+
<button onClick={handleNewMessages}>Show new messages</button>
193+
<button onClick={handleNewMessagesWithContent}>
194+
Show new message with pre-filled content
195+
</button>
196+
<button onClick={handleGetVisitorId}>Get visitor id</button>
197+
<button onClick={handleStartTour}>Start tour</button>
198+
<button onClick={handleTrackEvent}>Track event</button>
199+
<button onClick={handleTrackEventWithMetaData}>
200+
Track event with metadata
201+
</button>
202+
<button onClick={handleShowArticle}>Open article in Messenger</button>
203+
</>
204+
);
205+
};
206+
```
207+
### IntercomProps
208+
All the Intercom default attributes/props are camel cased (`appId` instead of `app_id`) in `react-use-intercom`, see [IntercomProps](src/types.ts) to see what attributes you can pass to `boot` or `update`. Or check the Intercom [docs](https://developers.intercom.com/installing-intercom/docs/javascript-api-attributes-objects)
209+
to see all the available attributes/props.
210+
211+
**Remark** - all the listed Intercom attributes [here](https://developers.intercom.com/installing-intercom/docs/javascript-api-attributes-objects) are snake cased, in `react-use-intercom` these are camel cased.
212+
213+
#### Custom attributes
214+
Still want to pass custom attributes to Intercom? Whether `boot` or `update` is used, you can add your custom properties by passing these through `customAttributes` in the `boot` or `update` method.
215+
216+
**Remark** - the keys of the `customAttributes` object should be snake cased (this is how Intercom wants them). They are rawly passed to Intercom.
217+
```javascript
218+
const { boot } = useIntercom();
219+
220+
boot({
221+
name: 'Russo',
222+
customAttributes: { custom_attribute_key: 'hi there' },
223+
})
224+
```
225+
226+
## Playground
227+
Small playground to showcase the functionalities of `react-use-intercom`.
228+
229+
### useIntercom
230+
[https://devrnt.github.io/react-use-intercom/#/useIntercom](https://devrnt.github.io/react-use-intercom/#/useIntercom)
231+
232+
### useIntercom (with Intercom tour)
233+
[https://devrnt.github.io/react-use-intercom/#/useIntercomTour](https://devrnt.github.io/react-use-intercom/#/useIntercomTour)
234+
235+
## Examples
236+
Go to [examples](https://github.com/devrnt/react-use-intercom/tree/main/apps/examples) to check out some integrations (Gatsby, NextJS...).
237+
238+
## TypeScript
239+
All the possible pre-defined options to pass to the Intercom instance are typed. So whenever you have to pass [IntercomProps](src/types.ts), all the possible properties will be available out of the box.
240+
These props are `JavaScript` 'friendly', so [camelCase](https://en.wikipedia.org/wiki/Camel_case). No need to pass the props with [snake_cased](https://en.wikipedia.org/wiki/Snake_case) keys.
241+
242+
**Remark** - if you want to pass custom properties, you should still use [snake_cased](https://en.wikipedia.org/wiki/Snake_case) keys.
243+
244+
245+
## Troubleshoot
246+
* I'm seeing `Please wrap your component with IntercomProvider` in the console.
247+
> Make sure `IntercomProvider` is initialized before calling `useIntercom()`. You only need to initialize `IntercomProvider` once. It is advised to initialize `IntercomProvider` as high as possible in your application tree.
248+
249+
> Make sure you aren't calling `useIntercom()` in the same component where you initialized `IntercomProvider`.
250+
251+
* I'm seeing `Some invalid props were passed to IntercomProvider. Please check following props: [properties]` in the console.
252+
> Make sure you're passing the correct properties to the `IntercomProvider`. Check [IntercomProvider](#intercomprovider) to see all the properties.
253+
> Mind that all the properties in `react-use-intercom` are camel cased, except for the `customAttributes` property in the `boot` and `update` method from `useIntercom`.
254+
255+
## Advanced
256+
257+
### Delay initialization
258+
259+
`<IntercomProvider />` uses an official intercom snippet and is directly initialized on load. In the background this snippet will load some external code that makes Intercom work. All of this magic happens on the initial load and in some use cases this can become problematic (E.g. when LCP is priority).
260+
261+
Since [v1.2.0](https://github.com/devrnt/react-use-intercom/releases/tag/v1.2.0) it's possible to delay this initialisation by passing `initializeDelay` in `<IntercomProvider />` (it's in milliseconds). However most of the users won't need to mess with this.
262+
263+
For reference see https://github.com/devrnt/react-use-intercom/pull/236 and https://forum.intercom.com/s/question/0D52G00004WxWLs/can-i-delay-loading-intercom-on-my-site-to-reduce-the-js-load

0 commit comments

Comments
 (0)