Skip to content

Commit b8ec023

Browse files
fix:added fix for TOC (#2861)
* fix:added fix for TOC * bump up alpha versions * bump up version for stop 4166 * bump up version in version.ts after yarn build * added ~ for elements-core apckage used in elements-dev-portal to keep consistency in previous code. --------- Co-authored-by: SB-ChetanKorde <chetan.korde@smartbear.com>
1 parent 47921de commit b8ec023

5 files changed

Lines changed: 49 additions & 6 deletions

File tree

packages/elements-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/elements-core",
3-
"version": "9.0.13",
3+
"version": "9.0.14",
44
"sideEffects": [
55
"web-components.min.js",
66
"src/web-components/**",

packages/elements-core/src/components/TableOfContents/TableOfContents.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,49 @@ export const TableOfContents = React.memo<TableOfContentsProps>(
8484
}, []);
8585
const updatedTree = updateTocTree(tree, '');
8686

87+
const findFirstMatchAndIndexMatch = React.useCallback(
88+
(items: TableOfContentsGroupItem[], id: string | undefined): [TableOfContentsGroupItem | undefined, boolean] => {
89+
let firstMatch: TableOfContentsGroupItem | undefined;
90+
let hasAnyLastIndexMatch = false;
91+
if (!id) return [firstMatch, hasAnyLastIndexMatch];
92+
93+
const walk = (arr: TableOfContentsGroupItem[], stack: TableOfContentsGroupItem[]): boolean => {
94+
for (const itm of arr) {
95+
const newStack = stack.concat(itm);
96+
97+
const matches = ('slug' in itm && (itm as any).slug === id) || ('id' in itm && (itm as any).id === id);
98+
if (matches) {
99+
if (!firstMatch) firstMatch = itm;
100+
const hasLastIndexMatch = newStack.some(el => 'index' in el && (el as any).index === lastActiveIndex);
101+
if (hasLastIndexMatch) hasAnyLastIndexMatch = true;
102+
}
103+
104+
if ('items' in itm && Array.isArray((itm as any).items)) {
105+
if (walk((itm as any).items, newStack)) return true;
106+
}
107+
}
108+
109+
return false;
110+
};
111+
112+
walk(items, []);
113+
return [firstMatch, hasAnyLastIndexMatch];
114+
},
115+
[lastActiveIndex],
116+
);
117+
118+
const [firstMatchItem, hasAnyLastIndexMatch] = React.useMemo(
119+
() => findFirstMatchAndIndexMatch(updatedTree, activeId),
120+
[updatedTree, activeId, findFirstMatchAndIndexMatch],
121+
);
122+
123+
React.useEffect(() => {
124+
if (!hasAnyLastIndexMatch && firstMatchItem && 'index' in firstMatchItem && firstMatchItem.index) {
125+
setLastActiveIndex(firstMatchItem.index);
126+
}
127+
// eslint-disable-next-line react-hooks/exhaustive-deps
128+
}, [firstMatchItem, hasAnyLastIndexMatch]);
129+
87130
const container = React.useRef<HTMLDivElement>(null);
88131
const child = React.useRef<HTMLDivElement>(null);
89132
const firstRender = useFirstRender();

packages/elements-dev-portal/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/elements-dev-portal",
3-
"version": "3.0.13",
3+
"version": "3.0.14",
44
"description": "UI components for composing beautiful developer documentation.",
55
"keywords": [],
66
"sideEffects": [
@@ -66,7 +66,7 @@
6666
"dependencies": {
6767
"@stoplight/markdown-viewer": "^5.7.1",
6868
"@stoplight/mosaic": "^1.53.5",
69-
"@stoplight/elements-core": "~9.0.13",
69+
"@stoplight/elements-core": "~9.0.14",
7070
"@stoplight/path": "^1.3.2",
7171
"@stoplight/types": "^14.0.0",
7272
"classnames": "^2.2.6",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// auto-updated during build
2-
export const appVersion = '3.0.10';
2+
export const appVersion = '3.0.14';

packages/elements/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/elements",
3-
"version": "9.0.13",
3+
"version": "9.0.14",
44
"description": "UI components for composing beautiful developer documentation.",
55
"keywords": [],
66
"sideEffects": [
@@ -63,7 +63,7 @@
6363
]
6464
},
6565
"dependencies": {
66-
"@stoplight/elements-core": "~9.0.13",
66+
"@stoplight/elements-core": "~9.0.14",
6767
"@stoplight/http-spec": "^7.1.0",
6868
"@stoplight/json": "^3.18.1",
6969
"@stoplight/mosaic": "^1.53.5",

0 commit comments

Comments
 (0)