import { VFC, ReactNode, MouseEvent, CSSProperties, useContext, useRef, useEffect, } from 'react'; import cx from 'classnames'; import { ElevatorContext } from './context'; import { Affix, IAffixImperativeHandlers, IAffixProps } from '../affix'; import { WindowScrollHandler } from '..'; export interface IElevatorLinkItem { link: string; title: ReactNode; } export type IElevatorLinksProps = Omit & { links: IElevatorLinkItem[]; className?: string; style?: CSSProperties; onClick?: (event: MouseEvent, link: string) => void; }; export const ElevatorLinks: VFC = ({ style, links, className, offsetTop, offsetBottom, onClick, ...restProps }) => { const { activeLink, getContainer, onLinkClick } = useContext(ElevatorContext); const affixRef = useRef(null); useEffect(() => { const linkIds = links.map(link => link.link); if (Array.from(new Set(linkIds)).length !== linkIds.length) { console.warn( 'Warning: In the links property of Elevator, the link value must be unique.' ); } }, [links]); const handleScroll = () => { affixRef?.current?.updatePosition(); }; const handleLinkClick = (event: MouseEvent, link: string) => { onLinkClick(link); onClick?.(event, link); }; return (
{links.map(link => (
handleLinkClick(e, link.link)} className={cx('zent-elevator__link', { 'zent-elevator__link--active': activeLink === link.link, })} > {link.title}
))}
{getContainer && }
); };