Dynamic, fast, accessible & zero dependency accordion for React

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

{ 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) => (
{title} 👇
); // component will get wrapped in
const DetailComponent = ({ content, onClick }: CompProps) => (

onClick(content)}>{content}

); const App = () => { return (
alert("You clicked on\n" + txt)} // set it to false if you want only one accordion to open multiExpand={true} SummaryComponent={SummaryComponent} DetailComponent={DetailComponent} />
); };">
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 
    
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...

Zero dependency profanity detector.

@cnakazawa/profane Zero dependency profanity detector based on Swearjar and Profane. Note: Some examples may contain offensive language for illustrati

Dec 28, 2022

A zero-dependency, strongly-typed web framework for Bun, Node and Cloudflare workers

nbit A simple, declarative, type-safe way to build web services and REST APIs for Bun, Node and Cloudflare Workers. Examples See some quick examples b

Sep 16, 2022

Zero Dependency, Vanilla JavaScript Tag Editor

Zero Dependency, Vanilla JavaScript Tag Editor

_____ |_ _|___ ___ ___ ___ ___ | | | .'| . | . | -_| _| |_| |__,|_ |_ |___|_| |___|___| version 0.4.4 Tagger: Zero dependenc

Nov 25, 2022

Dynamic-web-development - Dynamic web development used CSS and HTML

Dynamic-web-development - Dynamic web development used CSS and HTML

Dynamic-web-development ASSISNMENT I just used CSS and HTML to make a mobile int

Feb 8, 2022

dynamic-component-app is an angular application for dynamic component template creation

MyApp This project was generated with Angular CLI version 14.1.0. Development server Run ng serve for a dev server. Navigate to http://localhost:4200/

Aug 26, 2022

Zero Two Bot,A fully Modular Whatsapp Bot to do everything possible in WhatsApp by Team Zero Two

Zero Two Bot,A fully Modular Whatsapp Bot to do everything possible in WhatsApp by Team Zero Two

🍭 𝗭𝗲𝗿𝗼 𝗧𝘄𝗼 𝗠𝗗 🍭 A Moduler WhatsApp Bot designed for both PM and Groups - To take your boring WhatsApp usage into a whole different level. T

Dec 25, 2022

Multiplies a number by zero. Useful for when you need to multiply a number by zero

multiply-by-zero Multiplies a number by zero. Useful for when you need to multiply a number by zero Please consider checking out the links of this pro

Jul 3, 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.

Dec 8, 2022

Javascript accordion - tiny and simple.

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

Dec 8, 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
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A beautiful, responsive, highly customizable and accessible replacement for JavaScript's popup boxes. Zero dependencies.Alerts ,dialogs

AsgarAlert (v1) for JS Install <script defer src="/asgar-alert.js"></script> Examples The most basic message: asgar("Hello world!"); A message signali

Asgar Aliyev 5 Dec 20, 2022
A lightweight Adobe Photoshop .psd/.psb file parser in typescript with zero-dependency for WebBrowser and NodeJS

@webtoon/psd A lightweight Adobe Photoshop .psd/.psb file parser in typescript with zero-dependency for WebBrowser and NodeJS Browser Support Chrome F

null 830 Jan 1, 2023
A zero-dependency, buildless, terse, and type-safe way to write HTML in JavaScript.

hdot A sensible way to write HTML in JavaScript. Type-safe. Helps you follow the HTML spec. Terse. Almost character for character with plain HTML. Bui

Will Martin 31 Oct 24, 2022
Shifty is a tiny zero-dependency secrets generator, built for the web using TypeScript.

Shifty is a tiny zero-dependency secrets generator, built for the web using TypeScript. Installation yarn add @deepsource/shifty Usage Shifty is built

DeepSource 46 Nov 24, 2022
Jsonup - This is a zero dependency compile-time JSON parser written in TypeScript

jsonup This is a zero dependency compile-time JSON parser written in TypeScript.

TANIGUCHI Masaya 39 Dec 8, 2022