Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow navbar items to be hidden based on path #7832

Closed
1 of 2 tasks
kiwicopple opened this issue Jul 26, 2022 · 3 comments
Closed
1 of 2 tasks

Allow navbar items to be hidden based on path #7832

kiwicopple opened this issue Jul 26, 2022 · 3 comments
Labels
closed: duplicate This issue or pull request already exists in another issue or pull request feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.

Comments

@kiwicopple
Copy link

kiwicopple commented Jul 26, 2022

Have you read the Contributing Guidelines on issues?

Motivation

I would like to show/hide navbar items based on the path. For example

  • /docs/cli - show a github link and version dropdown for our CLI
  • /docs/supabase-js - show a github link and version dropdown for our client libraries

Implementation idea

The NavBar Item has a field activeBaseRegex, which I thought I could re-purpose for this, but it appears this function doesn't run on all NavBar items (only "Navbar link"). If this can be applied to all top-level links, then we could use the navbar__link--active CSS class to show/hide navbar items

One more thing

Thank you! We really love Docusaurus at supabase 🦖 . I'd be happy to swizzle this and test it on a live site if someone could let me know what approach to take

Self-service

  • I'd be willing to do some initial work on this proposal myself.
@kiwicopple kiwicopple added proposal This issue is a proposal, usually non-trivial change status: needs triage This issue has not been triaged by maintainers labels Jul 26, 2022
@Josh-Cena Josh-Cena added feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. closed: duplicate This issue or pull request already exists in another issue or pull request and removed proposal This issue is a proposal, usually non-trivial change status: needs triage This issue has not been triaged by maintainers labels Jul 27, 2022
@Josh-Cena
Copy link
Collaborator

Duplicate of #4389. You can find some useful tips there. I'm going to close this for now—if you have further questions feel free to either ask under that issue or open a discussion.

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2022
@kiwicopple
Copy link
Author

got it, thanks for the link to the other issue - it really helped. For anyone coming here, this is how I solved it:

I added a custom regex to my navbar item (supabaseCustomNavBarRegex)

  {
    to: 'gotrue',
    position: 'left',
    label: 'GoTrue',
    supabaseCustomNavBarRegex: '(^/gotrue$|gotrue/)',
  },

Then I swizzled the NavbarItem, and show/hide based on the regex:

import React from 'react'
import { useLocation } from '@docusaurus/router'
import { isRegexpStringMatch } from '@docusaurus/theme-common'
import NavbarItem from '@theme-original/NavbarItem'

export default function NavbarItemWrapper(props) {
  const { pathname } = useLocation()
  if (
    !props.supabaseCustomNavBarRegex ||
    isRegexpStringMatch(props.supabaseCustomNavBarRegex, pathname)
  ) {
    return <NavbarItem {...props} />
  } else {
    return null
  }
}

@slorber
Copy link
Collaborator

slorber commented Aug 8, 2022

Great solution.

In terms encapsulation it's not 100% ideal because if the component itself decide to render null, the parent component (responsible for placing the navbar items in the navbar) wouldn't know it, and could still apply some margin/spacing that shouldn't be there.

We'll implement something similar, but we'll likely filter the navbar items in the parent component

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: duplicate This issue or pull request already exists in another issue or pull request feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.
Projects
None yet
Development

No branches or pull requests

3 participants