Skip to content

Controlling the Widget

Mcdavid Obioha edited this page Dec 2, 2026 · 2 revisions

Controlling the Widget

Open it from your own button

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} />
    </>
  );
}
  • isOpen makes 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 isOpen out for uncontrolled use, and optionally set defaultOpen to start open.
  • onOpen and onClose still 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.

Delay or schedule the launcher

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.

Hide it

<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.

Events

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.

Custom message format

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.

Clone this wiki locally