Repository navigation
feat(tour): render the tour panel in React, with a ReactNode body (1.4.0) - #31
Conversation
The tour panel was assembled as an HTML string and injected, so a step could not carry React content. TourPanel now renders it as a React element that the tour passes to the tooltip as its content, keeping every class name and data attribute the delegated click handling, autoFocus and stylesheet rely on. - TourStepContent gains `body: ReactNode | (step) => ReactNode`. `text` is rendered as text by React; `content` stays HTML, injected only into the message element. - TooltipShowOptions.content accepts any React node; `html` applies to strings. - Fixes `text` being escaped twice on a step with no panel features, and navigation labels being inserted as unescaped markup.
A title was not one of the features that switch a step from a plain tooltip to the tour panel, so with `showClose: false` and no controls, media or progress the title was silently dropped. A title now renders the panel.
…mounts The unmount cleanup only destroyed the target watcher, backdrop and highlight, so a tour whose component unmounted mid-tour left its panel on screen with buttons nothing listened to any more. The cleanup now also hides the tooltip, ends the helper flow and resets the tour's state, without calling onExit or onEnd. A tour that is not running leaves the tooltip alone.
`.tip-magic-tour-body` now scrolls past `--tip-magic-tour-body-max-height` (60vh by default). The tooltip runs Floating UI's size() middleware and exposes the room beside the target as `--tip-magic-available-height`; the tour panel, now a flex column, caps itself to it so only the body shrinks. Plain tooltips read neither and are not clamped.
…body README and the published Flows/Properties pages describe the three step body forms, that only `content` is still injected as HTML, where a `body` gets its context from, the body's scroll limit, and that unmounting mid-tour ends the tour silently. A new "The Tourtip/React body" story shows a long, stateful React body.
A `body` step with no other panel features was shown as a plain tooltip, which ignores the pointer and has no scrolling body, so its React handlers never fired and a long body could not scroll. A `body` now always renders the panel.
A `content` step with no panel features was handed to the tooltip without `html`, so unless the consumer set `tooltipOptions.html` its markup showed as literal text - contradicting the documented contract that `content` is HTML. useTour now forces `html: true` for `content`, as it forces `false` for `text`.
The panel element was built once per step, so a `body` was a snapshot: a consumer that re-rendered with new `steps` while the tour ran (markdown that arrived after a fetch) kept showing the old content. The tooltip now holds a LiveTourPanel that reads its props from a TourPanelStore, and useTour pushes the current step's props into it whenever its steps or options change. Focus, scroll and the step callbacks are untouched, and the panel's DOM is kept.
…mount The unmount cleanup tore the tour down synchronously, so under StrictMode the simulated unmount closed a tour started from a guarded mount effect, and the remount never reopened it. The teardown - including destroying the backdrop, highlight and target watcher - now runs a microtask later and is skipped when the hook has mounted again. A real unmount still ends the tour.
React sets `muted` only as a property, while 1.3.0's HTML had the attribute, which iOS Safari's autoplay policy reads. A muted video now also gets the `muted` attribute and `defaultMuted` through a ref callback.
`body: ReactNode` accepted null and undefined, so a step could satisfy the "exactly one of text, body or content" union with no body at all. It is now `NonNullable<ReactNode> | ((step) => ReactNode)`. A body is present when it is not undefined, everywhere: an `undefined` body spread onto a `content` step - legal for the `body?: never` variant - no longer forces the panel the way `'body' in step` did.
The body now scrolls, which stops margins collapsing through it, so a body of paragraphs (rendered markdown) showed its first and last margins as extra space inside the panel.
README gains "Upgrading to 1.4": `tooltip.content` is null while a tour panel shows, a custom `progress.render` sits in a `display: contents` wrapper, and the other observable behavior changes. CLAUDE.md now says a `content` step without panel features is forced to `html: true` and a `body` step always renders the panel.
|
AI review (Claude
Gate: |
All steps shared one TourPanelStore. Setting it re-renders the subscribed panel synchronously, while the dispatch that swaps in the next step's keyed panel can land in a later render - a navigation inside startTransition, for one - so the outgoing panel briefly rendered the next step's body, remounting or re-rendering a component in the wrong panel. Every shown step now gets a fresh store, passed only to its own LiveTourPanel.
The in-place update matched the shown step by index, so when `steps` shifted - a condition filtering out an earlier step - the panel showed another step's content at the old target, and steps shrinking below the shown index left a stuck panel. A running tour now follows the shown step by its target: - still there, at the same or a new index: updated in place, the index moved without firing callbacks; - gone, with a step at its place: moved on as a navigation (onExit, onEnter, onStepChange); - gone, past the end: the tour ends with onEnd(false). An in-place update also re-shows the tooltip when a panel-derived option changed - a title appearing now sets aria-labelledby - and onExit is called on the step actually on screen rather than whatever `steps` holds at its index. Each presented panel gets a unique key, so a moved index cannot reuse a panel.
…ount The deferred unmount teardown hid the tooltip and ended the helper flow unconditionally. When a route change swapped one touring component for another whose mount effect starts a tour, the first one's teardown ran after the second had started, and took the second tour's panel and flow down. HIDE_TOOLTIP and END_FLOW take an optional ownership check (`onlyIfContent`, `onlyIfSteps`), and the unmount teardown passes the content it showed and the flow steps it started. The tour's own backdrop, highlight and watcher are still cleaned up.
…nent With the unmount teardown deferred, a target rendered inside the unmounting component detached first: the target watcher's MutationObserver reported it, and handleTargetLost ended the tour with onTargetMissing (or a console warning), onExit and onEnd(false). The unmount cleanup is now a layout effect that clears a mounted flag during the unmount commit, before the observer's callback, and handleTargetLost ignores a lost target once it is clear. StrictMode's simulated unmount sets it again on remount, so the watcher keeps working there.
`body` accepted booleans and '', and a function body could return null or undefined - all render nothing, yet the step still got the panel: an empty dialog that took focus. `body` no longer accepts a boolean, and at runtime a body that resolves to null, undefined, a boolean or '' is treated as no body, so the step follows the same path as one with no text or content. The panel decision now looks at the resolved body.
…a panel Forcing `html: true` for a `content` step with no panel features overrode an explicit `tooltipOptions.html: false` at the tour or step level, which a 1.3 consumer may rely on to show an untrusted string as text - injecting it. HTML is now the default for `content` there, and an explicit `false` wins.
The ref that wrote the `muted` attribute only ever added it, so a video whose `muted` went from true to false on the same src kept the attribute. The ref now sets `defaultMuted` either way; it reflects the `muted` attribute (checked in jsdom and Chromium), which also makes the separate setAttribute redundant.
|
AI review (Claude
That ends the review loop for this PR (review, fix, review the fixes, fix). Gate: |
…red targets A running step was followed by `target` alone. With two steps on the same element, inserting a step ahead of them left the same target at the shown index, so the panel silently showed the other step - no onExit, no onStepChange - and findIndex picked the earlier one when the target did move. TourStep gains an optional `id`. When the order of the visible steps changed, the step on screen is found again by its `id`, else by its `target` only when no other step shares it. A step that cannot be told apart is treated like a removed one: the step now at its place is shown as a navigation, with its callbacks. An unchanged order keeps the in-place update, so inline `steps` re-created on every render cannot loop.
The unmount teardown released the tooltip only if it still held the content this tour showed. A plain-text step's content is a string, so two tours showing the same text compared equal: when a route swap mounted a tour that starts in a layout effect - before the outgoing tour's deferred teardown - the outgoing tour hid it. `tooltip.show` takes an optional `owner` object, kept on the tooltip state until it is next shown or hidden, and HIDE_TOOLTIP's `onlyIfContent` becomes `onlyIfOwner`. useTour mints a fresh owner per show. The new test fails with the ownership check removed, as does the existing route-swap test.
…close button A `body` step always renders the panel, a role="dialog" that takes focus; with no title nothing named it. The panel is now labelled by its title or, without one, by its message element, which gets a generated id. The embed iframe had no title: it takes `video.title`, defaulting to "Tour video". The close button's fixed "Close" label is now `navigation.closeLabel`, defaulting to "Close", alongside the configurable Next/Back/Finish labels.
…r flow in step An in-place update re-showed the tooltip only when aria-labelledby or autoFocus changed, so a new `placement`, `maxWidth`, `showArrow` or other tooltip option on the step on screen was ignored. All shown options are now compared by value (`haveSameTooltipOptions`, ignoring content and owner) and the tooltip is shown again when any changed - still loop-safe, since equal values do not show. A plain-tooltip step's text is now updated in place too. The helper flow kept the `start()` snapshot of the steps, so after [A, B, C] -> [B, C] `helper.currentStep` was A while the tour showed B. The flow is rebuilt from the visible steps when they differ by value, and moved to the followed index.
A first step whose body renders nothing and that has no other panel feature was handed to the tooltip as '', which tooltip.show ignores - after start() had already set isActive and called onStart. start() now returns false, with no state change and no onStart, when the first renderable step has nothing to show. What counts as nothing is now what React renders no nodes for: null, undefined, a boolean, '', and fragments or arrays of only those, recursively. `body: <></>` and `body: []` no longer open an empty dialog.
The upgrade notes now say that TooltipState.content and the SHOW/MOVE payloads widened from string to ReactNode (exported context types), that UPDATE_TOOLTIP_CONTENT / tooltip.updateContent stay string and replace a running tour's panel, that CurrentTourStep.content is '' for a `body` step, that an untitled panel is labelled by its message, that start() returns false for a first step with nothing to show, and that the new owner/ownership payloads are additive.
|
External review (Cursor, Grok 4.7 High; focus on KISS, open-source practice and common sense): findings and resolution. Verdict: "ship after fixes". The reviewer judged the panel store, the per-step store, the deferred teardown, the mounted flag and the click delegation justified, and they stay. Each finding was reproduced at 0a3ebef by a test that failed before the fix.
Gate: Known gap, pre-existing and out of scope: an empty step reached later by |
…& Tours The two stories were their own 'The Tourtip/...' entries, which turned The Tourtip into a sidebar group, and were built with ad-hoc markup and styles. They are now the Auto Focus and React Body stories of Tooltip/Flow & Tours, built like the rest of that group: the MockDashboard, the tour-demo classes, in-demo buttons instead of controls, and a description in the group's style. Their stylesheet blocks are gone. Flows.mdx links to both, and Welcome.mdx's link to the tour playground points at its real story id.
An announcement is not about any element, so `TourStep.target` is now optional. A step without one opens centered in the viewport - fixed, so it stays centered as the page scrolls or resizes - with no arrow and the focus backdrop on by default, with nothing cut out of it. A tour- or step-level `focus` still turns the backdrop off or makes it dismissible. Everything else is the same panel: title, media, body, navigation, progress, autoFocus, Escape and the accessible name. With no target there is nothing to highlight, scroll to or watch, so onTargetMissing never fires for it; following a changed `steps` finds such a step by its `id`, or by position. Centered and anchored steps mix in one tour. Underneath, `TooltipShowOptions` gains `centered`: Tooltip then positions with a `centerInViewport` middleware and the `fixed` strategy, drops the arrow, and never animates a move between centered and anchored. `CurrentTourStep.target` becomes optional with it.
…wording The step now shows the image the Tour With Images story opens with, above its body, and its button reads "Show announcement" / "Showing announcement…" rather than the multi-step tours' "Tour in Progress...", since it is an announcement, not a tour.
A one-step tour with no target: it opens centered over a dimmed page, with a title, an image, a long React body that scrolls, and a "Got it" button, started from the demo's button. Built like the other Flow & Tours stories.
…the viewport A centered step (no target) with an image or a native video now takes its width from the media: its natural width, capped at --tip-magic-tour-centered-max-width (70vw) and an explicit maxWidth, with the title, body and footer wrapping inside it. The media keeps its aspect ratio and is never cropped: when it would leave too little height for the text (--tip-magic-tour-media-reserved-height) it is scaled down uniformly. The panel is never narrower than --tip-magic-tour-min-width (280px), and a smaller image is centered at its natural size. The panel stays hidden until the media's size is known (or it fails to load), so it does not open narrow and widen. Every centered panel is at most --tip-magic-tour-centered-max-height (80vh, 80dvh where supported) tall; its body scrolls past that. Done in CSS - fit-content width, `contain: inline-size` on everything but the media, and viewport-unit max sizes on the media - with `maxWidth: 'none'` on the tooltip for such a step: TooltipShowOptions.maxWidth and ParsedTooltipData.maxWidth now accept 'none'. Embeds, steps without media and anchored steps are unchanged.
The story gains buttons to show its announcement with the 400x200 image, a wide 1200x600 one, a portrait 400x700 one and a tiny 160x90 one, and describes the sizing rule.
A centered announcement can be just a video, with no title or text. The step
content union gains a media-only variant: no `text`, `body` or `content`, but
an `image` or a `video` (whose type is now exported as `TourVideo`). A step with
nothing to show still does not compile.
Such a step renders the panel like any other, and with no title and no text
its dialog is labelled by the video, which carries `aria-label` from
`video.title` (default "Tour video", for native videos as well as embeds). A
panel that is nothing but its media (no title, text or footer) reserves only
--tip-magic-tour-media-only-reserved-height below it, so the media may use
nearly all of the 80% height. Without buttons it can be closed by Escape, or by
a backdrop click with `focus: { dismissOnClick: true }`; a click on the media
itself does not close it.
…n Announcement Video Announcement is a centered step that is only a wide native video (Big Buck Bunny, 1920x1080) with no title, text or buttons: a click outside it or Escape closes it. The Announcement story now passes autoFocus: 'primary', as Hermes will, so Enter dismisses it.
…side A media-only panel showed 10px beside its video but 18px above and below it: the media's own 12px margins stacked on the panel padding, and an empty body followed it. The body is no longer rendered when there is no message, the panel's first and last parts drop their outer margin (as the message's children do), and media that is the panel's only part gets the difference between the horizontal and vertical padding above and below it - 10px on all four sides with the default theme.
With size() capping the panel at the room beside its target, an anchored panel near a viewport edge was clamped to a height smaller than its title, media and footer, which then overflowed the panel's box; because the panel fit, flip() never moved it. The panel content now has `min-height: min-content`: only the body gives way, and below that the panel overflows its side, so flip() places it where it fits (the Tour With Images story's first step moves beside the sidebar). Layout only, so verified in Chromium rather than jsdom.
…r flipping 581c287's `min-height: min-content` kept a squeezed panel's media inside it, but it also counted the body's whole text, so a long body stopped shrinking to the room beside the target and the panel ran off the viewport. Instead the panel's height cap never drops below --tip-magic-tour-min-height (240px): the body still gives way down to that, and with less room than that the panel overflows its side, which is what makes flip() move it. Checked in Chromium: the Tour With Images first step still flips beside the sidebar, and a long React body at 600x800 fits the viewport again.
`mediaPlacement: 'top' | 'left' | 'right'` (TourOptions, overridable per step; type TourMediaPlacement) defaults to 'top', the existing layout. Beside the text, the panel is a two-column grid: the media in one column, and in the other the title, the body - which takes the height left and scrolls - and the footer at the bottom. The close button sits at the panel's top-right corner, with a backdrop of its own when it is over the media. The text column takes up to half of the panel, between --tip-magic-tour-min-width (280px) and --tip-magic-tour-side-text-max-width (400px), with the panel padding as the gap; the media gets the rest at its aspect ratio and is never taller than the panel may be. A centered panel stays within --tip-magic-tour-centered-max-width and -max-height, an anchored one within the viewport and the room beside its target, and both within an explicit maxWidth; the tooltip's default 300px cap is lifted otherwise. Below a 640px viewport the media goes back on top (a media query), and a step with no media, or nothing but media, ignores the option.
Description
1.4.0 renders the tour panel in React, so a step can carry React content: a new
body: ReactNode. It also fixes four bugs in the panel.Logz.io's Hermes shows an announcement as a one-step tour whose body is markdown rendered by a React component. 1.3.0 built the panel as an HTML string and injected it, so that wasn't possible. See logzio/Artemis#898 and ORIONIQ-2232.
Type of Change
textandcontentsteps render as before. The type ofTooltipState.contentand theTipMagicActionpayloads widens fromstringtoReactNode, and both are visible throughuseTipMagicContext. Code that readsstate.tooltip.contentas a string would stop compiling. That's internal state, so this ships as a minor.Changes Made
TourPanel(src/hooks/useTour/TourPanel.tsx), replacing the HTML string builder.data-tour-action,data-tip-magic-primary,tip-magic-tour-*), so the delegated clicks,autoFocusand the CSS are unchanged.autoFocus: 'panel'broke.text(plain text);body: ReactNode | (step) => ReactNode(new);content(HTML, injected into the message element only, as before).TooltipShowOptions.contentaccepts aReactNode.htmlstill applies to strings only.bodyrenders insideTipMagicProvider's context, so any provider it needs (theme, router) must sit above the provider. This is documented in the README, Flows.mdx and CLAUDE.md.textescaped twice when the step had no panel features;nextLabel,backLabelandfinishLabelwere inserted as markup;onEndoronExit..tip-magic-tour-bodyhas a max height ofvar(--tip-magic-tour-body-max-height, 60vh)and scrolls;size()publishes--tip-magic-available-height, which only the tour panel reads, so plain tooltips are never clamped.Testing
npm run validatewith 390 tests;npm run buildandnpm run build:storybookpass.useTour.integration.test.tsx, extended).size()keeps the panel inside the viewport;onClickin the body works, and Escape ends the tour;flipwithsize()stays stable while scrolling or resizing.Additional Notes
:has()show a 20px header-to-media gap instead of 12px.tsconfig.build.jsonalso emits declarations for*.test.tsx.