ExpandableCard

Renders an expandable card with image, title, and content. Uses a progressive enhancement model where content is fully visible by default, and JavaScript enables interactive expand/collapse behavior.

Preview/Playground

Created by
Creator
Barsan
Card image

Card Title

Optional subtitle

Your content here...

Astro Astro
import ExpandableCard from '@astroanimate/core/ExpandableCard';

<ExpandableCard
  title="Card Title"
  subtitle="Optional subtitle"
  enhance
>
  <p>Your content here</p>
</ExpandableCard>

Installation

bash
npm install @astroanimate/core

Usage

import ExpandableCard from '@astroanimate/core/ExpandableCard';

<ExpandableCard
  title="Card Title"
  subtitle="Optional subtitle"
  enhance
>
  <p>Your content here</p>
</ExpandableCard>

Enables interactive expand/collapse behavior with smooth transitions.

Props

Prop Type Default Description
title string - Title text rendered in the card header (required)
subtitle string "" Subtitle text below the title
image string Unsplash URL Image URL for the card header
imageAlt string "Card image" Alt text for the image
initialOpen boolean false Whether the card starts in the expanded state
duration number 300 Transition duration in milliseconds
easing string "ease" CSS easing function for transitions
enhance boolean false Enable JavaScript-powered expand/collapse interaction (click, keyboard, aria attributes)
class string "" Additional class name for the root element

Behavior

  • Content is fully visible by default (no JavaScript required)
  • When enhance=true, JavaScript enables interactive expand/collapse behavior
  • JavaScript sets data-state attributes, while CSS handles all visual transitions
  • Initial state is controlled via initialOpen and rendered server-side
  • When collapsed, content height is animated using grid-template-rows
  • Trigger element becomes interactive only after JavaScript initializes
  • Chevron icon is hidden without JS and shown when enhancement is active

Use When

  • Collapsible content sections that should remain accessible without JavaScript
  • Progressive enhancement scenarios where interactivity is optional
  • Simple expand/collapse UI with smooth transitions
  • When keyboard-accessible toggle behavior is required

Avoid When

  • Content must be hidden by default without JavaScript (content is always visible initially)
  • Complex nested or multi-level accordion structures
  • Advanced animation choreography beyond simple expand/collapse

Accessibility

  • Respects prefers-reduced-motion (disables transitions)
  • When enhance=true:
    • Adds role="button", tabindex="0", and aria-expanded
    • Supports keyboard interaction (Enter and Space)
  • Focus-visible styles support keyboard navigation
  • Content remains fully accessible without JavaScript

Performance

  • CSS-first animation using grid-template-rows (no layout thrashing)
  • JavaScript only updates attributes (data-state, ARIA)
  • WeakMap prevents duplicate initialization
  • MutationObserver ensures cleanup when element is removed from the DOM

Credits