Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Layer): a layer that keeps its anchor clear reads an anchor move in the next frame, so the second frame is clear (#9123)

Cindy Zhangdfc83eedc12a
diff --git a/.changeset/layer-anchor-move-read-next-frame.md b/.changeset/layer-anchor-move-read-next-frame.md
new file mode 100644
index 0000000000..9f8f3f10da
--- /dev/null
+++ b/.changeset/layer-anchor-move-read-next-frame.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A tall tooltip, or any layer that keeps clear of its trigger, no longer covers the trigger for an extra frame when the page scrolls on a busy device. The layer re-checks its place in the frame after the scroll, before that frame paints, so the trigger stays visible from the second frame on.
+
+@cixzhang
diff --git a/packages/core/src/Layer/useLayer.test.tsx b/packages/core/src/Layer/useLayer.test.tsx
index f23e30e7d7..262c5b9302 100644
--- a/packages/core/src/Layer/useLayer.test.tsx
+++ b/packages/core/src/Layer/useLayer.test.tsx
@@ -2233,6 +2233,16 @@ describe('a layer that keeps its anchor clear (spec:AST-059 FR4)', () => {
const TRIGGER = rect(300, 400, 30, 100);
+ /** Let the next animation frame run, as the browser does before it paints. */
+ async function nextFrame() {
+ await act(
+ async () =>
+ new Promise<void>(resolve => {
+ requestAnimationFrame(() => resolve());
+ }),
+ );
+ }
+
/** Give the document a 720px-tall viewport for the duration of a test. */
function viewport720() {
return vi
@@ -2540,10 +2550,74 @@ describe('a layer that keeps its anchor clear (spec:AST-059 FR4)', () => {
placeBoxes(TRIGGER, rect(16, 350, 688, 300));
const before = observers.length;
act(() => move?.callback([{intersectionRatio: 0.4}]));
- expect(layerEl).toHaveAttribute('data-astryx-clears-anchor');
- // Re-armed on the new rect.
+ // Re-armed on the new rect at once; read in the next frame.
expect(move?.disconnected).toBe(true);
expect(observers.length).toBe(before + 1);
+ await nextFrame();
+ expect(layerEl).toHaveAttribute('data-astryx-clears-anchor');
+ vi.unstubAllGlobals();
+ });
+
+ it('reads a move its observer reports after paint in the next frame, before that frame paints, never in the reporting task', async () => {
+ // Placing a layer afresh in the task right after the frame that scrolled
+ // its anchor lets Chromium lay it out for one more frame against the
+ // anchor's position before the scroll, over the anchor (spec:AST-059 FR8:
+ // clear from the second painted frame). The read waits for the next
+ // frame, before it paints, and several reports in one frame share it.
+ const observers: {
+ callback: (entries: {intersectionRatio: number}[]) => void;
+ options: IntersectionObserverInit;
+ }[] = [];
+ vi.stubGlobal(
+ 'IntersectionObserver',
+ class {
+ constructor(
+ callback: (entries: {intersectionRatio: number}[]) => void,
+ options: IntersectionObserverInit,
+ ) {
+ observers.push({callback, options});
+ }
+ observe() {}
+ disconnect() {}
+ unobserve() {}
+ },
+ );
+ const latestMove = () =>
+ observers.filter(o => o.options?.rootMargin != null).at(-1);
+ const user = userEvent.setup();
+ const spy = placeBoxes(TRIGGER, rect(200, 350, 100, 200));
+ const {container, getByText} = render(<ClearHarness />);
+ await user.click(getByText('Trigger'));
+ const layerEl = container.querySelector('[popover]') as HTMLElement;
+ act(() => latestMove()?.callback([{intersectionRatio: 1}]));
+ await nextFrame();
+ expect(layerEl).not.toHaveAttribute('data-astryx-clears-anchor');
+
+ // A scroll carries the trigger under the layer; the observer reports it
+ // after the frame that painted the move, twice before the next frame.
+ spy.mockRestore();
+ const reads = vi.fn();
+ const covering = rect(16, 350, 688, 300);
+ vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(
+ function (this: HTMLElement) {
+ if (!this.hasAttribute('popover')) {
+ return TRIGGER;
+ }
+ reads();
+ return covering;
+ },
+ );
+ act(() => {
+ latestMove()?.callback([{intersectionRatio: 0.4}]);
+ latestMove()?.callback([{intersectionRatio: 0.6}]);
+ });
+ expect(layerEl).not.toHaveAttribute('data-astryx-clears-anchor');
+ expect(reads).not.toHaveBeenCalled();
+
+ // The next frame reads it once, switches it, and places it afresh.
+ await nextFrame();
+ expect(layerEl).toHaveAttribute('data-astryx-clears-anchor');
+ expect(reads).toHaveBeenCalled();
vi.unstubAllGlobals();
});
@@ -2595,9 +2669,10 @@ describe('a layer that keeps its anchor clear (spec:AST-059 FR4)', () => {
spy = placeBoxes(TRIGGER, rect(16, 350, 688, 300));
let armed = moves().length;
act(() => latest().callback([{intersectionRatio: 0.7}]));
- expect(layerEl).toHaveAttribute('data-astryx-clears-anchor');
expect(moves().length).toBe(armed + 1);
expect(latest().options.threshold).toBe(0.7);
+ await nextFrame();
+ expect(layerEl).toHaveAttribute('data-astryx-clears-anchor');
// A first report of 0 for a trigger that moved since the observer read
// its rectangle re-arms at once on the new rectangle.
diff --git a/packages/core/src/Layer/useLayer.tsx b/packages/core/src/Layer/useLayer.tsx
index cd02511470..9d6e8ac818 100644
--- a/packages/core/src/Layer/useLayer.tsx
+++ b/packages/core/src/Layer/useLayer.tsx
@@ -627,8 +627,9 @@ function failsAnchor(layer: HTMLElement, anchor: HTMLElement): boolean {
* every show and remount, and while the layer is open whenever its size, its
* content's size, its anchor's size or the viewport changes; each of those
* reads lands before the next frame paints. A same-size anchor that moves on
- * screen, in layout or because something scrolled, is read after paint, from
- * the anchor-move observer (FR8). Scroll events are not read: during one the
+ * screen, in layout or because something scrolled, is reported after paint by
+ * the anchor-move observer and read in the next frame, before it paints (FR8).
+ * Scroll events are not read: during one the
* anchor has moved but the layer anchored to it not yet, so the read would
* compare a moved anchor with a stale layer. Once switched, a layer keeps its
* anchor-clear list until it next opens, so it never jumps back over its
@@ -779,9 +780,16 @@ function observeMove(element: HTMLElement, onMove: () => void): () => void {
* range (spec:AST-059 FR4, FR8): the layer, its content or the anchor
* resizing (ResizeObserver, before paint), the viewport resizing (before
* paint), and the anchor moving on screen without resizing, in layout or by a
- * scroll (`observeMove`, after paint). Each calls `keepAnchorClear` for this layer and anchor while
- * `isCurrent` says they are still the live pair. Returns the function that
- * removes every observer and listener it added.
+ * scroll (`observeMove`, reported after paint). Each calls `keepAnchorClear`
+ * for this layer and anchor while `isCurrent` says they are still the live
+ * pair. A move is read in the next animation frame, not in the task that
+ * reports it: placing the layer afresh there, right after the frame that
+ * scrolled its anchor, lets Chromium lay it out for one more frame against
+ * the anchor's position before the scroll, over the anchor, so the second
+ * painted frame would still cover it. Read in the next frame, before it
+ * paints, the fresh placement uses the moved anchor. Reports in one frame
+ * share one read. Returns the function that removes every observer and
+ * listener it added.
*/
function observeAnchorClearance(
layer: HTMLElement,
@@ -806,11 +814,24 @@ function observeAnchorClearance(
}
observer.observe(anchor);
}
- const stopObservingMove = observeMove(anchor, reread);
+ let moveFrame: number | null = null;
+ const stopObservingMove = observeMove(anchor, () => {
+ if (moveFrame !== null) {
+ return;
+ }
+ moveFrame = view.requestAnimationFrame(() => {
+ moveFrame = null;
+ reread();
+ });
+ });
view.addEventListener('resize', reread);
return () => {
observer?.disconnect();
stopObservingMove();
+ if (moveFrame !== null) {
+ view.cancelAnimationFrame(moveFrame);
+ moveFrame = null;
+ }
view.removeEventListener('resize', reread);
};
}