All files / spotlight-tour/src SpotlightTour.tsx

100% Statements 54/54
100% Branches 48/48
100% Functions 9/9
100% Lines 49/49

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                                                  1x   1x   1x   1x                                                                                                                     407x   407x 69x 2x 2x   67x 1x 1x   66x 64x 64x   2x 2x 2x 2x 69x     407x                                                                                                         407x   407x   407x 407x 407x       407x 407x 69x     407x 407x               407x 407x 190x 15x 15x   175x   83x 190x 190x                           407x 70x                   407x   407x   291x 291x     291x               407x                                                                                                                                                     1x  
import * as React from "react";
import {
  useEffect,
  useImperativeHandle,
  useMemo,
  useState,
  type ReactNode,
  type Ref,
} from "react";
import { createPortal } from "react-dom";
import { clsx } from "clsx";
import { useReducedMotion } from "@usefy/use-reduced-motion";
import { useSpotlightTour } from "./useSpotlightTour";
import { Overlay } from "./components/Overlay/Overlay";
import { Tooltip } from "./components/Tooltip/Tooltip";
import type {
  RenderStepContext,
  SpotlightTourClassNames,
  TourController,
  TourLabels,
  UseSpotlightTourOptions,
} from "./types";
import styles from "./SpotlightTour.module.scss";
 
/** Default overlay dim color. */
const DEFAULT_MASK_COLOR = "rgba(0 0 0 / 0.55)";
/** Default spotlight/tooltip transition duration (ms). */
const DEFAULT_TRANSITION_MS = 300;
/** Default stacking context for the portal layer. */
const DEFAULT_Z_INDEX = 1000;
 
const DEFAULT_LABELS: TourLabels = {
  back: "Back",
  next: "Next",
  skip: "Skip",
  finish: "Done",
  close: "Close",
  gatedHint: "Complete the highlighted action to continue",
};
 
/** Props for {@link SpotlightTour}. Extends every {@link UseSpotlightTourOptions}. */
export interface SpotlightTourProps extends UseSpotlightTourOptions {
  // --- presentation ---
  /** Overlay color. @default 'rgba(0 0 0 / 0.55)' */
  maskColor?: string;
  /**
   * Spotlight/tooltip transition ms (0 disables). Automatically forced to 0
   * when the user prefers reduced motion. @default 300
   */
  transitionDuration?: number;
  /**
   * What a click on the dimmed area does: `'ignore'` (nothing — the tour is
   * deliberate), `'close'` (dismisses like Skip, fires `onSkip`), or `'next'`
   * (advances a step). @default 'ignore'
   */
  overlayClick?: "ignore" | "close" | "next";
  /** z-index of the portal layer. @default 1000 */
  zIndex?: number;
 
  // --- UI customization ---
  /** Button label overrides (i18n-friendly). */
  labels?: Partial<TourLabels>;
  /** Show the progress dots. @default true */
  showProgress?: boolean;
  /** Show the "2 / 5" step counter. @default true */
  showCounter?: boolean;
  /** Show the Skip button. @default true */
  showSkip?: boolean;
  /** Show the close (×) button. @default true */
  showClose?: boolean;
  /**
   * Replace the whole tooltip UI. The wrapper `SpotlightTour` renders stays
   * position-only; spread `ctx.tooltipProps` on your element for a11y.
   */
  renderStep?: (ctx: RenderStepContext) => ReactNode;
 
  // --- theming ---
  /** Color theme. `'system'` follows `prefers-color-scheme`. @default 'system' */
  theme?: "light" | "dark" | "system";
  /** Per-part class overrides. */
  classNames?: SpotlightTourClassNames;
  /** Extra class on the portal root element. */
  className?: string;
 
  /** Imperative access to the tour controls. */
  controllerRef?: React.Ref<TourController>;
}
 
/** Resolve the effective dark-mode boolean for the chosen theme (SSR-safe). */
function useResolvedDark(theme: "light" | "dark" | "system"): boolean {
  const [dark, setDark] = useState(theme === "dark");
 
  useEffect(() => {
    if (theme === "light") {
      setDark(false);
      return;
    }
    if (theme === "dark") {
      setDark(true);
      return;
    }
    if (typeof window === "undefined" || !window.matchMedia) {
      setDark(false);
      return;
    }
    const mq = window.matchMedia("(prefers-color-scheme: dark)");
    const update = () => setDark(mq.matches);
    update();
    mq.addEventListener?.("change", update);
    return () => mq.removeEventListener?.("change", update);
  }, [theme]);
 
  return dark;
}
 
/**
 * A guided onboarding tour: dims the screen, cuts an animated spotlight hole
 * around each step's target, and shows a step tooltip beside it. Built on
 * {@link useSpotlightTour} — every hook option (controlled/uncontrolled
 * `open`/`step`, missing-target policies, lifecycle callbacks) works here too.
 *
 * Portal-rendered into `document.body` after mount (nothing renders on the
 * server), themable via `--usefy-tour-*` CSS variables, and animated between
 * steps unless `transitionDuration` is 0 or the user prefers reduced motion.
 *
 * @example
 * ```tsx
 * import { SpotlightTour } from "@usefy/spotlight-tour";
 *
 * function App() {
 *   return (
 *     <SpotlightTour
 *       defaultOpen
 *       steps={[
 *         { title: "Welcome!", content: "A quick look around." }, // centered
 *         { target: "#search", title: "Search", content: "Find anything here." },
 *         { target: "#settings", content: "Tune your preferences.", placement: "left" },
 *       ]}
 *       onFinish={() => console.log("toured!")}
 *     />
 *   );
 * }
 * ```
 */
export function SpotlightTour(props: SpotlightTourProps): ReactNode {
  const {
    // presentation
    maskColor = DEFAULT_MASK_COLOR,
    transitionDuration = DEFAULT_TRANSITION_MS,
    overlayClick = "ignore",
    zIndex = DEFAULT_Z_INDEX,
    // UI customization
    labels,
    showProgress = true,
    showCounter = true,
    showSkip = true,
    showClose = true,
    renderStep,
    // theming
    theme = "system",
    classNames,
    className,
    controllerRef,
    // everything else drives the hook
    ...hookOptions
  } = props;
 
  const tour = useSpotlightTour(hookOptions);
 
  const reducedMotion = useReducedMotion();
  const effectiveDuration = reducedMotion ? 0 : transitionDuration;
  const isDark = useResolvedDark(theme);
 
  // Portal only after mount: nothing renders during SSR / the first hydration
  // pass, so no `document` access happens on the server.
  const [mounted, setMounted] = useState(false);
  useEffect(() => {
    setMounted(true);
  }, []);
 
  const mergedLabels: TourLabels = { ...DEFAULT_LABELS, ...labels };
  const gatedHintLabel = mergedLabels.gatedHint;
 
  // Step announcements for screen readers: the visually-hidden live region
  // below re-announces on each *settled* step (skipped-over steps and
  // pending/auto-scrolling states stay silent). Gated steps append the gate
  // hint so the requirement is conveyed non-visually too. The text is set
  // from an effect so the region always mounts empty first — aria-live only
  // announces *changes* to an existing region.
  const [announcement, setAnnouncement] = useState("");
  useEffect(() => {
    if (!tour.open) {
      setAnnouncement("");
      return;
    }
    if (!tour.settled || tour.pending) return;
    const title =
      typeof tour.step?.title === "string" ? `: ${tour.step.title}` : "";
    const hint = tour.gated ? `. ${gatedHintLabel}` : "";
    setAnnouncement(
      `Step ${tour.stepIndex + 1} of ${tour.stepCount}${title}${hint}`
    );
  }, [
    tour.open,
    tour.settled,
    tour.pending,
    tour.gated,
    tour.stepIndex,
    tour.stepCount,
    tour.step?.title,
    gatedHintLabel,
  ]);
 
  const controller = useMemo<TourController>(
    () => ({
      start: tour.start,
      next: tour.next,
      prev: tour.prev,
      goTo: tour.goTo,
      skip: tour.skip,
      finish: tour.finish,
    }),
    [tour.start, tour.next, tour.prev, tour.goTo, tour.skip, tour.finish]
  );
  useImperativeHandle(controllerRef, () => controller, [controller]);
 
  if (!mounted || !tour.open || !tour.step) return null;
 
  const step = tour.step;
  const centered = tour.geometry.spotlight === null;
 
  // Per-step disableOverlayClose downgrades 'close'/'next' to 'ignore'.
  const overlayClickHandler = step.disableOverlayClose
    ? undefined
    : overlayClick === "close"
      ? tour.skip
      : overlayClick === "next"
        ? tour.next
        : undefined;
 
  return createPortal(
    <div
      // `.light` only for an explicit theme="light" prop: it out-specifies a
      // host's [data-theme="dark"] hook (explicit prop > host attribute >
      // system). System-resolved light stays overridable by the host.
      className={clsx(
        styles.root,
        isDark && styles.dark,
        theme === "light" && styles.light,
        className
      )}
      // Publish the effective duration on the root so BOTH the overlay mask
      // and the tooltip inherit the *same* value — the tooltip's step-move
      // glide must match the spotlight morph, and reduced-motion /
      // transitionDuration=0 must reach the tooltip too (it is a sibling of
      // the overlay, so it can't inherit the overlay's local variable).
      style={
        {
          zIndex,
          "--usefy-tour-transition-duration": `${effectiveDuration}ms`,
        } as React.CSSProperties
      }
      data-tour-root=""
    >
      <Overlay
        spotlight={tour.geometry.spotlight}
        maskColor={maskColor}
        transitionDuration={effectiveDuration}
        spotlightClicks={step.spotlightClicks ?? true}
        onOverlayClick={overlayClickHandler}
        overlayProps={tour.getOverlayProps()}
        className={classNames?.overlay}
        spotlightClassName={classNames?.spotlight}
      />
      <div className={styles.srOnly} role="status" aria-live="polite">
        {announcement}
      </div>
      {/* While a 'wait'-policy target is pending (or an auto-scroll is in
          flight) there is nothing to anchor to yet — the dim layer shows and
          the tooltip holds off. A hidden focus holder keeps the trap alive by
          carrying the tooltip ref: it contains nothing focusable, so the trap
          pins focus to it and Tab cannot reach the dimmed page underneath. */}
      {tour.pending ? (
        <div
          ref={tour.getTooltipProps().ref as Ref<HTMLDivElement> | undefined}
          className={styles.srOnly}
          data-tour-focus-holder=""
        />
      ) : (
        <Tooltip
          step={step}
          index={tour.stepIndex}
          count={tour.stepCount}
          position={tour.geometry.tooltip}
          transitionDuration={effectiveDuration}
          centered={centered}
          gated={tour.gated}
          isFirst={tour.isFirst}
          isLast={tour.isLast}
          labels={mergedLabels}
          showProgress={showProgress}
          showCounter={showCounter}
          showSkip={showSkip}
          showClose={showClose}
          controller={controller}
          tooltipProps={tour.getTooltipProps()}
          classNames={classNames}
          renderStep={renderStep}
        />
      )}
    </div>,
    document.body
  );
}
 
SpotlightTour.displayName = "SpotlightTour";