Simple, fast, accessible accordion library with no dependency

Overview

React Fast Accordion ⚡️

Dynamic, fast, accessible & zero dependency accordion for React

Npm version Downloads

How it's fast?

  • Instead of adding event listener on all the items, it only adds to the container.
  • It has zero dependency, the animations are done using web animation API
  • Detail component dynamically gets added to the DOM

Demo

Screen recording of accordion

Edit React fast accordion demo

Video tutorial

Accordion tutorial-5

Installation

Install with npm

  npm install react-fast-accordion

Install with yarn

  yarn add react-fast-accordion

Example

import React from "react";
import { faker } from "@faker-js/faker";
import Accordion from "react-fast-accordion";

// Your list - array of objects, id is required
const data = Array.from({ length: 200 }, () => {
  return {
    id: faker.datatype.uuid(),
    title: faker.hacker.noun(),
    content: faker.hacker.phrase(),
  };
});

// create type if you need intellisense
type CompProps = typeof data[0] & {
  isOpen: boolean;
  onClick: (txt: string) => void;
};

// all the props get passed here with isOpen
const SummaryComponent = ({ title, isOpen }: CompProps) => (
  <div className="header">
    {title} <span className={(isOpen ? "open" : "") + " chevron"}>👇</span>
  </div>
);

// component will get wrapped in <div class="acc-content">
const DetailComponent = ({ content, onClick }: CompProps) => (
  <p onClick={() => onClick(content)}>{content}</p>
);

const App = () => {
  return (
    <div>
      <Accordion
        items={data}
        // you can pass any props,
        // it will be passed to the Detail & Summary
        onClick={(txt: string) => alert("You clicked on\n" + txt)}
        // set it to false if you want only one accordion to open
        multiExpand={true}
        SummaryComponent={SummaryComponent}
        DetailComponent={DetailComponent}
      />
    </div>
  );
};

Accordion props

Parameter Type Description Required
items Array<{id: string, ...}> List which you want to render Yes
SummaryComponent React.Element Component for rendering summary Yes
DetailComponent React.Element Component shown on expanding Yes
multiExpand boolean default:false Expand only one at a time No.
You might also like...

Fast and lightweight dependency-free vanilla JavaScript polyfill for native lazy loading / the awesome loading='lazy'-attribute.

loading="lazy" attribute polyfill Fast and lightweight vanilla JavaScript polyfill for native lazy loading, meaning the behaviour to load elements rig

Dec 30, 2022

EggyJS is a Javascript micro Library for simple, lightweight toast popups focused on being dependency-less, lightweight, quick and efficient.

EggyJS EggyJS is a Javascript micro Library for simple, lightweight toast popups. The goal of this library was to create something that meets the foll

Jan 8, 2023

A functional, immutable, type safe and simple dependency injection library inspired by angular.

func-di English | 简体中文 A functional, immutable, type safe and simple dependency injection library inspired by Angular. Why func-di Installation Usage

Dec 11, 2022

Simple click-triggered navigation submenus. Accessible and progressively enhanced.

Clicky Menus! A project by Mark Root-Wiley, MRW Web Design Clicky Menus lets you create a progressively-enhanced, accessible one-level dropdown menu t

Dec 6, 2022

A library of high-quality primitives that help you build accessible user interfaces with SolidJS.

Solid Aria A library of high-quality primitives that help you build accessible user interfaces with SolidJS. Primitives @solid-aria/primitives - Expor

Jan 7, 2023

An accessible dialog window library for all humans.

An accessible dialog window library for all humans.

Version 0.4.4 Requires jQuery 1.11.2 or higher (v2 not tested, v3 works but not extensively stress tested). Built by Humaan Modaal Modaal is a WCAG 2.

Dec 26, 2022

Vanilla JS Accessible Dialog Library

A11y Dialog Component a11y-dialog-component is a fast, lightweight and flexible vanilla JavaScript library to create accessible modal, non-modal or to

Nov 17, 2022

Accessible, lightweight, stylish modal library in pure JavaScript

accessible-minimodal Accessible, lightweight ( 8 kB), stylish modal library in pure JavaScript Example https://codepen.io/imhvost/pen/LYNazqo (with "

Aug 4, 2022

A TypeScript library for creating dependency snapshots.

Dependency Submission Toolkit @github/dependency-submission-toolkit is a TypeScript library for creating dependency snapshots and submitting them to t

Nov 22, 2022
Comments
  • Removed interval polling.

    Removed interval polling.

    1. Used ref for element animations Removed getElementById element grabbing as a ref can be used for this, and it eliminates the need to maintain the variable id attribute on the child elements.

    2. Moved opening state update to individual list components

    3. Removed interval polling The interval polling and possible change to a mutation observer is not ideal for performance. In both instances, every ListItem re-renders every time you open or close one item.

    For example, in the demo app, every click was causing 400 re-renders, even though there are only 200 ListItems. With changes in this PR, every click is 1 re-render.

    In this case, not only is there northing wrong with having an event listener per component, it's better for performance than the previously mentioned options, if for no other reason than just reducing re-renders.

    The clickHandler now contains the logic that opens or closes the accordion, and the end result doesn't need to clean anything up with a useEffect, it doesn't need to make any decisions based on parentElements, and doesn't rely on the existence of id attributes to determine what was clicked.

    I know this works differently from the existing implementation - just wanted to offer some other ways to do it with explanations.

    opened by justinformentin 2
  • DetailComponent doesn't show up for the first click

    DetailComponent doesn't show up for the first click

    image

    See the screenshot. The first item is clicked once. It changed classes, so I've got the isOpen prop to be true.
    The second item is clicked three times: open(no), close, open(yes). And it starts to work fine. The third item is just a closed item example.

    Sometimes some items work for the first click. But mostly not.

    I noticed that the CSS properties of the DetailComponent tag (max-height and opacity) do not change even when it works fine.

    question 
    opened by Yevhenii2 1
  • Feature: Automatically scroll element into view if needed

    Feature: Automatically scroll element into view if needed

    We need something like scrollIntoViewIfNeeded but it's not majorly supported and doesn't have smooth behaviour

    https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded

    Content scroll height needs to be added as well while scrolling the element

    enhancement help wanted good first issue 
    opened by ShivamJoker 2
Owner
Shivam
Creative Engineer 💻 | Married to JavaScript | Ex jQuery | Boyfriend of Typescript & React ⚛️
Shivam
Accordion Slider is a jQuery plugin that combines the functionality of a slider with that of an accordion.

Accordion Slider - jQuery slider plugin A responsive and touch-enabled jQuery accordion slider plugin that combines the functionality of a slider with

null 0 Dec 29, 2022
A dependency-free Vanilla JS Accordion Menu Nested

?? A dependency-free Vanilla JS Accordion Menu Nested No dependencies, no automation build tools. Copy/paste and ready to use. CSS and JS are inlined

Tomasz Bujnowicz 4 Dec 18, 2022
Javascript accordion - tiny and simple.

Javascript Accordion Javascript accordion - tiny and simple. Accordions are useful when you want to toggle between hiding and showing large amount of

Kenan Gündoğan 4 Dec 8, 2022
Easy responsive tabs - is a lightweight jQuery plugin which optimizes normal horizontal or vertical tabs to accordion on multi devices

Easy responsive tabs - is a lightweight jQuery plugin which optimizes normal horizontal or vertical tabs to accordion on multi devices like: web, tablets, Mobile (IPad & IPhone). This plugin adapts the screen size and changes its action accordingly.

Samson Onna 600 Dec 8, 2022
Responsive tabs-to-accordion script without jQuery, written using pure JavaScript

vanilla-tabs Responsive tabs-to-accordion script without jQuery, written using pure JavaScript Author Dmytro Kudleichuk LinkedIn GitHub Online Demo Se

Dmitriy Kudleichuk 7 Dec 20, 2022
Accordion Plugin written in Vanilla JavaScript.

Easy Accordion Accordion plugin written purely in JavaScript. Setup So, to start using the Easy Accordion plugin in your project, you can include the

Farhan Halai 1 Dec 16, 2020
Just a jQuery accordion plugin

BeefUp Just a jQuery accordion plugin https://schascha.github.io/BeefUp/ Examples: Toggle buttons Hashchange CSS animations Self block Installation Yo

Sascha Künstler 44 Jul 6, 2022
jQuery plugin that combines the functionality of a grid with that of an accordion.

Grid Accordion - jQuery plugin A responsive and touch-enabled jQuery grid accordion plugin that combines the functionality of a grid with that of an a

null 1 Dec 16, 2022
DOM ViewModel - A thin, fast, dependency-free vdom view layer

domvm (DOM ViewModel) A thin, fast, dependency-free vdom view layer (MIT Licensed) Introduction domvm is a flexible, pure-js view layer for building h

null 604 Dec 8, 2022
A dependency-free JavaScript ES6 slider and carousel. It’s lightweight, flexible and fast. Designed to slide. No less, no more

Glide.js is a dependency-free JavaScript ES6 slider and carousel. It’s lightweight, flexible and fast. Designed to slide. No less, no more What can co

null 6.7k Jan 3, 2023