Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/elements-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-core",
"version": "9.0.16",
"version": "9.0.17",
"sideEffects": [
"web-components.min.js",
"src/web-components/**",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,47 @@ export const TableOfContents = React.memo<TableOfContentsProps>(
}, []);
const updatedTree = updateTocTree(tree, '');

const findFirstMatchAndIndexMatch = React.useCallback(
(items: TableOfContentsGroupItem[], id: string | undefined): [TableOfContentsGroupItem | undefined, boolean] => {
let firstMatch: TableOfContentsGroupItem | undefined;
let hasAnyLastIndexMatch = false;
if (!id) return [firstMatch, hasAnyLastIndexMatch];

const walk = (arr: TableOfContentsGroupItem[], stack: TableOfContentsGroupItem[]): boolean => {
for (const itm of arr) {
const newStack = stack.concat(itm);

const matches = ('slug' in itm && (itm as any).slug === id) || ('id' in itm && (itm as any).id === id);
if (matches) {
if (!firstMatch) firstMatch = itm;
const hasLastIndexMatch = newStack.some(el => 'index' in el && (el as any).index === lastActiveIndex);
if (hasLastIndexMatch) hasAnyLastIndexMatch = true;
const findMatchingItems = (
updateTree: TableOfContentsGroupItem[],
activeId: string,
lastActiveIndex: string,
): [TableOfContentsGroupItem | undefined, boolean] => {
let exactMatch: TableOfContentsGroupItem | undefined; // matches activeId + lastActiveIndex
let partialMatch: TableOfContentsGroupItem | undefined; // matches only activeId

const searchInChildren = (items: TableOfContentsGroupItem[]) => {
for (const item of items) {
const hasSlug = 'slug' in item;
const hasId = 'id' in item;
const isIdMatch = (hasSlug && item.slug === activeId) || (hasId && item.id === activeId);

if (isIdMatch) {
const hasIndex = 'index' in item;
if (hasIndex && item.index === lastActiveIndex) {
exactMatch = exactMatch ?? item; // first exact match wins
} else {
partialMatch = partialMatch ?? item; // first partial match wins
}
}

if ('items' in itm && Array.isArray((itm as any).items)) {
if (walk((itm as any).items, newStack)) return true;
}
const hasItems = 'items' in item;
if (hasItems && Array.isArray(item.items)) {
searchInChildren(item.items);
}
}
};

return false;
};
searchInChildren(updateTree);

walk(items, []);
return [firstMatch, hasAnyLastIndexMatch];
},
[lastActiveIndex],
);
const hasExactMatch = exactMatch !== undefined;
const bestMatch = exactMatch ?? partialMatch; // prioritize exact match

const [firstMatchItem, hasAnyLastIndexMatch] = React.useMemo(
() => findFirstMatchAndIndexMatch(updatedTree, activeId),
[updatedTree, activeId, findFirstMatchAndIndexMatch],
);
return [bestMatch, hasExactMatch];
};

const [firstMatchItem, hasAnyLastIndexMatch] = React.useMemo(() => {
return findMatchingItems(updatedTree, activeId, lastActiveIndex);
}, [updatedTree, activeId, lastActiveIndex]);

React.useEffect(() => {
if (!hasAnyLastIndexMatch && firstMatchItem && 'index' in firstMatchItem && firstMatchItem.index) {
Expand Down
4 changes: 2 additions & 2 deletions packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-dev-portal",
"version": "3.0.16",
"version": "3.0.17",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down Expand Up @@ -66,7 +66,7 @@
"dependencies": {
"@stoplight/markdown-viewer": "^5.7.1",
"@stoplight/mosaic": "^1.53.5",
"@stoplight/elements-core": "~9.0.16",
"@stoplight/elements-core": "~9.0.17",
"@stoplight/path": "^1.3.2",
"@stoplight/types": "^14.0.0",
"classnames": "^2.2.6",
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements",
"version": "9.0.16",
"version": "9.0.17",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down Expand Up @@ -63,7 +63,7 @@
]
},
"dependencies": {
"@stoplight/elements-core": "~9.0.16",
"@stoplight/elements-core": "~9.0.17",
"@stoplight/http-spec": "^7.1.0",
"@stoplight/json": "^3.18.1",
"@stoplight/mosaic": "^1.53.5",
Expand Down