Accordion

Un componente que permite mostrar y ocultar elementos de una lista.

Sobre el componente

Este componente se utiliza para mostrar información compleja o detalles de manera clara y concisa.

Está compuesto por dos elementos principales:

  • Un componente Accordion que se encarga de renderizar los items de la lista.
  • Un componente AccordionItem que se encarga de mostrar el contenido del accordion.

Uso básico

Para utilizar el componente, simplemente debes definir una lista de objetos con las propiedades title y content. Luego, debes renderizar el componente Accordion y pasar la lista de objetos como propiedad items.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
/>

El código para AccordionItem es el siguiente:

import {
  forwardRef,
  useRef,
  useState,
  useEffect,
  useId,
  useCallback
} from 'react'
import clsx from 'clsx'

const CONTAINER_VARIANTS = {
  default: 'border-0 shadow-lg backdrop-blur-md',
  light: 'border-b-2',
  bordered: 'border rounded-sm',
  complete: 'border backdrop-blur-sm shadow-lg'
}

const BODY_VARIANTS = {
  default: 'backdrop-blur-md',
  light: 'bg-transparent dark:bg-transparent',
  bordered: 'border-x',
  complete: 'backdrop-blur-sm border-x'
}

const COLOR_BG = {
  default: 'bg-neutral-100/20 dark:bg-zinc-700/30 dark:shadow-neutral-100/5',
  primary: 'bg-blue-500/40 dark:shadow-blue-500/20',
  secondary: 'bg-indigo-500/40 dark:shadow-indigo-500/20',
  success: 'bg-green-400/50 dark:shadow-green-500/20',
  warning: 'bg-yellow-500/40 dark:shadow-yellow-500/20',
  danger: 'bg-red-500/40 dark:shadow-red-500/20'
}

const COLOR_BORDER = {
  default: 'border-zinc-700/20 dark:border-neutral-100/30',
  primary: 'border-blue-800 dark:border-blue-500',
  secondary: 'border-indigo-800 dark:border-indigo-500',
  success: 'border-green-800 dark:border-green-500',
  warning: 'border-yellow-800 dark:border-yellow-500',
  danger: 'border-red-800 dark:border-red-500'
}

const HOVER_BG = {
  default: 'hover:bg-white/50 dark:hover:bg-zinc-800',
  primary: 'hover:bg-blue-500/30 dark:hover:bg-blue-500/10',
  secondary: 'hover:bg-indigo-500/30 dark:hover:bg-indigo-500/20',
  success: 'hover:bg-green-500/60 dark:hover:bg-green-500/30',
  warning: 'hover:bg-yellow-500/60 dark:hover:bg-yellow-500/30',
  danger: 'hover:bg-red-500/30 dark:hover:bg-red-500/10'
}

const AccordionItem = forwardRef(
  (
    {
      index,
      title,
      subtitle,
      content,
      isActive = false,
      toggle,
      styleVariant = 'default',
      color = 'default',
      className,
      ...props
    },
    ref
  ) => {
    const autoId = useId()
    const headingId = `accordion-heading-${autoId}`
    const bodyId = `accordion-body-${autoId}`

    const contentRef = useRef(null)
    const [maxHeight, setMaxHeight] = useState('0px')

    useEffect(() => {
      if (isActive && contentRef.current) {
        setMaxHeight(`${contentRef.current.scrollHeight}px`)
      } else {
        setMaxHeight('0px')
      }
    }, [isActive])

    const handleToggle = useCallback(() => toggle(index), [toggle, index])

    const headerClasses = clsx(
      'flex items-center justify-between w-full py-2 px-3 font-medium gap-3 transition duration-300',
      CONTAINER_VARIANTS[styleVariant],
      styleVariant !== 'light' &&
        styleVariant !== 'bordered' &&
        COLOR_BG[color],
      styleVariant !== 'default' && COLOR_BORDER[color],
      HOVER_BG[color]
    )

    const bodyWrapperClasses = clsx(
      'overflow-hidden transition-all duration-300 ease-in-out',
      BODY_VARIANTS[styleVariant],
      styleVariant !== 'light' &&
        styleVariant !== 'bordered' &&
        COLOR_BG[color],
      styleVariant !== 'default' && COLOR_BORDER[color]
    )

    return (
      <div
        role='region'
        aria-labelledby={headingId}
        className={className}
        ref={ref}
        {...props}
      >
        <h2 id={headingId}>
          <button
            type='button'
            className={headerClasses}
            onClick={handleToggle}
            aria-expanded={isActive}
            aria-controls={bodyId}
            aria-label={`Toggle ${title}`}
          >
            <div className='flex-1'>
              <span className='flex justify-start items-center max-w-full'>
                {title}
              </span>
              {subtitle && (
                <p className='text-left text-sm font-light text-zinc-700 dark:text-neutral-200'>
                  {subtitle}
                </p>
              )}
            </div>
            <svg
              className={clsx(
                'w-3 h-3 shrink-0 transition-transform duration-300',
                isActive && 'rotate-180'
              )}
              aria-hidden='true'
              xmlns='http://www.w3.org/2000/svg'
              fill='none'
              viewBox='0 0 10 6'
            >
              <path
                stroke='currentColor'
                strokeLinecap='round'
                strokeLinejoin='round'
                strokeWidth='2'
                d='M9 5 5 1 1 5'
              />
            </svg>
          </button>
        </h2>

        <div
          id={bodyId}
          ref={contentRef}
          role='region'
          aria-labelledby={headingId}
          style={{ maxHeight }}
          className={bodyWrapperClasses}
        >
          <div className='p-5'>
            <p className='mb-2 text-zinc-700/70 dark:text-neutral-100/70'>
              {content}
            </p>
          </div>
        </div>
      </div>
    )
  }
)

export default AccordionItem

Y el código para Accordion es el siguiente:

import { useState, useCallback, forwardRef } from 'react'
import clsx from 'clsx'
import AccordionItem from '@/components/react/Accordion/AccordionItem'

const Accordion = forwardRef(
  (
    {
      items = [],
      multiple = false,
      styleVariant = 'default',
      color = 'default',
      className,
      ...props
    },
    ref
  ) => {
    const [activeIndexes, setActiveIndexes] = useState([])

    const toggleAccordion = useCallback(
      (index) => {
        setActiveIndexes((prev) => {
          const isActive = prev.includes(index)
          if (multiple) {
            return isActive ? prev.filter((i) => i !== index) : [...prev, index]
          }
          return isActive ? [] : [index]
        })
      },
      [multiple]
    )

    return (
      <div
        ref={ref}
        className={clsx('w-full', className)}
        role='tablist'
        aria-multiselectable={multiple}
        {...props}
      >
        {items.map((item, idx) => (
          <AccordionItem
            key={item.id ?? idx}
            index={idx}
            title={item.title}
            subtitle={item.subtitle}
            content={item.content}
            isActive={activeIndexes.includes(idx)}
            toggle={toggleAccordion}
            styleVariant={styleVariant}
            color={color}
          />
        ))}
      </div>
    )
  }
)

export default Accordion

Con subtitulos

Para agregar subtitulos al componente, simplemente debes agregar un objeto con la propiedad subtitle.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      subtitle: 'Subtitulo 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      subtitle: 'Subtitulo 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      subtitle: 'Subtitulo 3',
      content: '3rd content to show in the accordion'
    }
  ]}
/>

Multiple

Para tener multiples elementos abiertos al mismo tiempo, se puede utilizar el atributo multiple en el componente Accordion:

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  multiple={true}
/>

Variantes

Para cambiar el estilo del componente, se puede utilizar el atributo styleVariant en el componente Accordion. Este atributo acepta cuatro valores: default, light, bordered y complete.

Default

El estilo por defecto del componente es el default.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  styleVariant='default'
/>

Light

Si colocamos el atributo styleVariant en light.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  styleVariant='light'
/>

Bordered

Si colocamos el atributo styleVariant en bordered.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  styleVariant='bordered'
/>

Complete

Si colocamos el atributo styleVariant en complete.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  styleVariant='complete'
/>

Colores

Para cambiar el color del componente, se puede utilizar el atributo color en el componente Accordion. Este atributo acepta los siguientes valores: default, primary, secondary, success, warning y danger.

Default

El color por defecto del componente es el default.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  color='default'
/>

Primary

Si colocamos el atributo color en primary.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  color='primary'
/>

Secondary

Si colocamos el atributo color en secondary.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  color='secondary'
/>

Success

Si colocamos el atributo color en success.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  color='success'
/>

Warning

Si colocamos el atributo color en warning.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  color='warning'
/>

Danger

Si colocamos el atributo color en danger.

1st content to show in the accordion

2nd content to show in the accordion

3rd content to show in the accordion

<Accordion
  items={[
    {
      title: 'Accordion 1',
      content: '1st content to show in the accordion'
    },
    {
      title: 'Accordion 2',
      content: '2nd content to show in the accordion'
    },
    {
      title: 'Accordion 3',
      content: '3rd content to show in the accordion'
    }
  ]}
  color='danger'
/>