ExpandableCard

PreviousNext

An interactive card component that expands and collapses to reveal additional content, with optional image background, tags, and smooth animations

Expandable Card Title

Click to expand and see more details

This is the expandable content that appears when you click the card. It smoothly animates in and out using the ElasticHeight component.

'use client';

import { ExpandableCard } from '@/ui/cards/ExpandableCard';

export function ExpandableCardDemo() {
  return (
    <ExpandableCard title="Expandable Card Title" subtitle="Click to expand and see more details">
      <div className="p-20">
        <p className="text-grey">
          This is the expandable content that appears when you click the card. It smoothly animates
          in and out using the ElasticHeight component.
        </p>
      </div>
    </ExpandableCard>
  );
}

Installation

npx shadcn@latest add https://develop.trident-ui.pro.clubmed/r/expandable-card.json

Usage

import { ExpandableCard } from '@/docs/lib/ui/expandable-card';
 
export function Component() {
  return (
    <ExpandableCard
      title="Explore the Mediterranean"
      subtitle="Discover amazing destinations"
      image={{
        src: '/images/destination.jpg',
        alt: 'Mediterranean coast',
      }}
      tags={[
        {
          label: 'All Inclusive',
          backgroundColor: 'saffron',
          color: 'black',
        },
      ]}
      onClick={(isExpanded) => {
        console.log('Card expanded:', isExpanded);
      }}
    >
      <div className="p-20">
        <p>
          Experience pristine beaches, crystal-clear waters, and world-class amenities at our
          Mediterranean resorts.
        </p>
      </div>
    </ExpandableCard>
  );
}

API Reference

ExpandableCard

PropTypeDefaultDescription
titlestringundefinedRequired. The main heading text displayed in the card.
subtitlestring | nullundefinedOptional subtitle text displayed below the title.
hLevel1 | 2 | 3 | 4 | 5 | 63The semantic heading level for the title (h1-h6).
imageCardBackgroundPropsundefinedOptional background image configuration. When provided, renders an image with a gradient overlay and changes text color to white.
tagsComponentProps<typeof Tag>[] | nullundefinedOptional array of tag configurations to display at the top of the card.
onClick(isExpanded: boolean) => voidundefinedOptional callback function that fires when the card is clicked. Receives the new expanded state as a parameter.
componentstring"article"The HTML element type to render as the root element.
childrenReactNodeundefinedThe collapsible content that appears when the card is expanded. Smoothly animates using ElasticHeight.
classNamestringundefinedAdditional CSS classes to apply to the root card element.

The component also accepts all standard HTML element attributes based on the component prop (defaults to article attributes) via CardProps.

Notes

  • Interactive Behavior: The card automatically manages its own expanded/collapsed state internally using React's useState hook.
  • Smooth Animations: Uses the ElasticHeight component to provide smooth height transitions when expanding and collapsing content.
  • Keyboard Accessible: The clickable area uses the Clickable component, making it fully keyboard accessible with proper ARIA attributes.
  • Visual Indicator: A circular button with an arrow icon rotates 180 degrees to indicate the expanded/collapsed state.
  • Image Support: When an image is provided, the card displays it as a background with a gradient overlay and automatically changes text to white for better contrast.
  • Tag Display: Tags are rendered at the top of the card in a flex-wrap container, allowing multiple tags to flow naturally.
  • Semantic HTML: By default renders as an <article> element, but can be customized via the component prop for different semantic contexts.
  • Card Styling: Inherits all styling features from the base Card component, including themes (bordered, frame, none) and formats (horizontal, vertical, square, custom).
  • State Callback: The onClick callback receives the new expanded state (true/false), allowing parent components to react to state changes.
  • Border Behavior: When an image is present, the top border is automatically removed (border-t-0) for a seamless visual appearance.
  • Data Attributes: The component sets data-name="ExpandableCard" and data-open={isExpanded} attributes for styling and testing purposes.