Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@

import React from 'react';
import {useActiveDocContext} from '@docusaurus/plugin-content-docs/client';
import {useLayoutDoc} from '@docusaurus/theme-common/internal';
import useBaseUrl from '@docusaurus/useBaseUrl';
import {
isRegexpStringMatch,
useLayoutDoc,
} from '@docusaurus/theme-common/internal';
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
import type {Props} from '@theme/NavbarItem/DocNavbarItem';

export default function DocNavbarItem({
docId,
label: staticLabel,
docsPluginId,
activeBasePath,
activeBaseRegex,
...props
}: Props): JSX.Element | null {
const {activeDoc} = useActiveDocContext(docsPluginId);
const doc = useLayoutDoc(docId, docsPluginId);
const activeBaseUrl = useBaseUrl(activeBasePath);

// Draft items are not displayed in the navbar.
if (doc === null) {
Expand All @@ -29,10 +36,18 @@ export default function DocNavbarItem({
<DefaultNavbarItem
exact
{...props}
isActive={() =>
activeDoc?.path === doc.path ||
(!!activeDoc?.sidebar && activeDoc.sidebar === doc.sidebar)
}
isActive={(_match, location) => {
if (activeBaseRegex) {
return isRegexpStringMatch(activeBaseRegex, location.pathname);
}
if (activeBasePath) {
return location.pathname.startsWith(activeBaseUrl);
}
return (
activeDoc?.path === doc.path ||
(!!activeDoc?.sidebar && activeDoc.sidebar === doc.sidebar)
);
}}
label={staticLabel ?? doc.id}
to={doc.path}
/>
Expand Down
13 changes: 13 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ const config = {
docId: 'introduction',
label: 'Docs',
},
{
type: 'doc',
position: 'left',
docId: 'playground',
label: 'Playground',
activeBaseRegex: `/docs/playground`,
},
{
type: 'doc',
position: 'left',
docId: 'playground',
label: 'Playground',
},
Comment on lines +433 to +445
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see, the 2nd Playground link is active matched no matter what doc page we're at, whereas the first one is only matched when we are at docs/playground.

CleanShot 2022-09-10 at 22 41 32@2x

CleanShot 2022-09-10 at 22 41 42@2x

{
type: 'docSidebar',
position: 'left',
Expand Down