-
Notifications
You must be signed in to change notification settings - Fork 1
fix: prevent user from exiting interactive mode while in Theme mode and hide sidebar breadcrumbs #1073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix: prevent user from exiting interactive mode while in Theme mode and hide sidebar breadcrumbs #1073
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
bfa986f
feat: hide right sidebar title in Theme mode
jwartofsky-yext f8cd07e
cleaned up code
jwartofsky-yext 8632d61
prevent leaving interactive mode while in theme editor
jwartofsky-yext b7e46f8
Update component screenshots for visual-editor
github-actions[bot] 2ee2599
adjust comment
jwartofsky-yext e60adac
re-add block pointer events
jwartofsky-yext b275bb0
add cleanup and retry logic to pointer-blocking
jwartofsky-yext 1348882
update screenshot to main
jwartofsky-yext 8f9959e
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext 9d1d607
prevent clicking links in Theme mode
jwartofsky-yext 10b2dc1
move link blocking code
jwartofsky-yext 0f00c6a
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext 25d8ee8
move interactiveMode useEffect to InternalThemeEditor
jwartofsky-yext 27cb375
move interactiveMode useEffect back to ThemeHeader
jwartofsky-yext 9b2ad44
add jsdocs to previewFrameLinkBlocker
jwartofsky-yext 0da19b6
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext a5a720f
address coderabbit comments and retest
jwartofsky-yext e0c6298
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext 3ef54d4
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext 9ef9ef0
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext 03df53d
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext e263964
add actionable check to language dropdown
jwartofsky-yext 2b6085e
remove langauge dropdown changes. Add checks to prevent interacting w…
jwartofsky-yext 1b44c5c
revert language dropdown to main
jwartofsky-yext 1e9bfe7
add random AI comment to trigger build again. Maybe it's helpful too
jwartofsky-yext 84f04f5
Merge branch 'main' into fixJSONInThemeEditor
jwartofsky-yext File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
packages/visual-editor/src/internal/utils/previewFrameLinkBlocker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { PUCK_PREVIEW_IFRAME_ID } from "../../utils/applyTheme.ts"; | ||
|
|
||
| // Selector for interactive elements that should have their default navigation behavior blocked in preview mode, while still allowing hover/focus styles to apply. | ||
| const NON_ACTIONABLE_SELECTOR = [ | ||
| "a", | ||
| "area", | ||
| "[role='link']", | ||
| "[role='menuitem']", | ||
| "[data-slot='dropdown-menu-trigger']", | ||
| "[data-slot='dropdown-menu-item']", | ||
| ].join(", "); | ||
|
|
||
| /** | ||
| * Installs link-navigation blocking in a specific preview document while | ||
| * preserving hover and focus behavior. | ||
| * | ||
| * @param previewDocument - The iframe document that renders theme preview content. | ||
| * @returns A cleanup function that restores original link attributes and removes listeners/observers. | ||
| */ | ||
| export const createPreviewDocumentLinkBlocker = (previewDocument: Document) => { | ||
| const previewWindow = previewDocument.defaultView; | ||
|
|
||
| /** | ||
| * Returns true when the event target (or one of its ancestors) is a link-like | ||
| * element or a dropdown menu item/trigger that should not be actionable in preview. | ||
| */ | ||
| const isBlockedTarget = (event: Event) => { | ||
| const targetElement = event.target; | ||
| if ( | ||
| !targetElement || | ||
| typeof (targetElement as Element).closest !== "function" | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| return !!(targetElement as Element).closest(NON_ACTIONABLE_SELECTOR); | ||
| }; | ||
|
|
||
| /** | ||
| * Cancels action-triggering events for blocked interactive targets. | ||
| * Keyboard events are only blocked for activation keys. | ||
| */ | ||
| const preventLinkNavigation = (event: Event) => { | ||
| if (!isBlockedTarget(event)) { | ||
| return; | ||
| } | ||
|
|
||
| if (event.type === "keydown") { | ||
| const key = (event as KeyboardEvent).key; | ||
| if (key !== "Enter" && key !== " ") { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| event.stopImmediatePropagation(); | ||
| }; | ||
|
|
||
| /** | ||
| * Removes href/target from links so the browser cannot navigate by default. | ||
| * Original values are stored in data attributes for later restoration. | ||
| */ | ||
| const disableAnchorNavigationAttributes = () => { | ||
| previewDocument.querySelectorAll("a[href], area[href]").forEach((el) => { | ||
| if (!el.hasAttribute("data-ve-original-href")) { | ||
| el.setAttribute("data-ve-original-href", el.getAttribute("href") ?? ""); | ||
| } | ||
|
|
||
| el.removeAttribute("href"); | ||
|
|
||
| if (el.hasAttribute("target")) { | ||
| el.setAttribute( | ||
| "data-ve-original-target", | ||
| el.getAttribute("target") ?? "" | ||
| ); | ||
| el.removeAttribute("target"); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| disableAnchorNavigationAttributes(); | ||
| const anchorObserver = new MutationObserver(() => { | ||
| disableAnchorNavigationAttributes(); | ||
| }); | ||
| anchorObserver.observe(previewDocument.documentElement, { | ||
| childList: true, | ||
| subtree: true, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const eventTypes = [ | ||
| "click", | ||
| "auxclick", | ||
| "mousedown", | ||
| "mouseup", | ||
| "pointerdown", | ||
| "pointerup", | ||
| "touchstart", | ||
| "keydown", | ||
| ] as const; | ||
|
|
||
| eventTypes.forEach((eventType) => { | ||
| previewDocument.addEventListener(eventType, preventLinkNavigation, true); | ||
| previewWindow?.addEventListener(eventType, preventLinkNavigation, true); | ||
| }); | ||
|
|
||
| return () => { | ||
| anchorObserver.disconnect(); | ||
|
|
||
| previewDocument | ||
| .querySelectorAll("a[data-ve-original-href], area[data-ve-original-href]") | ||
| .forEach((el) => { | ||
| const originalHref = el.getAttribute("data-ve-original-href"); | ||
| if (originalHref !== null) { | ||
| el.setAttribute("href", originalHref); | ||
| } | ||
| el.removeAttribute("data-ve-original-href"); | ||
|
|
||
| if (el.hasAttribute("data-ve-original-target")) { | ||
| const originalTarget = el.getAttribute("data-ve-original-target"); | ||
| if (originalTarget !== null) { | ||
| el.setAttribute("target", originalTarget); | ||
| } | ||
| el.removeAttribute("data-ve-original-target"); | ||
| } | ||
| }); | ||
|
|
||
| eventTypes.forEach((eventType) => { | ||
| previewDocument.removeEventListener( | ||
| eventType, | ||
| preventLinkNavigation, | ||
| true | ||
| ); | ||
| previewWindow?.removeEventListener( | ||
| eventType, | ||
| preventLinkNavigation, | ||
| true | ||
| ); | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Watches the preview iframe lifecycle and applies link-navigation blocking | ||
| * to whichever preview document is currently active. | ||
| * | ||
| * @returns A cleanup function that detaches iframe observers and listeners. | ||
| */ | ||
| export const createPreviewFrameLinkBlocker = () => { | ||
| let detachLinkNavigationBlock: (() => void) | undefined; | ||
| let activeFrame: HTMLIFrameElement | null = null; | ||
|
|
||
| /** | ||
| * Rebinds document-level blockers when the iframe loads a new document. | ||
| */ | ||
| const onPreviewFrameLoad = () => { | ||
| detachLinkNavigationBlock?.(); | ||
| if (activeFrame?.contentDocument) { | ||
| detachLinkNavigationBlock = createPreviewDocumentLinkBlocker( | ||
| activeFrame.contentDocument | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Attaches blocker behavior to a new iframe element and detaches from any | ||
| * previously tracked iframe. | ||
| */ | ||
| const attachToPreviewFrame = (frame: HTMLIFrameElement) => { | ||
| if (activeFrame === frame) { | ||
| return; | ||
| } | ||
|
|
||
| activeFrame?.removeEventListener("load", onPreviewFrameLoad); | ||
| detachLinkNavigationBlock?.(); | ||
|
|
||
| activeFrame = frame; | ||
| activeFrame.addEventListener("load", onPreviewFrameLoad); | ||
| if (activeFrame.contentDocument) { | ||
| detachLinkNavigationBlock = createPreviewDocumentLinkBlocker( | ||
| activeFrame.contentDocument | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Locates the preview iframe and ensures blocker behavior is attached. | ||
| */ | ||
| const syncPreviewFrame = () => { | ||
| const previewFrame = document.getElementById( | ||
| PUCK_PREVIEW_IFRAME_ID | ||
| ) as HTMLIFrameElement | null; | ||
| if (!previewFrame) { | ||
| return; | ||
| } | ||
|
jwartofsky-yext marked this conversation as resolved.
|
||
|
|
||
| attachToPreviewFrame(previewFrame); | ||
| }; | ||
|
|
||
| const iframeObserver = new MutationObserver(syncPreviewFrame); | ||
| iframeObserver.observe(document, { | ||
| childList: true, | ||
| subtree: true, | ||
| attributes: true, | ||
| attributeFilter: ["href", "target"], | ||
| }); | ||
|
|
||
| syncPreviewFrame(); | ||
|
|
||
| return () => { | ||
| iframeObserver.disconnect(); | ||
| activeFrame?.removeEventListener("load", onPreviewFrameLoad); | ||
| detachLinkNavigationBlock?.(); | ||
| activeFrame = null; | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.