|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useState } from "react"; |
| 4 | +import { motion } from "framer-motion"; |
| 5 | + |
| 6 | +import * as Accordion from "@radix-ui/react-accordion"; |
| 7 | + |
| 8 | +interface FAQItem { |
| 9 | + id: number; |
| 10 | + question: string; |
| 11 | + answer: string; |
| 12 | + icon?: string; |
| 13 | + iconPosition?: string; |
| 14 | +} |
| 15 | + |
| 16 | +interface FaqSectionProps { |
| 17 | + data: FAQItem[]; |
| 18 | +} |
| 19 | + |
| 20 | +export default function FaqSection({ data }: FaqSectionProps) { |
| 21 | + const [openItem, setOpenItem] = useState<string | null>(null); |
| 22 | + |
| 23 | + return ( |
| 24 | + <div |
| 25 | + className="mx-auto max-w-md rounded-lg bg-white p-4" |
| 26 | + style={{ maxWidth: "700px", minWidth: "700px" }} |
| 27 | + > |
| 28 | + <div className="mb-4 text-sm text-gray-500">Every day, 9:01 AM</div> |
| 29 | + |
| 30 | + <Accordion.Root |
| 31 | + type="single" |
| 32 | + collapsible |
| 33 | + value={openItem || ""} |
| 34 | + onValueChange={(value) => setOpenItem(value)} |
| 35 | + > |
| 36 | + {data.map((item) => ( |
| 37 | + <Accordion.Item value={item.id.toString()} key={item.id} className="mb-2"> |
| 38 | + <Accordion.Header> |
| 39 | + <Accordion.Trigger |
| 40 | + className="flex w-full items-center justify-start gap-x-4" |
| 41 | + style={{ width: "100%" }} |
| 42 | + > |
| 43 | + <div |
| 44 | + className="relative flex items-center space-x-2 rounded-xl bg-gray-100 p-2 hover:bg-[#E0F7FA]" |
| 45 | + style={{ |
| 46 | + backgroundColor: openItem === item.id.toString() ? "#E0F7FA" : "", |
| 47 | + }} |
| 48 | + > |
| 49 | + {item.icon && ( |
| 50 | + <span |
| 51 | + className={`absolute bottom-6 ${ |
| 52 | + item.iconPosition === "right" ? "right-0" : "left-0" |
| 53 | + }`} |
| 54 | + style={{ |
| 55 | + transform: item.iconPosition === "right" ? "rotate(7deg)" : "rotate(-4deg)", |
| 56 | + }} |
| 57 | + > |
| 58 | + {item.icon} |
| 59 | + </span> |
| 60 | + )} |
| 61 | + <span className="font-medium text-gray-700">{item.question}</span> |
| 62 | + </div> |
| 63 | + |
| 64 | + <span className="cursor-pointer text-lg font-bold text-gray-400"> |
| 65 | + {openItem === item.id.toString() ? ( |
| 66 | + <svg |
| 67 | + xmlns="http://www.w3.org/2000/svg" |
| 68 | + viewBox="0 0 24 24" |
| 69 | + fill="#7CB9E8" |
| 70 | + className="size-6" |
| 71 | + > |
| 72 | + <path |
| 73 | + fillRule="evenodd" |
| 74 | + d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25Zm3 10.5a.75.75 0 0 0 0-1.5H9a.75.75 0 0 0 0 1.5h6Z" |
| 75 | + clipRule="evenodd" |
| 76 | + /> |
| 77 | + </svg> |
| 78 | + ) : ( |
| 79 | + <svg |
| 80 | + xmlns="http://www.w3.org/2000/svg" |
| 81 | + viewBox="0 0 24 24" |
| 82 | + fill="currentColor" |
| 83 | + className="size-6" |
| 84 | + > |
| 85 | + <path |
| 86 | + fillRule="evenodd" |
| 87 | + d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25ZM12.75 9a.75.75 0 0 0-1.5 0v2.25H9a.75.75 0 0 0 0 1.5h2.25V15a.75.75 0 0 0 1.5 0v-2.25H15a.75.75 0 0 0 0-1.5h-2.25V9Z" |
| 88 | + clipRule="evenodd" |
| 89 | + /> |
| 90 | + </svg> |
| 91 | + )} |
| 92 | + </span> |
| 93 | + </Accordion.Trigger> |
| 94 | + </Accordion.Header> |
| 95 | + <Accordion.Content asChild forceMount style={{ display: "block" }}> |
| 96 | + <motion.div |
| 97 | + initial="collapsed" |
| 98 | + animate={openItem === item.id.toString() ? "open" : "collapsed"} |
| 99 | + variants={{ |
| 100 | + open: { opacity: 1, height: "auto" }, |
| 101 | + collapsed: { opacity: 0, height: 0 }, |
| 102 | + }} |
| 103 | + transition={{ duration: 0.4 }} |
| 104 | + style={{ width: "100%", overflow: "hidden" }} |
| 105 | + > |
| 106 | + <div |
| 107 | + className="ml-7 mt-1 rounded-lg p-3 text-white md:ml-16" |
| 108 | + style={{ |
| 109 | + borderRadius: "12px", |
| 110 | + textAlign: "left", |
| 111 | + width: "100%", |
| 112 | + }} |
| 113 | + > |
| 114 | + <div className="relative max-w-xs rounded-2xl bg-blue-500 px-4 py-2 text-white"> |
| 115 | + {item.answer} |
| 116 | + <div className="absolute bottom-0 right-0 h-0 w-0 border-l-[10px] border-t-[10px] border-l-transparent border-t-blue-500"></div> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </motion.div> |
| 120 | + </Accordion.Content> |
| 121 | + </Accordion.Item> |
| 122 | + ))} |
| 123 | + </Accordion.Root> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
0 commit comments