Island guide

Your own content in the Island, such as a hint or the steps of a tour, through @cloudlet/toast/guide — while notifications wait in the stack.

On this page

useIslandGuide from @cloudlet/toast/guide lets your app hold the Toaster's Island with content of its own: a title, a text, a counter and a row of buttons. You give data and get callbacks; the Toaster draws it in its own theme. Onboarding in the Island is built on it.

A hint in the Island

show(content) puts the content in the Island, and the same call replaces it; hide() gives the Island back. Without an argument the hook takes the controller of the nearest CloudletProvider. Show the hint, then send a notification while it is there.

Last show: not shown
hint.tsx
"use client";

import { useState } from "react";
import { useOptionalCloudlet } from "@cloudlet/toast";
import { useIslandGuide } from "@cloudlet/toast/guide";

export function ShortcutHint() {
  const cloudlet = useOptionalCloudlet();
  const guide = useIslandGuide();
  const [result, setResult] = useState("not shown");
  const showHint = () => {
    const outcome = guide.show(
      {
        title: "Press ? for shortcuts",
        body: "Every command has one. The list opens from anywhere.",
        label: "Shortcut hint",
        actions: [
          { id: "later", label: "Later", variant: "quiet" },
          { id: "ok", label: "Got it", variant: "primary" },
        ],
        onAction: () => guide.hide(),
        onDismiss: () => guide.hide(),
      },
      // A person asked for it: focus goes to Got it.
      { focus: true },
    );
    setResult(outcome.status === "rejected" ? `rejected: ${outcome.reason}` : outcome.status);
  };
  return (
    <div className="grid justify-items-center gap-3">
      <div className="flex gap-2 text-sm">
        <button type="button" className="rounded-md border px-3 py-1.5" disabled={!cloudlet} onClick={showHint}>
          Show a hint
        </button>
        <button
          type="button"
          className="rounded-md border px-3 py-1.5"
          disabled={!cloudlet}
          onClick={() => cloudlet?.show({ title: "Waited for the hint", duration: null })}
        >
          Send a notification
        </button>
      </div>
      <output className="text-sm text-muted-foreground">
        {guide.active ? "The hint holds the Island" : `Last show: ${result}`}
      </output>
    </div>
  );
}

The guide holds the Island until hide(), or until its component unmounts or its controller changes. The subpath is imported on its own, so an app without a guide does not load its code.

Content

IslandGuideContent is data, given whole on every show:

  • title (required), body and counter, a short position such as 2/5 in monospace figures at the end of the title row;
  • actions: the buttons in order, each an IslandGuideAction with a unique id, a label, a variant (IslandGuideActionVariant: primary filled, quiet without a background, default) and disabled; an empty array draws no row;
  • pending: the guide waits for something outside it, such as the next step's target; the counter shows it and the guide is aria-busy;
  • label: the guide's name for screen readers (the title when omitted);
  • ref: the guide's name for agents, the ref of its own record on the agent bridge (label when omitted); see Agents;
  • onAction(id) on a press of an enabled button, and onDismiss() on Escape while focus is in the guide. Escape only asks: the guide stays until hide().

What show returns

show answers with an IslandGuideResult:

statusreasonWhat it means
shown—The Island shows the content now.
waitingno-hostThe guide holds the Island, but no Toaster of this controller is mounted; the content appears once one mounts.
rejectedoccupiedAnother useIslandGuide holds the Island: one guide per controller. Nothing changed.
rejectedno-controllerNo argument and no CloudletProvider with a controller.
rejecteddisposedThe controller was disposed.
rejectedinvalid-inputThe content is not an IslandGuideContent; also an invalid-input diagnostic.

Notifications wait

While a guide holds the Island it shows the guide alone. Notifications still arrive into the stack and its badge counts them, but none opens the Island, even with attention: 'open'; screen readers hear them as usual. Their timers run, so one that should outlive the guide takes duration: null or an action. After hide() the waiting notifications open as usual.

Focus and keyboard

The guide is a landmark (role="region") named by label. Tab reaches it in the Island's place, and F6 jumps to it and back to where focus was. A new title, counter or body is read in the Toaster's polite live region.

  • show(content, { focus: true }) (IslandGuideShowOptions) moves focus into the guide, on its primary button or its first enabled one: for a guide a person has just asked for. Without it focus stays where it is. When no Toaster is mounted yet, focus moves once one draws the guide.
  • role: 'back' and role: 'next' on two buttons (IslandGuideActionRole) let ← and → press them while focus is in the guide, mirrored in a right-to-left document.
  • After hide(), focus that was in the guide returns where it was before it entered.
steps.ts
import type { IslandGuideContent } from "@cloudlet/toast/guide";

export const step: IslandGuideContent = {
  title: "Find anything",
  counter: "2/3",
  label: "Product tour",
  actions: [
    { id: "skip", label: "Skip", variant: "quiet" },
    { id: "back", label: "Back", role: "back" },
    { id: "next", label: "Next", variant: "primary", role: "next" },
  ],
};

Drawing towards the Island

frame is where the guide comes to rest, an IslandGuideFrame (x, y, width, height in viewport pixels), and changes only when the content or the layout does. To follow the shape while it grows and morphs, subscribeFrame(listener) calls back on every animation frame of the motion, outside React; it returns the function that stops listening. Both belong to the hook that holds the Island and are null while it holds nothing or nothing is drawn.

hint-marker.tsx
"use client";

import { useEffect, useRef } from "react";
import { useIslandGuide } from "@cloudlet/toast/guide";

export function HintMarker() {
  const { show, hide, subscribeFrame } = useIslandGuide();
  const marker = useRef<HTMLDivElement>(null);
  // A dot that rides under the Island while the hint grows, moves and folds.
  useEffect(
    () =>
      subscribeFrame((frame) => {
        if (!marker.current) return;
        marker.current.hidden = !frame;
        if (frame) marker.current.style.translate = `${frame.x + frame.width / 2}px ${frame.y + frame.height + 8}px`;
      }),
    [subscribeFrame],
  );
  return (
    <>
      <button type="button" onClick={() => show({ title: "Look here", actions: [{ id: "ok", label: "OK", variant: "primary" }], onAction: hide })}>
        Show
      </button>
      <div ref={marker} hidden style={{ position: "fixed", left: 0, top: 0, width: 6, height: 6, borderRadius: 3, background: "currentColor" }} />
    </>
  );
}

Agents

With toasterAgent on the bridge, the Toaster's record says the Island is held, guest: { ref } with the content's ref (its label when there is none), and its open and show are refused with "A tour is in progress in the Island" until hide(). The guide itself has no record: describe its steps through your own adapter and give that record's ref as ref, so an agent knows where to steer it, as the tour's does.

Reference

useIslandGuide(controller?), typed as UseIslandGuide, returns UseIslandGuideResult:

FieldTypeDescription
show(content: IslandGuideContent, options?: IslandGuideShowOptions) => IslandGuideResultPuts the content in the Island or replaces it. Call it from an event handler or an effect, not during render.
hide() => voidGives the Island back.
activebooleanThis hook holds the Island.
frameIslandGuideFrame | nullThe box the guide comes to rest in.
subscribeFrame(listener: (frame: IslandGuideFrame | null) => void) => () => voidThe box on every frame of the motion; a listener that throws is reported as a callback-error diagnostic.

show, hide and subscribeFrame keep their identity across renders while the controller stays the same.