Build an accordion for your UI component library with React, TypeScript and Tailwind

Опубликовано: 02 Октябрь 2024
на канале: m6io
1,027
44

See it in action at https://m6io-accordion.vercel.app/

Starter code: https://github.com/m6io/accordion-com...

Step-by-step code: https://www.mahmoud.io/blog/accordion...

Chapter 1: Set up initial static template for Accordion
Chapter 2: Make the Accordion dynamic such that the triggers toggle the content to be open or closed
Chapter 3: Create a reusable "AccordionItem" component and move the "Accordion Trigger" and "Accordion Content" JSX into it
Chapter 4 - Create a reusable "Accordion" component, and create an internal state at the Accordion component to manage the children's open state and toggle behavior. This allows us to bypass the need for a context provider and consumer (although those are also viable solutions)
Chapter 5 - Create reusable "AccordionTrigger" and "AccordionContent" components for better modularity, reusability, and readability. This also allows for cleaner code when consuming the Accordion component API and allows for a stronger seperatoion of concerns between the trigger and the content. This will also allow for more flexibility in terms of customization and styling.
Chapter 6 - Expose an "animated" prop on the Accordion component to enable animation, and update children to use the prop.
Chapter 7 - Expose an "defaultOpenItem" prop on the Accordion component to enable a default open item, and update children to use the prop.

The NPM Registry REST API docs: https://github.com/npm/registry/blob/...

The custom Tailwind scrollbars: https://preline.co/docs/custom-scroll...