Skip to main content

mkdocs-toggle-sidebar-plugin

PyPI version License Python versions

This package allows you to toggle the left (navigation) and right (table of contents) sidebars on a couple of MkDocs themes such as:

You can play around with it and these themes on the test page.

The settings are stored using the localStorage object, so that it will persist between pages.

I wrote it after getting frustrated by the browser's Find in page function matching way to many links in the navigation sidebar instead of searching in the actual page's content.

Note on Zensical, MkDocs 1.x, ProperDocs, etc

To make it easy to keep it up to date for all my plugins, I hosted my intentions of what platforms to support on my website.

Usage

Setup

First install the PyPI package:

pip install mkdocs-toggle-sidebar-plugin

Add something like the following to your mkdocs.yml:

plugins:
- search
- toggle-sidebar

Key bindings

If enable_key_bindings is True (the default), the plugin adds the following key bindings:

Key Action
b toggle both (TOC and navigation)
m toggle navigation menu
t toggle TOC

For some themes like readthedocs navigation and TOC are combined. In this case the state of TOC is ignored, and only calls for navigation (or all) are interpreted.

Configuration options

You can overwrite the defaults like this:

plugins:
- search
- toggle-sidebar:
    async: False
    debug: True
    enabled: True
    inline: False
    javascript: ./toggle-sidebar.js
    show_navigation_by_default: False
    show_toc_by_default: False
    theme: material
    toggle_button: all
    button_toggle_both_tooltip: Toggle Navigation and Table of Contents
    button_toggle_nav_tooltip: Toggle Navigation
    button_toggle_toc_tooltip: Toggle Table of Contents
    button_toggle_icon: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"></path></svg>'
    enable_key_bindings: False

The following options exist:

Option Type Default value Description
async bool False Asynchronously load the JavaScript file created by the plugin
debug bool False Show some debug messages during mkdocs build (for example related to theme detection)
enabled bool True Can be used to disable the plugin. Usually used in combination with environment variables like enabled: !ENV [TOGGLE_SIDEBAR, false] as described in mkdocs's docs
inline bool False Instead of storing the javascript code in the file specified by javascript, it is directly copied into each page. Slightly increases page size, but can improve load times a little bit and reduce flickering on page (re-)load
javascript str "assets/javascripts/toggle-sidebar.js" The path where to store the output file
show_navigation_by_default bool True Whether to show the navigation by default
show_toc_by_default bool True Whether to show the table of contents by default
theme str auto Used for theme detection. With auto, the plugin tries to automatically detect the theme. But you can also force it to use a specific theme preset that you know will work. Currently supported values: material/ansible, mkdocs, readthedocs.
toggle_button str "none" Can be set to show a toggle button (see below)
button_toggle_both_tooltip str "Toggle Navigation and Table of Contents" Tooltip to show when toggle_button is both
button_toggle_nav_tooltip str "Toggle Navigation" Tooltip to show when toggle_button is navigation
button_toggle_toc_tooltip str "Toggle Table of Contents" Tooltip to show when toggle_button is toc
button_toggle_icon str SVG of hamburger menu (three vertical bars on top of each other) SVG to show for the toggle button. Should be 24px by 24px in size.
enable_key_bindings bool True Registers key bindings to toggle ToC (T), navigation (M), or both (B)

Toggle button

When you set the toggle_button option to navigation, toc or all, it will add a button that looks like a hamburger menu (three horizontal bars) on a theme-dependent location. It is usually in the nav or the top bar. Clicking the button will toggle the navigation, table of contents, or both (depending on the supplied value). By leaving the field empty or setting it to none, no button is added.

You can set a custom icon for the button, using the button_toggle_icon configuration. The icon should be a 24px square SVG file.

The tooltip shown when hovering over the button can also be changed. Depending on which value you set in toggle_button, a different option is used:

  • If toggle_button is all, then button_toggle_both_tooltip is used.
  • If toggle_button is navigation, then button_toggle_nav_tooltip is used.
  • If toggle_button is toc, then button_toggle_toc_tooltip is used.

Exported API functions

This plugin exposes some JavaScript functions, that can show, hide or toggle the visibility of the sidebars. You can see how they are called in docs/javascript-functions.md and how they are defined in src/mkdocs_toggle_sidebar_plugin/toggle-sidebar.js.

In short there are:

  • MkdocsToggleSidebarPlugin.setNavigationVisibility(show: bool)
  • MkdocsToggleSidebarPlugin.setTocVisibility(show: bool)
  • MkdocsToggleSidebarPlugin.setAllVisibility(showNavigation: bool, showTOC: bool)
  • MkdocsToggleSidebarPlugin.toggleNavigationVisibility()
  • MkdocsToggleSidebarPlugin.toggleTocVisibility()
  • MkdocsToggleSidebarPlugin.toggleAllVisibility()

The names and parameters should be self-explanatory.

Theme support

Below shows the latest themes that I have tested. The table is not updated regularly, but the plugin should generally work for other theme versions too.

Theme Theme version Plugin version Status
mkdocs-ansible 25.6.0 0.0.6 works
mkdocs-material 9.6.14 0.0.4+ works
mkdocs-materialx 10.1.5 0.1.0 works
mkdocs (default) 1.6.1 0.0.4+ works
readthedocs 1.6.1 0.0.4+ works

Just open an issue / PR if you use a strange theme or the info above is not up-to-date anymore.

Note to self

Test material theme:

./serve.sh

Test mkdocs theme:

./serve.sh --theme mkdocs

Test mkdocs, readthedocs and material themes:

./build.sh
python3 -m http.server --directory './public/'

Test oldest python version supported by me (3.9):

docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:3.9 ./serve.sh

Test newest available python version (currently 3.13):

docker run --rm -it -v "$PWD:/share" -w "/share" -p 8000:8000 --entrypoint=bash python:latest ./serve.sh

Notable changes

Vershon 0.1.2

  • Suppress keyboard event handlers in mkdocs and readthedocs theme when text is being edited (see #16)
  • Added enable_key_bindings flag (enabled by default), which can be disabled if key bindings are not wanted or problematic (see #16)

Version 0.1.1

  • Fixed show_navigation_by_default not working (see #15)
  • Fixed toggle button in combination with material(x)'s toc-integrate (see #14)
  • Show a warning and correct toggle_button: toc when navigation and TOC are merged (readthedocs theme or material(x) with toc-integrate) (see comment in #14)

Thank you @adamant-pwn for finding all and fixing most of the above.

Version 0.1.0

  • Added autodetection for MaterialX theme.
  • Removed dependency declaration of mkdocs

Version 0.0.9

  • Added button_toggle_both_tooltip, button_toggle_nav_tooltip, button_toggle_toc_tooltip and button_toggle_icon options for customizing the toggle button (see #12)

Version 0.0.8

  • Fixed toggle button not shown in certain window dimensions in Material theme (see #11)

Version 0.0.7

  • Fixed sidebar not hidden in material's blog mode (see #9). Thank you @ZnPdCo for finding and fixing the issue.

Version 0.0.6

  • Fixed toggle button appearing delayed on slow loading pages (see #6)
  • Fixed behavior when using Material's navigation.instant feature (see #5)
  • Added inline option that prevents page flickering on reload (see #4). It is now enabled by default and async is disabled by default, to prevent the flickering. To revert to the old behavior you can set async: True and inline: False in the plugin's config in your mkdocs.yml
  • Added theme option that allows you to override theme detection (see #3)
  • Added support for ansible theme (see #3)
  • Added fallback to check theme.extra.base_theme from mkdocs.yml when other theme detection logic fails (see #3)
  • Added debug option

Version 0.0.5

  • Bug fix: On small screens with the material theme the navigation would be hidden, even when the hamburger menu was opened.

Version 0.0.4

  • Export API via MkdocsToggleSidebarPlugin object. This lets you create custom buttons or key bindings to hide, show or toggle the side bars.
  • Added toggle_button option and implemented it for Material theme.

Version 0.0.3

  • Changed internal API:
    • Element hiding/restyling is now done via CSS, so it is easier to undo. You should no longer have problems on devices with small screens (like phones) having broken layouts.

Version 0.0.2

  • Added support for mkdocs and readthedocs theme.

Version 0.0.1

  • Prototype with mkdocs-material implementation.

Metadata

Release files for mkdocs-toggle-sidebar-plugin 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mkdocs-toggle-sidebar-plugin 0.1.2
File Size Uploaded
mkdocs_toggle_sidebar_plugin-0.1.2.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mkdocs-toggle-sidebar-plugin 0.1.2
File Interpreter ABI Platform
mkdocs_toggle_sidebar_plugin-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 32.8 kB

Release files / mkdocs_toggle_sidebar_plugin-0.1.2.tar.gz

Download URL mkdocs_toggle_sidebar_plugin-0.1.2.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e22449eba625dae4ad8ebaab60ee98e83b62105f9db9e43c820a88f1fa6118d4
BLAKE2b-256 checksum
How to use checksums
e8fcaa1fab8bb272d405ab1e494728a1738dbfaf6142eee99fc6bd5079fcb037
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / mkdocs_toggle_sidebar_plugin-0.1.2-py3-none-any.whl

Download URL mkdocs_toggle_sidebar_plugin-0.1.2-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce1fb6c28ad8c3e1a237ac87f2fe1668d573e57a2da6fe1c001e287a78d986ba
BLAKE2b-256 checksum
How to use checksums
103014e6dfc8b373a8dad78dbe63a6cab667c29b6b4fec4dd0547bebfd0e993f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page