import { Link, useColorMode, useLocale } from '@farmfe/docs/client'; import type { NotFoundProps, PublicSidebarItem, ThemeProps } from '@farmfe/docs/theme'; import type { ReactNode } from 'react'; import { useEffect, useId, useRef, useState } from 'react'; import { CodeBlock } from './code-block.js'; import { DiagramViewer } from './diagram-viewer.js'; import type { DocumentActionsLabels } from './document-actions.js'; import { Search } from './search.js'; export { CodeBlock, type CodeBlockProps } from './code-block.js'; export { DocumentActions, type DocumentActionsLabels, type DocumentActionsProps } from './document-actions.js'; export { Search, type SearchProps, SearchProvider } from './search.js'; export { SearchPage, type SearchPageLabels, type SearchPageProps } from './search-page.js'; function useMobileNavigation() { const [mobile, setMobile] = useState(false); useEffect(() => { let media: MediaQueryList; try { media = matchMedia('(max-width: 996px)'); } catch { return; } const update = () => setMobile(media.matches); update(); media.addEventListener('change', update); return () => media.removeEventListener('change', update); }, []); return mobile; } export function ColorModeControl() { const { colorMode, setColorMode } = useColorMode(); const chinese = useLocale().startsWith('zh'); const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); const next = colorMode === 'light' ? 'dark' : colorMode === 'dark' ? 'system' : 'light'; const names = chinese ? { light: '浅色', dark: '深色', system: '跟随系统' } : { light: 'Light', dark: 'Dark', system: 'Follow system' }; const label = chinese ? `当前主题:${names[colorMode]}。点击切换为${names[next]}。` : `Current theme: ${names[colorMode]}. Click to switch to ${names[next]}.`; return mounted ? : null; } export function MobileNavigation(props: ThemeProps & { navigation?: ReactNode }) { const dialog = useRef(null); const closeButton = useRef(null); const previousOverflow = useRef(null); const [open, setOpen] = useState(false); const id = useId(); function releaseScroll() { if (previousOverflow.current !== null) { document.body.style.overflow = previousOverflow.current; previousOverflow.current = null; } } function dismiss() { dialog.current?.close(); releaseScroll(); setOpen(false); } useEffect(() => { const element = dialog.current; const historyNavigation = () => { if (element?.open) dismiss(); }; window.addEventListener('popstate', historyNavigation); window.addEventListener('farm-docs:before-focus', historyNavigation); return () => { window.removeEventListener('popstate', historyNavigation); window.removeEventListener('farm-docs:before-focus', historyNavigation); if (element?.open) element.close(); releaseScroll(); }; }, []); return <> { releaseScroll(); setOpen(false); }} onCancel={event => { event.preventDefault(); dismiss(); }} onKeyDown={event => { if (event.key !== 'Tab') return; const stops = Array.from(event.currentTarget.querySelectorAll('a[href], area[href], button, input, select, textarea, summary, [tabindex], [contenteditable]')) .filter(element => (element.tabIndex >= 0 || (element.isContentEditable && !element.hasAttribute('tabindex'))) && !element.matches(':disabled') && !element.closest('[inert]') && element.getClientRects().length > 0 && getComputedStyle(element).visibility === 'visible') .sort((left, right) => (left.tabIndex > 0 ? left.tabIndex : Infinity) - (right.tabIndex > 0 ? right.tabIndex : Infinity)); const first = stops[0], last = stops[stops.length - 1]; if (first && last && ((event.shiftKey && document.activeElement === first) || (!event.shiftKey && document.activeElement === last))) { event.preventDefault(); (event.shiftKey ? last : first).focus(); } }} onClick={event => { if (event.target === event.currentTarget) { const bounds = event.currentTarget.getBoundingClientRect(); if (event.clientX < bounds.left || event.clientX > bounds.right || event.clientY < bounds.top || event.clientY > bounds.bottom) dismiss(); return; } if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; const anchor = event.target instanceof Element ? event.target.closest('a[href]') : null; // Release native modal focus before the existing document-level router acts. if (anchor && !anchor.hasAttribute('download') && anchor.getAttribute('target') !== '_blank') dismiss(); }}> {props.navigation} ; } export function Layout(props: ThemeProps) { const mobile = useMobileNavigation(); const slots = props.components; const Page = props.page.kind === 'blog' || props.page.blogView ? slots.BlogPage : slots.DocPage; return
Skip to content
{!mobile && }
{!mobile && }
; } function AlternateNavigation({ site, data, page }: ThemeProps) { const alternates = page.alternates ?? []; const languages = site.locales.items.flatMap(locale => alternates.filter(alternate => alternate.locale === locale && (alternate.version ?? null) === (page.version ?? null))); const versions = alternates.filter(alternate => alternate.locale === page.locale); return <>{([['Languages', languages], ['Versions', versions]] as const).map(([kind, items]) => items.length > 1 ? : null)}; } export function Navbar(props: ThemeProps) { const { site, data } = props; const mobile = useMobileNavigation(); const links = ; return
{mobile && {links}} />} {site.theme.logo && {site.theme.logo.alt}}{site.title} {links}
; } function SidebarItems({ items, active, expanded }: { items: PublicSidebarItem[]; active: string; expanded: readonly string[] }) { const containsActive = (items: PublicSidebarItem[]): boolean => items.some(item => item.type === 'doc' ? item.key === active : item.type === 'category' && containsActive(item.items)); return
    {items.map((item, index) =>
  • {item.type === 'category' ?
    {item.label}
    : {item.label}}
  • )}
; } export function Sidebar({ data, page, expandedCategories = [], items }: ThemeProps & { expandedCategories?: readonly string[]; items?: PublicSidebarItem[] }) { const sidebar = data.sidebars.find(item => item.collection === page.collection); const visible = items ?? sidebar?.items ?? []; return visible.length ? : null; } function PageContent({ page, children, beforeArticle }: { page: ThemeProps['page']; children: ReactNode; beforeArticle?: ReactNode }) { return
{page.fallbackSource && } {beforeArticle}
{children}
{(page.previous || page.next) && }
; } function sidebarAncestors(items: PublicSidebarItem[], key: string, ancestors: string[] = []): string[] | null { for (const item of items) { if (item.type === 'doc' && item.key === key) return ancestors; if (item.type === 'category') { const found = sidebarAncestors(item.items, key, [...ancestors, item.label]); if (found) return found; } } return null; } export function DocPage(props: ThemeProps & { homeLabel?: string; versionLabel?: string | null; tocLabel?: string; documentActionsLabels?: Partial; agentGuideUrl?: string | null }) { const { page, children, data, homeLabel = 'Home', versionLabel = page.version, tocLabel = 'On this page' } = props; const ancestors = sidebarAncestors(data.sidebars.find(sidebar => sidebar.collection === page.collection)?.items ?? [], page.key) ?? []; const metadata = page.kind === 'doc' ? <> {versionLabel && Version: {versionLabel}} {page.headings.length > 0 &&
{tocLabel}
} : null; return {children}; } export function BlogPage(props: ThemeProps) { const { page, data } = props; if (page.blogView?.fullContent && !props.listingPosts) throw new Error('[DOCS_BLOG_CONTENT] full listing articles were not loaded'); const post = page.blog; const views = ['blog-index', 'blog-archive', 'blog-tags'].flatMap(kind => { const target = data.pages.find(candidate => candidate.collection === page.collection && candidate.blogView?.kind === kind); return target ? [target] : []; }); return {views.length > 0 && } {post && (post.date || post.authors.length > 0 || post.tags.length > 0) &&
{post.date && } {post.authors.length > 0 &&
    {post.authors.map((author, index) =>
  • {author.image && {author.name}} {author.url ? {author.name} : {author.name}}
  • )}
} {post.tags.length > 0 && }
} }>{page.blogView?.fullContent && props.listingPosts?.length ? <>

{page.title}

{props.listingPosts?.map(post =>
{post.page.title} {post.page.blog?.date && } {post.children}
)} : props.children}
; } export function Toc({ page }: ThemeProps) { return ; } export function Footer({ site }: ThemeProps) { return
{site.theme.footer.links.map(link => {link.label})}{site.theme.footer.copyright &&

{site.theme.footer.copyright}

}
; } export function NotFound({ data }: NotFoundProps) { return

{data.notFound.heading}

{data.notFound.description}

{data.notFound.homeLabel}
; } export const components = { pre: CodeBlock }; export { LanguageIcon, NavigationIconLink, NavigationMenu } from './navigation.js'; export { filterSidebarSection, sidebarSection } from './sidebar-section.js';