|
| 1 | +import React, { useState, useRef, useEffect } from 'react' |
| 2 | +import { cn } from '../utils/cn' |
| 3 | + |
| 4 | +export type AccordionItemProps = { |
| 5 | + title: string |
| 6 | + children: React.ReactNode |
| 7 | + defaultOpen?: boolean |
| 8 | + className?: string |
| 9 | +} |
| 10 | + |
| 11 | +export const AccordionItem = React.forwardRef< |
| 12 | + HTMLDivElement, |
| 13 | + AccordionItemProps |
| 14 | +>(({ title, children, defaultOpen = false, className }, ref) => { |
| 15 | + const [isOpen, setIsOpen] = useState(defaultOpen) |
| 16 | + const contentRef = useRef<HTMLDivElement>(null) |
| 17 | + const [maxHeight, setMaxHeight] = useState<string>('0px') |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (contentRef.current) { |
| 21 | + const scrollHeight = contentRef.current.scrollHeight |
| 22 | + setMaxHeight(isOpen ? `${scrollHeight}px` : '0px') |
| 23 | + } |
| 24 | + }, [isOpen, children]) |
| 25 | + |
| 26 | + return ( |
| 27 | + <div |
| 28 | + ref={ref} |
| 29 | + className={cn( |
| 30 | + 'border-b border-white/10 py-4 transition-colors', |
| 31 | + className |
| 32 | + )} |
| 33 | + > |
| 34 | + <button |
| 35 | + onClick={() => setIsOpen(!isOpen)} |
| 36 | + className="flex w-full items-center justify-between text-left" |
| 37 | + > |
| 38 | + <svg |
| 39 | + className={cn( |
| 40 | + 'h-6 w-6 transform transition-transform', |
| 41 | + isOpen ? 'rotate-45 text-white' : 'text-white/80' |
| 42 | + )} |
| 43 | + fill="none" |
| 44 | + stroke="currentColor" |
| 45 | + viewBox="0 0 24 24" |
| 46 | + xmlns="http://www.w3.org/2000/svg" |
| 47 | + > |
| 48 | + <path |
| 49 | + strokeLinecap="round" |
| 50 | + strokeLinejoin="round" |
| 51 | + strokeWidth={2} |
| 52 | + d="M12 4v16m8-8H4" |
| 53 | + /> |
| 54 | + </svg> |
| 55 | + <span |
| 56 | + className={cn( |
| 57 | + 'ml-2 flex-1 text-left font-semibold transition-colors', |
| 58 | + isOpen ? 'text-white' : 'text-white/80' |
| 59 | + )} |
| 60 | + > |
| 61 | + {title} |
| 62 | + </span> |
| 63 | + </button> |
| 64 | + |
| 65 | + <div |
| 66 | + ref={contentRef} |
| 67 | + style={{ maxHeight }} |
| 68 | + className="transition-max-height overflow-hidden duration-300 ease-in-out" |
| 69 | + > |
| 70 | + <div className="mt-2 font-medium text-white/70">{children}</div> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + ) |
| 74 | +}) |
| 75 | + |
| 76 | +AccordionItem.displayName = 'AccordionItem' |
| 77 | + |
| 78 | +export type AccordionProps = { |
| 79 | + children: React.ReactElement<AccordionItemProps>[] |
| 80 | + className?: string |
| 81 | +} |
| 82 | + |
| 83 | +export const Accordion: React.FC<AccordionProps> = ({ |
| 84 | + children, |
| 85 | + className |
| 86 | +}) => { |
| 87 | + return ( |
| 88 | + <div |
| 89 | + className={cn( |
| 90 | + 'w-full rounded-2xl bg-[#2c2c54]/60 p-6 shadow-lg backdrop-blur-md', |
| 91 | + className |
| 92 | + )} |
| 93 | + > |
| 94 | + {React.Children.map(children, (child, idx) => |
| 95 | + React.cloneElement(child, { |
| 96 | + key: idx |
| 97 | + }) |
| 98 | + )} |
| 99 | + </div> |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +Accordion.displayName = 'Accordion' |
0 commit comments