Todae

Todae

Coachmark

Coachmark.Root points out one element on the page. The page around it dims and goes inert, the element itself stays usable through a hole in the dim page, and a popup beside it says what it does. Pass the element as targetRef. open is controlled, or defaultOpen leaves it uncontrolled.

On this page

It shows one step. A tour, the order of its steps, and whether the person has seen it are the product's, the same way a form's error text is: render one Coachmark.Root and change its targetRef and content from step to step, as Product tour does.

import { Button, Coachmark, Stack } from '@tounsoo/todae';
import { useRef, useState } from 'react';

export default function CoachmarkBasic() {
  const exportRef = useRef<HTMLButtonElement>(null);
  const [open, setOpen] = useState(false);
  const [exported, setExported] = useState(0);
  return (
    <Stack.Root direction="row" gap="condensed" wrap>
      <Button.Root onClick={() => setOpen(true)}>Show the tip</Button.Root>
      <Button.Root ref={exportRef} variant="secondary" onClick={() => setExported(exported + 1)}>
        {exported ? `Exported ${exported}×` : 'Export'}
      </Button.Root>
      <Coachmark.Root targetRef={exportRef} open={open} onOpenChange={setOpen}>
        <Coachmark.Title>Export moved here</Coachmark.Title>
        <Coachmark.Description>Save this report as a PDF or a CSV file. The button works while the tip is open, so try it.</Coachmark.Description>
        <Coachmark.Footer>
          <Coachmark.Close variant="primary">Got it</Coachmark.Close>
        </Coachmark.Footer>
      </Coachmark.Root>
    </Stack.Root>
  );
}

The popup is a Popover surface with role="dialog", placed by CSS anchors beside the target and flipped where there is no room, so it follows the target as the page scrolls. The dim page is its own top-layer box over the target, grown by coachmark.cutout.padding (0.25rem) and never by less than the focus ring needs, whose shadow in overlay.scrim.bg covers the rest of the screen. Presses pass through that box, so the target takes them. The hole's corners follow the target's own, so a pill button gets a pill hole. The popup is at most coachmark.max-width (20rem) wide.

As it opens, the target scrolls into view, and the dim page and the popup fade in together with the coachmark.motion.duration.enter and easing.enter tokens, which point at the popup motion role and, in the standard motion preset, the core enter curve, while the popup grows from popover.motion.scale as a Popover does; closing fades both out with the exit pair. When targetRef changes while it is open, as in a tour, the hole and the popup slide to the new target with coachmark.motion.duration.move and easing.move in Chrome; Safari and Firefox move them at once. Under reduced motion the popup only fades, a new target is reached at once, and the scroll is instant.

A tour

A tour of a few steps is a recipe built on one Coachmark.Root: Product tour keeps the step in state, keeps focus on Next from step to step, and has screen readers read each new step.

Accessible composition

Todae handles: The popup is a dialog named by Coachmark.Title and described by Coachmark.Description; an aria-labelledby or aria-describedby you pass wins. It is not aria-modal, since the target outside it stays usable. While it is open, everything else on the page is inert, except the target, the elements the target names in aria-controls, popovers, such as the target's own tooltip, a dialog that opens while it is open, such as one the target opens, and the toast region. A popover or dialog whose trigger is inert can't open, and a dialog the target opens takes focus as it opens, wherever it sits. On open, focus goes to initialFocusRef, else the popup's first tab stop, else the popup. Tab and Shift+Tab go round the popup's tab stops and then the target's; focus in a menu the target opened belongs to that menu. Escape asks to close, as does Coachmark.Close, and a press on the dim page does too with dismissOnBackdrop, though not one in a dialog that opened. When a new step makes the element that had focus inert, such as the old target, focus moves into the popup. On close, focus goes to restoreFocusRef, else back to what had focus before it opened.

You supply: A target that is a real control, such as Button.Root, so it can take focus and presses. A name: render Coachmark.Title, or pass aria-label. A way out, such as Coachmark.Close, above all when dismissOnEscape is false. What happens when the target is pressed: the coachmark stays open unless your handler closes it or moves on a step.

Watch for: In a browser without CSS anchor positioning, the dim page has no hole and the popup is not placed beside the target, as Platform says. A target that unmounts while the coachmark is open leaves the popup with nothing to sit by; close it or move on a step first. Text in the popup should say what the target does, not repeat the target's own name. Don't open a coachmark the person didn't ask for while they are in the middle of a task: it makes the rest of the page inert.

When to use

When: Pointing out one control that is new or easy to miss, or walking through a few of them as a short tour.

When not: A short hint on hover or focus → Tooltip. A task that needs the whole page out of the way → Dialog. A non-modal panel the person opened themselves → Popover.

Props

Coachmark.Root props
NameTypeDescription
targetRef RequiredReact.RefObject<HTMLElement | null>

The element to highlight. It stays usable through the hole, it is scrolled into view as the coachmark opens, and the popup sits beside it.

openboolean

Controlled open state. Leave it out for an uncontrolled coachmark.

defaultOpenboolean

Shows the coachmark on mount when uncontrolled. There is no trigger part, so once closed it opens again only on remount.

Default: false

onOpenChange(open: boolean, reason: CoachmarkCloseReason) => void

Dismiss requests. When controlled, staying open keeps the coachmark open. Reasons: escape, backdrop, close-button.

dismissOnEscapeboolean

Escape calls onOpenChange(false, "escape").

Default: true

dismissOnBackdropboolean

A press on the dim page calls onOpenChange(false, "backdrop"). Off by default, so a stray press doesn’t end a tour.

Default: false

initialFocusRefReact.RefObject<HTMLElement | null>

Where focus goes as it opens. Without it, the popup’s first tab stop, else the popup.

restoreFocusRefReact.RefObject<HTMLElement | null>

Where focus goes as it closes. Without it, back to what had focus before it opened.

aria-labelstring

Name when Coachmark.Title is omitted.

arrowsboolean

Whether arrows show at the popup’s cut-off edges when it scrolls, as on Popover.Root.

Default: true

Coachmark.Title props
NameTypeDescription
childrenReact.ReactNode

Title text.

Coachmark.Description props
NameTypeDescription
childrenReact.ReactNode

What the highlighted element does, or what to do with it.

Coachmark.Footer props
NameTypeDescription
Coachmark.Close props
NameTypeDescription
aria-labelstring

Accessible name. Text of two or more letters or digits, such as "Got it", names the button by itself. Otherwise the close string from StringsProvider names it, Close in English.

Default: "Close" when its text, not counting aria-hidden or SVG parts, has fewer than two letters or digits

onClickReact.MouseEventHandler<HTMLButtonElement>

Runs before the close request. Call event.preventDefault() to keep the coachmark open.