Skip to content

Commit 8e67694

Browse files
feat: add disable_search theme option (#2315)
Co-authored-by: Daniel McCloy <dan@mccloy.info>
1 parent cd00a86 commit 8e67694

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

docs/user_guide/search.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,36 @@ Set the ``search_as_you_type`` HTML theme option to ``True``.
7474
html_theme_options = {
7575
"search_as_you_type": True
7676
}
77+
78+
Disable the built-in search
79+
---------------------------
80+
81+
If your site uses a third-party search backend (e.g. the `Read the Docs
82+
server-side search addon <https://docs.readthedocs.com/platform/stable/addons.html>`_),
83+
you may want to disable the built-in pydata search entirely. Set the
84+
``disable_search`` HTML theme option to ``True``:
85+
86+
.. code:: python
87+
88+
html_theme_options = {
89+
"disable_search": True
90+
}
91+
92+
This does two things automatically:
93+
94+
* The ``#pst-search-dialog`` overlay is omitted from the page, so the
95+
built-in search dialog never appears and keyboard shortcuts (:kbd:`Ctrl` +
96+
:kbd:`K` / :kbd:`` + :kbd:`K`) do not open it.
97+
* ``navbar_persistent`` is set to an empty list, so the default search button
98+
is no longer rendered in the navbar.
99+
100+
.. note::
101+
102+
``disable_search`` only controls ``search-button-field`` (the full navbar
103+
button showing the search label and keyboard shortcut) via
104+
``navbar_persistent``. It does not remove ``search-button`` (the icon-only
105+
variant) or ``search-field.html`` if you have placed either of those
106+
explicitly elsewhere in your layout. Remove them manually if needed.
107+
108+
If you set ``navbar_persistent`` explicitly in your ``html_theme_options``,
109+
that value takes precedence and ``disable_search`` will not modify it.

src/pydata_sphinx_theme/__init__.py

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

51+
# If the user hasn't explicitly set navbar_persistent, default it based on
52+
# disable_search: show the search button field unless search is disabled.
53+
if "navbar_persistent" not in theme_options:
54+
if theme_options.get("disable_search", False):
55+
navbar_persistent = []
56+
else:
57+
navbar_persistent = ["search-button-field"]
58+
theme_options["navbar_persistent"] = navbar_persistent
59+
5160
# Set the anchor link default to be # if the user hasn't provided their own
5261
if not utils.config_provided_by_user(app, "html_permalinks_icon"):
5362
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ 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

4243
# Template placement in theme layouts
4344
navbar_start = navbar-logo
4445
navbar_center = navbar-nav
4546
navbar_end = theme-switcher, navbar-icon-links
46-
navbar_persistent = search-button-field
47+
navbar_persistent =
4748
article_header_start = breadcrumbs
4849
article_header_end =
4950
article_footer_items =

0 commit comments

Comments
 (0)