perf(anchor): reduce unnecessary calculations (#42018)

This commit is contained in:
Simon He 2023-04-27 15:27:55 +08:00 committed by GitHub
parent 9a6aaac0c0
commit 901957cf02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -245,8 +245,6 @@ const AnchorContent: React.FC<InternalAnchorProps> = (props) => {
const handleScrollTo = React.useCallback<(link: string) => void>(
(link) => {
setCurrentActiveLink(link);
const container = getCurrentContainer();
const scrollTop = getScroll(container, true);
const sharpLinkMatch = sharpMatcherRegex.exec(link);
if (!sharpLinkMatch) {
return;
@ -256,6 +254,8 @@ const AnchorContent: React.FC<InternalAnchorProps> = (props) => {
return;
}
const container = getCurrentContainer();
const scrollTop = getScroll(container, true);
const eleOffsetTop = getOffsetTop(targetElement, container);
let y = scrollTop + eleOffsetTop;
y -= targetOffset !== undefined ? targetOffset : offsetTop || 0;