2021-06-26 01:35:40 +00:00
|
|
|
import type { ExtractPropTypes } from 'vue';
|
|
|
|
import { defineComponent, nextTick, onBeforeUnmount, onMounted, watch } from 'vue';
|
2023-04-21 13:29:20 +00:00
|
|
|
import { initDefaultProps } from '../_util/props-util';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2023-01-27 08:00:17 +00:00
|
|
|
import useConfigInject from '../config-provider/hooks/useConfigInject';
|
2021-06-07 09:35:03 +00:00
|
|
|
import { useInjectAnchor } from './context';
|
2023-04-21 13:29:20 +00:00
|
|
|
import type { Key, VueNode } from '../_util/type';
|
|
|
|
import { objectType, anyType } from '../_util/type';
|
|
|
|
import type { CSSProperties } from '../_util/cssinjs/hooks/useStyleRegister';
|
2020-10-19 08:43:10 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export const anchorLinkProps = () => ({
|
|
|
|
prefixCls: String,
|
|
|
|
href: String,
|
2023-04-21 13:29:20 +00:00
|
|
|
title: anyType<VueNode | ((item: any) => VueNode)>(),
|
2022-03-26 14:52:54 +00:00
|
|
|
target: String,
|
2023-04-21 13:29:20 +00:00
|
|
|
/* private use */
|
|
|
|
customTitleProps: objectType<AnchorLinkItemProps>(),
|
2022-03-26 14:52:54 +00:00
|
|
|
});
|
2023-04-21 13:29:20 +00:00
|
|
|
export interface AnchorLinkItemProps {
|
|
|
|
key: Key;
|
|
|
|
class?: string;
|
|
|
|
style?: CSSProperties;
|
|
|
|
href?: string;
|
|
|
|
target?: string;
|
|
|
|
children?: AnchorLinkItemProps[];
|
|
|
|
title?: VueNode | ((item: AnchorLinkItemProps) => VueNode);
|
|
|
|
}
|
2018-06-02 10:11:50 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type AnchorLinkProps = Partial<ExtractPropTypes<ReturnType<typeof anchorLinkProps>>>;
|
2021-06-07 09:35:03 +00:00
|
|
|
|
2020-10-12 09:54:43 +00:00
|
|
|
export default defineComponent({
|
2022-09-26 13:33:41 +00:00
|
|
|
compatConfig: { MODE: 3 },
|
2018-06-02 10:11:50 +00:00
|
|
|
name: 'AAnchorLink',
|
2023-04-21 13:29:20 +00:00
|
|
|
inheritAttrs: false,
|
2022-03-26 14:52:54 +00:00
|
|
|
props: initDefaultProps(anchorLinkProps(), { href: '#' }),
|
2023-04-21 13:29:20 +00:00
|
|
|
slots: ['title', 'customTitle'],
|
|
|
|
setup(props, { slots, attrs }) {
|
2021-06-07 09:35:03 +00:00
|
|
|
let mergedTitle = null;
|
|
|
|
const {
|
|
|
|
handleClick: contextHandleClick,
|
|
|
|
scrollTo,
|
|
|
|
unregisterLink,
|
|
|
|
registerLink,
|
|
|
|
activeLink,
|
|
|
|
} = useInjectAnchor();
|
|
|
|
const { prefixCls } = useConfigInject('anchor', props);
|
2018-06-02 10:11:50 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const handleClick = (e: Event) => {
|
|
|
|
const { href } = props;
|
|
|
|
contextHandleClick(e, { title: mergedTitle, href });
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(href);
|
2021-06-07 09:35:03 +00:00
|
|
|
};
|
2019-03-11 06:43:23 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
watch(
|
|
|
|
() => props.href,
|
|
|
|
(val, oldVal) => {
|
|
|
|
nextTick(() => {
|
|
|
|
unregisterLink(oldVal);
|
|
|
|
registerLink(val);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2019-03-11 06:43:23 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
onMounted(() => {
|
|
|
|
registerLink(props.href);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2021-06-07 09:35:03 +00:00
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
unregisterLink(props.href);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2021-06-07 09:35:03 +00:00
|
|
|
|
|
|
|
return () => {
|
2023-04-21 13:29:20 +00:00
|
|
|
const { href, target, title = slots.title, customTitleProps = {} } = props;
|
2021-06-07 09:35:03 +00:00
|
|
|
const pre = prefixCls.value;
|
2023-04-21 13:29:20 +00:00
|
|
|
mergedTitle = typeof title === 'function' ? title(customTitleProps) : title;
|
2021-06-07 09:35:03 +00:00
|
|
|
const active = activeLink.value === href;
|
2023-04-21 13:29:20 +00:00
|
|
|
const wrapperClassName = classNames(
|
|
|
|
`${pre}-link`,
|
|
|
|
{
|
|
|
|
[`${pre}-link-active`]: active,
|
|
|
|
},
|
|
|
|
attrs.class,
|
|
|
|
);
|
2021-06-07 09:35:03 +00:00
|
|
|
const titleClassName = classNames(`${pre}-link-title`, {
|
|
|
|
[`${pre}-link-title-active`]: active,
|
|
|
|
});
|
|
|
|
return (
|
2023-04-21 13:29:20 +00:00
|
|
|
<div {...attrs} class={wrapperClassName}>
|
2021-06-07 09:35:03 +00:00
|
|
|
<a
|
|
|
|
class={titleClassName}
|
|
|
|
href={href}
|
2023-04-21 13:29:20 +00:00
|
|
|
title={typeof mergedTitle === 'string' ? mergedTitle : ''}
|
2021-06-07 09:35:03 +00:00
|
|
|
target={target}
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
2023-04-21 13:29:20 +00:00
|
|
|
{slots.customTitle ? slots.customTitle(customTitleProps) : mergedTitle}
|
2021-06-07 09:35:03 +00:00
|
|
|
</a>
|
|
|
|
{slots.default?.()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2018-06-02 10:11:50 +00:00
|
|
|
},
|
2020-10-12 09:54:43 +00:00
|
|
|
});
|