import classNames from 'classnames'; import * as React from 'react'; import type { ConfigConsumerProps } from '../config-provider'; import { ConfigContext } from '../config-provider'; import type { AntAnchor } from './Anchor'; import AnchorContext from './context'; export interface AnchorLinkBaseProps { prefixCls?: string; href: string; target?: string; title: React.ReactNode; className?: string; } export interface AnchorLinkProps extends AnchorLinkBaseProps { children?: React.ReactNode; } const AnchorLink: React.FC = (props) => { const { href = '#', title, prefixCls: customizePrefixCls, children, className, target } = props; const context = React.useContext(AnchorContext); const { registerLink, unregisterLink, scrollTo, onClick, activeLink } = context || {}; React.useEffect(() => { registerLink?.(href); return () => { unregisterLink?.(href); }; }, [href, registerLink, unregisterLink]); const handleClick = (e: React.MouseEvent) => { onClick?.(e, { title, href }); scrollTo?.(href); }; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('anchor', customizePrefixCls); const wrapperClassName = classNames(`${prefixCls}-link`, className, { [`${prefixCls}-link-active`]: activeLink === href, }); const titleClassName = classNames(`${prefixCls}-link-title`, { [`${prefixCls}-link-title-active`]: activeLink === href, }); return (
{title} {children}
); }; export default AnchorLink;