Skip to content

Commit c3f3ce0

Browse files
authored
[fix] fix sidebar search box (#3651)
1 parent 7867ca7 commit c3f3ce0

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/theme/DocItem/Layout/MobileSidebarDrawer.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState, useCallback } from 'react';
1+
import React, { useEffect, useRef, useState, useCallback } from 'react';
22
import clsx from 'clsx';
33
import { useLocation } from '@docusaurus/router';
44
import { useDocsSidebar } from '@docusaurus/plugin-content-docs/client';
@@ -20,6 +20,7 @@ export default function MobileSidebarDrawer(): JSX.Element | null {
2020
const isZH = currentLocale === 'zh-CN';
2121
const [open, setOpen] = useState(false);
2222
const [localeOpen, setLocaleOpen] = useState(false);
23+
const localeContainerRef = useRef<HTMLDivElement>(null);
2324

2425
const close = useCallback(() => setOpen(false), []);
2526

@@ -45,7 +46,12 @@ export default function MobileSidebarDrawer(): JSX.Element | null {
4546

4647
useEffect(() => {
4748
if (!localeOpen) return undefined;
48-
const onDocClick = () => setLocaleOpen(false);
49+
const onDocClick = (e: MouseEvent) => {
50+
const container = localeContainerRef.current;
51+
if (container && !container.contains(e.target as Node)) {
52+
setLocaleOpen(false);
53+
}
54+
};
4955
document.addEventListener('mousedown', onDocClick);
5056
return () => document.removeEventListener('mousedown', onDocClick);
5157
}, [localeOpen]);
@@ -59,8 +65,8 @@ export default function MobileSidebarDrawer(): JSX.Element | null {
5965
<SearchBar />
6066
</div>
6167
<div
68+
ref={localeContainerRef}
6269
className={clsx(styles.toolbarLocale, localeOpen && styles.toolbarLocaleOpen)}
63-
onMouseDown={e => e.stopPropagation()}
6470
>
6571
<button
6672
type="button"
@@ -87,10 +93,10 @@ export default function MobileSidebarDrawer(): JSX.Element | null {
8793
</button>
8894
<ul className={styles.localeMenu} role="menu">
8995
{locales.map(locale => {
90-
const baseTo = `pathname://${alternatePageUtils.createUrl({
96+
const baseTo = alternatePageUtils.createUrl({
9197
locale,
9298
fullyQualified: false,
93-
})}`;
99+
});
94100
const to = `${baseTo}${search}${hash}`;
95101
return (
96102
<li key={locale} role="none">

src/theme/DocSidebar/Desktop/styles.module.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@
5959
height: 2.25rem !important;
6060
}
6161

62+
/* Anchor the autocomplete dropdown to the input's left edge instead of the
63+
* navbar-default `left: -20%` rule that pulls the 560px-wide panel off the
64+
* left side of the viewport when the search bar lives in the sidebar.
65+
* The dropdown element is whatever autocomplete.js generates (a span/div),
66+
* so match by class substring. */
67+
.sidebarSearch :global(.navbar__search) [class*="dropdownMenu"] {
68+
left: 0 !important;
69+
right: auto !important;
70+
}
71+
72+
/* The Docusaurus sidebar container ships with `clip-path: inset(0)` (for the
73+
* collapse animation), which also clips our 560px search dropdown to the
74+
* 300px sidebar width. Disable it so the dropdown can overflow the sidebar. */
75+
:global(.theme-doc-sidebar-container) {
76+
clip-path: none !important;
77+
}
78+
6279
.sidebarLocale {
6380
flex: 0 0 auto;
6481
display: flex;

0 commit comments

Comments
 (0)