diff --git a/docs/RELEASE_CHECKLIST.md b/docs/RELEASE_CHECKLIST.md index 3861126..36812ef 100644 --- a/docs/RELEASE_CHECKLIST.md +++ b/docs/RELEASE_CHECKLIST.md @@ -77,6 +77,7 @@ Test on Chrome Stable, Firefox Stable, Edge Stable, and current Arc. Test Safari ### Compatibility and quality - [ ] Test a conventional multi-page site, a React/Vue SPA, Yahoo, Hacker News, and a page with open Shadow DOM. +- [ ] Serve the [dialog fixture](fixtures/dialog-annotations.html) over localhost. For each case, select the target, click and type in the note field, then save. In dialog cases, verify the page's dialog stays open. - [ ] On a blocked local HTML file in Chrome, choose **Allow local files**, approve Chrome's toggle, return to the file, and confirm App Notes reloads it automatically. - [ ] Confirm local notes default to **This file**; switch to **This folder** and verify sibling-file notes appear while a different folder remains isolated. - [ ] Restricted browser pages fail gracefully without broken controls. diff --git a/docs/fixtures/dialog-annotations.html b/docs/fixtures/dialog-annotations.html new file mode 100644 index 0000000..be326ca --- /dev/null +++ b/docs/fixtures/dialog-annotations.html @@ -0,0 +1,58 @@ + + + +App Notes dialog regression fixture + + +

Dialog annotation test

+

Choose a case, enable annotation mode, then select the button and add a note.

+ +

+ + + diff --git a/entrypoints/content/App.tsx b/entrypoints/content/App.tsx index 317407e..f9d416b 100644 --- a/entrypoints/content/App.tsx +++ b/entrypoints/content/App.tsx @@ -6,6 +6,7 @@ import { ElementHighlight } from './components/ElementHighlight'; import { ExitWarningToast } from './components/ExitWarningToast'; import { OverlayStyles } from './components/OverlayStyles'; import type { ContentEventBridge } from './event-bridge'; +import type { OverlayHost } from './overlay-host'; import { createAnnotationAnchor, deepElementFromPoint, @@ -56,7 +57,7 @@ interface ParkedDraft { interface ContentAppProps { readonly eventBridge: ContentEventBridge; - readonly getShadowHost: () => HTMLElement | null; + readonly overlay: OverlayHost; } function createPageContext(href: string): PageContext { @@ -94,8 +95,14 @@ function rectChanged(previous: DOMRect, next: DOMRect): boolean { || previous.height !== next.height; } +function getFocusedElement(): HTMLElement | null { + let element = document.activeElement; + while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement; + return element instanceof HTMLElement ? element : null; +} + /** Owns the page picker, draft lifecycle, and current-page annotation snapshot. */ -export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { +export function ContentApp({ eventBridge, overlay }: ContentAppProps) { const [active, setActive] = useState(false); const [hoveredRect, setHoveredRect] = useState(null); const [hoveredLabel, setHoveredLabel] = useState(''); @@ -120,9 +127,10 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { const clearHover = useCallback(() => { hoveredElementRef.current = null; + if (!selectedRef.current) overlay.setTarget(null); setHoveredRect(null); setHoveredLabel(''); - }, []); + }, [overlay]); const clearExitWarning = useCallback(() => { exitArmedRef.current = false; @@ -195,9 +203,8 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { }, [requestExitDraft]); const isEventInsideOverlay = useCallback((event: Event): boolean => { - const host = getShadowHost(); - return host !== null && event.composedPath().includes(host); - }, [getShadowHost]); + return event.composedPath().includes(overlay.host); + }, [overlay]); const loadAnnotationsForPage = useCallback(async (page: PageContext) => { const generation = ++loadGenerationRef.current; @@ -259,16 +266,17 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { pageUrl: pageRef.current.href, pageTitle: getCurrentPageTitle(pageRef.current.href), rect, - returnFocus: document.activeElement instanceof HTMLElement ? document.activeElement : null, + returnFocus: getFocusedElement(), storageKey: pageRef.current.storageKey, }; selectedRef.current = nextSelection; + overlay.setTarget(element); setSelected(nextSelection); updateDraft(''); clearExitWarning(); clearHover(); - }, [clearExitWarning, clearHover, updateDraft]); + }, [clearExitWarning, clearHover, overlay, updateDraft]); useEffect(() => { loadAnnotationsForPage(pageRef.current).catch(() => undefined); @@ -315,6 +323,7 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { } if (element !== hoveredElementRef.current) { + overlay.setTarget(element); hoveredElementRef.current = element; setHoveredRect(element.getBoundingClientRect()); setHoveredLabel(identifyElement(element)); @@ -417,6 +426,7 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { eventBridge, isEventInsideOverlay, openEditorForElement, + overlay, requestExitDraft, requestExitDraftNearSelection, setAnnotationMode, @@ -436,8 +446,13 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { element = resolveAnnotationAnchor(currentSelection.anchor) ?? element; } - if (element.isConnected) { - const nextRect = element.getBoundingClientRect(); + const nextRect = element.getBoundingClientRect(); + if (!element.isConnected || nextRect.width === 0 || nextRect.height === 0) { + // A page may dismiss its dialog while the user is writing. Keep the draft + // visible at its last position rather than trapped in the hidden dialog. + overlay.setTarget(null); + } else { + overlay.setTarget(element); if (rectChanged(currentSelection.rect, nextRect) || element !== currentSelection.element) { const nextSelection = { ...currentSelection, element, rect: nextRect }; selectedRef.current = nextSelection; @@ -463,6 +478,7 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { rect: element.getBoundingClientRect(), }; selectedRef.current = restoredSelection; + overlay.setTarget(element); setSelected(restoredSelection); updateDraft(parkedDraft.note); clearHover(); @@ -478,6 +494,7 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { return; } + overlay.setTarget(element); const rect = element.getBoundingClientRect(); if (element !== hoveredElementRef.current) { hoveredElementRef.current = element; @@ -509,7 +526,7 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) { layoutFrameRef.current = null; } }; - }, [active, clearHover, updateDraft]); + }, [active, clearHover, overlay, updateDraft]); useEffect(() => { const handleMessage = (message: unknown) => { diff --git a/entrypoints/content/index.tsx b/entrypoints/content/index.tsx index 0ae7fae..a2c6455 100644 --- a/entrypoints/content/index.tsx +++ b/entrypoints/content/index.tsx @@ -2,6 +2,7 @@ import { createRoot } from 'react-dom/client'; import { flushSync } from 'react-dom'; import { ContentApp } from './App'; import { createContentEventBridge } from './event-bridge'; +import { createOverlayHost } from './overlay-host'; const FIREWALLED_EVENTS = [ 'click', @@ -41,6 +42,10 @@ export default defineContentScript({ inset: '0', width: '100vw', height: '100vh', + margin: '0', + padding: '0', + border: '0', + background: 'transparent', zIndex: '2147483640', pointerEvents: 'none', overflow: 'visible', @@ -68,9 +73,10 @@ export default defineContentScript({ host.addEventListener(eventName, stopPagePropagation); } + const overlay = createOverlayHost(host, documentElement); const root = createRoot(container); flushSync(() => { - root.render( host} />); + root.render(); }); ctx.onInvalidated(() => { diff --git a/entrypoints/content/overlay-host.ts b/entrypoints/content/overlay-host.ts new file mode 100644 index 0000000..ae1c1a2 --- /dev/null +++ b/entrypoints/content/overlay-host.ts @@ -0,0 +1,57 @@ +const DIALOG_SELECTOR = 'dialog[open], [role="dialog"]:not(dialog), [role="alertdialog"]:not(dialog)'; + +function composedParent(element: Element): Element | null { + if (element.assignedSlot) return element.assignedSlot; + if (element.parentElement) return element.parentElement; + const root = element.getRootNode(); + return root instanceof ShadowRoot ? root.host : null; +} + +function findOverlayLayer(target: Element | null, supportsPopover: boolean): HTMLElement | null { + if (!target?.isConnected) return null; + + for (let current: Element | null = target; current; current = composedParent(current)) { + if (!(current instanceof HTMLElement)) continue; + if (current.matches(DIALOG_SELECTOR) || (supportsPopover && current.matches(':popover-open'))) { + return current; + } + } + return null; +} + +export interface OverlayHost { + readonly host: HTMLElement; + readonly setTarget: (target: Element | null) => void; +} + +export function createOverlayHost(host: HTMLElement, documentElement: HTMLElement): OverlayHost { + const supportsPopover = typeof host.showPopover === 'function'; + + return { + host, + setTarget(target) { + const layer = findOverlayLayer(target, supportsPopover); + const parent = layer ?? documentElement; + if (host.parentElement === parent) return; + + // Reuse the shadow root and React tree so moving between layers doesn't reset the draft. + const focused = host.shadowRoot?.activeElement; + host.removeAttribute('popover'); + parent.append(host); + + // A z-index can't raise the overlay above a native top-layer element. The host + // also has to live inside a modal to avoid its inert area (and custom focus traps). + if (layer && supportsPopover) { + host.setAttribute('popover', 'manual'); + try { + host.showPopover(); + } catch { + // Leave the inline overlay usable if the browser can't promote it. + host.removeAttribute('popover'); + } + } + + if (focused instanceof HTMLElement) focused.focus({ preventScroll: true }); + }, + }; +}