Skip to content

Feat Request: Native custom button support #64

@dylanh724

Description

@dylanh724

Here's what I did to add a custom button to my sphinx-book-theme:

  1. Add a js/customAlgoliaDocSearchBtn.js containing:
// #########################################
// _static/js/customAlgoliaDocSearchBtn.js
// - This script allows .
// - This script should be loaded deferred
// - Initial template suggestion thanks from yafimvo @ 
//   https://github.com/jupyter-book/jupyter-book/issues/1324#issuecomment-1512757574 
// #########################################

// Declare the selector for the search button
const SEARCH_BUTTON_SELECTOR = '.search-button__button';
const searchButtons = document.querySelectorAll(SEARCH_BUTTON_SELECTOR);

// On click, for all search bars found, emulate CTRL+K to bring up search UI for both buttons
searchButtons.forEach(button => {
    button.addEventListener('click', () => {
  	  console.log('[js/customAlgoliaDocSearchBtn.js] Clicked search btn -> emulating CTRL+K');
  	  const event = new KeyboardEvent('keydown', {
  		  key: 'k',
  		  ctrlKey: true,
  		  bubbles: true // Allows the event to propagate up through the DOM
  	  });
  	  document.dispatchEvent(event);
    });
});
  1. In conf.py, add:
html_js_files.append(("js/customAlgoliaDocSearchBtn.js", {"defer": "defer"}))

I forgot if I did anything else, but that's just about it. You could wrap this in your extension and allow a custom button that'll just simply trigger a CTLR+K to hook into your existing code. You'd just need a conf.py var that replaces the current SEARCH_BUTTON_SELECTOR for it to magically work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions