Skip to content

fix(dismissable-layer): guard isTopMostLayer against empty layers array#650

Open
EduardF1 wants to merge 2 commits into
kobaltedev:mainfrom
EduardF1:fix/dismissable-layer-empty-stack
Open

fix(dismissable-layer): guard isTopMostLayer against empty layers array#650
EduardF1 wants to merge 2 commits into
kobaltedev:mainfrom
EduardF1:fix/dismissable-layer-empty-stack

Conversation

@EduardF1
Copy link
Copy Markdown

@EduardF1 EduardF1 commented May 4, 2026

Problem

isTopMostLayer crashes with TypeError when layers array is empty.

Root cause

layers[layers.length - 1].node throws when layers is empty, since layers[-1] is undefined. Can happen during teardown or before a layer is registered.

Fix

Add a length guard:

function isTopMostLayer(node: HTMLElement | null) {
  return layers.length > 0 && layers[layers.length - 1].node === node;
}
```n
Returning `false` for an empty stack is correct  no layer is on top.

Fixes #649

@netlify
Copy link
Copy Markdown

netlify Bot commented May 4, 2026

👷 Deploy request for kobalte pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7eadb77

@EduardF1
Copy link
Copy Markdown
Author

EduardF1 commented May 8, 2026

Hi maintainers, friendly nudge on this small fix. The change guards isTopMostLayer against the empty-layers array case that surfaces in the linked issue, with no behavioral change when there are layers. Happy to iterate if any naming or test placement should be adjusted.

@EduardF1
Copy link
Copy Markdown
Author

Friendly ping on this one. The fix is a small defensive guard:
isTopMostLayer was reading layers[layers.length - 1] without checking
for an empty array, which I hit after programmatically closing the
last dismissable layer. One-line change, no behavior shift in the
happy path. Happy to add a test or rebase if useful. Thanks for the
work on this lib!

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.

1 participant