Repository navigation
Controlling the Widget
Pass isOpen and onOpenChange to control the widget from outside:
import { useState } from 'react';
function Page() {
const [open, setOpen] = useState(false);
return (
<>
<button onClick={() => setOpen(true)}>Talk to sales</button>
<WhatsAppChat phoneNumber="+12025550123" isOpen={open} onOpenChange={setOpen} />
</>
);
}-
isOpenmakes the widget controlled: it shows exactly what you pass.onOpenChange(next)fires when the visitor clicks the launcher, the close button, or presses Escape; you decide whether to update your state. - Leave
isOpenout for uncontrolled use, and optionally setdefaultOpento start open. -
onOpenandonClosestill fire in both modes.
Need to open it from a component far from the widget, or open it with the form already filled in? Use useWhatsAppWidget() instead of passing state down.
showAfter keeps the launcher hidden until a condition is met:
<WhatsAppChat phoneNumber="+12025550123" showAfter={5000} /> {/* after 5 seconds */}
<WhatsAppChat phoneNumber="+12025550123" showAfter={{ scrollPercent: 40 }} /> {/* after 40% scroll */}
<WhatsAppChat phoneNumber="+12025550123" showAfter={{ ms: 3000, scrollPercent: 25 }} /> {/* both */}With both ms and scrollPercent, both conditions must be met. Once shown it stays shown. Pair it with showNotificationDot and a greeting to draw attention without a popup.
<WhatsAppChat phoneNumber="+12025550123" hidden={user.isAdmin} /> {/* your own condition */}
<WhatsAppChat phoneNumber="+12025550123" hideOnPaths={['/checkout/*', '/login']} /> {/* by path */}hideOnPaths matches window.location.pathname. * matches any characters, so /checkout/* hides on /checkout/pay and every deeper path. It keeps up with client-side navigation (Next.js, React Router and similar), because the widget listens for popstate and for history.pushState/replaceState calls. To do that it wraps those two History methods once per page. It always calls the original and does not change their behavior.
While hideOnPaths is set, the widget renders nothing until it has read the current path on the client.
| Prop | When it runs | Receives |
|---|---|---|
onOpen |
The form opens | nothing |
onClose |
The form closes | nothing |
onOpenChange |
The visitor opens or closes it | open: boolean |
onSend |
Just before WhatsApp opens (or when a live chat starts) |
details (name, subject, message, page?) and, when agents are used, the selected agent |
onBeforeHandoff |
After onSend, before WhatsApp opens |
the lead payload; async callbacks are awaited for up to 1.5 s |
onDeflection |
The visitor answers an AI instant reply | { reply, resolved, submission } |
Examples:
<WhatsAppChat
phoneNumber="+12025550123"
onOpen={() => analytics.track('whatsapp_widget_opened')}
onSend={(details, agent) => analytics.track('whatsapp_lead', { subject: details.subject, agent: agent?.id })}
/>onSend is the place to record a lead in your own system, because the message itself travels through WhatsApp and never through your server.
messageTemplate receives the form details and returns the exact text to send:
<WhatsAppChat
phoneNumber="+12025550123"
messageTemplate={({ name, subject, message }) => `[Website] ${subject}\n${message}\n— ${name}`}
/>Hidden fields arrive as empty strings. Pass defaultMessage to prefill the message box, for example from the product the visitor is viewing.