Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/examples/scrollIntoView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const App = () => {
>
Create
</button>
<div style={{ height: 1000 }} />
<div style={{ height: 1500 }} />
<button className="ant-target" ref={updateBtnRef}>
Update
</button>
Expand Down Expand Up @@ -65,7 +65,7 @@ const App = () => {
),
target: () => updateBtnRef.current,
scrollIntoViewOptions: {
block: 'start'
block: 'start', behavior:'smooth'
}
},
{
Expand Down
13 changes: 12 additions & 1 deletion src/hooks/useTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default function useTarget(
// Exist target element. We should scroll and get target position
if (!isInViewPort(targetElement) && open) {
targetElement.scrollIntoView(scrollIntoViewOptions);
return;
}

const { left, top, width, height } =
targetElement.getBoundingClientRect();
const nextPosInfo: PosInfo = { left, top, width, height, radius: 0 };
Expand All @@ -65,12 +65,23 @@ export default function useTarget(
const getGapOffset = (index: number) =>
(Array.isArray(gap?.offset) ? gap?.offset[index] : gap?.offset) ?? 6;

const scrollEndUpdatePos = ()=>{
if(window.requestAnimationFrame){
window.requestAnimationFrame(updatePos);
return;
}
setTimeout(updatePos,1000/60);
}

useLayoutEffect(() => {
updatePos();
// update when window resize
window.addEventListener('resize', updatePos);
// update when window scroll end
window.addEventListener('scrollend',scrollEndUpdatePos);
return () => {
window.removeEventListener('resize', updatePos);
window.removeEventListener('scrollend',scrollEndUpdatePos);
};
}, [targetElement, open, updatePos]);

Expand Down
Loading