Tabs & Spaces Developers

Build

Building a Custom tab

A Custom tab is a web page you host. The workspace shows it in a sandboxed frame that fills the tab, and tells it who's looking.

The Add a tab dialog with Custom tab chosen: the workspace's app tabs (Notes from Team notes, and Help desk), and Make your own.
Add tab → Custom tab lists every installed app's tabs.

Connect

With the SDK:

ts
import { connect } from "@tabs-and-spaces/sdk";

const tands = await connect();
document.querySelector("h1")!.textContent = `Hello, ${tands.context.user.name}`;

connect() says hello to the workspace and resolves once it answers. It rejects when the page isn't open in a workspace (someone opened its address directly), or when the workspace doesn't answer within ten seconds. Show something useful in that case, such as "Open this page from a Tabs & Spaces workspace".

Without the SDK, the conversation is a few postMessage calls. See The frame protocol.

What you're told

tands.context holds:

FieldWhat it is
user.id, user.nameWho opened the tab
user.roleTheir role in the workspace: owner, admin, member or guest. Use it to keep your app's settings to owners and admins (on your server, from the token). Missing from deployments before 0.4.
workspace.id, workspace.slug, workspace.nameThe workspace
workspace.readOnlyThe workspace is read-only until its bill is paid. See When the workspace is read-only.
space.id, space.nameThe space, and its name as the reader sees it: General and My Space in their language, a direct message by its other people's names
space.kindgeneral, personal (someone's My Space), normal, or dm (a direct message)
tab.idThis tab, in this space. The same app tab added to two spaces has two ids.
tab.appTabIdWhich of your manifest's tabs this is
app.idYour app's id, from your manifest
localeThe reader's language, like en or fr

That's everything it's told up front. To see more of the workspace (its tasks, files, messages, people), your app asks for permissions and reads it, as the person looking can see it; to be told as things change, it listens.

Follow the theme

tands.theme is "light" or "dark", and tands.styles the workspace's own colours (its accent, as CSS variables). The simplest way to follow both, with the design stylesheet:

ts
const tands = await connect({ applyTheme: true });

That sets data-theme on <html> and the workspace's colours as CSS variables, and keeps them up to date. To do it yourself, onTheme() tells you when either changes:

ts
document.documentElement.dataset.theme = tands.theme;
tands.onTheme((theme, styles) => (document.documentElement.dataset.theme = theme));

The frame's color-scheme matches the workspace's. A page that leaves its background transparent sits on the tab's own background.

To look like the rest of the workspace, use its colours, type and parts: see Design language, with a stylesheet to download.

When the workspace is read-only

A workspace whose bill is long overdue becomes read-only until it's paid. People can still open your tab, and your page can still read its data, but writes are refused with WORKSPACE_READ_ONLY. That includes setting values, creating, updating or deleting records, and reporting events from your page. The workspace's own pages grey out whatever would change something, so yours should too.

Events your server reports are still taken. The workspace holds them and starts their triggers once it's paid, oldest first. It holds a week's worth at most, and up to 200 events. See While the workspace is read-only.

tands.readOnly says whether the workspace is read-only now, and onReadOnly() tells you when that changes while your page is open. The workspace checks about once a minute, so a paid bill takes up to a minute to reach your page:

ts
const show = (readOnly: boolean) => {
  form.hidden = readOnly;
  notice.hidden = !readOnly; // "Read-only until this workspace's bill is paid."
};
show(tands.readOnly);
tands.onReadOnly(show);

Your server is told too: workspace.readOnly is in the token's context. Actions the workspace sends you carry it as well, so you can refuse a change there before the workspace would.

Call your server

token() gives you a token to send with your requests. It returns the current one, or fetches a fresh one when less than a minute is left:

ts
const response = await fetch("/api/contacts", {
  headers: { authorization: `Bearer ${await tands.token()}` },
});

Your server checks the token, then uses context from it, not from the request. See Checking who's looking.

The sandbox

Your page runs in an iframe with these permissions:

AllowedWhy
allow-scriptsYour page runs
allow-same-originYour page keeps its own cookies and storage, on your origin
allow-formsForms submit
allow-popups, allow-popups-to-escape-sandboxLinks can open new tabs, which aren't sandboxed
allow-downloadsFiles can be downloaded

Not allowed:

  • navigating the workspace's own window;
  • device features such as the camera, microphone or location;
  • full screen.

The frame's referrer policy is origin. Your page fills the tab's width and height, so design for any size, down to a phone's.

Under the frame, the workspace shows where it comes from ("From crm.example.com") and a Reload button. Reloading loads your page again, and it gets a fresh token.

Pages on your origin only

Every page your manifest lists must be on the same origin as the manifest itself. Inside the frame, your page can link and navigate anywhere on that origin. The workspace only talks to your origin: messages from any other origin, or from any window but your frame, are ignored.

Search every page: actions, the manifest, the SDK, tokens…