Floating Button
Floating Button is a React and Tailwind CSS button component from Bund UI, licensed MIT. Copy it and paste it into your project.
Bund UI
MIT
buttons
What it is
Floating Button is a React and Tailwind CSS button component from Bund UI, licensed MIT. Copy it and paste it into your project.
How to use it
Open it in the catalogue, press copy, and paste it into your project. The prompt you copy carries the code and the rules that tell your agent to reproduce it without changing anything.
Five free copies a month. No card.
Needs
npm i motion usehooks-ts lucide-react
Licence
This component comes from Bund UI and is redistributed under the MIT licence, which permits commercial use. The copyright notice travels with the code.
Floating Button
"use client";
import React, { ReactNode } from "react";
import { AnimatePresence, motion } from "motion/react";
import { useOnClickOutside } from "usehooks-ts";
type FloatingButtonProps = {
className?: string;
children: ReactNode;
triggerContent: ReactNode;
};
type FloatingButtonItemProps = {
children: ReactNode;
};
const list = {
visible: {
opacity: 1,
transition: {
staggerChildren: 0.1,
staggerDirection: -1
}
},
hidden: {
opacity: 0,
transition: {
when: "afterChildren",
staggerChildren: 0.1
}
}
};
const item = {
visible: { opacity: 1, y: 0 },
hidden: { opacity: 0, y: 5 }
};
const btn = {
visible: { rotate: "45deg" },
hidden: { rotate: 0 }
};
function FloatingButton({ children, triggerContent }: FloatingButtonProps) {
const ref = React.useRef<HTMLDivElement | null>(null);
const [isOpen, setIsOpen] = React.useState(false);
useOnClickOutside(ref as React.RefObject<HTMLDivElement>, () => setIsOpen(false));
return (
<div className="relative flex flex-col items-center">
<AnimatePresence>
<motion.ul
key="list"
className="absolute bottom-14 flex flex-col items-center gap-2"
initial="hidden"
animate={isOpen ? "visible" : "hidden"}
variants={list}>
{children}
</motion.ul>
<motion.div
key="button"
variants={btn}
animate={isOpen ? "visible" : "hidden"}
ref={ref}
onClick={() => setIsOpen(!isOpen)}
className="cursor-pointer">
{triggerContent}
</motion.div>
</AnimatePresence>
</div>
);
}
function FloatingButtonItem({ children }: FloatingButtonItemProps) {
return <motion.li variants={item}>{children}</motion.li>;
}
export { FloatingButton, FloatingButtonItem };
import { cn } from "@/lib/utils";
import { DribbbleIcon, FacebookIcon, LinkedinIcon, PlusIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import { FloatingButton, FloatingButtonItem } from "./floating-button";
export default function FloatingButtonExample() {
const items = [
{
id: "facebook",
icon: <FacebookIcon />,
bgColor: "bg-[#1877f2]"
},
{
id: "dribbble",
icon: <DribbbleIcon />,
bgColor: "bg-[#ea4c89]"
},
{
id: "linkedin",
icon: <LinkedinIcon />,
bgColor: "bg-[#0a66c2]"
}
];
return (
<FloatingButton
triggerContent={
<Button size="icon" className="size-12 rounded-full">
<PlusIcon className="size-5" />
</Button>
}>
{items.map((item) => (
<FloatingButtonItem key={item.id}>
<Button size="icon" className={cn("size-12 rounded-full", item.bgColor)}>
{item.icon}
</Button>
</FloatingButtonItem>
))}
</FloatingButton>
);
}