Skip to content

Commit 90865d8

Browse files
authored
never close the sidebar when the mouse is over it (#1283)
* never close the sidebar when the mouse is over it closes #1281 * prettier * :focus-within also allows to tab to the results * keep the backdrop while tabbing * add missing "side bar is open" selectors * simpler focus
1 parent 7d7ce82 commit 90865d8

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/client/search-init.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,19 @@ input.addEventListener("focus", load, {once: true});
1010
input.addEventListener("keydown", load, {once: true});
1111

1212
// Focus on meta-K and /
13-
const toggle = document.querySelector("#observablehq-sidebar-toggle")!;
13+
const toggle = document.querySelector<HTMLInputElement>("#observablehq-sidebar-toggle")!;
1414
addEventListener("keydown", (event) => {
1515
if (
1616
(event.code === "KeyK" && event.metaKey && !event.altKey && !event.ctrlKey) ||
1717
(event.key === "/" && !event.metaKey && !event.altKey && !event.ctrlKey && event.target === document.body)
1818
) {
1919
// Force the sidebar to be temporarily open while the search input is
20-
// focused. (We can’t use :focus-within because the sidebar isn’t focusable
21-
// while it is invisible, and we don’t want to keep the sidebar open
22-
// persistently after you blur the search input.)
23-
toggle.classList.add("observablehq-sidebar-open");
24-
input.focus();
20+
// focused. We click on the toggle if necessary to make the input visible so
21+
// it be focused. Then the :focus-within selector takes over and keeps it
22+
// open until the user blurs the input.
23+
if (toggle.checked) input.focus();
24+
else toggle.click(), input.focus(), toggle.click();
2525
input.select();
2626
event.preventDefault();
2727
}
2828
});
29-
30-
// Allow the sidebar to close when the search input is blurred.
31-
input.addEventListener("blur", () => toggle.classList.remove("observablehq-sidebar-open"));

src/style/layout.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@
299299
left 150ms 0ms ease;
300300
}
301301

302-
.observablehq-sidebar-open ~ #observablehq-sidebar,
302+
#observablehq-sidebar:focus-within,
303303
#observablehq-sidebar-toggle:checked ~ #observablehq-sidebar {
304304
left: 0;
305305
visibility: initial;
@@ -319,12 +319,13 @@
319319
z-index: 2;
320320
}
321321

322-
.observablehq-sidebar-open ~ #observablehq-sidebar-backdrop,
322+
#observablehq-sidebar-backdrop:has(~ #observablehq-sidebar:focus-within),
323323
#observablehq-sidebar-toggle:checked ~ #observablehq-sidebar-backdrop {
324324
display: initial;
325325
}
326326

327327
@media (prefers-color-scheme: dark) {
328+
#observablehq-sidebar:focus-within,
328329
#observablehq-sidebar-toggle:checked ~ #observablehq-sidebar {
329330
box-shadow: 0 0 8px 4px rgba(0, 0, 0, 0.5);
330331
}

0 commit comments

Comments
 (0)