question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Theming: use custom components as navbar/sidebar/footer items

See original GitHub issue

Edit from @slorber:

Temporary recommended workaround

Until we have first-class support and a convenient API to support this, here’s the recommended way to add custom navbar items to your site (see also https://github.com/facebook/docusaurus/pull/7231)

Create a file in src/theme/NavbarItem/ComponentTypes.js to add a custom navbar item type to the existing mapping:

import ComponentTypes from '@theme-original/NavbarItem/ComponentTypes';
import MyAwesomeNavbarItem from '@site/src/components/NavbarItems/MyAwesomeNavbarItem';

export default {
  ...ComponentTypes,
  'custom-myAwesomeNavbarItem': MyAwesomeNavbarItem,
};

Use it in your config:

module.exports = {
  themeConfig: {
    navbar: {
      items: [
        {
          type: 'custom-myAwesomeNavbarItem', 
          position: "left",
          itemProp: 44, 
          anotherProp: "xyz"
        },
        //... other navbar items
      ]
    }
  }
}

Note: using the custom- prefix is important: the config validation schema will only allow item types with this prefix.


Original issue

Have you read the Contributing Guidelines on issues?

Description

We’d like to propose a way to add custom navbar item components through the navbar items configuration, so they remain part of the regular layout of items in the navbar and mobile primary menu. To our understanding three things need to change:

  1. Adjust the navbar items configuration validation to allow objects with a custom type property and a custom component property, similar to how the docs plugin allows a custom docItemComponent property to override the default rendering component. This object should allow arbitrary properties, which are passed as React properties to the custom component.

  2. Adjust the NavbarItem component to allow for custom types, require their configured custom component and passing any additional custom properties to the custom component.

  3. Improve API documentation on how navbar items should distinguish between if they are rendered for mobile or desktop, so the user can properly implement the component in case such a distinction is needed.

Open questions:

  1. Do we regard any unknown type as custom type or do we require a certain pattern?
  2. Do we require the custom component property to be configured or do we not? I guess in case the user swizzles the NavbarItem component and associates the type with a custom component there, requiring the component in the navbar items configuration is not strictly necessary.

Has this been requested on Canny?

No response

Motivation

We’ve implemented a custom dropdown component, which currently abuses the Docusaurus provided dropdown component configuration to bypass the configuration validation. This worked until beta.17, but from beta.18 onwards our implementation breaks, because of a major refactor in the navbar, which brought us to the point where we would like to propose a properly supported way of adding custom components to the navbar.

API design

Configuration of the custom component in the navbar items configuration:

navbar: {
  items: [
    {
      type: 'custom-type',
      component: '@theme/CustomComponent',
      customProperty: ...
    },
    ...
  ],
  ...
}

Have you tried building it?

We tried swizzling the NavbarItem component, but we ran into multiple issues, including correctly typing the custom component in the type to component map and configuration validation warnings. We’d be happy to create a PR after discussing the implementation proposed above.

Self-service

  • I’d be willing to contribute this feature to Docusaurus myself.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
lukedukeuscommented, Aug 30, 2022

Oh, that makes sense, thanks. I didn’t think to try that because I assumed it was just a suggestion of how it should work. This can be closed then?

0reactions
slorbercommented, Nov 16, 2022

@medmin it’s really difficult to understand what you mean. Please add a repro, at least a screenshot of dev vs prod + a config sample.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Theme Structure and Layout — PyData Theme documentation
Each footer element is wrapped in a <div> with a footer-item class, allowing you to style the structure of these items with custom...
Read more >
How to Add Custom Navigation Menus in WordPress Themes
In your WordPress dashboard, go to Appearance » Theme File Editor. In the right-hand menu, select the template that you want to edit....
Read more >
Theme configuration - Docusaurus
Navbar docs version; Navbar doc sidebar; Navbar with custom HTML. Note that the dropdown base item is a clickable link as well, so...
Read more >
Header, Footer, Sidebar Editor & Page Layouts
So, for a quick start, go with a pre-built header, footer, and sidebar template to be used as a basis for your own...
Read more >
How to Create a Navigation Bar and Sidebar Using React
import { BrowserRouter as Router, Route, Link, withRouter } from "react-router-dom";. Create a component underneath SideNav that wraps SideNav: · const ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found