An accordion is a UI component that allows content to be expanded or collapsed with a click on the headers.
- Expandable Sections: Only one section is displayed at a time, while others remain hidden.
- Toggle Mechanism: Users can click headers to expand or collapse content.
- Space Efficiency: Saves screen space by collapsing unused sections.
- Interactive: Enhances user experience by making navigation through content easier.
Accordions are widely used in FAQs, menus, and content-heavy websites to improve readability and organization.
In this app, the state keeps track of which section is expanded. The index of the expanded section is set as "active." When no sections are expanded, it's set to -1. All the functionality in this app is handled by this simple state.
const [active, setActive] = useState<number>(-1);For styling this app, Tailwind CSS is used. Since this is a very small and simple app, there's no issue using Tailwind here. But for much bigger projects, I'd rather use module-based CSS.
To ensure type security, I used TypeScript in this app. Of course, for this kind of app, it's not necessary to use TypeScript, but for self-practicing purposes, I used TypeScript here. And not to forget, I used Framer Motion to create the expand animation.
And that's pretty much it about this tiny project. This is the first of thousands of projects yet to be published 😅 Stay tuned!
- Install Git: Download from git-scm.com.
- Open VS Code.
- Clone Repository:
- Go to
View->Command Paletteor pressCtrl+Shift+P. - Type
Git: Cloneand selectGit: Clone. - Enter the repository URL (
https://github.com/Sachintha-Lakruwan/FAQ-accordion.git) and clickClone. - Choose a local directory to save the repository.
- Open the cloned repository when prompted.
- Go to
- Install Node.js: Download from nodejs.org.
- Install npm Packages:
npm install
- Start the Development Server:
npm run dev
- Open in Browser: Navigate to
http://localhost:5173/(or specified port).
