Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 style(page): grow the TOC panel out of the rail on expand and collapse (#22210)

Innei942ec57101ec
diff --git a/src/features/PageEditor/TableOfContents/index.test.tsx b/src/features/PageEditor/TableOfContents/index.test.tsx
index de189559bae..6b13cb73f5f 100644
--- a/src/features/PageEditor/TableOfContents/index.test.tsx
+++ b/src/features/PageEditor/TableOfContents/index.test.tsx
@@ -7,6 +7,21 @@ vi.mock('react-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));
+// Records the panel's motion targets and renders a plain div, so the panel
+// still unmounts as soon as it collapses.
+const panelMotion = vi.hoisted(() => ({
+ exit: undefined as unknown,
+ initial: undefined as unknown,
+}));
+
+vi.mock('motion/react-m', () => ({
+ div: ({ animate: _animate, children, exit, initial, transition: _transition, ...rest }: any) => {
+ panelMotion.exit = exit;
+ panelMotion.initial = initial;
+ return <div {...rest}>{children}</div>;
+ },
+}));
+
// jsdom has no layout: keep the first heading at the pane top and the rest
// below the active threshold.
const setBody = (content: HTMLElement, html: string) => {
@@ -128,6 +143,29 @@ describe('TableOfContents', () => {
expect(pane.scrollTo).toHaveBeenCalledWith(expect.objectContaining({ behavior: 'auto' }));
});
+ it('grows the panel out of the rail and folds it back', async () => {
+ setup();
+ await flush();
+
+ fireEvent.focus(screen.getByRole('button'));
+
+ expect(panelMotion.initial).toMatchObject({ opacity: 0, scale: 0.9 });
+ expect(panelMotion.exit).toMatchObject({ opacity: 0, pointerEvents: 'none', scale: 0.9 });
+ });
+
+ it('fades the panel without scaling when reduced motion is preferred', async () => {
+ vi.stubGlobal('matchMedia', (query: string) => ({
+ matches: query === '(prefers-reduced-motion: reduce)',
+ }));
+ setup();
+ await flush();
+
+ fireEvent.focus(screen.getByRole('button'));
+
+ expect(panelMotion.initial).toMatchObject({ opacity: 0, scale: 1 });
+ expect(panelMotion.exit).toMatchObject({ opacity: 0, scale: 1 });
+ });
+
it('keeps the first heading active when the page fits without scrolling', async () => {
const { pane } = setup();
Object.defineProperty(pane, 'scrollHeight', { configurable: true, value: 0 });
diff --git a/src/features/PageEditor/TableOfContents/index.tsx b/src/features/PageEditor/TableOfContents/index.tsx
index 1709d2eca26..9672ac2da2e 100644
--- a/src/features/PageEditor/TableOfContents/index.tsx
+++ b/src/features/PageEditor/TableOfContents/index.tsx
@@ -2,6 +2,8 @@
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
+import { AnimatePresence } from 'motion/react';
+import * as m from 'motion/react-m';
import type { RefObject } from 'react';
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -16,6 +18,9 @@ const MIN_HEADINGS = 2;
// Kept tight to the pane edge: the strip the rail takes is width wide tables lose.
const RAIL_INSET = 4;
const RAIL_PADDING_INLINE = 4;
+// The panel grows out of the rail and folds back into it.
+const PANEL_COLLAPSED_SCALE = 0.9;
+const PANEL_SPRING = { damping: 30, stiffness: 320, type: 'spring' } as const;
const prefersReducedMotion = () =>
window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
@@ -85,7 +90,9 @@ const styles = stylex.create({
overflowY: 'auto',
padding: 6,
position: 'absolute',
- transform: 'translateY(-50%)',
+ // The rail's center, so scaling reads as the panel unfolding from it. The
+ // vertical centering is a motion `y`, which composes with the scale.
+ transformOrigin: { 'default': 'right center', ':dir(rtl)': 'left center' },
width: 240,
},
title: {
@@ -246,6 +253,9 @@ const TableOfContents = memo<TableOfContentsProps>(({ contentRef, onVisibleChang
if (!visible) return null;
+ // Reduced motion keeps the fade but drops the scale.
+ const collapsedScale = prefersReducedMotion() ? 1 : PANEL_COLLAPSED_SCALE;
+
return (
<nav
aria-label={t('pageEditor.toc.title')}
@@ -275,31 +285,42 @@ const TableOfContents = memo<TableOfContentsProps>(({ contentRef, onVisibleChang
/>
))}
</div>
- {expanded && (
- <div {...stylex.props(styles.panel)}>
- <div {...stylex.props(styles.title)}>{t('pageEditor.toc.title')}</div>
- {headings.map((heading, index) => (
- <div
- aria-current={index === activeIndex ? 'location' : undefined}
- key={index}
- role="link"
- {...stylex.props(
- styles.item,
- styles.itemIndent(ITEM_INDENT[heading.level]),
- index === activeIndex && styles.itemActive,
- )}
- tabIndex={0}
- title={heading.text}
- onClick={() => scrollToHeading(heading, index)}
- onKeyDown={(event) => {
- if (event.key === 'Enter') scrollToHeading(heading, index);
- }}
- >
- {heading.text}
- </div>
- ))}
- </div>
- )}
+ <AnimatePresence>
+ {expanded && (
+ <m.div
+ animate={{ opacity: 1, pointerEvents: 'auto', scale: 1 }}
+ // The spring settles well after the panel is invisible; until it
+ // unmounts it must not catch hovers or clicks meant for the page.
+ exit={{ opacity: 0, pointerEvents: 'none', scale: collapsedScale }}
+ // `y` centers the panel on the rail; no target animates it.
+ initial={{ opacity: 0, scale: collapsedScale, y: '-50%' }}
+ transition={PANEL_SPRING}
+ {...stylex.props(styles.panel)}
+ >
+ <div {...stylex.props(styles.title)}>{t('pageEditor.toc.title')}</div>
+ {headings.map((heading, index) => (
+ <div
+ aria-current={index === activeIndex ? 'location' : undefined}
+ key={index}
+ role="link"
+ {...stylex.props(
+ styles.item,
+ styles.itemIndent(ITEM_INDENT[heading.level]),
+ index === activeIndex && styles.itemActive,
+ )}
+ tabIndex={0}
+ title={heading.text}
+ onClick={() => scrollToHeading(heading, index)}
+ onKeyDown={(event) => {
+ if (event.key === 'Enter') scrollToHeading(heading, index);
+ }}
+ >
+ {heading.text}
+ </div>
+ ))}
+ </m.div>
+ )}
+ </AnimatePresence>
</nav>
);
});