Skip to content

Commit

Permalink
fix(TransitionView): handle parent without definite height (#2584)
Browse files Browse the repository at this point in the history
  • Loading branch information
YossiSaadi authored Nov 14, 2024
1 parent 753edb8 commit 356255e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.slide {
position: absolute;
width: 100%;
height: 100%;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SlideTransitionProps } from "./SlideTransition.types";
import styles from "./SlideTransition.module.scss";

const SlideTransition = forwardRef(
({ direction, children, className }: SlideTransitionProps, ref: React.ForwardedRef<HTMLDivElement>) => {
({ direction, style, children, className }: SlideTransitionProps, ref: React.ForwardedRef<HTMLDivElement>) => {
return (
<motion.div
ref={ref}
Expand All @@ -17,6 +17,7 @@ const SlideTransition = forwardRef(
exit="exit"
transition={slideAnimationTransition}
className={cx(styles.slide, className)}
style={style}
>
{children}
</motion.div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { VibeComponentProps } from "../../types";

export interface SlideTransitionProps extends VibeComponentProps {
direction: SlideDirection;
style?: React.CSSProperties;
children: React.ReactNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { SlideDirection } from "../SlideTransition.types";

export const slideAnimationVariants = {
initial: (direction: SlideDirection) => ({
x: direction === "forward" ? "10%" : "-10%"
x: direction === "forward" ? "10%" : "-10%",
opacity: 0
}),
enter: {
x: 0
x: 0,
opacity: 1
},
exit: (direction: SlideDirection) => ({
x: direction === "forward" ? "-100%" : "100%"
x: direction === "forward" ? "-10%" : "10%",
opacity: 0
})
};

export const slideAnimationTransition = {
duration: 0.2,
duration: 0.1,
ease: [0.0, 0.0, 0.4, 1.0]
};
22 changes: 19 additions & 3 deletions packages/core/src/components/TransitionView/TransitionView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from "react";
import React, { forwardRef, useEffect, useRef, useState } from "react";
import cx from "classnames";
import { AnimatePresence } from "framer-motion";
import { TransitionViewProps } from "./TransitionView.types";
Expand All @@ -9,18 +9,34 @@ import SlideTransition from "../SlideTransition/SlideTransition";

const TransitionView = forwardRef(
(
{ activeStep, direction, id, className, "data-testid": dataTestId, children }: TransitionViewProps,
{ activeStep, direction, height, id, className, "data-testid": dataTestId, children }: TransitionViewProps,
ref: React.ForwardedRef<HTMLDivElement>
) => {
const slideTransitionRef = useRef<HTMLDivElement>(null);
const [contentHeight, setContentHeight] = useState<number>(height);
const slideTransitionHeight = height || contentHeight > 0 ? "100%" : "auto";

useEffect(() => {
if (!slideTransitionRef.current) return;
setContentHeight(slideTransitionRef.current.scrollHeight);
}, [height, slideTransitionRef]);

return (
<div
id={id}
className={cx(styles.slideshow, className)}
data-testid={dataTestId || getTestId(ComponentDefaultTestId.TRANSITION_VIEW, id)}
ref={ref}
style={{ height: height ?? contentHeight }}
>
<AnimatePresence initial={false} custom={direction}>
<SlideTransition key={activeStep} direction={direction}>
<SlideTransition
key={activeStep}
direction={direction}
// it must be "auto" on initial load to consider scrollable content in contentHeight calculation
style={{ height: slideTransitionHeight }}
ref={slideTransitionRef}
>
{children[activeStep]}
</SlideTransition>
</AnimatePresence>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SlideDirection } from "../SlideTransition/SlideTransition.types";
export interface TransitionViewProps extends VibeComponentProps {
activeStep: number;
direction: TransitionDirection;
height?: number;
children: React.ReactNode[];
}

Expand Down

0 comments on commit 356255e

Please sign in to comment.