Skip to main content

Accordion

A collapsible accordion component to display expandable/collapsible content. It allows you to create a list of accordion items that can be toggled open and closed.

๐Ÿ“ฆ Importโ€‹

import { Accordion } from "@sahilphondekar/react-component-library";

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
itemsArray<Object>โ€” (required)Array of objects representing each accordion item. Each object should contain a title (string) and content (node or string).
allowMultiplebooleanfalseWhether multiple items can be open simultaneously.
defaultOpenFirstbooleanfalseWhether the first item should be open by default.
classNamestring""Class name for the outer container.
itemClassNamestring""Class name for each accordion item.
titleClassNamestring""Class name for the title button of each accordion item.
contentClassNamestring""Class name for the content of each accordion item.
iconOpen`stringReactElement`โ€” (optional)
iconClosed`stringReactElement`โ€” (optional)
iconSizestring"16px"Size of the icons.

๐Ÿงฉ AccordionItem Propsโ€‹

Each item in the items array should follow this structure:

NameTypeDescription
titlestringThe title to be displayed in the accordion header.
content`nodestring`
isOpenbooleanWhether the accordion item is open.
onTogglefunctionFunction to toggle the state of the accordion item.
classNamestringCustom class name for the accordion item.
titleClassNamestringCustom class name for the title button.
contentClassNamestringCustom class name for the content area.
iconOpen`stringReactElement`
iconClosed`stringReactElement`
iconSizestringSize of the open/closed icons.

๐ŸŽจ Styling Notesโ€‹

  • Accordion Item: Styled with border and shadow (border border-gray-200 shadow-sm).
  • Title Button: w-full text-left px-4 py-3 font-medium (customizable).
  • Content: overflow-hidden transition-all duration-300 (content height is dynamically adjusted based on whether it's open or closed).
  • Icons: You can customize the open/closed icons using the iconOpen and iconClosed props. Both string (SVG) and ReactElement types are supported.

๐Ÿงช Exampleโ€‹

<Accordion
items={[
{ title: "Section 1", content: <div>Content for section 1</div> },
{ title: "Section 2", content: "This is some content for section 2" },
]}
allowMultiple={true}
defaultOpenFirst={true}
iconSize="20px"
/>

๐Ÿ”„ Functionalityโ€‹

  • Accordion Toggle: Each item can be expanded or collapsed by clicking on the title. The content is revealed/hidden with a smooth transition.
  • Multiple Open Items: If allowMultiple is set to true, multiple accordion items can be open at once.
  • Icon Customization: You can customize the open and closed icons for each item with the iconOpen and iconClosed props.
  • Dynamic Height: The height of the content area dynamically adjusts to accommodate the content, and it transitions smoothly when opening/closing.

๐Ÿง  Tipsโ€‹

  • Use allowMultiple if you want to enable multiple items to be open at the same time.
  • Customize the open and closed icons to match your design by passing a string (SVG) or React element.
  • You can pass any React node as the content, allowing flexibility for complex content.