2024-04-06 16:11:17 +08:00
|
|
|
import type { ReactElement } from 'react';
|
|
|
|
import React, { useContext, useMemo } from 'react';
|
2022-11-09 12:28:04 +08:00
|
|
|
import { LeftOutlined, RightOutlined } from '@ant-design/icons';
|
2024-05-20 14:26:51 +08:00
|
|
|
import type { GetProp, MenuProps } from 'antd';
|
2023-07-20 19:27:33 +08:00
|
|
|
import { createStyles } from 'antd-style';
|
2024-04-06 16:11:17 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2022-11-30 11:05:41 +08:00
|
|
|
import useMenu from '../../hooks/useMenu';
|
2023-07-30 19:16:30 +08:00
|
|
|
import SiteContext from '../slots/SiteContext';
|
|
|
|
import type { SiteContextProps } from '../slots/SiteContext';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2024-05-20 14:26:51 +08:00
|
|
|
type MenuItemType = Extract<GetProp<MenuProps, 'items'>[number], { type?: 'item' }>;
|
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const useStyle = createStyles(({ token, css }) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
const { colorSplit, iconCls, fontSizeIcon } = token;
|
|
|
|
|
|
|
|
return {
|
|
|
|
prevNextNav: css`
|
|
|
|
width: calc(100% - 234px);
|
|
|
|
margin-inline-end: 170px;
|
|
|
|
margin-inline-start: 64px;
|
|
|
|
overflow: hidden;
|
2024-04-06 16:11:17 +08:00
|
|
|
font-size: ${token.fontSize}px;
|
2022-11-09 12:28:04 +08:00
|
|
|
border-top: 1px solid ${colorSplit};
|
|
|
|
display: flex;
|
|
|
|
`,
|
2023-04-18 15:29:34 +08:00
|
|
|
pageNav: css`
|
2022-11-09 12:28:04 +08:00
|
|
|
flex: 1;
|
|
|
|
height: 72px;
|
|
|
|
line-height: 72px;
|
|
|
|
text-decoration: none;
|
|
|
|
|
|
|
|
${iconCls} {
|
2023-06-24 15:50:33 +08:00
|
|
|
color: #999;
|
2022-11-09 12:28:04 +08:00
|
|
|
font-size: ${fontSizeIcon}px;
|
2024-04-06 16:11:17 +08:00
|
|
|
transition: all ${token.motionDurationSlow};
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.chinese {
|
2024-04-06 16:11:17 +08:00
|
|
|
margin-inline-start: ${token.marginXXS}px;
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
`,
|
2023-04-18 15:29:34 +08:00
|
|
|
prevNav: css`
|
2022-11-09 12:28:04 +08:00
|
|
|
text-align: start;
|
2023-06-14 17:54:11 +08:00
|
|
|
display: flex;
|
|
|
|
justify-content: flex-start;
|
|
|
|
align-items: center;
|
2023-06-24 15:50:33 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
.footer-nav-icon-after {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.footer-nav-icon-before {
|
|
|
|
position: relative;
|
|
|
|
line-height: 0;
|
2023-06-05 22:34:03 +08:00
|
|
|
vertical-align: middle;
|
2024-04-06 16:11:17 +08:00
|
|
|
transition: inset-inline-end ${token.motionDurationSlow};
|
2023-06-05 22:34:03 +08:00
|
|
|
margin-inline-end: 1em;
|
|
|
|
inset-inline-end: 0;
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
&:hover .footer-nav-icon-before {
|
2023-06-05 22:34:03 +08:00
|
|
|
inset-inline-end: 0.2em;
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
`,
|
2023-04-18 15:29:34 +08:00
|
|
|
nextNav: css`
|
2022-11-09 12:28:04 +08:00
|
|
|
text-align: end;
|
2023-06-14 17:54:11 +08:00
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
align-items: center;
|
2023-06-24 15:50:33 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
.footer-nav-icon-before {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.footer-nav-icon-after {
|
|
|
|
position: relative;
|
|
|
|
margin-bottom: 1px;
|
|
|
|
line-height: 0;
|
2023-06-05 22:34:03 +08:00
|
|
|
vertical-align: middle;
|
2024-04-06 16:11:17 +08:00
|
|
|
transition: inset-inline-start ${token.motionDurationSlow};
|
2023-06-05 22:34:03 +08:00
|
|
|
margin-inline-start: 1em;
|
|
|
|
inset-inline-start: 0;
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
&:hover .footer-nav-icon-after {
|
2023-06-05 22:34:03 +08:00
|
|
|
inset-inline-start: 0.2em;
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
};
|
2023-07-20 19:27:33 +08:00
|
|
|
});
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-06-05 09:46:52 +08:00
|
|
|
const PrevAndNext: React.FC<{ rtl?: boolean }> = ({ rtl }) => {
|
2023-07-20 19:27:33 +08:00
|
|
|
const { styles } = useStyle();
|
2023-06-05 09:46:52 +08:00
|
|
|
const beforeProps = { className: 'footer-nav-icon-before' };
|
|
|
|
const afterProps = { className: 'footer-nav-icon-after' };
|
|
|
|
|
2023-06-05 22:34:03 +08:00
|
|
|
const before = rtl ? <RightOutlined {...beforeProps} /> : <LeftOutlined {...beforeProps} />;
|
|
|
|
const after = rtl ? <LeftOutlined {...afterProps} /> : <RightOutlined {...afterProps} />;
|
2023-06-05 09:46:52 +08:00
|
|
|
|
|
|
|
const [menuItems, selectedKey] = useMenu({ before, after });
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-07-30 19:16:30 +08:00
|
|
|
const { isMobile } = useContext<SiteContextProps>(SiteContext);
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2022-11-30 11:05:41 +08:00
|
|
|
return [
|
|
|
|
flatMenu[activeMenuItemIndex - 1] as MenuItemType,
|
|
|
|
flatMenu[activeMenuItemIndex + 1] as MenuItemType,
|
|
|
|
];
|
2022-11-09 12:28:04 +08:00
|
|
|
}, [menuItems, selectedKey]);
|
|
|
|
|
2023-07-30 19:16:30 +08:00
|
|
|
if (isMobile) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
return (
|
2023-07-20 19:27:33 +08:00
|
|
|
<section className={styles.prevNextNav}>
|
2023-04-18 15:29:34 +08:00
|
|
|
{prev &&
|
|
|
|
React.cloneElement(prev.label as ReactElement, {
|
2023-07-20 19:27:33 +08:00
|
|
|
className: classNames(styles.pageNav, styles.prevNav, prev.className),
|
2023-04-18 15:29:34 +08:00
|
|
|
})}
|
|
|
|
{next &&
|
|
|
|
React.cloneElement(next.label as ReactElement, {
|
2023-07-20 19:27:33 +08:00
|
|
|
className: classNames(styles.pageNav, styles.nextNav, next.className),
|
2023-04-18 15:29:34 +08:00
|
|
|
})}
|
2022-11-09 12:28:04 +08:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PrevAndNext;
|