Skip to content

Commit 23067d8

Browse files
committedSep 13, 2024
refactor: replace calculation by intersectionRatio option
1 parent 02ea07f commit 23067d8

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed
 

‎src/step.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ export const Step: React.FC<StepProps> = ({
6767

6868
useEffect(() => {
6969
if (isIntersecting && scrollProgressEntry) {
70-
const { height, top } = scrollProgressEntry.boundingClientRect;
71-
const progress = Math.min(1, Math.max(0, (window.innerHeight * offset - top) / height));
70+
const progress = scrollProgressEntry.intersectionRatio;
7271
if (onStepProgress) {
7372
onStepProgress({
74-
progress,
73+
progress: direction === 'up' ? 1 - progress : progress,
7574
data,
7675
element: scrollProgressEntry.target,
7776
entry: scrollProgressEntry,

‎src/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ interface GetProgressRootMarginParams {
7474
*/
7575
export const getProgressRootMargin = ({ direction, offset, nodeOffsetHeight, innerHeight }: GetProgressRootMarginParams) => {
7676
if (!nodeOffsetHeight) return '0px';
77-
const offsetHeight = (nodeOffsetHeight / innerHeight);
78-
if (direction === 'down') return `${(offsetHeight - offset) * 100}% 0px ${(offset * 100) - 100}% 0px`;
79-
return `-${offset * 100}% 0px ${(offsetHeight * 100) - (100 - (offset * 100))}% 0px`;
77+
const offsetHeightRatio = (nodeOffsetHeight / innerHeight);
78+
if (direction === 'down') return `${(offsetHeightRatio - offset) * 100}% 0px ${(offset * 100) - 100}% 0px`;
79+
return `-${offset * 100}% 0px ${(offsetHeightRatio * 100) - (100 - (offset * 100))}% 0px`;
8080
}

0 commit comments

Comments
 (0)