Skip to the page
Docs articles

npm package

Load the widget from your own code with @notert/widget, with every call typed for TypeScript and a hook for React.

Last updated

View as Markdown
On this page

@notert/widget loads the same widget the snippet does, from your own code rather than your page's HTML. Every call is typed, so your editor completes each screen, event and option, and a React hook keeps your own Help button in step with the widget.

npm install @notert/widget

Load it

Call loadNotert once in the browser with your workspace's id, which the install guide writes into your snippet as the first value of init:

import { loadNotert } from "@notert/widget";

loadNotert({ workspaceId: "YOUR_WORKSPACE_ID", theme: "page" });

It takes every option init does, such as launcher: false for your own button or language, and loads the widget's script once however often it's called. On a server it does nothing, so a component that renders on both sides can call it.

Use it in place of the snippet, not beside it. A page that has both still draws one widget, and only the first init counts.

Call it

import { notert } from "@notert/widget";

notert.identify(await fetchNotertToken());
notert.open({ screen: "new", text: "I found a bug: " });
notert.on("conversation:started", ({ conversationId }) => {
  analytics.track("Support conversation started", { conversationId });
});

notert has every call in the JavaScript reference: init, identify, open, close, toggle, on, off, appearance, suggest, context, track, hideLauncher, showLauncher and shutdown. Each works before the widget has loaded, which plays them back in order once it has, and calls the same widget whether loadNotert or a pasted snippet put it on the page.

The types it exports, such as NotertPlace, NotertEvents and NotertInitOptions, name what each call takes, so a misspelled screen or event fails your build rather than warning in the browser's console.

In React

import { NotertProvider, useNotert } from "@notert/widget/react";

export function App() {
  return (
    <NotertProvider workspaceId="YOUR_WORKSPACE_ID" launcher={false} theme="page">
      <HelpButton />
    </NotertProvider>
  );
}

function HelpButton() {
  const { toggle, isOpen, unseen, online } = useNotert();
  return (
    <button type="button" aria-expanded={isOpen} onClick={() => toggle()}>
      Help
      {unseen > 0 ? (
        <span className="badge">{unseen}</span>
      ) : (
        online && (
          <span className="dot">
            <span className="sr-only">Online</span>
          </span>
        )
      )}
    </button>
  );
}
  • NotertProvider loads the widget once as it mounts, with the props it mounts with. Later props change nothing, and unmounting it leaves the widget on the page: call notert.shutdown() to take it off.
  • useNotert() gives open, close and toggle, and keeps isOpen, unseen and online current through the widget's change event. Before the widget has loaded, it reads closed, with nothing unseen and no one online.

useNotert works anywhere on the page, under the provider or not, as long as something loads the widget. React 18 or later is needed only for this part.

toggle and open take a screen too, as open does, so a menu of your own can open each part: onClick={() => open({ screen: "new", text: "I found a bug: " })}.

In Next.js

NotertProvider and useNotert use React state, so in the App Router they run in a client component. Put the provider in a file of its own marked "use client", and wrap your root layout's children in it:

// app/notert-provider.tsx
"use client";

import { NotertProvider } from "@notert/widget/react";

export function Notert({ children }: { children: React.ReactNode }) {
  return (
    <NotertProvider workspaceId="YOUR_WORKSPACE_ID" theme="page">
      {children}
    </NotertProvider>
  );
}
// app/layout.tsx
import { Notert } from "./notert-provider";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <Notert>{children}</Notert>
      </body>
    </html>
  );
}

A component that calls useNotert(), such as your Help button, is marked "use client" too. The provider adds the widget's script as it mounts in the browser, so a page rendered on the server carries nothing of it.

Tell it who signed in

import { notert } from "@notert/widget";
import { useEffect } from "react";

export function NotertIdentity({ user }: { user: { notertToken: string } | null }) {
  useEffect(() => {
    // A token your server signed for this user, or null once they've signed out.
    notert.identify(user ? user.notertToken : null);
  }, [user]);
  return null;
}

See single-page apps for when to call it, and identify your signed-in users for the token.

Start here

Up and down arrowsto moveEnterto openEscto close