Skip to content

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

TypeScript

fix(core): ignore a pointerup with no pointerId when no press is held (#10695)

Harjoth Kharaa60baf519c53
diff --git a/.changeset/press-feedback-pointerup-no-contact.md b/.changeset/press-feedback-pointerup-no-contact.md
new file mode 100644
index 0000000000..5e906bd6e7
--- /dev/null
+++ b/.changeset/press-feedback-pointerup-no-contact.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Press feedback: a `pointerup` that carries no `pointerId`, such as a bare event a test helper or drag script dispatches, no longer throws `Cannot read properties of null (reading 'live')` when no press is held. It is ignored, as `pointercancel` already does.
+
+@harjothkhara
diff --git a/packages/core/src/utils/pressFeedback.test.ts b/packages/core/src/utils/pressFeedback.test.ts
index 1e7bca06f1..e3e69ab53e 100644
--- a/packages/core/src/utils/pressFeedback.test.ts
+++ b/packages/core/src/utils/pressFeedback.test.ts
@@ -1049,6 +1049,38 @@ describe('two touches in a row', () => {
});
});
+describe('a pointerup that matches no held press', () => {
+ it('is ignored when it carries no pointerId and nothing is held', () => {
+ // The input stream reports a listener's throw through `reportError`.
+ const reportError = vi.fn();
+ vi.stubGlobal('reportError', reportError);
+ try {
+ // A page's own drag script or test helper can dispatch a bare event
+ // named `pointerup`; a real PointerEvent always has a numeric pointerId.
+ document.body.dispatchEvent(new Event('pointerup', {bubbles: true}));
+ } finally {
+ vi.unstubAllGlobals();
+ }
+ expect(reportError).not.toHaveBeenCalled();
+ });
+
+ it('leaves a held finger press alone', () => {
+ const reportError = vi.fn();
+ vi.stubGlobal('reportError', reportError);
+ coarsePrimaryPointer();
+ try {
+ // No box component is mounted, so the finger holds a press but no contact.
+ press(rows[0], 'pointerdown', 10, 10);
+ vi.advanceTimersByTime(PRESS_ONSET_MS);
+ document.body.dispatchEvent(new Event('pointerup', {bubbles: true}));
+ } finally {
+ vi.unstubAllGlobals();
+ }
+ expect(reportError).not.toHaveBeenCalled();
+ expect(rows[0].getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
+ });
+});
+
describe('a phone or tablet paints a mouse or Space press itself', () => {
// `(pointer: coarse)` reads the PRIMARY pointer. Where it matches, the
// stylesheet drops every `:active` arm, so a trackpad click or a Space press
diff --git a/packages/core/src/utils/pressFeedback.ts b/packages/core/src/utils/pressFeedback.ts
index 57115e9b4e..b885350798 100644
--- a/packages/core/src/utils/pressFeedback.ts
+++ b/packages/core/src/utils/pressFeedback.ts
@@ -656,7 +656,7 @@ function onPointerUp(event: PointerEvent): void {
if (event.target instanceof Node) {
lastTouchTarget = event.target;
}
- if (contact?.pointerId === event.pointerId) {
+ if (contact != null && contact.pointerId === event.pointerId) {
// Read before the release step: a press that travelled or that a scroll
// took is dead or gone by now.
const pressed = contact.live;