React component to wrap content in Collapsible element with trigger to open and close.
npx @tessl/cli install tessl/npm-react-collapsible@2.10.0React Collapsible is a flexible and accessible component for creating collapsible content sections with customizable triggers. Unlike accordions, multiple sections can be open simultaneously, making it ideal for FAQ sections, content organization, and space-efficient UI layouts.
npm install react-collapsibleimport Collapsible from "react-collapsible";
import { CollapsibleProps } from "react-collapsible";For CommonJS:
const Collapsible = require("react-collapsible");import React from "react";
import Collapsible from "react-collapsible";
// Simple usage
<Collapsible trigger="Click to expand">
<p>This content can be expanded or collapsed.</p>
<p>It can contain any React elements or HTML.</p>
</Collapsible>
// Controlled usage
const [isOpen, setIsOpen] = useState(false);
<Collapsible
trigger="Toggle Content"
open={isOpen}
handleTriggerClick={() => setIsOpen(!isOpen)}
>
<div>Controlled collapsible content</div>
</Collapsible>React Collapsible is built around a single component with comprehensive customization options:
Collapsible class component that wraps content and provides trigger functionalityThe main component for creating collapsible content sections with customizable triggers and content.
import Collapsible from "react-collapsible";
declare class Collapsible extends React.Component<CollapsibleProps> {}
export default Collapsible;Control the timing, easing, and behavior of expand/collapse animations for smooth user experiences.
interface AnimationProps {
transitionTime?: number;
transitionCloseTime?: number | null;
easing?: string;
overflowWhenOpen?: "hidden" | "visible" | "auto" | "scroll" | "inherit" | "initial" | "unset";
}Comprehensive callback system for responding to state changes and user interactions throughout the collapsible lifecycle.
interface EventProps {
handleTriggerClick?: (accordionPosition?: string | number) => void;
onOpen?: () => void;
onClose?: () => void;
onOpening?: () => void;
onClosing?: () => void;
onTriggerOpening?: () => void;
onTriggerClosing?: () => void;
}React Collapsible includes comprehensive accessibility support:
aria-expanded, aria-disabled, aria-controls, and aria-labelledbyrole="button", content has role="region"tabIndex for keyboard navigationcontentHiddenWhenClosed to hide content from screen readers when collapsedIf transitions are not working smoothly, ensure:
height: 0 that don't expand)transitionTime is set to a reasonable value (default: 400ms)For better performance with expensive or large content:
<Collapsible
trigger="Performance optimized"
lazyRender={true}
contentHiddenWhenClosed={true}
transitionTime={300}
>
<ExpensiveComponent />
</Collapsible>Ensure proper accessibility by:
contentHiddenWhenClosed={true} to hide collapsed content from screen readerstabIndex for keyboard navigationtriggerElementProps for aria-labelinterface CollapsibleProps extends React.HTMLProps<Collapsible> {
// Required props
trigger: string | React.ReactElement<any>;
// State control
open?: boolean;
triggerDisabled?: boolean;
// Content behavior
lazyRender?: boolean;
contentHiddenWhenClosed?: boolean;
// Element customization
contentContainerTagName?: string;
triggerTagName?: string;
containerElementProps?: object;
triggerElementProps?: object;
contentElementId?: string;
// Styling
classParentString?: string;
className?: string;
openedClassName?: string;
triggerStyle?: null | React.CSSProperties;
triggerClassName?: string;
triggerOpenedClassName?: string;
contentOuterClassName?: string;
contentInnerClassName?: string;
// Advanced features
triggerWhenOpen?: string | React.ReactElement<any>;
triggerSibling?: string | React.ReactElement<any> | (() => React.ReactElement<any>);
accordionPosition?: string | number;
tabIndex?: number;
// Animation (see Animation sub-doc for details)
transitionTime?: number;
transitionCloseTime?: number | null;
easing?: string;
overflowWhenOpen?: "hidden" | "visible" | "auto" | "scroll" | "inherit" | "initial" | "unset";
// Events (see Events sub-doc for details)
handleTriggerClick?: (accordionPosition?: string | number) => void;
onOpen?: () => void;
onClose?: () => void;
onOpening?: () => void;
onClosing?: () => void;
onTriggerOpening?: () => void;
onTriggerClosing?: () => void;
}The component provides sensible defaults for most properties:
// Key default values
{
transitionTime: 400, // 400ms animation duration
easing: "linear", // Linear animation timing
open: false, // Start closed
triggerTagName: "span", // Trigger element type
contentContainerTagName: "div", // Content container type
classParentString: "Collapsible", // Base CSS class
lazyRender: false, // Render content immediately
triggerDisabled: false, // Enable trigger interaction
overflowWhenOpen: "hidden", // Hide overflow when open
contentHiddenWhenClosed: false // Content visible to screen readers when closed
}