An open source design system that's fully customizable and agent ready
TypeScript
fix(core): the touch-inset pass resolves no style for unscaled controls (#9987)
Christopher Chedeau2db57bd97686
diff --git a/.changeset/hit-area-offset-size-check.md b/.changeset/hit-area-offset-size-check.mdnew file mode 100644index 0000000000..48913b0d3c--- /dev/null+++ b/.changeset/hit-area-offset-size-check.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[perf] Under a finger, the touch-inset pass of a Switch or InfoTip resolves a control's style only when a transform scales it.++@vjeuxdiff --git a/packages/core/src/utils/hitArea.test.ts b/packages/core/src/utils/hitArea.test.tsindex dde0fea6ef..ab54d41acc 100644--- a/packages/core/src/utils/hitArea.test.ts+++ b/packages/core/src/utils/hitArea.test.ts@@ -188,6 +188,18 @@ describe('watchHitArea', () => {};}+ /** Give `element` the whole-pixel layout size that jsdom leaves at 0. */+ function layOut(element: HTMLElement, width: number, height: number): void {+ Object.defineProperty(element, 'offsetWidth', {+ configurable: true,+ value: width,+ });+ Object.defineProperty(element, 'offsetHeight', {+ configurable: true,+ value: height,+ });+ }+function show(...elements: Element[]): void {intersection?.(elements.map(@@ -279,6 +291,7 @@ describe('watchHitArea', () => {height: '24px',boxSizing: 'border-box',});+ layOut(control, 24, 24);place(control, half(rect(100, 100, 24, 24)));place(byId('link'), half(rect(132, 100, 60, 24)));place(byId('other'), half(rect(400, 400, 24, 24)));@@ -291,6 +304,25 @@ describe('watchHitArea', () => {stop();});+ it('reads no computed style for a control at its layout size', () => {+ const control = byId('switch');+ const other = byId('other');+ layOut(control, 24, 24);+ place(control, rect(100, 100, 24, 24));+ place(other, rect(400, 400, 24, 24));+ place(byId('link'), rect(132, 100, 60, 24));+ const getComputedStyle = vi.spyOn(window, 'getComputedStyle');+ const stops = [control, other].map(element =>+ watchHitArea(element, () => {}),+ );+ show(control, other);+ flush();+ // One control is at its layout size and the other has none, so neither+ // resolves style in the measuring pass.+ expect(getComputedStyle).not.toHaveBeenCalled();+ stops.forEach(stop => stop());+ });+it('measures nothing off screen', () => {const control = byId('switch');place(control, rect(100, 100, 24, 24));@@ -303,6 +335,9 @@ describe('watchHitArea', () => {});it('batches every pending control into one frame: all reads, then all writes', () => {+ // The clock holds still, so a slow machine cannot spend the frame budget+ // and split the batch. The budget has its own case below.+ vi.spyOn(performance, 'now').mockReturnValue(0);const first = byId('switch');const second = byId('other');place(first, rect(100, 100, 24, 24));diff --git a/packages/core/src/utils/hitArea.ts b/packages/core/src/utils/hitArea.tsindex ad47dd7091..3d93264d23 100644--- a/packages/core/src/utils/hitArea.ts+++ b/packages/core/src/utils/hitArea.ts@@ -356,6 +356,24 @@ function screenScale(element: Element,onScreen: HitRect,): {x: number; y: number} {+ // The offset size is the layout size in whole pixels, unaffected by+ // transforms, and costs no style resolution. A box with none, such as an SVG+ // element or a jsdom box, reads as unscaled.+ const box = element as Partial<HTMLElement>;+ const offsetWidth = box.offsetWidth ?? 0;+ const offsetHeight = box.offsetHeight ?? 0;+ if (offsetWidth === 0 || offsetHeight === 0) {+ return UNSCALED;+ }+ // Within a pixel of its layout size the box reads as unscaled, which bounds+ // the inset error at about a pixel. Only a box that a transform shrinks or+ // grows reads its exact size from the computed style.+ if (+ Math.abs(onScreen.right - onScreen.left - offsetWidth) <= 1 &&+ Math.abs(onScreen.bottom - onScreen.top - offsetHeight) <= 1+ ) {+ return UNSCALED;+ }const style = element.ownerDocument.defaultView?.getComputedStyle(element);if (style == null || !style.width.endsWith('px')) {return UNSCALED;