# posAtCoords throws "Cannot read properties of undefined (reading 'top')" when content has no client rects

**URL:** https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790
**Category:** Uncategorized
**Created:** [June 8, 2026, 1:24pm UTC](https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790 "2026-06-08T13:24:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![stefan-boghean](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/stefan-boghean/32/5487_2.png) [@stefan-boghean](https://discuss.codemirror.net/u/stefan-boghean)
#### Post date: [June 8, 2026, 1:24pm UTC](https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790/1 "2026-06-08T13:24:40Z")

</div>

`EditorView.posAtCoords` can throw `TypeError: Cannot read properties of undefined (reading 'top')` when the editor content reports \*\*no client rects\*\* for the queried position (e.g. the editor, or an ancestor, is `display:none` / detached / mid-layout at the moment of the call).

Root cause seems to be in `InlineCoordsScan.scan`, when the scanned positions yield no client rectangles at all, none of `above`, `below` or `closestRect` are ever assigned:

`if (!closestRect) { let side = above && (!below || (this.y - above.bottom < below.top - this.y)) ? above : below; this.y = (side.top + side.bottom) / 2; // <-- side is undefined → throws return this.scan(positions, getRects /*, true */); }`

We encountered this error via the `hoverTooltip` plugin, which on hover schedules a delayed `startHover` which synchronously calls `view.posAtCoords(lastMove)`. If the editor loses its client rects within that delay - examples a tile that re-renders/collapses/scrolls out - the coordinate scan returns an empty list thus the error is thrown.

Since all of this happens in a span of about 300 ms, it is really hard to provide a reproducible example, but a suggested fix would be for the scan to handle no client rectangles found.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [June 8, 2026, 1:41pm UTC](https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790/2 "2026-06-08T13:41:11Z")

</div>

This patch should help.

> **[Handle case where no elements are rendered in posAtCoords scan · b3727908aa](https://code.haverbeke.berlin/codemirror/view/commit/b3727908aa45fce55e4d2b71cf091f6a4facec93)**
>
> FIX: Fix a crash when calling on a line with only
> \`display: none\` content.
> 
> See https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790

---

<div class="post-metadata">

### Author: ![microbit-matt](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/microbit-matt/32/1070_2.png) [@microbit-matt](https://discuss.codemirror.net/u/microbit-matt)
#### Post date: [September 3, 2026, 11:19am UTC](https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790/3 "2026-09-03T11:19:51Z")

</div>

I think I’m seeing logged issues related to this post and I’m not sure the fix fully addresses it.

In [b3727908aa45fce55e4d2b71cf091f6a4facec93](https://code.haverbeke.berlin/codemirror/view/commit/b3727908aa45fce55e4d2b71cf091f6a4facec93) the fix returns `positions[0]`, a document position, but scan’s callers expect an index into positions. It should probably be `{i: 0, after: false}`.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [September 3, 2026, 12:43pm UTC](https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790/4 "2026-09-03T12:43:21Z")

</div>

Yes, you’re right. See this follow-up patch.

> **[Fix bug in handling of posAtCoords around non-rendered elements · 4727997fb6](https://code.haverbeke.berlin/codemirror/view/commit/4727997fb6f1e3196d1316477edb9f31f97f414a)**
>
> FIX: Fix the patch that avoids a crash in \`posAtCoords\` for non-rendered
> elements.
> 
> See https://discuss.codemirror.net/t/posatcoords-throws-cannot-read-properties-of-undefined-reading-top-when-content-has-no-client-rects/9790
