Skip to content

Commit 5bbd943

Browse files
committed
fix(RefreshControl): the cross-fade's copy shows the indicator band
Under Reduce Motion, the copy of the list was clipped at its own box, so it hid the indicator band above the list. The spinner vanished in one frame when a refresh ended, and blinked at a release. The copy is now clipped to the scroller's visible box, past its own edges. The cross-fade reads the scroller's box with the list's, before it lays the copy, and takes the host's scale from placeOver. So laying the copy forces no second layout. The interaction case covers the indicator, and a unit case checks that nothing reads layout once the copy is laid.
1 parent c625a0d commit 5bbd943

4 files changed

Lines changed: 287 additions & 30 deletions

File tree

‎packages/core/src/motion/reducedMotionCrossFade.test.ts‎

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* @input reducedMotionCrossFade.ts, with fakeAnimate for Web Animations
66
* @output Proof of the cross-fade through a copy: the copy fades out where
77
* the element showed while the element lands and fades in, on one easing.
8-
* The copy sits next to the clipping box and is clipped to it. It goes once
9-
* both fades end or on cancel. An element that cannot be copied gets null.
8+
* The copy sits next to the clipping box and is clipped to its visible box,
9+
* past the copy's own edges, with no layout read once the copy is laid. It
10+
* goes once both fades end or on cancel. An element that cannot be copied
11+
* gets null.
1012
* @position Testing for /packages/core/src/motion/reducedMotionCrossFade.ts
1113
*/
1214

@@ -34,6 +36,8 @@ function box(left: number, top: number, width: number, height: number) {
3436

3537
let animations: FakeAnimation[] = [];
3638
const nativeAnimate = Element.prototype.animate;
39+
/** The scroller's layout reads and the copy's placement, when a case logs them. */
40+
let layoutLog: string[] | null = null;
3741

3842
beforeEach(() => {
3943
const fake = fakeAnimate();
@@ -44,6 +48,7 @@ beforeEach(() => {
4448

4549
afterEach(() => {
4650
Element.prototype.animate = nativeAnimate;
51+
layoutLog = null;
4752
document.body.replaceChildren();
4853
});
4954

@@ -58,14 +63,22 @@ function list() {
5863
wrapper.append(scroller);
5964
document.body.append(wrapper);
6065
content.getBoundingClientRect = () => box(0, 167, 400, 900);
61-
scroller.getBoundingClientRect = () => box(0, 0, 400, 600);
66+
scroller.getBoundingClientRect = () => {
67+
layoutLog?.push('read the scroller');
68+
return box(0, 0, 400, 600);
69+
};
6270
for (const [name, value] of [
6371
['clientTop', 0],
6472
['clientLeft', 0],
6573
['clientWidth', 400],
6674
['clientHeight', 600],
6775
] as const) {
68-
Object.defineProperty(scroller, name, {value});
76+
Object.defineProperty(scroller, name, {
77+
get: () => {
78+
layoutLog?.push('read the scroller');
79+
return value;
80+
},
81+
});
6982
}
7083
return {wrapper, scroller, content};
7184
}
@@ -106,7 +119,7 @@ describe('playReducedMotionCrossFade', () => {
106119
expect(fadeOut.keyframes[0].easing).toBe(fade.easing);
107120
});
108121

109-
it('clips the copy to the scroller it is laid outside of', () => {
122+
it("clips the copy to the visible box of the scroller it is laid outside of, past the copy's own edges", () => {
110123
const {wrapper, scroller, content} = list();
111124
// The copy's own box once laid: where the list showed, 900 px tall.
112125
const nativeBox = Element.prototype.getBoundingClientRect;
@@ -120,12 +133,45 @@ describe('playReducedMotionCrossFade', () => {
120133
} finally {
121134
Element.prototype.getBoundingClientRect = nativeBox;
122135
}
123-
// The scroller shows 600 px: the copy's last 467 px are cut.
136+
// The scroller shows 600 px: the copy's last 467 px are cut. Its top
137+
// edge is 167 px below the scroller's: what the copy holds above its own
138+
// box, such as an indicator band above a list, shows there as it did.
124139
expect(copyIn(wrapper)?.style.getPropertyValue('clip-path')).toBe(
125-
'inset(0px 0px 467px 0px)',
140+
'inset(-167px 0px 467px 0px)',
126141
);
127142
});
128143

144+
it('reads the scroller before it lays the copy: once the copy is laid, nothing reads layout', () => {
145+
const {wrapper, scroller, content} = list();
146+
const log: string[] = [];
147+
layoutLog = log;
148+
const insert = wrapper.insertBefore.bind(wrapper);
149+
wrapper.insertBefore = <T extends Node>(node: T, child: Node | null) => {
150+
log.push('lay the copy');
151+
return insert(node, child);
152+
};
153+
const nativeBox = Element.prototype.getBoundingClientRect;
154+
Element.prototype.getBoundingClientRect = function (this: Element) {
155+
if (this.hasAttribute('inert')) {
156+
log.push('read the copy');
157+
return box(0, 167, 400, 900);
158+
}
159+
return nativeBox.call(this);
160+
};
161+
try {
162+
playReducedMotionCrossFade(content, fade, () => {}, scroller);
163+
} finally {
164+
Element.prototype.getBoundingClientRect = nativeBox;
165+
}
166+
expect(log).toContain('read the scroller');
167+
// Laying the copy reads its box once, to place it. A read after its last
168+
// write would lay the page out a second time.
169+
expect(log.slice(log.indexOf('lay the copy'))).toEqual([
170+
'lay the copy',
171+
'read the copy',
172+
]);
173+
});
174+
129175
it('removes the copy once both fades end, and reports it finished', async () => {
130176
const {wrapper, scroller, content} = list();
131177
const played = playReducedMotionCrossFade(

‎packages/core/src/motion/reducedMotionCrossFade.ts‎

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
* two. The copy is inert and hidden from assistive technology
1717
* (transientCopy.ts). It is laid next to the box that clips the element,
1818
* never inside a scroll container. So a scroll that the change makes does not
19-
* move it.
19+
* move it. It is clipped to that box's visible part, past its own edges, so
20+
* what the element draws outside its box, such as an indicator band above a
21+
* list, shows in it as it did.
2022
*/
2123

2224
import {ENTRANCE_OPACITY_FLOOR} from './opacityFloor';
@@ -56,6 +58,9 @@ export function playReducedMotionCrossFade(
5658
if (host == null || rect.width === 0 || rect.height === 0) {
5759
return null;
5860
}
61+
// Read with the element's box, before any write: laying the copy writes
62+
// layout, and a read after it would lay the page out again.
63+
const visible = clip == null ? null : visibleBox(clip);
5964
const copy = isCopyable(element) ? inertCopy(element) : null;
6065
if (copy == null) {
6166
return null;
@@ -68,9 +73,9 @@ export function playReducedMotionCrossFade(
6873
copy.style.setProperty(property, 'none');
6974
}
7075
copy.style.setProperty('pointer-events', 'none');
71-
placeOver(copy, host, rect, anchor.nextSibling);
72-
if (clip != null) {
73-
clipTo(copy, clip);
76+
const scale = placeOver(copy, host, rect, anchor.nextSibling);
77+
if (visible != null) {
78+
clipTo(copy, rect, visible, scale);
7479
}
7580
land();
7681
// Both fades start in this task, on the same frame, with the same easing.
@@ -114,25 +119,46 @@ export function playReducedMotionCrossFade(
114119
};
115120
}
116121

122+
interface Edges {
123+
top: number;
124+
right: number;
125+
bottom: number;
126+
left: number;
127+
}
128+
129+
/**
130+
* `clip`'s visible box in viewport pixels: its padding box, inside the
131+
* borders and scroll bars.
132+
*/
133+
function visibleBox(clip: HTMLElement): Edges {
134+
const box = clip.getBoundingClientRect();
135+
const top = box.top + clip.clientTop;
136+
const left = box.left + clip.clientLeft;
137+
return {
138+
top,
139+
left,
140+
bottom: top + clip.clientHeight,
141+
right: left + clip.clientWidth,
142+
};
143+
}
144+
117145
/**
118-
* Clips `copy` to `clip`'s visible box: its padding box, inside the borders
119-
* and scroll bars. Laid outside `clip`, the copy is no longer clipped by it.
146+
* Clips `copy`, laid over `rect`, to `visible`: the visible box of the box it
147+
* is laid outside of, which no longer clips it. An inset is negative where
148+
* that box shows past the copy's own edge.
120149
*/
121-
function clipTo(copy: HTMLElement, clip: HTMLElement): void {
122-
const clipRect = clip.getBoundingClientRect();
123-
const top = clipRect.top + clip.clientTop;
124-
const left = clipRect.left + clip.clientLeft;
125-
const bottom = top + clip.clientHeight;
126-
const right = left + clip.clientWidth;
127-
const box = copy.getBoundingClientRect();
150+
function clipTo(
151+
copy: HTMLElement,
152+
rect: DOMRect,
153+
visible: Edges,
154+
scale: {x: number; y: number},
155+
): void {
128156
// The inset is in the copy's own pixels: a scaled host draws them larger.
129-
const scale =
130-
copy.offsetWidth > 0 && box.width > 0 ? box.width / copy.offsetWidth : 1;
131157
const inset = [
132-
top - box.top,
133-
box.right - right,
134-
box.bottom - bottom,
135-
left - box.left,
136-
].map(value => `${Math.max(0, value) / scale}px`);
158+
(visible.top - rect.top) / scale.y,
159+
(rect.right - visible.right) / scale.x,
160+
(rect.bottom - visible.bottom) / scale.y,
161+
(visible.left - rect.left) / scale.x,
162+
].map(value => `${value}px`);
137163
copy.style.setProperty('clip-path', `inset(${inset.join(' ')})`);
138164
}

‎packages/core/src/motion/transientCopy.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,16 @@ export function keepInherited(copy: HTMLElement, host: Element): void {
215215
* Lay `element` over `rect` (viewport coordinates), absolutely, as a child of
216216
* `host` (before `beforeNode`, else last), wherever its containing block is:
217217
* no ancestor's position changes. A copy's scrolled descendants then restore their offsets, and the
218-
* CSS animations and transitions its element ran carry on in it.
218+
* CSS animations and transitions its element ran carry on in it. Returns the
219+
* host's scale on each axis, so a caller converts viewport pixels to the
220+
* element's own without reading its box again.
219221
*/
220222
export function placeOver(
221223
element: HTMLElement,
222224
host: Element,
223225
rect: DOMRect,
224226
beforeNode: Node | null = null,
225-
): void {
227+
): {x: number; y: number} {
226228
Object.assign(element.style, {
227229
position: 'absolute',
228230
boxSizing: 'border-box',
@@ -267,6 +269,7 @@ export function placeOver(
267269
node => carried.copyOf.get(node) ?? null,
268270
);
269271
}
272+
return {x: scaleX, y: scaleY};
270273
}
271274

272275
/**

0 commit comments

Comments
 (0)