From de4e52bd63a85b8251d8369866e0b3ffd37b6059 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Tue, 20 Feb 2024 10:06:07 +0700
Subject: Add smooth animation on top banner

---
 src/core/components/elements/Navbar/TopBanner.jsx | 43 +++++++++++++----------
 1 file changed, 25 insertions(+), 18 deletions(-)

(limited to 'src/core/components/elements/Navbar/TopBanner.jsx')

diff --git a/src/core/components/elements/Navbar/TopBanner.jsx b/src/core/components/elements/Navbar/TopBanner.jsx
index a757c260..4342149d 100644
--- a/src/core/components/elements/Navbar/TopBanner.jsx
+++ b/src/core/components/elements/Navbar/TopBanner.jsx
@@ -1,21 +1,28 @@
-import odooApi from '@/core/api/odooApi'
-import { useQuery } from 'react-query'
-import Image from 'next/image'
-import Link from '../Link/Link'
-import { TopBannerSkeleton } from '../Skeleton/TopBannerSkeleton'
+import odooApi from '@/core/api/odooApi';
+import Image from 'next/image';
+import { useQuery } from 'react-query';
+import clsxm from '~/libs/clsxm';
+import Link from '../Link/Link';
 
 const TopBanner = () => {
-  const fetchTopBanner = async () => await odooApi('GET', '/api/v1/banner?type=top-banner')
-  const topBanner = useQuery('topBanner', fetchTopBanner, { refetchOnWindowFocus: false })
+  const topBanner = useQuery({
+    queryKey: 'topBanner',
+    queryFn: async () => await odooApi('GET', '/api/v1/banner?type=top-banner'),
+    refetchOnWindowFocus: false,
+  });
 
-  if (topBanner.isLoading) {
-    return <TopBannerSkeleton />
-  }
+  const backgroundColor = topBanner.data?.[0]?.backgroundColor || 'transparent';
+  const hasData = topBanner.data?.length > 0;
 
   return (
-    topBanner.isFetched &&
-    topBanner.data?.length > 0 && (
-      <div style={{ backgroundColor: topBanner.data[0]?.backgroundColor || 'transparent' }}>
+    <div
+      style={{ backgroundColor }}
+      className={clsxm('overflow-y-hidden transition-all duration-700', {
+        'h-[40px]': hasData,
+        'h-0': topBanner.isLoading,
+      })}
+    >
+      {hasData && (
         <Link href={topBanner.data[0]?.url}>
           <Image
             src={topBanner.data[0].image}
@@ -25,9 +32,9 @@ const TopBanner = () => {
             className='object-cover object-center h-full mx-auto'
           />
         </Link>
-      </div>
-    )
-  )
-}
+      )}
+    </div>
+  );
+};
 
-export default TopBanner
+export default TopBanner;
-- 
cgit v1.2.3

