Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b345410

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

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed
 

‎src/step.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export const Step: React.FC<StepProps> = ({
4646

4747

4848
const progressRootMargin = useMemo(
49-
() => getProgressRootMargin({ direction, offset, nodeOffsetHeight, innerHeight }),
50-
[direction, offset, nodeOffsetHeight, innerHeight]
49+
() => getProgressRootMargin({ offset, nodeOffsetHeight, innerHeight }),
50+
[offset, nodeOffsetHeight, innerHeight]
5151
);
5252

5353
const { ref: scrollProgressRef, entry: scrollProgressEntry } = useInView({
@@ -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: progress,
7574
data,
7675
element: scrollProgressEntry.target,
7776
entry: scrollProgressEntry,

‎src/utils.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const getRootMargin = ({ offset }: { offset: number }) => {
5656

5757

5858
interface GetProgressRootMarginParams {
59-
direction: string;
6059
offset: number;
6160
nodeOffsetHeight: number;
6261
innerHeight: number;
@@ -66,15 +65,13 @@ interface GetProgressRootMarginParams {
6665
* Calculates the root margin for progress tracking based on scroll direction and element dimensions.
6766
*
6867
* @param {Object} params - The parameters for calculating the root margin.
69-
* @param {string} params.direction - The scroll direction ('up' or 'down').
7068
* @param {number} params.offset - The offset value, typically between 0 and 1.
7169
* @param {number} params.nodeOffsetHeight - The offset height of the node.
7270
* @param {number} params.innerHeight - The inner height of the viewport.
7371
* @returns {string} The calculated root margin string in the format "top right bottom left".
7472
*/
75-
export const getProgressRootMargin = ({ direction, offset, nodeOffsetHeight, innerHeight }: GetProgressRootMarginParams) => {
73+
export const getProgressRootMargin = ({ offset, nodeOffsetHeight, innerHeight }: GetProgressRootMarginParams) => {
7674
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`;
75+
const offsetHeightRatio = (nodeOffsetHeight / innerHeight);
76+
return `${(offsetHeightRatio - offset) * 100}% 0px ${(offset * 100) - 100}% 0px`;
8077
}

0 commit comments

Comments
 (0)
Please sign in to comment.