nextui/apps/docs/components/demos/custom-button.tsx
2023-06-13 18:29:03 -03:00

43 lines
1.2 KiB
TypeScript

"use client";
import {useRef} from "react";
import {Button} from "@nextui-org/react";
import confetti from "canvas-confetti";
export const CustomButton = () => {
const buttonRef = useRef<HTMLButtonElement | null>(null);
const handleConfetti = () => {
const {clientWidth, clientHeight} = document.documentElement;
const boundingBox = buttonRef.current?.getBoundingClientRect?.();
const targetY = boundingBox?.y ?? 0;
const targetX = boundingBox?.x ?? 0;
const targetWidth = boundingBox?.width ?? 0;
const targetCenterX = targetX + targetWidth / 2;
confetti({
zIndex: 999,
particleCount: 100,
spread: 70,
origin: {
y: targetY / clientHeight,
x: targetCenterX / clientWidth,
},
});
};
return (
<Button
ref={buttonRef}
disableRipple
className="relative overflow-visible rounded-full hover:-translate-y-1 px-12 shadow-xl bg-background/30 after:content-[''] after:absolute after:rounded-full after:inset-0 after:bg-background/40 after:z-[-1] after:transition after:!duration-500 hover:after:scale-150 hover:after:opacity-0"
size="lg"
onPress={handleConfetti}
>
Press me
</Button>
);
};