Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The step element does not highlight on scrollIntoViewOptions: { … #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 26 additions & 10 deletions src/hooks/useTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,36 @@ export default function useTarget(
const updatePos = useEvent(() => {
if (targetElement) {
// Exist target element. We should scroll and get target position
const calculatePosition = () => {
const { left, top, width, height } =
targetElement.getBoundingClientRect();
const nextPosInfo: PosInfo = { left, top, width, height, radius: 0 };
setPosInfo(origin => {
if (JSON.stringify(origin) !== JSON.stringify(nextPosInfo)) {
return nextPosInfo;
}
return origin;
});
};

if (!isInViewPort(targetElement) && open) {
targetElement.scrollIntoView(scrollIntoViewOptions);
}

const { left, top, width, height } =
targetElement.getBoundingClientRect();
const nextPosInfo: PosInfo = { left, top, width, height, radius: 0 };
const observer = new IntersectionObserver(
(entries, observer) => {
const [entry] = entries;
if (entry.isIntersecting) {
setTimeout(() => calculatePosition(), 300); // debounce to let scroll finish
observer.disconnect();
}
},
{ threshold: 1 },
);

setPosInfo(origin => {
if (JSON.stringify(origin) !== JSON.stringify(nextPosInfo)) {
return nextPosInfo;
}
return origin;
});
observer.observe(targetElement);
} else {
calculatePosition();
}
} else {
// Not exist target which means we just show in center
setPosInfo(null);
Expand Down
Loading