ant-design/.dumi/theme/common/PrevAndNext.tsx
二货爱吃白萝卜 45eeee60bb
Some checks are pending
Publish Any Commit / build (push) Waiting to run
🔀 Sync mirror to Gitee / mirror (push) Waiting to run
✅ test / lint (push) Waiting to run
✅ test / test-react-legacy (16, 1/2) (push) Waiting to run
✅ test / test-react-legacy (16, 2/2) (push) Waiting to run
✅ test / test-react-legacy (17, 1/2) (push) Waiting to run
✅ test / test-react-legacy (17, 2/2) (push) Waiting to run
✅ test / test-node (push) Waiting to run
✅ test / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test / test-coverage (push) Blocked by required conditions
✅ test / build (push) Waiting to run
✅ test / test lib/es module (es, 1/2) (push) Waiting to run
✅ test / test lib/es module (es, 2/2) (push) Waiting to run
✅ test / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
feat: Add unstable api for React 19 compitable (#51979)
* chore: add unstable entrance

* chore: rest of it

* chore: use React 19

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: fix lint

* chore: test ignore 19 preload

* chore: bump rc-util

* fix: warning of pure render

* fix: warning of 19

* chore: adjust ts

* test: fix test logic

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* chore: restore file

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: fix test case

* test: update test

* test: fix test case

* test: update snapshot

* test: fix coverage

* test: fix coverage

* test: add ignore image
2024-12-18 14:09:49 +08:00

166 lines
4.3 KiB
TypeScript

import type { ReactElement } from 'react';
import React, { useContext, useMemo } from 'react';
import { LeftOutlined, RightOutlined } from '@ant-design/icons';
import type { GetProp, MenuProps } from 'antd';
import { createStyles } from 'antd-style';
import classNames from 'classnames';
import useMenu from '../../hooks/useMenu';
import SiteContext from '../slots/SiteContext';
import type { SiteContextProps } from '../slots/SiteContext';
type MenuItemType = Extract<GetProp<MenuProps, 'items'>[number], { type?: 'item' }>;
const useStyle = createStyles(({ token, css }) => {
const { colorSplit, iconCls, fontSizeIcon } = token;
return {
prevNextNav: css`
width: calc(100% - 234px);
margin-inline-end: 170px;
margin-inline-start: 64px;
overflow: hidden;
font-size: ${token.fontSize}px;
border-top: 1px solid ${colorSplit};
display: flex;
`,
pageNav: css`
flex: 1;
height: 72px;
line-height: 72px;
text-decoration: none;
${iconCls} {
color: #999;
font-size: ${fontSizeIcon}px;
transition: all ${token.motionDurationSlow};
}
.chinese {
margin-inline-start: ${token.marginXXS}px;
}
`,
prevNav: css`
text-align: start;
display: flex;
justify-content: flex-start;
align-items: center;
.footer-nav-icon-after {
display: none;
}
.footer-nav-icon-before {
position: relative;
line-height: 0;
vertical-align: middle;
transition: inset-inline-end ${token.motionDurationSlow};
margin-inline-end: 1em;
inset-inline-end: 0;
}
&:hover .footer-nav-icon-before {
inset-inline-end: 0.2em;
}
`,
nextNav: css`
text-align: end;
display: flex;
justify-content: flex-end;
align-items: center;
.footer-nav-icon-before {
display: none;
}
.footer-nav-icon-after {
position: relative;
margin-bottom: 1px;
line-height: 0;
vertical-align: middle;
transition: inset-inline-start ${token.motionDurationSlow};
margin-inline-start: 1em;
inset-inline-start: 0;
}
&:hover .footer-nav-icon-after {
inset-inline-start: 0.2em;
}
`,
};
});
const flattenMenu = (menuItems: MenuProps['items']): MenuProps['items'] | null => {
if (Array.isArray(menuItems)) {
return menuItems.reduce<Exclude<MenuProps['items'], undefined>>((acc, item) => {
if (!item) {
return acc;
}
if ('children' in item && item.children) {
return acc.concat(flattenMenu(item.children) ?? []);
}
return acc.concat(item);
}, []);
}
return null;
};
const PrevAndNext: React.FC<{ rtl?: boolean }> = ({ rtl }) => {
const { styles } = useStyle();
const beforeProps = { className: 'footer-nav-icon-before' };
const afterProps = { className: 'footer-nav-icon-after' };
const before = rtl ? <RightOutlined {...beforeProps} /> : <LeftOutlined {...beforeProps} />;
const after = rtl ? <LeftOutlined {...afterProps} /> : <RightOutlined {...afterProps} />;
const [menuItems, selectedKey] = useMenu({ before, after });
const { isMobile } = useContext<SiteContextProps>(SiteContext);
const [prev, next] = useMemo(() => {
const flatMenu = flattenMenu(menuItems);
if (!flatMenu) {
return [null, null];
}
let activeMenuItemIndex = -1;
flatMenu.forEach((menuItem, i) => {
if (menuItem && menuItem.key === selectedKey) {
activeMenuItemIndex = i;
}
});
return [
flatMenu[activeMenuItemIndex - 1] as MenuItemType,
flatMenu[activeMenuItemIndex + 1] as MenuItemType,
];
}, [menuItems, selectedKey]);
if (isMobile) {
return null;
}
return (
<section className={styles.prevNextNav}>
{prev &&
React.cloneElement(
prev.label as ReactElement<{
className: string;
}>,
{
className: classNames(styles.pageNav, styles.prevNav, prev.className),
},
)}
{next &&
React.cloneElement(
next.label as ReactElement<{
className: string;
}>,
{
className: classNames(styles.pageNav, styles.nextNav, next.className),
},
)}
</section>
);
};
export default PrevAndNext;