Open History to see the last commit
1import { DatabaseZapIcon } from "lucide-react";
2import { AnimatePresence, motion } from "motion/react";
3import { useEffect, useMemo, useRef, useState } from "react";
4
5export function ToggleFloatingWindowButton({
6 isClosed,
7 activeStep,
8 toggleWindow,
9}: {
10 toggleWindow: () => void;
11 isClosed: boolean;
12 activeStep: string | null;
13}) {
14 const referenceContainerRef = useRef<HTMLDivElement>(null);
15
16 const [mounted, setMounted] = useState(false);
17
18 useEffect(() => {
19 setMounted(true);
20 });
21
22 const containerWidth = useMemo(() => {
23 if (mounted && isClosed && referenceContainerRef.current) {
24 return referenceContainerRef.current.offsetWidth;
25 } else {
26 // default width
27 return 56;
28 }
29 }, [isClosed, mounted]);
30
31 const rotationDegrees = useMemo(() => {
32 return isClosed ? 0 : 360;
33 }, [isClosed]);
34
35 return (
36 <>
37 <motion.button
38 className="relative min-w-14 h-14 text-black inline-flex items-center justify-center gap-2 cursor-pointer bg-orange-500 py-4 px-0 rounded-full hover:bg-orange-600"
39 type="button"
40 onClick={() => toggleWindow()}
41 animate={{
42 // width: isClosed ? "auto" : "56px",
43 width: `${containerWidth.toString()}px`,
44 }}
45 initial
46 transition={{
47 duration: 0.2,
48 ease: "easeInOut",
49 }}
50 >
51 <motion.div
52 initial={false}
53 animate={{
54 rotate: rotationDegrees,
55 }}
56 className="absolute top-0 left-0 h-full flex items-center justify-center w-14 shrink-0"
57 >
58 <DatabaseZapIcon className="block" />
59 </motion.div>
60 <div className="w-6 h-14" />
61 <AnimatePresence>
62 {isClosed && (
63 <div className="mx-4 h-0 pointer-events-none opacity-0">
64 {activeStep}
65 </div>
66 )}
67 </AnimatePresence>
68 <motion.div
69 initial={false}
70 className="absolute right-0 top-0 m-4"
71 animate={{
72 opacity: isClosed ? 1 : 0,
73 }}
74 transition={{
75 duration: isClosed ? 0.2 : 0,
76 ease: "easeInOut",
77 delay: isClosed ? 0.2 : 0,
78 }}
79 >
80 {activeStep}
81 </motion.div>
82 </motion.button>
83 {/*
84 This is a reference container that we use
85 to calculate the width of the button
86 */}
87 <div
88 ref={referenceContainerRef}
89 className="absolute h-0 overflow-hidden top-0 left-0 min-w-14 text-black inline-flex items-center justify-center gap-2 pointer-none whitespace-nowrap"
90 aria-hidden="true"
91 >
92 <div className="w-6" aria-hidden="true" />
93 <div className="mx-4 overflow-hidden" aria-hidden="true">
94 {activeStep}
95 </div>
96 </div>
97 </>
98 );
99}