Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(siderbar): sidebar is not stable in dev and sometimes is set by h1 in md #1675

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions e2e/tests/heading-title.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ test.describe('heading-title test', async () => {
await page.goto(`http://localhost:${appPort}/guide`, {
waitUntil: 'networkidle',
});
const h1 = await page.$('h1');
const h1s = await page.$$('h1');
expect(h1s.length).toBe(1);

const h1 = h1s[0];
const className = await page.evaluate(h1 => h1?.className, h1);
expect(className).toContain('title_3b154'); // hash in css module should stable
expect(className).toContain('rspress-doc-title');
const text = await page.evaluate(h1 => h1?.textContent, h1);
expect(text).toContain('Heading Title');
expect(await page.evaluate(h1 => h1?.id, h1)).toBe('heading-title');
Expand Down
36 changes: 2 additions & 34 deletions packages/theme-default/src/components/Sidebar/SidebarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { useEffect, useRef } from 'react';
import {
isProduction,
normalizeHrefInRuntime as normalizeHref,
usePageData,
} from '@rspress/runtime';
import { normalizeHrefInRuntime as normalizeHref } from '@rspress/runtime';
import { Link, Tag } from '@theme';
import styles from './index.module.scss';
import { SidebarGroup } from './SidebarGroup';
import { type SidebarItemProps, highlightTitleStyle } from '.';
import { renderInlineMarkdown } from '../../logic';
import { useRenderer } from '../../logic/useRerender';

const removeExtension = (path: string) => {
return path.replace(/\.(mdx?)$/, '');
};

export function SidebarItem(props: SidebarItemProps) {
const { item, depth = 0, activeMatcher, id, setSidebarData } = props;
const active = 'link' in item && item.link && activeMatcher(item.link);
const { page } = usePageData();
const ref = useRef<HTMLDivElement>(null);
const textRef = useRef<string>(item.text);
const rerender = useRenderer();
useEffect(() => {
if (active) {
ref.current?.scrollIntoView({
Expand All @@ -30,26 +18,6 @@ export function SidebarItem(props: SidebarItemProps) {
}
}, []);

// In development, we use the latest title after hmr
if (
!isProduction() &&
item._fileKey === removeExtension(page.pagePath) &&
page.title
) {
textRef.current = page.title;
}

useEffect(() => {
// Fix the sidebar text not update when navigating to the other nav item
// https://github.com/web-infra-dev/rspress/issues/770
if (item.text === textRef.current) {
return;
}
textRef.current = item.text;
// Trigger rerender to update the sidebar text
rerender();
}, [item.text]);

if ('items' in item) {
return (
<SidebarGroup
Expand Down Expand Up @@ -82,7 +50,7 @@ export function SidebarItem(props: SidebarItemProps) {
}}
>
<Tag tag={item.tag} />
<span>{renderInlineMarkdown(textRef.current)}</span>
<span>{renderInlineMarkdown(item.text)}</span>
</div>
</Link>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const H1 = (props: React.ComponentProps<'h1'>) => {
return (
<h1
{...props}
className={`text-3xl mb-10 leading-10 tracking-tight ${styles.title}`}
className={`rspress-doc-title text-3xl mb-10 leading-10 tracking-tight ${styles.title}`}
/>
);
};
Expand Down
Loading