All components
Sticky Banner
notificationsAceternity UI component.
responsive · 580px
Install
Same command in any shadcn project — React (Vite/CRA), Next.js, Remix, Astro, and more:
$
npx shadcn@latest add https://your-domain/r/sticky-banner.jsonUsage
"use client";
import { StickyBanner } from "@/registry/aceternity-ui/sticky-banner";
export default function Demo() {
return (
<div className="relative h-[600px] w-full overflow-y-auto bg-background">
<StickyBanner className="bg-gradient-to-r from-violet-600 to-indigo-600">
<div className="flex items-center gap-3 text-white">
<span className="text-sm font-semibold">
🎉 New components are here!
</span>
<a
href="#"
className="rounded-full bg-white/20 px-3 py-1 text-xs font-semibold text-white hover:bg-white/30 transition-colors"
onClick={(e) => e.preventDefault()}
>
Learn more →
</a>
</div>
</StickyBanner>
<div className="flex flex-col gap-8 p-8">
<div className="h-32 rounded-xl bg-muted" />
<div className="h-32 rounded-xl bg-muted" />
<div className="h-32 rounded-xl bg-muted" />
<div className="h-32 rounded-xl bg-muted" />
<div className="h-32 rounded-xl bg-muted" />
</div>
</div>
);
}Component source
"use client";
import React, { SVGProps, useState } from "react";
import { motion, useMotionValueEvent, useScroll } from "motion/react";
import { cn } from "@/lib/utils";
export const StickyBanner = ({
className,
children,
hideOnScroll = false,
}: {
className?: string;
children: React.ReactNode;
hideOnScroll?: boolean;
}) => {
const [open, setOpen] = useState(true);
const { scrollY } = useScroll();
useMotionValueEvent(scrollY, "change", (latest) => {
console.log(latest);
if (hideOnScroll && latest > 40) {
setOpen(false);
} else {
setOpen(true);
}
});
return (
<motion.div
className={cn(
"sticky inset-x-0 top-0 z-40 flex min-h-14 w-full items-center justify-center bg-transparent px-4 py-1",
className,
)}
initial={{
y: -100,
opacity: 0,
}}
animate={{
y: open ? 0 : -100,
opacity: open ? 1 : 0,
}}
transition={{
duration: 0.3,
ease: "easeInOut",
}}
>
{children}
<motion.button
initial={{
scale: 0,
}}
animate={{
scale: 1,
}}
className="absolute top-1/2 right-2 -translate-y-1/2 cursor-pointer"
onClick={() => setOpen(!open)}
>
<CloseIcon className="h-5 w-5 text-white" />
</motion.button>
</motion.div>
);
};
const CloseIcon = (props: SVGProps<SVGSVGElement>) => {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
{...props}
>
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path d="M18 6l-12 12" />
<path d="M6 6l12 12" />
</svg>
);
};Source: Aceternity UI