4 min read

SPA mounting

Mount and unmount the shelf yourself with window.TeamShelf — for React, Next.js, Vue and other single-page apps.

The script tag mounts the shelf automatically. In a single-page app you may want control instead — mount it only after sign-in, or only on certain routes. The embed exposes a tiny global for that.

The global API

Loading embed.js defines window.TeamShelf:

TypeScript
window.TeamShelf.mount({
  team: "ts_yxT2lwZi7337",          // required — your team key
  baseUrl: "https://app.teamshelf.ai", // optional, defaults to where embed.js was loaded from
  position: "bottom-right",          // optional: a corner, or "left" | "right"; default: the team's setting
  expanded: false,                   // start expanded, default false
  userToken: token,                  // optional: a signed-in user's token from your backend
});

window.TeamShelf.identify(token);    // switch to a signed-in user, or renew their token
window.TeamShelf.logout();           // switch to a new anonymous visitor
window.TeamShelf.unmount();          // remove the shelf
window.TeamShelf.version;            // e.g. "0.1.0"
  • mount() replaces any shelf that’s already mounted, so calling it twice is safe.
  • unmount() removes the shelf and its root element. Calling it when nothing is mounted does nothing.
  • userToken, identify() and logout() are for apps with their own sign-in. See Signed-in users below.

Loading the script

Load embed.js without data-team so it doesn’t auto-mount, then call mount() when you’re ready:

HTML
<script src="https://app.teamshelf.ai/embed.js" async></script>

Because the script is async, window.TeamShelf may not exist yet when your code runs. A small helper waits for it:

JavaScript
function loadTeamShelf() {
  if (window.TeamShelf) return Promise.resolve(window.TeamShelf);
  return new Promise((resolve, reject) => {
    const s = document.createElement("script");
    s.src = "https://app.teamshelf.ai/embed.js";
    s.async = true;
    s.onload = () => resolve(window.TeamShelf);
    s.onerror = reject;
    document.head.appendChild(s);
  });
}

React

Mount once at the top of the part of your app that should show the shelf, and unmount on cleanup:

TSX
import { useEffect } from "react";

export function TeamShelf({ team }: { team: string }) {
  useEffect(() => {
    let cancelled = false;
    loadTeamShelf().then((ts) => {
      if (!cancelled) ts.mount({ team });
    });
    return () => {
      cancelled = true;
      window.TeamShelf?.unmount();
    };
  }, [team]);
  return null;
}

In React’s development Strict Mode, effects run twice. That’s fine — the second mount() replaces the first.

Next.js

Put the component in a client component file and render it from your root layout:

TSX
// app/teamshelf.tsx
"use client";
import { useEffect } from "react";

export function TeamShelf({ team }: { team: string }) {
  useEffect(() => {
    loadTeamShelf().then((ts) => ts.mount({ team }));
    return () => window.TeamShelf?.unmount();
  }, [team]);
  return null;
}
TSX
// app/layout.tsx
import { TeamShelf } from "./teamshelf";

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

Because the layout persists across navigations, the shelf stays mounted and keeps its live connection as users move between pages.

Vue

JavaScript
import { onMounted, onBeforeUnmount } from "vue";

export default {
  setup() {
    onMounted(async () => {
      const ts = await loadTeamShelf();
      ts.mount({ team: "ts_yxT2lwZi7337" });
    });
    onBeforeUnmount(() => window.TeamShelf?.unmount());
  },
};

Route changes

The shelf is independent of your router — it lives outside your component tree, in its own shadow root. Mount it once in a component that persists across routes and leave it alone. Unmounting and remounting on every navigation works, but it reconnects the live event stream each time and resets what’s open in the message view.

To hide the shelf on certain pages, call unmount() when entering them and mount() when leaving.

Signed-in users

Without a token, each browser is its own anonymous visitor. If your app has its own sign-in, pass a token signed by your backend so the user’s conversations follow them across browsers and devices. Identify signed-in users covers creating the secret and signing the token.

Pass the token when you mount, if the user is already signed in:

JavaScript
ts.mount({ team: "ts_yxT2lwZi7337", userToken });

Then keep the widget in step with your app’s sign-in state:

JavaScript
// After the user signs in
window.TeamShelf.identify(token);

// When the user signs out
window.TeamShelf.logout();
  • identify(token) remounts the shelf as that user. Calling it again with a fresh token for the same user just renews the token.
  • logout() remounts the shelf as a new anonymous visitor, not the one from before sign-in. The next person on a shared computer doesn’t see the previous visitor’s chats.

Anonymous conversations from before sign-in aren’t merged into the user’s conversations.

When the token expires

When the token is rejected or expires, the widget dispatches a teamshelf:token-expired event on window, and shows an error until it gets a new token. Fetch a fresh one from your backend and call identify():

JavaScript
window.addEventListener("teamshelf:token-expired", async () => {
  const res = await fetch("/api/teamshelf-token", { credentials: "same-origin" });
  if (res.ok) window.TeamShelf.identify((await res.json()).token);
  else window.TeamShelf.logout(); // signed out of your app
});

In React, add the listener in the same effect that mounts the shelf, and remove it on cleanup.