collapse.tsx


Overview

The collapse.tsx file defines a reusable React functional component named Collapse. This component provides a user interface element that can expand or collapse to show or hide content, commonly known as an accordion or collapsible panel.

Collapse is built on top of the Collapsible component from a UI library and enhances it by adding a customizable title section with optional right-aligned content and an icon, making it suitable for use in complex UI layouts where toggling visibility of content is needed.


Detailed Documentation

Component: Collapse

Description

Collapse is a wrapper component providing an interactive collapsible section with a header that includes an icon, a title, and an optional right-aligned content area. It manages the open/closed state either controlled or uncontrolled, and provides event hooks for state changes.

Props

The Collapse component accepts the following props, which extend the CollapsibleProps type (from @radix-ui/react-collapsible), except for the title prop from the underlying component which it overrides:

Prop Name

Type

Default

Description

title

ReactNode

undefined

The content to display as the main title on the collapsible header. Can be text or JSX.

rightContent

ReactNode

undefined

Optional content to display on the right side of the header, allowing additional controls or info.

children

ReactNode

The content to show or hide inside the collapsible panel.

open

boolean

undefined

Controlled prop to indicate if the collapsible should be open or closed.

defaultOpen

boolean

false

Initial open state when uncontrolled.

onOpenChange

(open: boolean) => void

undefined

Callback function called when the open state changes.

disabled

boolean

undefined

If true, disables user interaction with the collapsible.

Returns

A React element representing a collapsible section with a header and content area.

Usage Example

import { Collapse } from './collapse';

function Example() {
  return (
    <Collapse title="More Details" rightContent={<button>Help</button>} defaultOpen>
      <p>This is the detailed content that can be expanded or collapsed.</p>
    </Collapse>
  );
}

Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class Collapse {
        +title: ReactNode
        +rightContent: ReactNode
        +children: ReactNode
        +open?: boolean
        +defaultOpen: boolean
        +onOpenChange?: (open: boolean) => void
        +disabled?: boolean
        +render(): ReactElement
    }

    class Collapsible {
        +defaultOpen?: boolean
        +open?: boolean
        +onOpenChange?: (open: boolean) => void
        +disabled?: boolean
    }

    class CollapsibleTrigger
    class CollapsibleContent
    class ListCollapse

    Collapse *-- Collapsible : uses
    Collapse *-- CollapsibleTrigger : uses inside Collapsible
    Collapse *-- CollapsibleContent : uses inside Collapsible
    Collapse *-- ListCollapse : displays icon in header

Summary

The collapse.tsx file offers a simple, accessible, and customizable collapsible UI component. It abstracts the complexity of managing open/close state and UI layout, while allowing flexible content and integration with the application's design system. This component is essential for creating dynamic, space-efficient interfaces where content visibility needs to be toggled by users.