Skip to content

Commit d8708f4

Browse files
committed
feat: add disable_search theme option
Add a new boolean theme option `disable_search` (default: `False`) that allows users to completely remove the built-in PyData search dialog and its associated navbar button. This is useful when a third-party search addon provides its own UI and keyboard shortcut, such as the Read the Docs server-side search addon, which would otherwise conflict with PyData's dialog and Ctrl+K handler. Changes: - theme.conf: add `disable_search = False` option - layout.html: wrap `#pst-search-dialog` in a new `{% block search_dialog %}` block gated on `theme_disable_search`. The dedicated block also gives downstream themes/overrides a clean extension point without copying the entire `content` block. - pydata-sphinx-theme.js: add null-guards in `toggleSearchField()` and `setupSearchButtons()` so both functions are safe when the dialog element is absent. Previously both functions unconditionally queried `#pst-search-dialog`, throwing a `TypeError` if the element was not in the DOM. - __init__.py: when `disable_search=True`, automatically remove `search-button-field` from `navbar_persistent` so the user only needs to set one option. Closes: #202 Related: #795, #1933 Signed-off-by: Philip Schmid <phisch@cisco.com>
1 parent 213af5a commit d8708f4

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/pydata_sphinx_theme/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ def update_config(app):
4848
"a value (leave undefined), or set to an empty list."
4949
)
5050

51+
# When search is disabled, remove the built-in search button from the navbar
52+
# so the user only needs to set one option instead of also clearing navbar_persistent.
53+
if theme_options.get("disable_search", False):
54+
navbar_persistent = theme_options.get(
55+
"navbar_persistent", ["search-button-field"]
56+
)
57+
if isinstance(navbar_persistent, str):
58+
navbar_persistent = [s.strip() for s in navbar_persistent.split(",")]
59+
theme_options["navbar_persistent"] = [
60+
t for t in navbar_persistent if t.strip() != "search-button-field"
61+
]
62+
5163
# Set the anchor link default to be # if the user hasn't provided their own
5264
if not utils.config_provided_by_user(app, "html_permalinks_icon"):
5365
app.config.html_permalinks_icon = "#"

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ var toggleSearchField = () => {
185185
// if the input field is the hidden one (the one associated with the
186186
// search button) then toggle the button state (to show/hide the field)
187187
const searchDialog = document.getElementById("pst-search-dialog");
188+
if (!searchDialog) {
189+
// Search dialog was disabled via disable_search theme option; nothing to do.
190+
return;
191+
}
188192
const hiddenInput = searchDialog.querySelector("input");
189193
if (input === hiddenInput) {
190194
if (searchDialog.open) {
@@ -301,6 +305,10 @@ var setupSearchButtons = () => {
301305

302306
// If user clicks outside the search modal dialog, then close it.
303307
const searchDialog = document.getElementById("pst-search-dialog");
308+
if (!searchDialog) {
309+
// Search dialog was disabled via disable_search theme option; nothing to do.
310+
return;
311+
}
304312
// Dialog click handler includes clicks on dialog ::backdrop.
305313
searchDialog.addEventListener("click", closeDialogOnBackdropClick);
306314
};

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,17 @@
6969
</button>
7070
{%- endif %}
7171

72-
{# A search field pop-up that will only show when the search button is clicked #}
72+
{%- block search_dialog %}
73+
{# A search field pop-up that will only show when the search button is clicked. #}
74+
{# Set disable_search = True in html_theme_options to omit this dialog entirely, #}
75+
{# e.g. when using a third-party search addon (such as Read the Docs server-side #}
76+
{# search) that provides its own UI and keyboard shortcut. #}
77+
{%- if not theme_disable_search | tobool %}
7378
<dialog id="pst-search-dialog">
7479
{% include "../components/search-field.html" %}
7580
</dialog>
81+
{%- endif %}
82+
{%- endblock search_dialog %}
7683

7784
{% include "sections/announcement.html" %}
7885

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/theme.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ logo =
3636
logo_link =
3737
surface_warnings = True
3838
back_to_top_button = True
39+
disable_search = False
3940
search_as_you_type = False
4041
shorten_urls = True
4142

0 commit comments

Comments
 (0)