# CoachMark

> One stop of a tour — a 300px popover with an arrow, anchored to the thing it explains, which holds the Located wash while the stop is open.

Source: https://raft-design.botiverse.build/components/coach-mark. Generated from the current release; where it differs from what you remember, this page is right.

A coach mark is one stop of a tour of a screen the person has just entered: a bubble that points at one thing, says what it is in a sentence, and moves on with Next. When to run a tour, how many stops, what they say and where focus goes after it are in [Coach marks](https://raft-design.botiverse.build/patterns/coach-marks.md); this page is the part that draws a stop. Experimental, from `@botiverse/raft-ui/wip`.

```tsx
import { useCallback, useRef, useState } from "react";
import { Bell, Hash } from "lucide-react";
import {
  Button,
  ListRow,
  ListRowBody,
  ListRowIcon,
  ListRowTitle,
  PopoverTitle,
  Textarea,
  Text,
} from "@botiverse/raft-ui";
import {
  CoachMark,
  CoachMarkActions,
  CoachMarkContent,
  CoachMarkCount,
  CoachMarkNext,
  PopoverBody,
} from "@botiverse/raft-ui/wip";

const STOPS = [
  {
    title: "Your channel",
    description: "Where you and your agents work on one job.",
    side: "right",
  },
  {
    title: "Notifications",
    description: "What needs you, an answer or a review, shows up here.",
    side: "bottom",
  },
] as const;

export function CoachMarkTourComposition() {
  const [stop, setStop] = useState<number | null>(null);
  const [channelOpen, setChannelOpen] = useState(false);
  const [checked, setChecked] = useState(false);
  const channelRef = useRef<HTMLButtonElement>(null);
  const bellRef = useRef<HTMLButtonElement>(null);
  const composerRef = useRef<HTMLTextAreaElement>(null);
  const anchors = [channelRef, bellRef];

  const end = useCallback(() => {
    setStop(null);
    composerRef.current?.focus();
  }, []);
  const start = useCallback(() => setStop(0), []);
  const openChannel = useCallback(() => setChannelOpen(true), []);
  const check = useCallback(() => setChecked(true), []);
  const next = useCallback(() => setStop((s) => (s === null ? null : s + 1)), []);
  const current = stop === null ? null : STOPS[stop];
  const last = stop === STOPS.length - 1;

  return (
    <div className="flex size-full min-h-96 flex-col gap-4 p-6">
      <Button variant="outline" className="self-start" onClick={start} disabled={stop !== null}>
        Start the tour
      </Button>
      <div className="flex items-start justify-between gap-4">
        <div className="w-56">
          <ListRow
            cell
            ref={channelRef}
            selected={channelOpen}
            aria-current={channelOpen}
            onClick={openChannel}
          >
            <ListRowIcon>
              <Hash />
            </ListRowIcon>
            <ListRowBody>
              <ListRowTitle>fix-login</ListRowTitle>
            </ListRowBody>
          </ListRow>
        </div>
        <div className="flex items-center gap-2">
          {checked ? (
            <span className="text-footnote text-foreground-muted">Nothing needs you</span>
          ) : null}
          <Button
            ref={bellRef}
            variant="ghost"
            size="icon-md"
            aria-label="Notifications"
            onClick={check}
          >
            <Bell />
          </Button>
        </div>
      </div>
      <Textarea ref={composerRef} placeholder="Message #fix-login" className="mt-auto" />
      {current && stop !== null ? (
        <CoachMark anchor={anchors[stop]} onDismiss={end}>
          <CoachMarkContent side={current.side}>
            <PopoverBody>
              <PopoverTitle>{current.title}</PopoverTitle>
              <Text.Sans size="small">{current.description}</Text.Sans>
            </PopoverBody>
            <CoachMarkActions>
              <CoachMarkCount>
                {stop + 1} of {STOPS.length}
              </CoachMarkCount>
              {last ? null : (
                <Button variant="ghost" onClick={end}>
                  Skip
                </Button>
              )}
              <CoachMarkNext onClick={last ? end : next}>{last ? "Done" : "Next"}</CoachMarkNext>
            </CoachMarkActions>
          </CoachMarkContent>
        </CoachMark>
      ) : null}
    </div>
  );
}
```

## When to use

| Situation                                                   | Use                                                    |
| ----------------------------------------------------------- | ------------------------------------------------------ |
| A tour of a screen the person enters for the first time     | CoachMark, one per stop                                |
| One sentence about a control, on hover                      | [Tooltip](https://raft-design.botiverse.build/components/tooltip.md)                       |
| A little choice or input the person opened                  | [Popover](https://raft-design.botiverse.build/components/popover.md)                       |
| An error, a confirmation or anything the person must answer | [Feedback](https://raft-design.botiverse.build/patterns/feedback.md) |

## Anatomy

- `CoachMark`: the root of one stop. `anchor` is its target, an element or a ref (read after every render, so a ref that moves to another element moves the stop); `open` (default `true`) shows it; `onDismiss` is called when the person ends the tour from the bubble: Esc in it, or a `PopoverClose` in it. Required.
- `CoachMarkContent`: the bubble, 300px, 8px from the target and at least 8px from the window's edge, with an arrow on whichever side it opens (`side`, default `bottom`; `align`, default `center`). Required.
- `PopoverBody` with `PopoverTitle` and `PopoverDescription`: the thing's name and one sentence about it.
- `CoachMarkAction`: optional, one more thing the stop offers, an `outline` button on its own row under the text (Turn on notifications).
- `CoachMarkActions`: the last row. `CoachMarkCount` ("2 of 6") at its start, then Skip (a `ghost` Button) and `CoachMarkNext`.
- `CoachMarkNext`: Next, or Done on the last stop, the pink button.

The product owns the tour: which stop is open, its words, and what Skip, Next and Done do. Render one `CoachMark` for the open stop and change its `anchor` and text for the next one.

## Behavior

- The target holds the Located wash at full strength while its stop is open, and loses it when the stop closes or moves on: `CoachMark` sets `data-located="held"` on it and puts back what was there ([Located](https://raft-design.botiverse.build/foundations/states.md#located)). A target that is already selected keeps its selection. For a list row, anchor the whole row, so the wash takes the row's inset shape (`fill-cell-selected`).
- It is non-modal. No scrim, and a press outside and focus leaving the bubble don't close it: the person may be answering the agent under it.
- In the Tab order the bubble comes right after its target: Tab from the target (or its last control, or the last control before it when the target takes no focus) goes to the bubble's first button, Tab from its last button goes on to what follows the target, and Shift+Tab goes back the same way. The bubble has no trigger for Base UI to return to, so CoachMark places it.
- When a stop opens, and when it moves to the next target (another `anchor`), focus goes to `CoachMarkNext`, unless the person is typing in a field: then it stays there. When the same `anchor`, a ref, comes to point at a new element (a row given a new key), the stop stays where it is and focus moves to `CoachMarkNext` only if it was already in the bubble. Pass a different ref (or element) for each stop, never the same ref moved to the next target.
- Esc while focus is in the bubble calls `onDismiss`: the person ended the tour. Esc elsewhere, in the composer for instance, goes on to what has focus (closing its suggestions, cancelling an edit) and leaves the tour open.
- Closing moves no focus: the product puts it in the composer when the tour ends, by Skip, Done or Esc.

## Accessibility

- The bubble has the `dialog` role and is named by its `PopoverTitle`.
- Focus moves only as above, never out of a field the person is typing in.
- The Located wash is only a look; the bubble's title names the thing for a screen reader.

## Rules

- Make the bubble 300px wide; `CoachMarkContent` does.
- Write a title and one sentence. A stop that needs two sentences is two stops.
- Count the stops on every bubble; leave Skip off the last one, which has Done.
- Point each stop at one thing, and anchor it to something every platform has.
- The other rules of a tour are in [Coach marks](https://raft-design.botiverse.build/patterns/coach-marks.md#rules).

## API

### CoachMark

One stop of a tour: a non-modal popover anchored to its target, which holds the Located wash while the stop is open (design/patterns/coach-marks.md). The product owns the tour: which stop is open, the copy, and where focus goes when it ends.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `anchor` (required) | `Element \| RefObject<Element \| null>` |  | What this stop is about, an element or a ref to one. While the stop is open its target holds the Located wash (`data-located="held"`); pass the whole row for a list row. A ref is read after every render, so a ref that moves to another element (a row given a new key) moves the wash too. |
| `children` | `ReactNode` |  |  |
| `onDismiss` | `() => void` |  | Called when the person ends the tour from the bubble: Esc while focus is in it, or a `PopoverClose` in it. Skip and Done are the product's own buttons and call its own handlers. A press outside and focus leaving never close it, and Esc pressed anywhere else goes on to what has focus. |
| `open` | `boolean` | `true` | Whether the stop is showing. |

### CoachMarkAction

One more thing the stop offers (Turn on notifications), an `outline` button on its own row under the text.

Also takes the attributes of its element.

### CoachMarkActions

The bubble's last row: `CoachMarkCount` at the start, then Skip and `CoachMarkNext` at the end.

Also takes the attributes of its element.

### CoachMarkContent

The bubble: 300px, 8px from its target and at least 8px from the window's edge (or frame inset), with an arrow on whichever side it opens.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `collisionPadding` | `Padding` | `8` | Additional space to maintain from the edge of the collision boundary: 8px from the window's edge, and 8px from a frame inset where a shell keeps one. A value you pass is kept as it is. |
| `container` | `HTMLElement \| ShadowRoot \| RefObject<HTMLElement \| ShadowRoot \| null>` |  |  |
| `portalProps` | `PopoverPortalProps` |  |  |
| `align` | `Align` | `"center"` | How to align the popup relative to the specified side. |
| `alignOffset` | `number \| OffsetFunction` | `0` | Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment. |
| `arrowPadding` | `number` | `5` | Minimum distance to maintain between the arrow and the edges of the popup. |
| `collisionAvoidance` | `CollisionAvoidance` |  | Determines how to handle collisions when positioning the popup. |
| `collisionBoundary` | `Boundary` | `'clipping-ancestors'` | An element or a rectangle that delimits the area that the popup is confined to. |
| `disableAnchorTracking` | `boolean` | `false` | Whether to disable the popup from tracking any layout shift of its positioning anchor. |
| `positionMethod` | `"fixed" \| "absolute"` | `'absolute'` | Determines which CSS `position` property to use. |
| `render` | `ReactElement \| ((props, state) => ReactElement)` |  | Allows you to replace the component's HTML element with a different tag, or compose it with another component. |
| `side` | `Side` | `"bottom"` | Which side of the anchor element to align the popup against. May automatically change to avoid collisions. |
| `sideOffset` | `number \| OffsetFunction` | `8` | Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment. |
| `sticky` | `boolean` | `false` | Whether to maintain the popup in the viewport after the anchor element was scrolled out of view. |

Also takes the attributes of its element.

### CoachMarkCount

Which stop this is, "2 of 6", in Space Mono.

Also takes the attributes of its element.

### CoachMarkNext

Next, or Done on the last stop: the pink button, and where focus goes when the stop opens.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `loading` | `boolean` |  | Busy: shows the spinner and blocks activation (click, Enter, Space and form submission) while keeping the button focusable. A busy button is `aria-disabled`, not natively `disabled`, so the focus a press left on it stays there and is still there when the wait ends. |
| `loadingLabel` | `string` |  | The spinner's accessible name while `loading`: say what is happening. |
| `size` | `"md" \| "lg" \| "xl" \| "icon-sm" \| "icon-md" \| "icon-lg" \| DeprecatedButtonSize` |  | `md` (32px) and `icon-md` for rows, cards, banners, toolbars and panel headers; `lg` and `icon-lg` (the control height) for dialog footers, forms and beside a field; `xl` for a full-width call to action; `icon-sm` (28px, a 44px hit area) for an icon-only action on a line of a row or a card, such as a comment header's More, never in a toolbar. The size follows the place: see Button › Size by place. |
| `focusableWhenDisabled` | `boolean` | `false` | Whether the button should be focusable when disabled. |
| `nativeButton` | `boolean` | `true` | Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). |
| `render` | `ReactElement \| ((props, state) => ReactElement)` |  | Allows you to replace the component's HTML element with a different tag, or compose it with another component. |

Also takes the attributes of its element.

## Changes

- 0.102.0 (Added): **`CoachMark`, one stop of a tour** (work in progress, from `@botiverse/raft-ui/wip`; [CoachMark](https://raft-design.botiverse.build/components/coach-mark/), [Coach marks](https://raft-design.botiverse.build/patterns/coach-marks/)). `CoachMark` takes the stop's `anchor`, `open` and `onDismiss`; `CoachMarkContent` is the 300px bubble with an arrow, 8px from its target; `CoachMarkAction`, `CoachMarkActions`, `CoachMarkCount` and `CoachMarkNext` (the pink Next or Done) lay out its rows, with `PopoverBody`, `PopoverTitle` and `PopoverDescription` for the text. While a stop is open its target holds the Located wash (`data-located="held"`). The bubble is non-modal: a press outside and focus leaving don't close it, focus goes to Next when a stop opens unless the person is typing in a field, its buttons come right after its target in the Tab order, and Esc in the bubble (or a `PopoverClose`) calls `onDismiss`, while Esc anywhere else goes on to what has focus.
