From 860eb4774e427fd8c0eb2cd0fa80c8cf284046e4 Mon Sep 17 00:00:00 2001 From: Chef Yogi <99634186+Chef-Yogi@users.noreply.github.com> Date: Fri, 8 Nov 2024 17:51:04 +0800 Subject: [PATCH] chore: fix transition issue --- apps/web/src/components/AdPanel/Card.tsx | 93 ++++++++----------- .../AdPanel/Variations/ExpandableAd.tsx | 29 +++++- 2 files changed, 62 insertions(+), 60 deletions(-) diff --git a/apps/web/src/components/AdPanel/Card.tsx b/apps/web/src/components/AdPanel/Card.tsx index b99dcd691af30..9d69b0df9c3ee 100644 --- a/apps/web/src/components/AdPanel/Card.tsx +++ b/apps/web/src/components/AdPanel/Card.tsx @@ -1,5 +1,5 @@ import { Box, BoxProps, CloseIcon, IconButton, useMatchBreakpoints } from '@pancakeswap/uikit' -import { useEffect, useRef } from 'react' +import { forwardRef, useEffect, useRef } from 'react' import styled from 'styled-components' import { useShowAdPanel } from './useShowAdPanel' @@ -12,13 +12,11 @@ const BaseCard = styled(Box)<{ $isExpanded?: boolean }>` border-radius: ${({ theme }) => theme.radii.default}; border: 1px solid ${({ theme }) => theme.colors.cardBorder}; - transition: min-height 0.2s ease-in-out; + transition: height 0.2s ease-in-out 0.05s; ${({ $isExpanded }) => $isExpanded && ` - min-height: 453px; - height:auto; `} ` @@ -82,56 +80,39 @@ interface AdCardProps extends BoxProps { alt?: string isExpanded?: boolean - - isDismissible?: boolean - - /** - * Force mobile view to show Modal on expanding FAQ - */ - forceMobile?: boolean } -export const AdCard = ({ - children, - imageUrl, - alt, - isExpanded, - forceMobile, - isDismissible = true, - ...props -}: AdCardProps) => { - const imageRef = useRef(null) - - // Drag handle, Slider and other slots will come here - const { isDesktop } = useMatchBreakpoints() - const [, setShowAdPanel] = useShowAdPanel() - - const isMobile = forceMobile || !isDesktop - - useEffect(() => { - if (imageRef.current) { - if (isExpanded) { - imageRef.current.animate([{ opacity: 0, zIndex: -1, transform: 'scale(0.96)' }], { - duration: 200, - fill: 'forwards', - easing: 'ease-in-out', - }) - } else { - imageRef.current.animate([{ opacity: 1, zIndex: 1, transform: 'scale(1)' }], { - duration: 200, - fill: 'forwards', - easing: 'ease-in-out', - }) +export const AdCard = forwardRef( + ({ children, imageUrl, alt, isExpanded, ...props }, ref) => { + const imageRef = useRef(null) + + // Drag handle, Slider and other slots will come here + const { isDesktop } = useMatchBreakpoints() + const [, setShowAdPanel] = useShowAdPanel() + + useEffect(() => { + if (imageRef.current) { + if (isExpanded) { + imageRef.current.animate([{ opacity: 0, zIndex: -1, transform: 'scale(0.96)' }], { + duration: 50, + fill: 'forwards', + easing: 'ease-in-out', + }) + } else { + imageRef.current.animate([{ opacity: 1, zIndex: 1, transform: 'scale(1)' }], { + duration: 200, + fill: 'forwards', + easing: 'ease-in-out', + }) + } } - } - }, [imageRef, isExpanded]) + }, [imageRef, isExpanded]) - return ( - - {children} - {isDismissible && ( + return ( + + {children} setShowAdPanel(false)} role="button" aria-label="Close Ad Panel" @@ -140,10 +121,10 @@ export const AdCard = ({ - )} - - {imageUrl && {alt} - - - ) -} + + {imageUrl && {alt} + + + ) + }, +) diff --git a/apps/web/src/components/AdPanel/Variations/ExpandableAd.tsx b/apps/web/src/components/AdPanel/Variations/ExpandableAd.tsx index d8de47f10aa81..f871d85c65b11 100644 --- a/apps/web/src/components/AdPanel/Variations/ExpandableAd.tsx +++ b/apps/web/src/components/AdPanel/Variations/ExpandableAd.tsx @@ -1,4 +1,5 @@ import { Box, Flex, Modal, ModalV2, Text, useMatchBreakpoints, useModalV2 } from '@pancakeswap/uikit' +import { useLayoutEffect, useMemo, useRef } from 'react' import { styled } from 'styled-components' import { BodyText } from '../BodyText' import { ExpandButton } from '../Button' @@ -27,10 +28,14 @@ export const ExpandableAd = (props: AdPlayerProps) => { const { isOpen, onDismiss, setIsOpen } = useModalV2() const { isDesktop } = useMatchBreakpoints() const { slideExpanded, toggleSlideExpanded } = useIsSlideExpanded() + const extendedRef = useRef(null) + const adCardRef = useRef(null) - const isMobile = props.forceMobile || !isDesktop + const isMobile = useMemo(() => props.forceMobile || !isDesktop, [props.forceMobile, isDesktop]) - const isExpanded = !isMobile ? slideExpanded[adId] || false : false + const isExpanded = useMemo(() => { + return !isMobile ? slideExpanded[adId] || false : false + }, [isMobile, slideExpanded]) const handleExpand = () => { toggleSlideExpanded(adId, true) @@ -42,11 +47,27 @@ export const ExpandableAd = (props: AdPlayerProps) => { onDismiss() } + useLayoutEffect(() => { + if (isExpanded && extendedRef.current && adCardRef.current) { + const targetCard = adCardRef.current + targetCard.style.overflow = `hidden` + const { scrollHeight } = extendedRef.current + targetCard.style.height = `${scrollHeight + 100}px` + setTimeout(() => { + targetCard.style.height = `auto` + }, 300) + } else if (!isExpanded && adCardRef.current) { + const targetCard = adCardRef.current + targetCard.style.height = `164px` + targetCard.style.overflow = `` + } + }, [isExpanded]) + return ( - + {isExpanded ? ( - + {title}