Skip to content

feat(preview): block link navigation in live-preview routes #46

Closed

Description

@Ardnived

Summary

When an editor is working in the SahajCloud live-preview iframe (/preview and /preview/embed), clicking any link inside the rendered content navigates the iframe away from the document being edited — breaking the live-preview session and forcing the editor to reopen the preview. We want preview mode to be read-only for navigation: every link on screen should be visually present but inert, so editors can scroll and read without accidentally leaving the previewed page.

Background

Preview routes render the same components as the live site — rich-text links and inline relationships go through the Link atom (RichText.tsx:120-166), and the full-chrome /preview route also renders nav/header/footer links via LayoutChrome. Because links come from many components (Link atom, Button-as-link, Breadcrumbs, social icons, cards), the robust fix is a single capture-phase click interceptor scoped to the preview tree, rather than threading a "disabled" prop through every component.

Proposed changes

1. Intercept anchor clicks within the preview subtree

Add a capture-phase click handler in the Preview component (covering both /preview and /preview/embed, which both render <Preview>). The handler finds the nearest <a> ancestor of the event target via event.target.closest('a') and, if one exists, calls event.preventDefault() so navigation is blocked. Middle-click / cmd-click open-in-new-tab should also be suppressed (handle via auxclick or by clearing on capture).

2. Keep the rest of the page interactive

Only <a> navigation is blocked. Buttons and media controls (meditation play/pause, captions, etc.) must keep working, since those drive the preview itself (e.g. PLAYBACK_TIME_UPDATE). Do not block <button> elements.

3. Allow same-page anchor links; block everything else

Same-page anchor links (href starting with #, e.g. table-of-contents jumps) must continue to work so editors can scroll within the document. Block every other <a> click — internal route links, external (target="_blank"), etc. Detect via the anchor's getAttribute('href') (or comparing pathname to the current location) so a #heading link is left alone while a real navigation is prevented.

Acceptance criteria

  • In /preview (full chrome), clicking a rich-text link, a nav link, a card, or a breadcrumb does not navigate; the URL bar / iframe stays on the preview document
  • In /preview/embed, the same links are inert
  • Same-page anchor links (href="#...") still scroll to their target within the preview
  • Cmd/Ctrl-click and middle-click on a (non-anchor) link do not open a new tab
  • Buttons and media controls (play/pause, captions, embed button) still function
  • The live useLivePreview content updates and postMessage seek sync are unaffected
  • No effect on the normal site — non-preview routes navigate exactly as before
  • pnpm lint, pnpm exec tsc --noEmit, pnpm test:run all pass

Files affected

  • pages/preview/_components/Preview.tsx — add capture-phase click/auxclick interceptor (likely via a small useEffect wrapper or a <div onClickCapture> container)
  • (maybe) pages/preview/_components/index.ts — if the interceptor is extracted to a small helper/hook

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions