Skip to content

Commit f2ba14a

Browse files
authored
feat: switch to readthedocs search addon (#398)
1 parent 7ccd91a commit f2ba14a

File tree

4 files changed

+44
-102
lines changed

4 files changed

+44
-102
lines changed

_static/css/pydata-overrides.css

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -125,84 +125,3 @@ sphinx search extension interface.
125125
.search-button__wrapper.show .search-button__search-container {
126126
width: 15%;
127127
}
128-
129-
/* Handle actual styling of the RtD search extension's screen */
130-
.search__outer {
131-
background-color: var(--pst-color-on-background);
132-
border: var(--pst-color-border);
133-
border-radius: 0.25em;
134-
}
135-
136-
.search__outer__input {
137-
background-color: var(--pst-color-on-background);
138-
color: var(--pst-color-text-base);
139-
font-size: var(--pst-font-size-icon);
140-
}
141-
142-
.search__result__title {
143-
color: var(--pst-color-primary);
144-
border-bottom-color: var(--pst-color-primary)
145-
}
146-
147-
.search__result__content,
148-
.search__result__subheading,
149-
.search__error__box {
150-
color: var(--pst-color-text-base);
151-
}
152-
153-
.search__result__subheading span {
154-
border-bottom-color: var(--pst-color-text-base);
155-
}
156-
157-
/* Dark theme config */
158-
html[data-theme="dark"] {
159-
160-
.search__outer .search__result__content span,
161-
.search__outer .search__result__title span {
162-
/* Dark mode background color */
163-
/* background-color: var(--pst-color-muted-highlight); */
164-
color: var(--pst-color-primary);
165-
border-bottom-color: var(--pst-color-primary);
166-
}
167-
168-
/* Set color for the line between search text and results */
169-
.search__outer .bar:after,
170-
.search__outer .bar:before {
171-
background: var(--pst-color-primary);
172-
}
173-
174-
.rtd__search__credits {
175-
border: 1px solid var(--pst-color-border);
176-
border-width: 1px 0 0 0;
177-
}
178-
}
179-
180-
.rtd__search__credits {
181-
background-color: var(--pst-color-background);
182-
color: var(--pst-color-text-muted);
183-
/* So text is centered vertically */
184-
height: 37px;
185-
}
186-
187-
.rtd__search__credits a {
188-
color: var(--pst-color-link);
189-
}
190-
191-
.search__outer .search__result__content span,
192-
.search__outer .search__result__title span {
193-
border-bottom-color: var(--pst-color-text-base);
194-
}
195-
196-
/* Make sure "X" remains visible */
197-
.search__cross__img {
198-
fill: var(--pst-color-text-base);
199-
}
200-
201-
/* Prevent hover from actually changing the color by setting it to what it
202-
already is */
203-
.outer_div_page_results:hover,
204-
.search__result__box .active {
205-
background-color: var(--pst-color-on-background);
206-
}
207-
208-
/* End of styling of the RtD search extension's screen */

_static/js/pydata-search-close.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Script to allow use of readthedocs-sphinx-search extension with the pydata
2-
// theme
1+
// Script to allow use of readthedocs-sphinx-search extension with the pydata theme
32
//
43
// Based in part on:
54
// https://github.com/pydata/pydata-sphinx-theme/blob/v0.13.3/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js#L167-L272
@@ -28,54 +27,80 @@ var findSearchInput = () => {
2827
}
2928
};
3029

31-
/** Function to hide the search field */
32-
var hideSearchField = () => {
33-
30+
// Hide Pydata theme's search
31+
var hidePydataSearch = () => {
3432
let input = findSearchInput();
3533
let searchPopupWrapper = document.querySelector(".search-button__wrapper");
3634
let hiddenInput = searchPopupWrapper.querySelector("input");
37-
35+
3836
if (input === hiddenInput) {
3937
searchPopupWrapper.classList.remove("show");
4038
}
41-
39+
4240
if (document.activeElement === input) {
4341
input.blur();
4442
}
4543
};
4644

47-
/** Add an event listener for hideSearchField() for Escape*/
45+
// Hide the ReadTheDocs search (addon version)
46+
function hideRtdSearch() {
47+
// Grab the search element from the DOM
48+
const searchElement = document.querySelector('readthedocs-search');
49+
searchElement.closeModal();
50+
}
51+
52+
// Hide any open search screens
53+
function hideSearch() {
54+
hidePydataSearch();
55+
hideRtdSearch();
56+
}
57+
58+
// Show the ReadTheDocs search (addon version)
59+
function showRtDSearch() {
60+
const searchElement = document.querySelector('readthedocs-search');
61+
searchElement.showModal();
62+
63+
// If we're displaying ReadTheDocs search, make sure to hide the Pydata theme's search
64+
hidePydataSearch();
65+
}
66+
67+
// Add event listeners for key strokes
4868
var addEventListenerForSearchKeyboard = () => {
4969
window.addEventListener(
5070
"keydown",
5171
(event) => {
5272
// Allow Escape key to hide the search field
5373
if (event.code == "Escape") {
54-
hideSearchField();
74+
hidePydataSearch();
75+
}
76+
77+
// Open the ReadTheDocs search modal when Ctrl+K is pressed
78+
if (event.ctrlKey && event.key === 'k') {
79+
event.preventDefault(); // Prevent default behavior of Ctrl+K
80+
showRtDSearch()
5581
}
5682
},
5783
true
5884
);
5985
};
6086

61-
/** Activate callbacks for search button popup */
87+
// Activate callbacks for search button popup
6288
var setupSearchButtons = () => {
6389
addEventListenerForSearchKeyboard();
90+
91+
// Add event listeners to elements with class "search-button__button"
92+
const searchButtons = document.querySelectorAll('.search-button__button');
93+
searchButtons.forEach(button => {
94+
button.addEventListener('click', showRtDSearch);
95+
});
6496
};
6597

6698
// Custom code to manage closing the RtD search dialog properly
6799
$(document).ready(function(){
68-
$(".search__cross").click(function(){
69-
hideSearchField();
70-
});
71-
$(".search__outer__wrapper.search__backdrop").click(function(){
72-
hideSearchField();
73-
});
74100
$(".search-button__overlay").click(function(){
75-
// Shouldn't be necessary since it's currently hidden by CSS, but just in
76-
// case
101+
// Shouldn't be necessary since it's currently hidden by CSS, but just in case
77102
console.log("Close by search-button__overlay");
78-
hideSearchField();
103+
hidePydataSearch();
79104
});
80105
});
81106

conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
'sphinx.ext.autodoc',
9999
'sphinx_copybutton',
100100
'sphinx_design',
101-
'sphinx_search.extension',
102101
'sphinx.ext.intersphinx',
103102
]
104103

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Babel==2.16.0
22
myst-parser==2.0.0
33
pydata-sphinx-theme==0.14.4
4-
readthedocs-sphinx-search==0.3.2
54
sphinx==7.2.6
65
sphinx-copybutton==0.5.2
76
sphinx-hoverxref==1.3.0

0 commit comments

Comments
 (0)