import type { NormalizedSidebarGroup, SidebarItem as SidebarItemType, } from '@rspress/core'; import { useActiveMatcher } from '@rspress/core/runtime'; import { Link, renderInlineMarkdown, Tag } from '@rspress/core/theme'; import clsx from 'clsx'; import type React from 'react'; import { useEffect, useRef, useTransition } from 'react'; import './SidebarItem.scss'; import scrollIntoView from 'scroll-into-view-if-needed'; export function SidebarItemRaw({ active, text, tag, link, context, className, left, right, onClick, depth, }: { className?: string; active: boolean; text: string; tag: SidebarItemType['tag']; link: string | undefined; depth: number; context?: string; left?: React.ReactNode; right?: React.ReactNode; onClick?: React.MouseEventHandler; }) { const ref = useRef(null); const [isPending, startTransition] = useTransition(); useEffect(() => { if (active && ref.current) { scrollIntoView(ref.current, { scrollMode: 'if-needed', block: 'center', inline: 'center', boundary: element => { const isBoundary = element.classList.contains('rp-doc-layout__sidebar') || element.classList.contains('rspress-doc'); return !isBoundary; }, }); } }, [active]); const innerContent = ( <>
{left}
{right}
); if (link) { return ( {innerContent} ); } return (
{innerContent}
); } export interface SidebarItemProps { item: SidebarItemType | NormalizedSidebarGroup; depth: number; className?: string; } export function SidebarItem(props: SidebarItemProps) { const { item, depth, className } = props; const activeMatcher = useActiveMatcher(); const active = Boolean( 'link' in item && item.link && activeMatcher(item.link), ); return ( ); }