// Styled-Components Version
// Full working example (React 18+). Install: npm i styled-components react
// Usage: <Accordion items={[{ title: 'Item 1', content: 'Content 1' }, ...]} />
import React, { useState } from 'react';
import styled, { css, keyframes } from 'styled-components';
const fadeIn = keyframes`
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
`;
const expand = keyframes`
from {
height: 0;
opacity: 0;
}
to {
height: var(--max-height);
opacity: 1;
}
`;
const AccordionContainer = styled.div`
border: 1px solid #e2e8f0;
border-radius: 8px;
overflow: hidden;
max-width: 600px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
`;
const AccordionItem = styled.div`
border-bottom: 1px solid #f1f5f9;
&:last-child {
border-bottom: none;
}
`;
const Header = styled.button`
width: 100%;
padding: 1rem 1.5rem;
background: white;
border: none;
text-align: left;
font-size: 1rem;
font-weight: 500;
color: #374151;
cursor: pointer;
display: flex;
align-items: center;
justify-content: space-between;
transition: background-color 0.2s ease;
&:hover {
background: #f8fafc;
}
${({ isOpen }) => isOpen && css`
background: #f1f5f9;
`}
`;
const Icon = styled.span`
transition: transform 0.3s ease;
${({ isOpen }) => isOpen && css`
transform: rotate(180deg);
`}
`;
const Content = styled.div`
overflow: hidden;
background: #f8fafc;
transition: all 0.3s ease;
${({ isOpen }) => isOpen
? css`
animation: ${expand} 0.3s ease-out forwards;
max-height: 500px; /* Adjust based on content */
`
: css`
max-height: 0;
`}
`;
const ContentInner = styled.div`
padding: 1.5rem;
opacity: 0;
animation: ${fadeIn} 0.3s ease-out 0.1s forwards;
`;
const Accordion = ({ items }) => {
const [activeIndex, setActiveIndex] = useState(null);
const handleToggle = (index) => {
setActiveIndex(activeIndex === index ? null : index);
};
return (
<AccordionContainer>
{items.map((item, index) => (
<AccordionItem key={index}>
<Header
isOpen={activeIndex === index}
onClick={() => handleToggle(index)}
aria-expanded={activeIndex === index}
>
{item.title}
<Icon isOpen={activeIndex === index}>▼</Icon>
</Header>
<Content isOpen={activeIndex === index}>
<ContentInner>{item.content}</ContentInner>
</Content>
</AccordionItem>
))}
</AccordionContainer>
);
};
export default Accordion;
// Utility-First CSS-in-JS Version (using Panda CSS)
// Panda CSS is a runtime CSS-in-JS library with utility-first syntax (Tailwind-like).
// Install: npm i panda-css @pandacss/dev
// Setup: npx panda css init (configures recipe for accordion utilities).
// Full working example.
// panda.config.ts (simplified setup excerpt)
import { defineConfig } from 'panda-css';
export default defineConfig({
// ... presets: ['@pandacss/preset-radix']
recipes: {
accordion: {
slots: {
container: {},
item: {},
header: {},
icon: { base: { tw: 'transition-transform duration-300 ease-in-out' } },
content: { base: { tw: 'overflow-hidden bg-slate-50' } },
inner: { base: { tw: 'p-6 opacity-0 animate-fadeIn' } },
},
variants: {
open: {
header: { base: { tw: 'bg-slate-100' } },
icon: { base: { tw: 'rotate-180' } },
content: { base: { tw: 'animate-expand max-h-96' } },
},
},
},
},
globalCss: {
'@keyframes': {
fadeIn: {
from: { opacity: 0, transform: 'translateY(-10px)' },
to: { opacity: 1, transform: 'translateY(0)' },
},
expand: {
from: { height: 0, opacity: 0 },
to: { height: 'var(--max-h)', opacity: 1 },
},
},
},
});
// Component code
import React, { useState } from 'react';
import { css } from 'panda-css';
const accordionStyles = css({
accordion: {
container: {
base: {
tw: 'border border-slate-200 rounded-lg overflow-hidden max-w-xl shadow-sm',
},
},
item: { base: { tw: 'border-b border-slate-100 last:border-b-0' } },
header: {
base: {
tw: 'w-full p-4 bg-white border-none text-left text-base font-medium text-slate-700 cursor-pointer flex items-center justify-between hover:bg-slate-50 transition-colors',
},
},
content: {
base: { tw: 'overflow-hidden bg-slate-50 transition-all duration-300' },
},
inner: { base: { tw: 'p-6 opacity-0 animate-[fadeIn_0.3s_ease-out_0.1s_forwards]' } },
},
});
const Accordion = ({ items }) => {
const [activeIndex, setActiveIndex] = useState(null);
const handleToggle = (index) => {
setActiveIndex(activeIndex === index ? null : index);
};
return (
<div className={accordionStyles.container()}>
{items.map((item, index) => (
<div key={index} className={accordionStyles.item()}>
<button
className={accordionStyles.header({ open: activeIndex === index })}
onClick={() => handleToggle(index)}
aria-expanded={activeIndex === index}
>
{item.title}
<span className={accordionStyles.icon({ open: activeIndex === index })}>
▼
</span>
</button>
<div className={accordionStyles.content({ open: activeIndex === index })}>
<div className={accordionStyles.inner()}>{item.content}</div>
</div>
</div>
))}
</div>
);
};
export default Accordion;
Trade-offs
| Aspect | Styled-Components (Component-First) | Utility-First (e.g., Panda CSS / Tailwind CSS-in-JS) |
|---|
| Bundle Size | Larger (generates unique classNames per component instance, potential duplication without optimization). | Smaller/more efficient (atomic utilities shared across app, tree-shakable). |
| Developer Experience | Expressive for complex, semantic styles; props for dynamic theming; familiar CSS syntax. | Rapid prototyping; scanable HTML; design system enforced via utilities; autocomplete in IDEs. |
| Maintainability | Great for reusable, encapsulated components; styles co-located with logic. | Consistent across team (no "CSS snowflakes"); easier to refactor (change utility = global change). |
| Performance | Runtime overhead (styles injected dynamically); SSR hydration mismatches possible. | Zero-runtime (Panda extracts to CSS); faster critical render; better caching. |
| Learning Curve | Requires CSS expertise; prop drilling for variants. | Steep initially (memorize utilities); scales well with docs/cheatsheets. |
| Dynamic Styles | Excellent (props/functions/keyframe interpolation). | Good via variants/recipes (Panda) or arbitrary values (Tailwind [...], Panda css({ ... })); less flexible. |
| Accessibility/SEO | Semantic by default; easy custom selectors. | Relies on HTML semantics + utilities; Headless UI pairs well. |
| Collaboration | Designers/devs need CSS knowledge. | Designers can contribute via Figma/Tailwind plugins; non-devs scan classes easily. |
| When to Use | Complex UIs, heavy customization, teams with strong CSS skills. | Prototyping, consistent design systems, large apps prioritizing speed/maintainability. |
Summary: Styled-components shines in bespoke, prop-driven designs but can lead to bloat/maintenance debt. Utility-first excels in speed/consistency but trades expressiveness for atomic reusability. Hybrid (e.g., Panda recipes) bridges the gap. Choose based on team size, app scale, and perf needs.