Skip to content

Fix(interaction): handle null targets in scroll/click to prevent Type Error #852#1025

Open
BryanBradfo wants to merge 1 commit intomicrosoft:masterfrom
BryanBradfo:fix/scroll-null-pointer-852
Open

Fix(interaction): handle null targets in scroll/click to prevent Type Error #852#1025
BryanBradfo wants to merge 1 commit intomicrosoft:masterfrom
BryanBradfo:fix/scroll-null-pointer-852

Conversation

@BryanBradfo
Copy link

Description

This PR fixes a TypeError reported in issue #852 ("Cannot read properties of null (reading 'scrollLeft')").

This error occurs when scrollLeft or scrollTop is accessed on a null element, which can happen in certain edge cases (e.g., inside React/Next.js applications where elements might be detached or managing shadow DOM).

Changes

I added optional chaining (?.) and a fallback (|| 0) to handle null targets safely in:

  • packages/clarity-js/src/interaction/scroll.ts
  • packages/clarity-js/src/interaction/click.ts
  • packages/clarity-js/src/interaction/pointer.ts

Verification

  • Ran npm run build successfully.
  • Verified that the changes introduce null-checks without altering the logic for valid elements.

Fixes #852

cc @AbdelrhmanMagdy (Since you looked into this previously, I thought you might want to review this fix).

@BryanBradfo
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uncaught TypeError: Cannot read properties of null (reading 'scrollLeft')

1 participant