Skip to content

Adaptive Navbar docsVersionDropdown for multi-SDK doc websites #3930

Closed

Description

@sidvenu

🚀 Feature

If a Docusaurus project has documentation for 2 or more SDKs (which is supported in the latest releases thanks to multi-doc-plugin support - see #2528 (comment)), the Navbar docsVersionDropdown should have the ability to display the versions of the SDK page the user is currently in.

Suppose the user is in the page of Android SDK Docs. The Navbar should display versions of Android SDK. If they go to iOS docs, the navbar should now display versions of iOS SDK.

Have you read the Contributing Guidelines on issues?

Yes.

Motivation

We are a company that has multiple tiers of SDKs that has different versioning timelines.

Pitch

Having a multi-doc website with a navbar version dropdown that doesn't change according to the SDK you select confuses many people.

The structure of Navbar dropdown right now: {type: "docsVersionDropdown", docsPluginId: "ios"}
We can have something similar to: {type: "docsVersionDropdown", defaultDocsPluginId: "ios", dynamicVersioningChange: true} for enabling dynamic versioning change according to the SDK docs the user is currently viewing. The defaultDocsPluginId can be used for knowing what plugin version to display in non-docs pages like Home, Blogs etc.

Future Scope

An 'override' parameter in each plugin for choosing which pluginId versioning has to be displayed in the navbar.

Activity

  1. added
    feature
    status: needs triage
    on Dec 17, 2020
  2. sidvenu commented on Dec 17, 2020

    @sidvenu
    Author

    @slorber as discussed in Discord. What do you think of the proposal? Would love to start work on this if this is approved.

  3. slorber commented on Dec 18, 2020

    @slorber
    Collaborator

    What about adding multiInstance: true in config so that it adapts?
    The docsPluginId option can serve as the default instance if multi-instance support is enabled?

  4. sidvenu commented on Dec 18, 2020

    @sidvenu
    Author

    That makes sense. However, I feel the name docsPluginId alone does not explain how it behaves when it is in multiInstance. A better name like defaultDocsPluginId would give an insight into how it behaves in both cases.

  5. StanGirard commented on Mar 11, 2021

    @StanGirard

    @sidvenu Have you found any workaround for now to get it working?

  6. slorber commented on Mar 11, 2021

    @slorber
    Collaborator

    if anyone has an existing site online with the UX problem, that would likely help to understand better what a solution could look like

  7. slorber commented on Mar 12, 2021

    @slorber
    Collaborator

    FYI some idea I have that could also solve this issue: #4389 (comment)

  8. StanGirard commented on Mar 12, 2021

    @StanGirard

    @slorber That is exactly what we are looking for. Thanks for being so pro-active.

  9. ngaer commented on Apr 14, 2021

    @ngaer

    Currently could be solved using custom NavbarItem (src/theme/NavbarItem.js)

    import React from 'react';
    import OriginalNavBarItem from '@theme-original/NavbarItem';
    import { useLocation }  from '@docusaurus/router';
    
    export default function NavbarItem(props) {
      const { docsPluginId, type } = props
      const { pathname } = useLocation()
    
      if (type === 'docsVersionDropdown' && pathname.search(new RegExp(`^/${docsPluginId}/`, 'g')) === -1) {
        return null
      }
    
      return (
        <>
          <OriginalNavBarItem {...props} />
        </>
      );
    }
  10. slorber commented on Apr 14, 2021

    @slorber
    Collaborator

    Thanks @ngaer , looks like a nice temporary workaround

  11. iamayushdas commented on May 18, 2021

    @iamayushdas
    Contributor
    docsPluginId

    can you help me in using this

  12. Josh-Cena commented on Oct 30, 2021

    @Josh-Cena
    Collaborator

    Closing in favor of #4389.

  13. added
    closed: duplicate
    and removed
    status: needs triage
    on Mar 10, 2022
  14. randomJoe211 commented on Oct 17, 2022

    @randomJoe211

    Currently could be solved using custom NavbarItem (src/theme/NavbarItem.js)

    import React from 'react';
    import OriginalNavBarItem from '@theme-original/NavbarItem';
    import { useLocation }  from '@docusaurus/router';
    
    export default function NavbarItem(props) {
      const { docsPluginId, type } = props
      const { pathname } = useLocation()
    
      if (type === 'docsVersionDropdown' && pathname.search(new RegExp(`^/${docsPluginId}/`, 'g')) === -1) {
        return null
      }
    
      return (
        <>
          <OriginalNavBarItem {...props} />
        </>
      );
    }

    Hi @ngaer, thank you so much for the workaround! 💐 It really helped.
    One question is that after applying the workaround, the docsVersionDropdown of doc instances that are versioned also disappeared. How can I get them out, and only hide the docsVersionDropdown for the doc instances that are not versioned?

  15. slorber commented on Oct 19, 2022

    @slorber
    Collaborator

    @randomJoe211 it's your responsability to implement the logic that decides if something is displayed or not, we can't really help you here

      if (type === 'docsVersionDropdown' && pathname.search(new RegExp(`^/${docsPluginId}/`, 'g')) === -1) {
        return null
      }

    You have to create the if condition that makes sense for your project. The above may work for @ngaer but won't work for all use-cases, and you have to adapt it. In fact, it is likely to not work for many projects because it assumes the pathname contains the docsPluginId, which is generally false.

    There is an undocumented hook that you can use to know which plugin created the current route:

    import useRouteContext from '@docusaurus/useRouteContext';
    
    const {name,id} = useRouteContext().plugin;
  16. randomJoe211 commented on Oct 20, 2022

    @randomJoe211

    Thank you slorber! You are right about creating the if condition that makes sense for my project. I adjusted the condition as follows and it is working well:

    if (type === 'docsVersionDropdown'){
      const paths = pathname.split('/')
      if(paths[1]==='myPluginId') {
      return null
      }
    }

    As a workaround, this is good enough. Still, I look forward to a standard feature to resolve this issue.

  17. aneesha178 commented on Jan 7, 2023

    @aneesha178

    Currently could be solved using custom NavbarItem (src/theme/NavbarItem.js)

    import React from 'react';
    import OriginalNavBarItem from '@theme-original/NavbarItem';
    import { useLocation }  from '@docusaurus/router';
    
    export default function NavbarItem(props) {
      const { docsPluginId, type } = props
      const { pathname } = useLocation()
    
      if (type === 'docsVersionDropdown' && pathname.search(new RegExp(`^/${docsPluginId}/`, 'g')) === -1) {
        return null
      }
    
      return (
        <>
          <OriginalNavBarItem {...props} />
        </>
      );
    }

    Hi @ngaer / @randomJoe211 / @slorber , I seem to have a similar requirement. The only difference is I want the Navbar dropdown items to change with the versions. I was wondering if you could help me with the logic here?

    I want to customize my Navbar in such a way that, say when versions 1.2.0 and below are viewed, only Dropdown A should be visible. When versions 1.3.0 and above are viewed, Dropdown B or more must be visible.

    For clear understanding:
    Navbar when browsing version 1.3.0 and above:

    image

    Navbar when browsing version 1.2.0 and below:

    image

  18. slorber commented on Jan 19, 2023

    @slorber
    Collaborator

    @aneesha178 track #4389 for an upcoming official solution to solve this problem through config files.

    Until then I suggest taking a look at CSS classes that we add to the top-level <html> element of your Docusaurus site, those classes should help you create CSS selectors that hide/show relevant items depending on context.

    <html class="plugin-docs plugin-id-default docs-version-2.2.0">

    You could for example create selectors like this:

    html:not(.docs-version-2.2.0) .myNavbarItem {
      display: none;
    }

    You'd have to assign a custom class like .myNavbarItem to the element you want to hide for v2.2.0, this can be provided through navbar item themeConfig className

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed: duplicatefeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions