|
| 1 | +import { useEffect, useState, useCallback, useRef } from 'react'; |
| 2 | +import LinkIcon from '../svg/LinkIcon'; |
| 3 | + |
| 4 | +declare const mixpanel: { track: (event: string, props?: Record<string, unknown>) => void }; |
| 5 | + |
| 6 | +type ExtendedAccordionProps = { |
| 7 | + title: string; |
| 8 | + id: string; |
| 9 | + section?: string; |
| 10 | + children: React.ReactNode; |
| 11 | +} |
| 12 | + |
| 13 | +function slugify(text: string): string { |
| 14 | + return text.toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-').trim(); |
| 15 | +} |
| 16 | + |
| 17 | +export default function ExtendedAccordion({ title, id: idProp, section, children }: ExtendedAccordionProps) { |
| 18 | + const id = idProp || slugify(title); |
| 19 | + const [isOpen, setIsOpen] = useState(false); |
| 20 | + const [copied, setCopied] = useState(false); |
| 21 | + const contentRef = useRef<HTMLDivElement>(null); |
| 22 | + |
| 23 | + // Auto-open if URL hash matches on mount |
| 24 | + useEffect(() => { |
| 25 | + if (typeof window !== 'undefined' && window.location.hash === `#${id}`) { |
| 26 | + setIsOpen(true); |
| 27 | + setTimeout(() => { |
| 28 | + document.getElementById(id)?.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
| 29 | + }, 100); |
| 30 | + } |
| 31 | + }, [id]); |
| 32 | + |
| 33 | + // Set hidden="until-found" on the inner content div when closed. |
| 34 | + // This makes text searchable via cmd+f in Chrome/Firefox. |
| 35 | + // In Safari (no until-found support), display:block !important in CSS keeps |
| 36 | + // the grid trick working so content is still findable via cmd+f. |
| 37 | + useEffect(() => { |
| 38 | + const el = contentRef.current; |
| 39 | + if (!el) return; |
| 40 | + if (!isOpen) { |
| 41 | + el.setAttribute('hidden', 'until-found'); |
| 42 | + } else { |
| 43 | + el.removeAttribute('hidden'); |
| 44 | + } |
| 45 | + }, [isOpen]); |
| 46 | + |
| 47 | + // Listen for beforematch: fires in Chrome/Firefox when browser find |
| 48 | + // highlights text inside a hidden="until-found" element. |
| 49 | + useEffect(() => { |
| 50 | + const el = contentRef.current; |
| 51 | + if (!el) return; |
| 52 | + const handleBeforeMatch = () => { |
| 53 | + setIsOpen(true); |
| 54 | + const url = new URL(window.location.href); |
| 55 | + url.hash = id; |
| 56 | + window.history.replaceState(null, '', url.toString()); |
| 57 | + }; |
| 58 | + el.addEventListener('beforematch', handleBeforeMatch); |
| 59 | + return () => el.removeEventListener('beforematch', handleBeforeMatch); |
| 60 | + }, [id]); |
| 61 | + |
| 62 | + const handleToggle = useCallback(() => { |
| 63 | + const opening = !isOpen; |
| 64 | + setIsOpen(opening); |
| 65 | + if (opening) { |
| 66 | + if (typeof mixpanel !== 'undefined') { |
| 67 | + mixpanel.track("[Docs] FAQ Item Expanded", { |
| 68 | + "faq-question": title, |
| 69 | + ...(section && { "faq-section": section }), |
| 70 | + }); |
| 71 | + } |
| 72 | + const url = new URL(window.location.href); |
| 73 | + url.hash = id; |
| 74 | + window.history.replaceState(null, '', url.toString()); |
| 75 | + } else { |
| 76 | + const url = new URL(window.location.href); |
| 77 | + url.hash = ''; |
| 78 | + window.history.replaceState(null, '', url.toString()); |
| 79 | + } |
| 80 | + }, [isOpen, title, section, id]); |
| 81 | + |
| 82 | + const handleCopyLink = useCallback(async (e: React.MouseEvent | React.KeyboardEvent) => { |
| 83 | + e.stopPropagation(); |
| 84 | + const url = new URL(window.location.href); |
| 85 | + url.hash = id; |
| 86 | + const urlString = url.toString(); |
| 87 | + try { |
| 88 | + await navigator.clipboard.writeText(urlString); |
| 89 | + } catch { |
| 90 | + const ta = document.createElement('textarea'); |
| 91 | + ta.value = urlString; |
| 92 | + ta.style.position = 'fixed'; |
| 93 | + ta.style.left = '-999999px'; |
| 94 | + document.body.appendChild(ta); |
| 95 | + ta.select(); |
| 96 | + document.execCommand('copy'); |
| 97 | + document.body.removeChild(ta); |
| 98 | + } |
| 99 | + setCopied(true); |
| 100 | + setTimeout(() => setCopied(false), 2000); |
| 101 | + }, [id]); |
| 102 | + |
| 103 | + return ( |
| 104 | + <div id={id} className={`extended-accordion${isOpen ? ' open' : ''}`}> |
| 105 | + <button |
| 106 | + className="accordion-header" |
| 107 | + onClick={handleToggle} |
| 108 | + aria-expanded={isOpen} |
| 109 | + aria-controls={`${id}-body`} |
| 110 | + > |
| 111 | + <span className="accordion-title-row"> |
| 112 | + <strong className="accordion-title">{title}</strong> |
| 113 | + <span |
| 114 | + role="button" |
| 115 | + tabIndex={0} |
| 116 | + className={`accordion-anchor-link${copied ? ' copied' : ''}`} |
| 117 | + onClick={handleCopyLink} |
| 118 | + onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') handleCopyLink(e); }} |
| 119 | + aria-label={`Copy link to ${title}`} |
| 120 | + title={copied ? 'Copied!' : 'Copy link to this section'} |
| 121 | + > |
| 122 | + <LinkIcon /> |
| 123 | + </span> |
| 124 | + </span> |
| 125 | + <svg |
| 126 | + viewBox="0 0 24 24" |
| 127 | + stroke="currentColor" |
| 128 | + fill="none" |
| 129 | + strokeWidth="2" |
| 130 | + height="18" |
| 131 | + className={`accordion-chevron${isOpen ? ' rotated' : ''}`} |
| 132 | + > |
| 133 | + <path d="M9 5l7 7-7 7" strokeLinecap="round" strokeLinejoin="round" /> |
| 134 | + </svg> |
| 135 | + </button> |
| 136 | + <div |
| 137 | + id={`${id}-body`} |
| 138 | + className="accordion-body" |
| 139 | + role="region" |
| 140 | + aria-labelledby={id} |
| 141 | + > |
| 142 | + <div ref={contentRef} className="accordion-content"> |
| 143 | + {children} |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + ); |
| 148 | +} |
0 commit comments