Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(docs): bring back search in the navbar and make the docs home field work

thinhlpga72880c50f7e
diff --git a/docs/src/components/DocSearch/index.tsx b/docs/src/components/DocSearch/index.tsx
index 94406280f..1b23ef241 100644
--- a/docs/src/components/DocSearch/index.tsx
+++ b/docs/src/components/DocSearch/index.tsx
@@ -21,8 +21,9 @@ type IndexState =
let cachedIndex: NextraIndex | null = null
type DocSearchProps = {
- /** `navbar` is the inline desktop field; `panel` is the full-width mobile one. */
- variant?: 'navbar' | 'panel'
+ /** `navbar` is the inline desktop field; `panel` is the full-width mobile
+ * one; `hero` is the large field on the docs landing page. */
+ variant?: 'navbar' | 'panel' | 'hero'
className?: string
/** Called after a result is picked, so the mobile menu can close itself. */
onNavigate?: () => void
@@ -84,8 +85,15 @@ const DocSearch = ({
)
return
if (e.key === '/' || (e.key.toLowerCase() === 'k' && (e.metaKey || e.ctrlKey))) {
+ // Several fields can be mounted at once (the navbar's is only CSS-hidden
+ // on small screens), and every one listens: only the first visible one
+ // takes the shortcut, so focus never lands in a hidden field.
+ const target = Array.from(
+ document.querySelectorAll<HTMLInputElement>('input[data-doc-search]')
+ ).find((input) => input.offsetParent !== null)
+ if (!target || target !== inputRef.current) return
e.preventDefault()
- inputRef.current?.focus()
+ target.focus()
}
}
document.addEventListener('keydown', onKeyDown)
@@ -145,6 +153,7 @@ const DocSearch = ({
<SearchIcon className="absolute left-3 size-4 text-gray-400 pointer-events-none" />
<input
ref={inputRef}
+ data-doc-search=""
type="search"
value={query}
spellCheck={false}
@@ -161,7 +170,8 @@ const DocSearch = ({
}}
onKeyDown={onInputKeyDown}
className={cn(
- 'w-full rounded-lg border border-gray-200 bg-gray-50 py-2 pl-9 pr-12',
+ 'w-full border border-gray-200 bg-gray-50 pl-9 pr-12',
+ variant === 'hero' ? 'rounded-2xl py-3.5' : 'rounded-lg py-2',
'text-sm text-black placeholder:text-gray-400',
'transition-colors focus:border-gray-300 focus:bg-white focus:outline-none',
'[&::-webkit-search-cancel-button]:appearance-none'
diff --git a/docs/src/components/DocsHome.tsx b/docs/src/components/DocsHome.tsx
index 3732ff9b7..9b5e0108d 100644
--- a/docs/src/components/DocsHome.tsx
+++ b/docs/src/components/DocsHome.tsx
@@ -1,6 +1,7 @@
/* eslint-disable @next/next/no-img-element */
-import { Computer, Search, Rocket, Bot, type LucideIcon } from 'lucide-react'
+import { Computer, Rocket, Bot, type LucideIcon } from 'lucide-react'
import LogoJanSVG from '@/assets/icons/logo-jan.svg'
+import DocSearch from './DocSearch'
type Section = {
icon: LucideIcon
@@ -61,16 +62,7 @@ const DocsHome = () => {
References for Jan Desktop and Jan Agent.
</p>
- <button
- type="button"
- className="mt-8 w-full max-w-xl mx-auto flex items-center gap-3 rounded-2xl border border-black/10 dark:border-white/10 bg-black/[0.03] dark:bg-white/[0.06] px-4 py-3.5 text-black/40 dark:text-white/40 hover:border-black/25 dark:hover:border-white/25 hover:bg-black/[0.05] transition-all"
- >
- <Search className="size-4 shrink-0" />
- <span className="flex-1 text-left text-sm">Search docs...</span>
- <kbd className="text-[11px] font-medium px-1.5 py-0.5 rounded-md border border-black/15 dark:border-white/15 bg-white dark:bg-white/10">
- ⌘K
- </kbd>
- </button>
+ <DocSearch variant="hero" className="mt-8 max-w-xl mx-auto" />
</div>
<div className="grid md:grid-cols-2 gap-5 mt-16 lg:mt-20 max-w-3xl mx-auto px-4">
diff --git a/docs/src/components/Navbar.tsx b/docs/src/components/Navbar.tsx
index 905156c58..e3139f58a 100644
--- a/docs/src/components/Navbar.tsx
+++ b/docs/src/components/Navbar.tsx
@@ -7,6 +7,7 @@ import { FaXTwitter, FaLinkedinIn } from 'react-icons/fa6'
import { ChevronDown } from 'lucide-react'
import { Button } from './ui/button'
import LogoJanSVG from '@/assets/icons/logo-jan.svg'
+import DocSearch from './DocSearch'
import { useDownloadLink } from '@/hooks/useDownloadLink'
type MenuItem = {
@@ -48,6 +49,8 @@ const Navbar = ({ noScroll }: { noScroll?: boolean }) => {
const currentPath = router.asPath
const isLanding = currentPath === '/'
+ // The docs home has its own large search field.
+ const isDocsHome = router.pathname === '/docs'
useEffect(() => {
const handleScroll = () => {
@@ -149,6 +152,14 @@ const Navbar = ({ noScroll }: { noScroll?: boolean }) => {
</li>
)
})}
+ {/* Landing page keeps the navbar transparent over artwork, where a
+ filled input would read as a stray form field. The docs home
+ has its own field, and two would compete for Cmd+K. */}
+ {!isLanding && !isDocsHome && (
+ <li>
+ <DocSearch />
+ </li>
+ )}
<li>
<a
href={downloadHref}
@@ -268,6 +279,15 @@ const Navbar = ({ noScroll }: { noScroll?: boolean }) => {
</button>
</div>
+ {/* Nextra's own sidebar search is unreachable here: our custom
+ hamburger drives this modal, not Nextra's sidebar state. */}
+ <div className="mb-6">
+ <DocSearch
+ variant="panel"
+ onNavigate={() => setIsMobileMenuOpen(false)}
+ />
+ </div>
+
{/* Menu Items */}
<nav className="mb-6">
<ul className="space-y-4">