Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/RELEASE_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 58 additions & 0 deletions docs/fixtures/dialog-annotations.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>App Notes dialog regression fixture</title>
<style>
body { font: 16px system-ui, sans-serif; }
#layer { width: 310px; min-height: 210px; padding: 18px; border: 2px solid; }
#layer[data-case="custom"] {
position: fixed;
top: 25%;
left: 30%;
background: white;
z-index: 2147483647;
}
</style>

<h1>Dialog annotation test</h1>
<p>Choose a case, enable annotation mode, then select the button and add a note.</p>
<nav aria-label="Test cases">
<a href="?case=page">Regular page</a> ·
<a href="?case=modal">Native modal</a> ·
<a href="?case=shadow">Modal in Shadow DOM</a> ·
<a href="?case=popover">Native popover</a> ·
<a href="?case=custom">Custom dialog</a>
</nav>
<p><button id="outside">Regular page target</button></p>

<script>
const scenario = new URLSearchParams(location.search).get('case');
if (['modal', 'shadow', 'popover', 'custom'].includes(scenario)) {
const wrapper = document.body.appendChild(document.createElement('div'));
const root = scenario === 'shadow' ? wrapper.attachShadow({ mode: 'open' }) : wrapper;
if (root !== wrapper) root.append(document.querySelector('style').cloneNode(true));

const nativeModal = scenario === 'modal' || scenario === 'shadow';
const layer = document.createElement(nativeModal ? 'dialog' : 'div');
layer.id = 'layer';
layer.dataset.case = scenario;
layer.innerHTML = '<button id="inside">Target in ' + scenario + '</button>';
root.append(layer);

if (nativeModal) layer.showModal();
if (scenario === 'popover') {
layer.popover = 'auto';
layer.showPopover();
}
if (scenario === 'custom') {
layer.setAttribute('role', 'dialog');
layer.setAttribute('aria-modal', 'true');
const target = layer.querySelector('button');
document.addEventListener('focusin', event => {
if (!layer.contains(event.target)) target.focus();
});
target.focus();
}
}
</script>
</html>
39 changes: 28 additions & 11 deletions entrypoints/content/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,7 +57,7 @@ interface ParkedDraft {

interface ContentAppProps {
readonly eventBridge: ContentEventBridge;
readonly getShadowHost: () => HTMLElement | null;
readonly overlay: OverlayHost;
}

function createPageContext(href: string): PageContext {
Expand Down Expand Up @@ -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<DOMRect | null>(null);
const [hoveredLabel, setHoveredLabel] = useState('');
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -417,6 +426,7 @@ export function ContentApp({ eventBridge, getShadowHost }: ContentAppProps) {
eventBridge,
isEventInsideOverlay,
openEditorForElement,
overlay,
requestExitDraft,
requestExitDraftNearSelection,
setAnnotationMode,
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion entrypoints/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -68,9 +73,10 @@ export default defineContentScript({
host.addEventListener(eventName, stopPagePropagation);
}

const overlay = createOverlayHost(host, documentElement);
const root = createRoot(container);
flushSync(() => {
root.render(<ContentApp eventBridge={eventBridge} getShadowHost={() => host} />);
root.render(<ContentApp eventBridge={eventBridge} overlay={overlay} />);
});

ctx.onInvalidated(() => {
Expand Down
57 changes: 57 additions & 0 deletions entrypoints/content/overlay-host.ts
Original file line number Diff line number Diff line change
@@ -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 });
},
};
}