Skip to content

Commit ec55a19

Browse files
committed
feat: swizzle search component to modify the version labels reliably
1 parent ca186f2 commit ec55a19

File tree

3 files changed

+593
-122
lines changed

3 files changed

+593
-122
lines changed

docs/index.mdx

Lines changed: 4 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ export const InlineSearch = () => {
1919
const [isExpanded, setIsExpanded] = useState(false);
2020
const [searchQuery, setSearchQuery] = useState('');
2121
const [debouncedQuery, setDebouncedQuery] = useState('');
22-
const [isIframeReady, setIsIframeReady] = useState(false);
2322
const inputRef = useRef(null);
2423
const iframeRef = useRef(null);
2524
const debounceTimerRef = useRef(null);
26-
const dropdownSelections = useRef({});
2725

2826
// Detect Mac for keyboard shortcut hint
2927
const isMac = typeof navigator !== 'undefined' && navigator.platform.toUpperCase().indexOf('MAC') >= 0;
@@ -48,8 +46,6 @@ export const InlineSearch = () => {
4846
e.preventDefault();
4947
setIsExpanded(false);
5048
setSearchQuery('');
51-
setIsIframeReady(false);
52-
dropdownSelections.current = {};
5349
}
5450
};
5551

@@ -88,7 +84,7 @@ export const InlineSearch = () => {
8884
const iframeDoc = iframe.contentDocument || iframe.contentWindow?.document;
8985
if (!iframeDoc) return;
9086

91-
// Inject CSS to hide elements - must be very aggressive
87+
// Inject CSS to hide elements
9288
const style = iframeDoc.createElement('style');
9389
style.textContent = `
9490
/* Hide announcement bar */
@@ -126,7 +122,7 @@ export const InlineSearch = () => {
126122
visibility: hidden !important;
127123
}
128124
129-
/* Hide the search input box in iframe - all variations */
125+
/* Hide the search input box in iframe */
130126
.ais-SearchBox,
131127
.ais-SearchBox-form,
132128
.ais-SearchBox-input,
@@ -170,7 +166,7 @@ export const InlineSearch = () => {
170166

171167
iframeDoc.addEventListener('click', handleLinkClick, true);
172168

173-
// Also observe for dynamically added links
169+
// Observe for dynamically added links
174170
const linkObserver = new MutationObserver((mutations) => {
175171
mutations.forEach((mutation) => {
176172
mutation.addedNodes.forEach((node) => {
@@ -189,96 +185,8 @@ export const InlineSearch = () => {
189185
childList: true,
190186
subtree: true
191187
});
192-
193-
// Initialize and restore dropdown selections before showing iframe
194-
const initializeDropdowns = () => {
195-
const selects = iframeDoc.querySelectorAll('select');
196-
197-
// Check if we have selects with options
198-
let hasValidSelects = false;
199-
selects.forEach((select) => {
200-
if (select.options && select.options.length > 0) {
201-
hasValidSelects = true;
202-
}
203-
});
204-
205-
if (!hasValidSelects) {
206-
return false; // Not ready yet
207-
}
208-
209-
// Set dropdowns to saved values or default to "Stable"
210-
selects.forEach((select, idx) => {
211-
if (!select.options) return;
212-
213-
// Get the select element's identifier (use index or name/id if available)
214-
const selectId = select.name || select.id || `select-${idx}`;
215-
216-
// If we have a saved value, restore it
217-
if (dropdownSelections.current[selectId]) {
218-
const savedValue = dropdownSelections.current[selectId];
219-
select.value = savedValue;
220-
// Update selectedIndex to match
221-
for (let i = 0; i < select.options.length; i++) {
222-
if (select.options[i].value === savedValue) {
223-
select.selectedIndex = i;
224-
break;
225-
}
226-
}
227-
} else {
228-
// First time: find and set to "Stable"
229-
for (let i = 0; i < select.options.length; i++) {
230-
const option = select.options[i];
231-
const label = option.getAttribute('label') || option.text;
232-
233-
if (label.includes('Stable')) {
234-
select.value = option.value;
235-
select.selectedIndex = i;
236-
dropdownSelections.current[selectId] = option.value;
237-
break;
238-
}
239-
}
240-
}
241-
242-
// Trigger events to ensure Algolia picks up the change
243-
select.dispatchEvent(new Event('change', { bubbles: true }));
244-
select.dispatchEvent(new Event('input', { bubbles: true }));
245-
246-
const form = select.closest('form');
247-
if (form) {
248-
form.dispatchEvent(new Event('change', { bubbles: true }));
249-
}
250-
251-
// Listen for user changes to this dropdown
252-
select.addEventListener('change', () => {
253-
dropdownSelections.current[selectId] = select.value;
254-
});
255-
});
256-
257-
return true; // Successfully initialized
258-
};
259-
260-
// Wait for dropdowns to initialize before showing iframe
261-
if (initializeDropdowns()) {
262-
// Dropdowns initialized successfully, show iframe
263-
setIsIframeReady(true);
264-
} else {
265-
// Watch for select elements to appear
266-
const selectObserver = new MutationObserver(() => {
267-
if (initializeDropdowns()) {
268-
setIsIframeReady(true);
269-
selectObserver.disconnect();
270-
}
271-
});
272-
273-
selectObserver.observe(iframeDoc.body, {
274-
childList: true,
275-
subtree: true
276-
});
277-
}
278188
} catch (e) {
279189
console.error('Cannot inject styles into iframe:', e);
280-
// Show iframe anyway even if injection failed
281-
setIsIframeReady(true);
282190
}
283191
};
284192

@@ -351,8 +259,6 @@ export const InlineSearch = () => {
351259
onClick={() => {
352260
setIsExpanded(false);
353261
setSearchQuery('');
354-
setIsIframeReady(false);
355-
dropdownSelections.current = {};
356262
}}
357263
style={{
358264
background: 'transparent',
@@ -384,27 +290,6 @@ export const InlineSearch = () => {
384290
position: 'relative'
385291
}}
386292
>
387-
{/* Loading indicator while iframe initializes */}
388-
{!isIframeReady && (
389-
<div
390-
style={{
391-
position: 'absolute',
392-
top: 0,
393-
left: 0,
394-
right: 0,
395-
bottom: 0,
396-
display: 'flex',
397-
alignItems: 'center',
398-
justifyContent: 'center',
399-
backgroundColor: 'white',
400-
zIndex: 10
401-
}}
402-
>
403-
<div style={{ color: 'var(--ifm-color-content-secondary)' }}>
404-
Loading search...
405-
</div>
406-
</div>
407-
)}
408293
<iframe
409294
ref={iframeRef}
410295
src={iframeSrc}
@@ -413,10 +298,7 @@ export const InlineSearch = () => {
413298
style={{
414299
width: '100%',
415300
height: '100%',
416-
border: 'none',
417-
visibility: isIframeReady ? 'visible' : 'hidden',
418-
opacity: isIframeReady ? 1 : 0,
419-
transition: 'opacity 0.2s ease'
301+
border: 'none'
420302
}}
421303
title="Search Documentation"
422304
/>

0 commit comments

Comments
 (0)