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

Feat: add scroll direction option to useLottieInteractivity hook #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
46 changes: 30 additions & 16 deletions src/hooks/useLottieInteractivity.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { AnimationSegment } from "lottie-web";
import React, { useEffect, ReactElement } from "react";
import React, { useEffect } from "react";
import { InteractivityProps } from "../types";

// helpers
export function getContainerVisibility(container: Element): number {
const { top, height } = container.getBoundingClientRect();

const current = window.innerHeight - top;
const max = window.innerHeight + height;
return current / max;
export function getContainerVisibility(
container: Element,
direction: "horizontal" | "vertical",
): number {
if (direction === "horizontal") {
const { left, width } = container.getBoundingClientRect();
const current = window.innerWidth - left;
const max = window.innerWidth + width;
return current / max;
} else {
const { top, height } = container.getBoundingClientRect();
const current = window.innerHeight - top;
const max = window.innerHeight + height;
return current / max;
}
}

export function getContainerCursorPosition(
Expand All @@ -17,10 +26,8 @@ export function getContainerCursorPosition(
cursorY: number,
): { x: number; y: number } {
const { top, left, width, height } = container.getBoundingClientRect();

const x = (cursorX - left) / width;
const y = (cursorY - top) / height;

return { x, y };
}

Expand All @@ -29,13 +36,18 @@ export type InitInteractivity = {
animationItem: InteractivityProps["lottieObj"]["animationItem"];
actions: InteractivityProps["actions"];
mode: InteractivityProps["mode"];
scrollDirection?: InteractivityProps["scrollDirection"];
};

// Easing function
const easeOutQuad = (t: number): number => t * (2 - t);

export const useInitInteractivity = ({
wrapperRef,
animationItem,
mode,
actions,
scrollDirection = "vertical",
}: InitInteractivity) => {
useEffect(() => {
const wrapper = wrapperRef.current;
Expand All @@ -50,13 +62,14 @@ export const useInitInteractivity = ({
let assignedSegment: number[] | null = null;

const scrollHandler = () => {
const currentPercent = getContainerVisibility(wrapper);
const currentPercent = getContainerVisibility(wrapper, scrollDirection);
const easedPercent = easeOutQuad(currentPercent);
// Find the first action that satisfies the current position conditions
const action = actions.find(
({ visibility }) =>
visibility &&
currentPercent >= visibility[0] &&
currentPercent <= visibility[1],
easedPercent >= visibility[0] &&
easedPercent <= visibility[1],
);

// Skip if no matching action was found!
Expand All @@ -73,7 +86,7 @@ export const useInitInteractivity = ({
const frameToGo =
action.frames[0] +
Math.ceil(
((currentPercent - action.visibility[0]) /
((easedPercent - action.visibility[0]) /
(action.visibility[1] - action.visibility[0])) *
action.frames[1],
);
Expand Down Expand Up @@ -246,22 +259,23 @@ export const useInitInteractivity = ({
case "cursor":
return cursorModeHandler();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mode, animationItem]);
}, [mode, animationItem, actions, wrapperRef, scrollDirection]);
};

const useLottieInteractivity = ({
actions,
mode,
lottieObj,
}: InteractivityProps): ReactElement => {
scrollDirection,
}: InteractivityProps & { scrollDirection?: "horizontal" | "vertical" }) => {
const { animationItem, View, animationContainerRef } = lottieObj;

useInitInteractivity({
actions,
animationItem,
mode,
wrapperRef: animationContainerRef,
scrollDirection,
});

return View;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type InteractivityProps = {
lottieObj: { View: ReactElement } & LottieRefCurrentProps;
actions: Action[];
mode: "scroll" | "cursor";
scrollDirection?: "horizontal" | "vertical";
};

export type LottieComponentProps = LottieOptions & {
Expand Down