ant-design/.dumi/theme/slots/Content/DocAnchor.tsx

136 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-04-05 15:15:10 +08:00
import React from 'react';
import { Anchor } from 'antd';
import { createStyles, useTheme } from 'antd-style';
2024-04-05 15:15:10 +08:00
import type { AnchorLinkItemProps } from 'antd/es/anchor/Anchor';
import classNames from 'classnames';
import { useRouteMeta, useTabMeta } from 'dumi';
const useStyle = createStyles(({ token, css }) => {
const { antCls } = token;
return {
anchorToc: css`
scrollbar-width: thin;
scrollbar-color: unset;
${antCls}-anchor {
${antCls}-anchor-link-title {
font-size: ${token.fontSizeSM}px;
}
}
`,
tocWrapper: css`
position: fixed;
2024-02-02 13:46:26 +08:00
top: ${token.headerHeight + token.contentMarginTop - 8}px;
inset-inline-end: 0;
width: 160px;
2024-04-05 15:15:10 +08:00
padding: ${token.paddingXS}px;
border-radius: ${token.borderRadius}px;
box-sizing: border-box;
margin-inline-end: calc(16px - 100vw + 100%);
z-index: 10;
.toc-debug {
color: ${token.purple6};
&:hover {
color: ${token.purple5};
}
}
> div {
box-sizing: border-box;
width: 100%;
2024-02-02 13:46:26 +08:00
max-height: calc(100vh - ${token.headerHeight + token.contentMarginTop + 24}px) !important;
2024-04-05 15:15:10 +08:00
margin: auto;
overflow: auto;
2024-04-05 15:15:10 +08:00
padding: ${token.paddingXXS}px;
backdrop-filter: blur(8px);
}
@media only screen and (max-width: ${token.screenLG}px) {
display: none;
}
`,
articleWrapper: css`
padding: 0 170px 32px 64px;
&.rtl {
padding: 0 64px 144px 170px;
}
@media only screen and (max-width: ${token.screenLG}px) {
&,
&.rtl {
padding: 0 ${token.paddingLG * 2}px;
}
}
`,
};
});
interface DocAnchorProps {
showDebug?: boolean;
debugDemos?: string[];
}
2024-04-05 15:15:10 +08:00
interface AnchorItem {
id: string;
title: string;
children?: AnchorItem[];
2024-04-05 15:15:10 +08:00
}
const DocAnchor: React.FC<DocAnchorProps> = ({ showDebug, debugDemos = [] }) => {
const { styles } = useStyle();
const token = useTheme();
const meta = useRouteMeta();
const tab = useTabMeta();
2024-04-05 15:15:10 +08:00
const renderAnchorItem = (item: AnchorItem): AnchorLinkItemProps => ({
href: `#${item.id}`,
title: item.title,
key: item.id,
children: item.children
?.filter((child) => showDebug || !debugDemos.includes(child.id))
2024-04-05 15:15:10 +08:00
.map<AnchorLinkItemProps>((child) => ({
key: child.id,
href: `#${child.id}`,
title: (
2024-04-05 15:15:10 +08:00
<span className={classNames({ 'toc-debug': debugDemos.includes(child.id) })}>
{child?.title}
</span>
),
})),
});
2024-04-05 15:15:10 +08:00
const anchorItems = React.useMemo<AnchorItem[]>(
() =>
(tab?.toc || meta.toc).reduce<AnchorItem[]>((result, item) => {
if (item.depth === 2) {
result.push({ ...item });
} else if (item.depth === 3) {
const parent = result[result.length - 1];
if (parent) {
parent.children = parent.children || [];
parent.children.push({ ...item });
}
}
return result;
}, []),
[tab?.toc, meta.toc],
);
if (!meta.frontmatter.toc) {
return null;
}
return (
<section className={styles.tocWrapper}>
<Anchor
affix={false}
className={styles.anchorToc}
targetOffset={token.anchorTop}
showInkInFixed
2024-04-05 15:15:10 +08:00
items={anchorItems.map<AnchorLinkItemProps>(renderAnchorItem)}
/>
</section>
);
};
export default DocAnchor;