Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | import type * as React from "react";
/**
* An axis-aligned rectangle in viewport coordinates.
*
* Used throughout the positioning engine for target rects, spotlight rects,
* and tooltip boxes. Matches the `x`/`y`/`width`/`height` subset of
* `DOMRect`, so a `getBoundingClientRect()` result can be spread into one.
*
* @example
* ```ts
* import type { Rect } from "@usefy/spotlight-tour/headless";
*
* const rect: Rect = { x: 100, y: 200, width: 320, height: 48 };
* ```
*/
export interface Rect {
x: number;
y: number;
width: number;
height: number;
}
/**
* The element a tour step highlights, in one of three forms:
*
* - a **CSS selector** string (resolved with `document.querySelector`),
* - a **React ref** to the element,
* - a **function** returning the element (or `null` when absent).
*
* @example
* ```tsx
* import type { TourStep } from "@usefy/spotlight-tour/headless";
*
* const searchRef = React.useRef<HTMLInputElement>(null);
*
* const steps: TourStep[] = [
* { target: "#search-bar", content: "Search anything here." },
* { target: searchRef, content: "Or point a ref at it." },
* { target: () => document.querySelector(".settings"), content: "Or a function." },
* ];
* ```
*/
export type TourTarget =
| string
| React.RefObject<Element | null>
| (() => Element | null);
/**
* Preferred tooltip side relative to the spotlight.
*
* `'auto'` ranks sides fit-first: it picks the roomiest side among those where
* the tooltip actually fits, falling back to the roomiest overall when none
* fit. Whatever the preference, the engine flips to the opposite side when the
* preferred one overflows, and shifts along the cross axis to stay visible.
*
* @example
* ```ts
* import type { TourPlacement } from "@usefy/spotlight-tour/headless";
*
* const placement: TourPlacement = "bottom";
* ```
*/
export type TourPlacement = "top" | "bottom" | "left" | "right" | "auto";
/**
* What the tour does when a step's target cannot be resolved:
*
* - `'skip'` — advance to the next resolvable step in the direction the user
* was navigating (default).
* - `'wait'` — hold in a `pending` state up to `waitTimeout` ms, retrying the
* target, then fall back to skipping.
* - `'center'` — show the step anyway as a centered modal (no spotlight).
*
* @example
* ```ts
* import type { TourStep } from "@usefy/spotlight-tour/headless";
*
* const step: TourStep = {
* target: "#lazy-widget",
* content: "This widget loads asynchronously.",
* missingTarget: "wait",
* waitTimeout: 5000,
* };
* ```
*/
export type MissingTargetPolicy = "skip" | "wait" | "center";
/**
* One unit of a tour: a target to spotlight plus the tooltip content and
* per-step behavior overrides.
*
* @example
* ```tsx
* import type { TourStep } from "@usefy/spotlight-tour/headless";
*
* const steps: TourStep[] = [
* { title: "Welcome!", content: "Let's take a quick look around." }, // centered
* {
* target: "#search-bar",
* title: "Search",
* content: "Find anything from here.",
* placement: "bottom",
* spotlightPadding: 12,
* onEnter: () => console.log("entered search step"),
* },
* ];
* ```
*/
export interface TourStep {
/** Element to spotlight. Omit for a centered (modal) step. */
target?: TourTarget;
/** Tooltip heading. */
title?: React.ReactNode;
/** Tooltip body. */
content: React.ReactNode;
/** Preferred tooltip side; 'auto' picks the roomiest. @default 'auto' */
placement?: TourPlacement;
/** Extra px around the target inside the spotlight. @default 8 */
spotlightPadding?: number;
/** Spotlight corner radius in px. @default 8 */
spotlightRadius?: number;
/** What to do when the target can't be resolved. @default 'skip' */
missingTarget?: MissingTargetPolicy;
/** ms to wait in 'wait' mode before falling back to skip. @default 3000 */
waitTimeout?: number;
/**
* Require a user action to advance: the step stays `gated` (Next rendered
* disabled, ArrowRight/Enter blocked) until `event` fires on the resolved
* target — or on `selector`, resolved against `document` — after which the
* tour advances automatically.
*
* Accessibility notes: explain the required action in the step's `content`
* (the default UI also shows a `labels.gatedHint` line while gated). Gate
* targets are pointer-interactive by design — keyboard users can still
* navigate Back or dismiss with Escape. If you need the gate action itself
* to be fully keyboard-reachable, render it inside the tooltip via
* `renderStep` so it participates in the focus trap.
*/
advanceOn?: { event: "click"; selector?: string };
/** Allow clicking through the spotlight onto the target. @default true */
spotlightClicks?: boolean;
/** Scroll the target into view when off-screen. @default true */
scrollIntoView?: boolean;
/**
* Make the `SpotlightTour` `overlayClick` modes (`'close'` / `'next'`)
* behave as `'ignore'` for this step — a dimmed-area click does nothing.
* Useful for gated steps the user must not accidentally dismiss.
* @default false
*/
disableOverlayClose?: boolean;
/**
* Called when the tour enters this step. Only fires for steps that actually
* settle on screen: centered steps, steps whose target resolved, and
* `'wait'`-policy steps (visibly pending counts as entered). Steps jumped
* over by the `'skip'` missing-target policy never fire it.
*/
onEnter?: () => void;
/**
* Called when the tour leaves this step (navigation, close, or a
* `'wait'`-policy timeout skipping it). Always paired with a preceding
* `onEnter`; skipped-over steps fire neither.
*/
onLeave?: () => void;
}
/**
* Options for {@link useSpotlightTour}. `open` and `step` each follow the
* controllable-state pattern: pass `open`/`step` to control from the outside,
* or `defaultOpen`/`defaultStep` to let the hook own the state — the
* `on*Change` callbacks fire in both modes.
*
* @example
* ```tsx
* import { useSpotlightTour } from "@usefy/spotlight-tour/headless";
*
* function Onboarding() {
* const tour = useSpotlightTour({
* steps: [
* { target: "#search", content: "Search here" },
* { target: "#settings", content: "Tune things here" },
* ],
* defaultOpen: false,
* onFinish: () => console.log("done!"),
* });
*
* return <button onClick={() => tour.start()}>Start tour</button>;
* }
* ```
*/
export interface UseSpotlightTourOptions {
/** The ordered list of tour steps. */
steps: TourStep[];
// --- open ownership ---
/** Controlled open state. */
open?: boolean;
/** Initial open state when uncontrolled. @default false */
defaultOpen?: boolean;
/** Fires whenever the open state changes (both modes). */
onOpenChange?: (open: boolean) => void;
// --- step ownership ---
/** Controlled step index. */
step?: number;
/** Initial step index when uncontrolled. @default 0 */
defaultStep?: number;
/** Fires whenever the step index changes (both modes). */
onStepChange?: (index: number) => void;
// --- behavior ---
/**
* localStorage persistence key; omit to disable persistence.
* A finished/skipped tour with a `tourId` will not auto-open again
* (key format: `usefy-tour:<tourId>`).
*/
tourId?: string;
/** Enable arrow-key/Escape keyboard navigation. @default true */
keyboard?: boolean;
/** Lock background scroll while the tour is open. @default true */
scrollLock?: boolean;
/** Called when the tour completes (Done / next on the last step). */
onFinish?: () => void;
/** Called when the tour is skipped, with the step index it was on. */
onSkip?: (atStep: number) => void;
}
/**
* Live positioning data for the active step, rAF-batched — state commits only
* when the underlying values actually change.
*
* @example
* ```tsx
* import { useSpotlightTour } from "@usefy/spotlight-tour/headless";
*
* function CustomOverlay() {
* const { geometry } = useSpotlightTour({ steps: [{ target: "#hero", content: "Hi" }], defaultOpen: true });
* if (!geometry.spotlight) return null; // centered step (or not measured yet)
* const { x, y, width, height, radius } = geometry.spotlight;
* return <div style={{ position: "fixed", left: x, top: y, width, height, borderRadius: radius }} />;
* }
* ```
*/
export interface SpotlightGeometry {
/** Spotlight rect in viewport coords, or null for a centered step. */
spotlight: (Rect & { radius: number }) | null;
/** Resolved tooltip position (null until measured). */
tooltip: {
x: number;
y: number;
placement: string;
arrow: { x: number; y: number };
} | null;
}
/**
* Imperative tour controls, exposed via `controllerRef` on `SpotlightTour`
* (and structurally identical to the control slice of
* {@link UseSpotlightTourReturn}). All methods are stable references.
*
* @example
* ```tsx
* import { useRef } from "react";
* import { SpotlightTour, type TourController } from "@usefy/spotlight-tour";
*
* function App() {
* const controller = useRef<TourController>(null);
* return (
* <>
* <button onClick={() => controller.current?.start()}>Help</button>
* <SpotlightTour
* controllerRef={controller}
* steps={[{ target: "#search", content: "Search here" }]}
* />
* </>
* );
* }
* ```
*/
export interface TourController {
/** Open the tour, optionally jumping to a step index (clamped). */
start: (at?: number) => void;
/** Advance one step; on the last step this finishes the tour. */
next: () => void;
/** Go back one step; no-op on the first step. */
prev: () => void;
/** Jump to a step index (clamped to the valid range). */
goTo: (index: number) => void;
/** Close the tour as skipped (fires `onSkip`). */
skip: () => void;
/** Close the tour as completed (fires `onFinish`). */
finish: () => void;
}
/**
* Button label overrides for the default tooltip UI (i18n-friendly).
*
* @example
* ```tsx
* <SpotlightTour
* steps={steps}
* labels={{ back: "이전", next: "다음", skip: "건너뛰기", finish: "완료", close: "닫기" }}
* />
* ```
*/
export interface TourLabels {
/** Back button. @default "Back" */
back: string;
/** Next button. @default "Next" */
next: string;
/** Skip button. @default "Skip" */
skip: string;
/** Next button on the last step. @default "Done" */
finish: string;
/** Accessible name of the close (×) button. @default "Close" */
close: string;
/**
* Hint shown (and announced) while a step's `advanceOn` gate is unmet.
* @default "Complete the highlighted action to continue"
*/
gatedHint: string;
}
/**
* Per-part class overrides for `SpotlightTour`.
*
* @example
* ```tsx
* <SpotlightTour steps={steps} classNames={{ tooltip: "my-tooltip", overlay: "my-overlay" }} />
* ```
*/
export type SpotlightTourClassNames = Partial<
Record<
| "overlay"
| "spotlight"
| "tooltip"
| "arrow"
| "header"
| "content"
| "footer"
| "dots"
| "counter",
string
>
>;
/**
* Context handed to `renderStep` when replacing the default tooltip UI.
* Spread `tooltipProps` on your top-level element to keep the dialog
* semantics; positioning is handled by the wrapper `SpotlightTour` renders.
*
* @example
* ```tsx
* <SpotlightTour
* steps={steps}
* renderStep={({ step, index, count, controller, tooltipProps }) => (
* <section {...tooltipProps} style={{ background: "#222", color: "#fff", padding: 16 }}>
* <p>{step.content}</p>
* <button onClick={controller.next}>Got it ({index + 1}/{count})</button>
* </section>
* )}
* />
* ```
*/
export interface RenderStepContext {
/** The active step. */
step: TourStep;
/** The active step index. */
index: number;
/** Total number of steps. */
count: number;
/** Imperative tour controls. */
controller: TourController;
/** True when the step's advance gate is unmet. */
gated: boolean;
/** A11y props to spread on your tooltip element. */
tooltipProps: React.HTMLAttributes<HTMLElement>;
}
/**
* Everything {@link useSpotlightTour} returns: tour state, live geometry, and
* `useCallback`-stable controls plus prop getters for building a bespoke UI.
*
* @example
* ```tsx
* import { useSpotlightTour } from "@usefy/spotlight-tour/headless";
*
* function MyTour() {
* const { open, step, stepIndex, stepCount, isLast, next, prev, skip, getTooltipProps } =
* useSpotlightTour({
* steps: [
* { target: "#a", content: "First" },
* { target: "#b", content: "Second" },
* ],
* defaultOpen: true,
* });
*
* if (!open || !step) return null;
* return (
* <div {...getTooltipProps()}>
* {step.content}
* <button onClick={prev}>Back</button>
* <button onClick={next}>{isLast ? "Done" : "Next"}</button>
* <button onClick={skip}>Skip ({stepIndex + 1}/{stepCount})</button>
* </div>
* );
* }
* ```
*/
export interface UseSpotlightTourReturn {
/** Whether the tour is open. */
open: boolean;
/** The active step index. */
stepIndex: number;
/** The active step object, or null when closed / out of range. */
step: TourStep | null;
/** Total number of steps. */
stepCount: number;
/** True when the active step is the first one. */
isFirst: boolean;
/** True when the active step is the last one. */
isLast: boolean;
/** Live geometry, rAF-batched; re-renders only when values change. */
geometry: SpotlightGeometry;
/** True while waiting for a 'wait'-policy target or auto-scroll to settle. */
pending: boolean;
/** True when the current step has an unmet advanceOn gate. */
gated: boolean;
/**
* True once the active step has actually settled on screen: its target
* resolved (or it is a centered step). False while closed, while a
* `'wait'`-policy target is still pending, and during the commits in which
* a `'skip'`-policy cascade is jumping over unresolvable steps.
*
* For announcement gating (`aria-live`) and other "the user can now see
* this step" side effects, combine it with `!pending` — `settled` stays
* true while an auto-scroll to an already-resolved target is still in
* flight, but `pending` covers that window (this is exactly what the
* styled `SpotlightTour` does).
*/
settled: boolean;
// --- controls (useCallback-stable) ---
/** Open the tour, optionally jumping to a step index (clamped). */
start: (at?: number) => void;
/** Advance one step; on the last step this finishes the tour. */
next: () => void;
/** Go back one step; no-op on the first step. */
prev: () => void;
/** Jump to a step index (clamped to the valid range). */
goTo: (index: number) => void;
/** Close the tour as skipped (fires `onSkip` with the current index). */
skip: () => void;
/** Close the tour as completed (fires `onFinish`). */
finish: () => void;
// --- prop getters for custom UIs (a11y baked in) ---
/** Props (incl. ref) to spread on your tooltip element. */
getTooltipProps: () => React.HTMLAttributes<HTMLElement> & {
ref: React.Ref<HTMLElement>;
};
/** Props to spread on your overlay element. */
getOverlayProps: () => React.HTMLAttributes<HTMLElement>;
}
|