A sticky-banner component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { SVGProps, useState } from "react";
3import { motion, useMotionValueEvent, useScroll } from "motion/react";
4import { cn } from "@/lib/utils";
5
6export const StickyBanner = ({
7 className,
8 children,
9 hideOnScroll = false,
10}: {
11 className?: string;
12 children: React.ReactNode;
13 hideOnScroll?: boolean;
14}) => {
15 const [open, setOpen] = useState(true);
16 const { scrollY } = useScroll();
17
18 useMotionValueEvent(scrollY, "change", (latest) => {
19 console.log(latest);
20 if (hideOnScroll && latest > 40) {
21 setOpen(false);
22 } else {
23 setOpen(true);
24 }
25 });
26
27 return (
28 <motion.div
29 className={cn(
30 "sticky inset-x-0 top-0 z-40 flex min-h-14 w-full items-center justify-center bg-transparent px-4 py-1",
31 className,
32 )}
33 initial={{
34 y: -100,
35 opacity: 0,
36 }}
37 animate={{
38 y: open ? 0 : -100,
39 opacity: open ? 1 : 0,
40 }}
41 transition={{
42 duration: 0.3,
43 ease: "easeInOut",
44 }}
45 >
46 {children}
47
48 <motion.button
49 initial={{
50 scale: 0,
51 }}
52 animate={{
53 scale: 1,
54 }}
55 className="absolute top-1/2 right-2 -translate-y-1/2 cursor-pointer"
56 onClick={() => setOpen(!open)}
57 >
58 <CloseIcon className="h-5 w-5 text-white" />
59 </motion.button>
60 </motion.div>
61 );
62};
63
64const CloseIcon = (props: SVGProps<SVGSVGElement>) => {
65 return (
66 <svg
67 xmlns="http://www.w3.org/2000/svg"
68 width="24"
69 height="24"
70 viewBox="0 0 24 24"
71 fill="none"
72 stroke="currentColor"
73 strokeWidth="2"
74 strokeLinecap="round"
75 strokeLinejoin="round"
76 {...props}
77 >
78 <path stroke="none" d="M0 0h24v24H0z" fill="none" />
79 <path d="M18 6l-12 12" />
80 <path d="M6 6l12 12" />
81 </svg>
82 );
83};
84