Basic Accordion
smoothui-registry/basic-accordionFreeComponent
A BasicAccordion component for SmoothUI.
Install it
npx shadcn@latest add https://www.smoothui.dev/r/basic-accordion.jsonSource
1"use client";23import { ChevronDown } from "lucide-react";4import { motion, useReducedMotion } from "motion/react";5import { useState } from "react";67const CHEVRON_ROTATION_DEGREES = 180;8const CHEVRON_ANIMATION_DURATION = 0.2;910export interface AccordionItem {11 content: React.ReactNode;12 id: string | number;13 title: string;14}1516export interface BasicAccordionProps {17 allowMultiple?: boolean;18 className?: string;19 defaultExpandedIds?: Array<string | number>;20 items: AccordionItem[];21}2223export default function BasicAccordion({24 items,25 allowMultiple = false,26 className = "",27 defaultExpandedIds = [],28}: BasicAccordionProps) {29 const [expandedItems, setExpandedItems] =30 useState<Array<string | number>>(defaultExpandedIds);31 const shouldReduceMotion = useReducedMotion();3233 const toggleItem = (id: string | number) => {34 if (expandedItems.includes(id)) {35 setExpandedItems(expandedItems.filter((item) => item !== id));36 } else if (allowMultiple) {37 setExpandedItems([...expandedItems, id]);38 } else {39 setExpandedItems([id]);40 }41 };4243 return (44 <div45 className={`flex w-full flex-col divide-y divide-border overflow-hidden rounded-lg border ${className}`}46 >47 {items.map((item) => {48 const isExpanded = expandedItems.includes(item.id);4950 return (51 <div className="overflow-hidden" key={item.id}>52 <button53 aria-controls={`accordion-content-${item.id}`}54 aria-expanded={isExpanded}55 className="flex min-h-[44px] w-full cursor-pointer items-center justify-between gap-2 bg-background px-4 py-3 text-left transition-colors hover:bg-primary focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"56 id={`accordion-header-${item.id}`}57 onClick={() => toggleItem(item.id)}58 type="button"59 >60 <h3 className="font-medium">{item.title}</h3>61 <motion.div62 animate={{ rotate: isExpanded ? CHEVRON_ROTATION_DEGREES : 0 }}63 className="shrink-0"64 transition={{65 duration: shouldReduceMotion ? 0 : CHEVRON_ANIMATION_DURATION,66 }}67 >68 <ChevronDown className="h-5 w-5" />69 </motion.div>70 </button>7172 {/* Content stays mounted (height-animated, not unmounted) so the73// … truncated
What it pulls in
npm packages
Files it writes
More from SmoothUI Registry
All 178 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Agent Avataragent-avatar | SmoothUI Registry | Components | Free | no deps | |
| Ai Approvalai-approval | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Artifactai-artifact | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Branchai-branch | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Citationai-citation | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Context Meterai-context-meter | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Conversationai-conversation | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Coreai-core | SmoothUI Registry | Components | Free | 1 dep |
