-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ fix: Improved FAQ layout * chore: fix styles * Update faq.mdx --------- Co-authored-by: Arjun Aditya <[email protected]>
- Loading branch information
1 parent
8df9e1d
commit 2c79b5f
Showing
2 changed files
with
69 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ChevronDownMini, ChevronUpMini } from "@medusajs/icons"; | ||
import React, { ReactNode, useState, useRef, useEffect } from "react"; | ||
|
||
interface FAQ { | ||
question: string; | ||
answer: string | ReactNode; | ||
} | ||
|
||
export function FAQItem({ children }: Readonly<{ children: ReactNode[] }>) { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [question, answer] = React.Children.toArray(children); | ||
const answerRef = useRef<HTMLDivElement>(null); | ||
const [height, setHeight] = useState<number | undefined>(0); | ||
|
||
useEffect(() => { | ||
if (isOpen && answerRef.current) { | ||
setHeight(answerRef.current.scrollHeight); | ||
} else { | ||
setHeight(0); | ||
} | ||
}, [isOpen]); | ||
|
||
return ( | ||
<div className="py-0.5"> | ||
<div className="card bg-medusa-bg-subtle dark:bg-medusa-bg-base shadow-card-rest dark:shadow-card-rest-dark w-full py-1 px-0.5 rounded-md"> | ||
<button | ||
className="flex w-full justify-between items-center text-left bg-transparent border-none cursor-pointer" | ||
onClick={() => setIsOpen(!isOpen)} | ||
aria-expanded={isOpen} | ||
> | ||
<span className="text-lg font-medium cursor-pointer">{question}</span> | ||
{isOpen ? ( | ||
<ChevronUpMini className="text-gray-500" /> | ||
) : ( | ||
<ChevronDownMini className="text-gray-500" /> | ||
)} | ||
</button> | ||
<div | ||
ref={answerRef} | ||
className={`overflow-hidden transition-all duration-300 ease-in-out`} | ||
style={{ height: height }} | ||
role="region" | ||
> | ||
<div className="px-[0.3rem] py-[0.8rem]">{answer}</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export function FAQ({ children }: Readonly<{ children: ReactNode }>) { | ||
return <div className="flex flex-col">{children}</div>; | ||
} |