accordion
ui-layouts/accordionFreeComponent
ui-layouts publishes no description for this item.
Install it
npx shadcn@latest add https://www.ui-layouts.com/r/accordion.jsonSource
1'use client';23import { cn } from '@/lib/utils';4import { ChevronDown } from 'lucide-react';5import { AnimatePresence, motion } from 'motion/react';6import React, { type ReactNode, useCallback } from 'react';78/**9 * Interface for AccordionContext values10 */11interface AccordionContextType {12 /**13 * Whether the accordion item is active14 */15 isActive?: boolean;16 /**17 * The value of the accordion item18 */19 value?: string;20 /**21 * Function to change the active index22 */23 onChangeIndex?: (value: string) => void;24}2526/**27 * Context for accordion components28 */29const AccordionContext = React.createContext<AccordionContextType>({});30/**31 * Hook to use the accordion context32 */33const useAccordion = () => React.useContext(AccordionContext);3435/**36 * Container component for accordion items37 */38export function AccordionContainer({39 children,40 className,41}: {42 children: ReactNode;43 className?: string;44}) {45 return <div className={cn('grid grid-cols-2 gap-1', className)}>{children}</div>;46}4748/**49 * Wrapper component for accordion items50 */51export function AccordionWrapper({ children }: { children: ReactNode }) {52 return <div>{children}</div>;53}5455/**56 * Interface for Accordion props57 */58interface AccordionProps {59 /**60 * Children components61 */62 children: ReactNode;63 /**64 * Whether multiple items can be active at the same time65 */66 multiple?: boolean;67 /**68 * Default active index69 */70 defaultValue?: string | string[];71}7273/**74 * Accordion component75 */76export function Accordion({ children, multiple, defaultValue }: AccordionProps) {77 /**78 * State for active index79 */80 const [activeIndex, setActiveIndex] = React.useState<string | string[] | null>(81 multiple ? (Array.isArray(defaultValue) ? defaultValue : []) : defaultValue || null82 );8384 /**85 * Function to change the active index86 */87 const onChangeIndex = useCallback(88 (value: string) => {89 setActiveIndex((currentActiveIndex) => {90 if (!multiple) {91 return value === currentActiveIndex ? null : value;92 }9394 if (Array.isArray(currentActiveIndex)) {95 if (currentActiveIndex.includes(value)) {96 return currentActiveIndex.filter((i) => i !== value);97 }98 return [...currentActiveIndex, value];99 }100101 return [value];102 });103 },104 [multiple]105 );106107 return React.Children.map(children, (child) => {108 if (!React.isValidElement(child)) return null;109110// … truncated
Files it writes
More from ui-layouts
All 1 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 3d-marquee3d-marquee | ui-layouts | Components | Free | no deps · 2 files | |
| align-slideralign-slider | ui-layouts | Components | Free | 6 deps · 2 files | |
| animal-imagemaskinganimal-imagemasking | ui-layouts | Components | Free | 1 dep | |
| animated-beamanimated-beam | ui-layouts | Components | Free | no deps | |
| animated-beam-bidirectionalanimated-beam-bidirectional | ui-layouts | Components | Free | no deps | |
| animated-beam-defaultanimated-beam-default | ui-layouts | Components | Free | no deps | |
| animated-beam-multiple-inputanimated-beam-multiple-input | ui-layouts | Components | Free | no deps | |
| animated-beam-multiple-outputanimated-beam-multiple-output | ui-layouts | Components | Free | no deps |
