Skip to content

Commit ecc4694

Browse files
committed
Add keyboard shortcut for focusing on sidebar search
1 parent 3b8336d commit ecc4694

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
diff --git a/node_modules/@astrojs/starlight-docsearch/DocSearch.astro b/node_modules/@astrojs/starlight-docsearch/DocSearch.astro
2+
index f50c208..82218df 100644
3+
--- a/node_modules/@astrojs/starlight-docsearch/DocSearch.astro
4+
+++ b/node_modules/@astrojs/starlight-docsearch/DocSearch.astro
5+
@@ -110,14 +110,14 @@ const docsearchTranslations: DocSearchTranslationProps = {
6+
margin-inline-start: auto;
7+
}
8+
.DocSearch-Button-Keys::before {
9+
- content: '';
10+
- width: 1em;
11+
- height: 1em;
12+
- -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M17 2H7a5 5 0 0 0-5 5v10a5 5 0 0 0 5 5h10a5 5 0 0 0 5-5V7a5 5 0 0 0-5-5Zm3 15a3 3 0 0 1-3 3H7a3 3 0 0 1-3-3V7a3 3 0 0 1 3-3h10a3 3 0 0 1 3 3v10Z'%3E%3C/path%3E%3Cpath d='M15.293 6.707a1 1 0 1 1 1.414 1.414l-8.485 8.486a1 1 0 0 1-1.414-1.415l8.485-8.485Z'%3E%3C/path%3E%3C/svg%3E");
13+
- mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M17 2H7a5 5 0 0 0-5 5v10a5 5 0 0 0 5 5h10a5 5 0 0 0 5-5V7a5 5 0 0 0-5-5Zm3 15a3 3 0 0 1-3 3H7a3 3 0 0 1-3-3V7a3 3 0 0 1 3-3h10a3 3 0 0 1 3 3v10Z'%3E%3C/path%3E%3Cpath d='M15.293 6.707a1 1 0 1 1 1.414 1.414l-8.485 8.486a1 1 0 0 1-1.414-1.415l8.485-8.485Z'%3E%3C/path%3E%3C/svg%3E");
14+
- -webkit-mask-size: 100%;
15+
- mask-size: 100%;
16+
- background-color: currentColor;
17+
+ content: var(--docsearch-shortcut);
18+
+ font-size: 0.75em;
19+
+ font-weight: 600;
20+
+ opacity: 0.8;
21+
+ border: 1px solid var(--sl-color-gray-4);
22+
+ border-radius: 0.25rem;
23+
+ padding: 0.125rem 0.375rem;
24+
+ background-color: var(--sl-color-gray-6);
25+
}
26+
}
27+
</style>
28+
@@ -128,6 +128,11 @@ const docsearchTranslations: DocSearchTranslationProps = {
29+
class StarlightDocSearch extends HTMLElement {
30+
constructor() {
31+
super();
32+
+ const isApple = /Mac|iPhone|iPod|iPad/.test(navigator.platform);
33+
+ const shortcut = isApple ? '⌘K' : 'Ctrl+K';
34+
+
35+
+ this.style.setProperty('--docsearch-shortcut', `'${shortcut}'`);
36+
+
37+
window.addEventListener('DOMContentLoaded', async () => {
38+
const { default: docsearch } = await import('@docsearch/js');
39+
const options = { ...config, container: 'sl-doc-search' };

src/components/overrides/Sidebar.astro

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,30 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
255255
}
256256
}
257257
});
258+
259+
document.addEventListener(
260+
"keydown",
261+
(keyboardEvent) => {
262+
const target = keyboardEvent.target;
263+
264+
const isInput =
265+
target instanceof EventTarget &&
266+
(("tagName" in target &&
267+
(target.tagName === "INPUT" ||
268+
target.tagName === "TEXTAREA" ||
269+
target.tagName === "SELECT")) ||
270+
("isContentEditable" in target && target.isContentEditable));
271+
272+
if (keyboardEvent.key === "/" && !isInput) {
273+
keyboardEvent.preventDefault();
274+
keyboardEvent.stopPropagation();
275+
searchInput.focus();
276+
}
277+
},
278+
{
279+
capture: true,
280+
},
281+
);
258282
}
259283

260284
// Initialize when DOM is loaded

0 commit comments

Comments
 (0)