You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have three instances of docs, each segmented into three categories with their respective sidebars: "concepts" "guides" and "reference". These docs can be called "V1" "V2" and "V3", but are fixed docs once released so versioning isn't helpful in the canonical sense. When on "V2"-"Concepts" - clicking the Guides button on the navbar will take me to "V2"-"Guides", along with the appropriate sidebar.
I'm not totally sure to understand your usecase, a better description would be appreciated, and also if you can put a deploy preview of your doc site with 3 docs instance online on Netlify/Vercel/whatever, that would help to figure out the UX you try to achieve.
We could work on defining a few useful activation conditions and the API surface, and also enable composition with AND/OR logic.
Is this what you are looking for?
Reacted by Stan Girard, Connor Martin, Arijit, Nikita Gaer, Vir Desai, Abdallah Abdedaiem, Marika Marszalkowski, Junjie Tang, Vanderhoof, Gurinder Singh and 5 more
changed the title [-]Nav bar that routes relative to the current doc plugin Id[/-][+]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items?)[/+]on Apr 5, 2021
changed the title [-]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items?)[/-][+]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items with activation strategy?)[/+]on Apr 5, 2021
changed the title [-]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items with activation strategy?)[/-][+]Dynamic navbar: navbar item activation strategies[/+]on Jul 8, 2021
Hey, multiple people have asked me whether it's possible to only display certain navbar items when user is logged in (by sending HTTP requests).
I like the activeWhen API, and I think we can make the type be like:
activeWhen: {strategy: string,params: any}
And then have a custom @theme/useNavbarItemIsActive hook, meant to be swizzled if the user wants additional logic so that this is accessible on the client side.
We are a static site generator and not a hybrid framework like Next.js.
This means that we can only check for user authentication status once in the browser.
Having navbar items that require user authentication means that there will be navbar layout shifts happening after React hydration, and I'm not sure it's a good pattern to encourage: the strategies should rather work on the server to avoid layout shifts. But if we allow custom user-provided strategies we can't really prevent the user to do that anyway.
And then have a custom @theme/useNavbarItemIsActive hook, meant to be swizzled
My plan was more to have a map of strategies in @theme/NavbarStrategies/index.tsx, similar to what we have for NavbarItem. I think having an empty map of custom strategies could also be useful so that user can swizzle a smaller part: @theme/NavbarStrategies/CustomStrategies.tsx.
We should probably add something similar to allow providing custom Navbar Items. .
I'd like the infrastructure to look similar in both cases and make it easy to extend/maintain (small swizzle API surface).
My plan was more to have a map of strategies in @theme/NavbarStrategies/index.tsx,
I can't think of many use cases besides loggedIn and routeMatch 🤦♂️ But having a CustomStrategies.tsx is similar to what I have in mind
Agree that there will be layout shifts, but that doesn't sound like what we can help anyways (I've seen this behavior on a lot of sites using REST API for login). We can allow the user to set a default render state to minimize the impact, maybe?
the strategies should rather work on the server to avoid layout shifts
Makes sense. Strategies that we offer should conform to this, but we probably can't limit the users too much either
In any case, I might set my hands on this if no-one will be working on this soon
I don't know for you but in 2.2.0 useActiveDocContext(props.docsPluginId) always returns an Object, I had to check if the property activeDoc is actually defined inside this object :
importReactfrom"react";import{useActiveDocContext}from'@docusaurus/plugin-content-docs/client';importDocsVersionDropdownNavbarItemfrom'@theme-original/NavbarItem/DocsVersionDropdownNavbarItem';exportdefaultfunctionDocsVersionDropdownNavbarItemWrapper(props){// do not display this navbar item if current page is not a docconst{ activeDoc }=useActiveDocContext(props.docsPluginId);if(!activeDoc){returnnull;}return<DocsVersionDropdownNavbarItem{...props}/>;}
Can this be applied not to the /docs folder, but on the contrary, to the versioned multi-instance docs that reside next to /docs?
Can this be applied not to the /docs folder, but on the contrary, to the versioned multi-instance docs that reside next to /docs?
I don't know what you mean here, a repro would help.
You can pass a pluginId as hook arg so if you have a 2nd docs plugin instance you can use its id here: useActiveDocContext("my-android-sdk-plugin-id");
So I have several instances of docs, each with its own sidebar and versioning. The instances are located in separate folders in the root:
/docs (not versioned)
/instance-1 (3 versions)
/instance-2 (2 versions)
etc.
To make the versions work, I added the version dropdowns to each versioned item in the navbar, thus making several version dropdowns on the UI.
With the code above, I wanted to make a single navbar dropdown that will appear only for versioned instances and contain versions depending on the instance you are currently reading.
I was able to achieve that with the following found code:
importReactfrom"react";importDocsVersionDropdownNavbarItemfrom'@theme-original/NavbarItem/DocsVersionDropdownNavbarItem';import{useLocation}from'@docusaurus/router';exportdefaultfunctionDocsVersionDropdownNavbarItemWrapper(props){const{ docsPluginId, className, type }=propsconst{ pathname }=useLocation()constdoesPathnameContainDocsPluginId=pathname.includes(docsPluginId)if(!doesPathnameContainDocsPluginId){returnnull}return<DocsVersionDropdownNavbarItem{...props}/>;}
The only thing, in docusaurus.config.js, I had to create 3 separate dropdown items, but this doesn't affect the result.
Is this still not finished yet? An active docs-aware version dropdown is all we need.
Docs Multi-Instance can result into multiple docs with their own versions. I don't know how the Docs Multi-instance was released without taking into consideration the versions per instance.
Docs Multi-Instance can result into multiple docs with their own versions. I don't know how the Docs Multi-instance was released without taking into consideration the versions per instance.
Because there are workarounds possible now to achieve this and this is an open-source project with limited volunteer maintainer time to implement things. It's generally considered that if there's a way to achieve something, even if that way is swizzling or custom components that it's not a high priority and that providing a usable base set of features for 75+% of use cases/sites is the goal not making APIs for every single advanced use case someone wants.
For my site, I wanted to avoid displaying the version dropdown when the active route is a blog post. Here is how I achieved it.
I created src/theme/NavbarItem/DocsVersionDropdownNavbarItem.js and added the following code, and that alone solved the problem for me.
importReactfrom'react';importDocsVersionDropdownNavbarItemfrom'@theme-original/NavbarItem/DocsVersionDropdownNavbarItem';import{useLocation}from'@docusaurus/router';exportdefaultfunctionDocsVersionDropdownNavbarItemWrapper(props){constlocation=useLocation();constunversionedRoutes=[// any route that starts with `/blog`/^\/blog(?:\/[\w-]+)?(?:\/#\w+)?$/g]functioncheckPathname(pathname){// Check if the provided pathname matches any of the regexes in the listreturnunversionedRoutes.some(regex=>regex.test(pathname))}if(checkPathname(location.pathname)){returnnull;}return<DocsVersionDropdownNavbarItem{...props}/>;}
importReactfrom"react";import{useActiveDocContext}from'@docusaurus/plugin-content-docs/client';importDocsVersionDropdownNavbarItemfrom'@theme-original/NavbarItem/DocsVersionDropdownNavbarItem';exportdefaultfunctionDocsVersionDropdownNavbarItemWrapper(props){// do not display this navbar item if current page is not a docconstactiveDocContext=useActiveDocContext(props.docsPluginId);if(!activeDocContext){returnnull;}return<DocsVersionDropdownNavbarItem{...props}/>;}
Is this possible? Or perhaps I'm understanding your code wrong. From what I can see, it doesn't seem to do what I'd expect (hide dropdowns except for the one that corresponds to the active plugin id)
This is a brilliant solution except changing
if (!activeDocContext) {
return null;
}
to
if (!activeDocContext.activeDoc) {
return null;
}
Because useActiveDocContext() returns an object similar to the following:
If the versioned doc is not the current doc, activeDoc and activeVersion will be undefined. Making use of this, you can control the display of the docsVersionDropdown item.
Activity
slorber commentedon Mar 12, 2021
Hi,
I'm not totally sure to understand your usecase, a better description would be appreciated, and also if you can put a deploy preview of your doc site with 3 docs instance online on Netlify/Vercel/whatever, that would help to figure out the UX you try to achieve.
This looks kind-of related to #3930
My idea was we could build a generic system where each navbar item has an "activation strategy".
This means the navbar items could be different on a per-route basis, for both mobile and desktop.
This "strategy config" must be:
Here's a badly designed example:
We could work on defining a few useful activation conditions and the API surface, and also enable composition with AND/OR logic.
Is this what you are looking for?
Yorkemartin commentedon Mar 16, 2021
This would address my issue perfectly, I would love it if this were implemented
[-]Nav bar that routes relative to the current doc plugin Id[/-][+]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items?)[/+][-]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items?)[/-][+]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items with activation strategy?)[/+]dswiecki commentedon Jun 25, 2021
@slorber Hi, thank you for a great tool ;) do you have this feature on 2.0.0 roadmap ?
slorber commentedon Jun 25, 2021
@dswiecki this has been requested quite often so we'll build it, however this is not the simplest one to design, and we have more important features to work on in the short term, cf the beta blog post https://docusaurus.io/blog/2021/05/12/announcing-docusaurus-two-beta#whats-next
[-]Nav bar that routes relative to the current doc plugin Id (dynamic navbar items with activation strategy?)[/-][+]Dynamic navbar: navbar item activation strategies[/+]Josh-Cena commentedon Aug 13, 2021
Hey, multiple people have asked me whether it's possible to only display certain navbar items when user is logged in (by sending HTTP requests).
I like the
activeWhenAPI, and I think we can make the type be like:And then have a custom
@theme/useNavbarItemIsActivehook, meant to be swizzled if the user wants additional logic so that this is accessible on the client side.slorber commentedon Aug 13, 2021
We are a static site generator and not a hybrid framework like Next.js.
This means that we can only check for user authentication status once in the browser.
Having navbar items that require user authentication means that there will be navbar layout shifts happening after React hydration, and I'm not sure it's a good pattern to encourage: the strategies should rather work on the server to avoid layout shifts. But if we allow custom user-provided strategies we can't really prevent the user to do that anyway.
This post section explains the problem with gifs: https://www.joshwcomeau.com/react/the-perils-of-rehydration/#schrodingers-user:
My plan was more to have a map of strategies in
@theme/NavbarStrategies/index.tsx, similar to what we have for NavbarItem. I think having an empty map of custom strategies could also be useful so that user can swizzle a smaller part:@theme/NavbarStrategies/CustomStrategies.tsx.We should probably add something similar to allow providing custom Navbar Items. .
I'd like the infrastructure to look similar in both cases and make it easy to extend/maintain (small swizzle API surface).
I can support you if you want to work on this
Josh-Cena commentedon Aug 14, 2021
I can't think of many use cases besides
loggedInandrouteMatch🤦♂️ But having aCustomStrategies.tsxis similar to what I have in mindAgree that there will be layout shifts, but that doesn't sound like what we can help anyways (I've seen this behavior on a lot of sites using REST API for login). We can allow the user to set a default render state to minimize the impact, maybe?
Makes sense. Strategies that we offer should conform to this, but we probably can't limit the users too much either
In any case, I might set my hands on this if no-one will be working on this soon
I imagine the API surface to be like:
And then called in
NavbarItem:So that you can do something like:
We can even have the syntactic sugar
to make the API close to what you have
Josh-Cena commentedon Oct 21, 2021
This doesn't look like an explainable / intuitive API to me... Is this an attempt to improve SSR?
slorber commentedon Oct 21, 2021
Just updated this to showcase that it can cover the need of #5756, it is not at all a final/definitive API
11 remaining items
aleksimo commentedon Mar 24, 2023
Can this be applied not to the
/docsfolder, but on the contrary, to the versioned multi-instance docs that reside next to /docs?slorber commentedon Apr 7, 2023
I don't know what you mean here, a repro would help.
You can pass a pluginId as hook arg so if you have a 2nd docs plugin instance you can use its id here:
useActiveDocContext("my-android-sdk-plugin-id");aleksimo commentedon Apr 7, 2023
So I have several instances of docs, each with its own sidebar and versioning. The instances are located in separate folders in the root:
To make the versions work, I added the version dropdowns to each versioned item in the navbar, thus making several version dropdowns on the UI.
With the code above, I wanted to make a single navbar dropdown that will appear only for versioned instances and contain versions depending on the instance you are currently reading.
I was able to achieve that with the following found code:
The only thing, in
docusaurus.config.js, I had to create 3 separate dropdown items, but this doesn't affect the result.preparations for versions for each package
fharper commentedon Jul 13, 2023
For me the use case is being able to render some navbar items only when the user selected a specific docs version.
luchtech commentedon Dec 30, 2023
Is this still not finished yet? An active docs-aware version dropdown is all we need.
Docs Multi-Instance can result into multiple docs with their own versions. I don't know how the Docs Multi-instance was released without taking into consideration the versions per instance.
homotechsual commentedon Dec 30, 2023
Because there are workarounds possible now to achieve this and this is an open-source project with limited volunteer maintainer time to implement things. It's generally considered that if there's a way to achieve something, even if that way is swizzling or custom components that it's not a high priority and that providing a usable base set of features for 75+% of use cases/sites is the goal not making APIs for every single advanced use case someone wants.
pavinduLakshan commentedon Feb 25, 2024
For my site, I wanted to avoid displaying the version dropdown when the active route is a blog post. Here is how I achieved it.
I created
src/theme/NavbarItem/DocsVersionDropdownNavbarItem.jsand added the following code, and that alone solved the problem for me.Hope this would help someone else!
slorber commentedon Feb 25, 2024
@pavinduLakshan using
useRouteContextis likely a better, more robust solution.I didn't try but this should work:
AnthonyTsu1984 commentedon Sep 9, 2024
This is a brilliant solution except changing
to
Because
useActiveDocContext()returns an object similar to the following:If the versioned doc is not the current doc,
activeDocandactiveVersionwill beundefined. Making use of this, you can control the display of the docsVersionDropdown item.Ethan-Arrowood commentedon Nov 25, 2025
Does @pavinduLakshan solution still work on Docusaurus v3.9.1 ?
I tried adding that file and it seems to just break my site completely. Is this going to work come v4 too?My mistake; its working as specified literally, but trying to adjust it to work with TS is giving me some issues.
I updated the file ending
.tsxand then used this: