Skip to content

Commit 8d211be

Browse files
authored
Fix/7579 toc scroll console error (#12433)
### Related Ticket(s) https://jsw.ibm.com/browse/ADCMS-8128 ### Description The calculation that the ToC component does to map all the sections of the page and link them to the ToC's buttons was also being called on pages without the ToC enabled. That was causing the property 'position' to be undefined due to ToC trying to compute multiple items but only finding one, resulting in multiple of these errors in the console: <img width="528" height="64" alt="image" src="https://github.com/user-attachments/assets/82391ea4-ea2e-45e2-8f9a-e0cce3133217" /> ### Changelog - packages/web-components/src/components/table-of-contents/table-of-contents.ts added some conditions to validate if there are enough elements to trigger the calculations.
1 parent 05104b7 commit 8d211be

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/web-components/src/components/table-of-contents/table-of-contents.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license
33
*
4-
* Copyright IBM Corp. 2020, 2025
4+
* Copyright IBM Corp. 2020, 2026
55
*
66
* This source code is licensed under the Apache-2.0 license found in the
77
* LICENSE file in the root directory of this source tree.
@@ -309,11 +309,19 @@ class C4DTableOfContents extends MediaQueryMixin(
309309
: null,
310310
position: elem.getBoundingClientRect().y,
311311
}))
312-
.filter((elem, index, arr) =>
313-
elem.height === null
314-
? arr[index - 1].position < arr[index - 1].height!
315-
: elem.position - 50 - this.stickyOffset > -elem.height
316-
);
312+
.filter((elem, index, arr) => {
313+
if (elem.height === null) {
314+
if (index <= 0) {
315+
return false;
316+
}
317+
const prev = arr[index - 1];
318+
if (!prev) {
319+
return false;
320+
}
321+
return prev.position < (prev.height ?? 0);
322+
}
323+
return elem.position - 50 - this.stickyOffset > -elem.height;
324+
});
317325

318326
// Sets last section as active at the end of page in case there is not enough height for it to dynamically activate
319327
const bottomReached =

0 commit comments

Comments
 (0)