mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-20 20:36:19 +02:00
Use transform instead of scrolling
Use spring animation
This commit is contained in:
parent
5f563c6423
commit
0bacbd3ff6
1 changed files with 13 additions and 30 deletions
|
@ -28,48 +28,31 @@ if (typeof window !== "undefined") {
|
||||||
|
|
||||||
const MotionButton = motion(Button);
|
const MotionButton = motion(Button);
|
||||||
|
|
||||||
// There's a bug in Safari 15.4 that causes `scroll` to no work as intended
|
|
||||||
const isSafariV154 =
|
|
||||||
typeof window !== "undefined"
|
|
||||||
? /Version\/15.[4-9]\sSafari/.test(navigator.userAgent)
|
|
||||||
: false;
|
|
||||||
|
|
||||||
export const ControlledScrollDiv: React.VoidFunctionComponent<{
|
export const ControlledScrollDiv: React.VoidFunctionComponent<{
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
className?: string;
|
className?: string;
|
||||||
}> = ({ className, children }) => {
|
}> = ({ className, children }) => {
|
||||||
const { setScrollPosition, availableSpace, scrollPosition } =
|
const { availableSpace, scrollPosition } = usePollContext();
|
||||||
usePollContext();
|
|
||||||
|
|
||||||
const ref = React.useRef<HTMLDivElement>(null);
|
const ref = React.useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const didSetInitialScrollPosition = React.useRef(false);
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (ref.current) {
|
|
||||||
if (!isSafariV154) {
|
|
||||||
ref.current.scroll({
|
|
||||||
left: scrollPosition,
|
|
||||||
behavior: didSetInitialScrollPosition?.current ? "smooth" : "auto",
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
ref.current.scrollLeft = scrollPosition;
|
|
||||||
}
|
|
||||||
didSetInitialScrollPosition.current = true;
|
|
||||||
}
|
|
||||||
}, [scrollPosition]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className={clsx("flex min-w-0 overflow-hidden", className)}
|
className={clsx(" min-w-0 overflow-hidden", className)}
|
||||||
style={{ width: availableSpace, maxWidth: availableSpace }}
|
style={{ width: availableSpace, maxWidth: availableSpace }}
|
||||||
onScroll={(e) => {
|
|
||||||
const div = e.target as HTMLDivElement;
|
|
||||||
setScrollPosition(div.scrollLeft);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{children}
|
<motion.div
|
||||||
|
className="flex h-full"
|
||||||
|
transition={{
|
||||||
|
type: "spring",
|
||||||
|
mass: 0.5,
|
||||||
|
}}
|
||||||
|
initial={{ x: 0 }}
|
||||||
|
animate={{ x: scrollPosition * -1 }}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</motion.div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue