FAQAccordion
The FAQAccordion component displays frequently asked questions in an interactive, collapsible accordion format.
Overview
Section titled “Overview”- Type: Content Component
- Category: Interactive Content
- Complexity: Moderate
- File:
src/components/FAQAccordion.astro
Features
Section titled “Features”- Collapsible Sections - Click to expand/collapse answers
- Category Filtering - Show FAQs by category
- Configuration-Driven - Content from
faqData.ts - SEO-Friendly - Proper heading structure
- Keyboard Accessible - Full keyboard navigation support
- Smooth Animations - Transitions when expanding/collapsing
interface Props { faqs?: FAQItem[]; category?: string;}Prop Details
Section titled “Prop Details”| Prop | Type | Default | Description |
|---|---|---|---|
faqs | FAQItem[] | All FAQs from config | Array of FAQ items to display |
category | string | undefined | Filter FAQs by category |
FAQ Item Interface
Section titled “FAQ Item Interface”interface FAQItem { question: string; answer: string; category?: string;}---import FAQAccordion from '@/components/FAQAccordion.astro';---
<FAQAccordion />---import FAQAccordion from '@/components/FAQAccordion.astro';---
<!-- Show only membership FAQs --><FAQAccordion category="membership" />---import FAQAccordion from '@/components/FAQAccordion.astro';
const customFaqs = [ { question: 'How do I become a member?', answer: 'Visit our membership page and fill out the application form.', category: 'membership', }, { question: 'What are the benefits?', answer: 'Members receive access to networking events, professional development, and more.', category: 'membership', },];---
<FAQAccordion faqs={customFaqs} />FAQ Configuration File
Section titled “FAQ Configuration File”FAQs are defined in src/data/faqData.ts:
export interface FAQItem { question: string; answer: string; category?: string;}
export const faqData: FAQItem[] = [ { question: 'What is FiNAN?', answer: 'FiNAN (Filipino Nurses Association in the Nordic Region) is a non-profit organization connecting Filipino nurses across the Nordic countries.', category: 'general', }, { question: 'How can I join FiNAN?', answer: 'Visit our membership page and complete the online application form. Membership is open to all Filipino nurses working in the Nordic region.', category: 'membership', }, // ... more FAQs];Adding FAQs
Section titled “Adding FAQs”-
Open the FAQ data file
Navigate to:
src/data/faqData.ts -
Add a new FAQ object
{question: 'Your question here?',answer: 'Your detailed answer here.',category: 'general', // or 'membership', 'events', etc.}, -
Choose a category (optional but recommended)
Common categories:
general- About FiNANmembership- Joining and member benefitsevents- Event-related questionscontact- How to reach us
-
Build and test
Terminal window pnpm build -
Verify on FAQ page
Check that your FAQ appears at
/faq
Interactive Behavior
Section titled “Interactive Behavior”Client-Side Script
Section titled “Client-Side Script”The accordion uses JavaScript for interactivity:
<script is:inline src="/js/faq-accordion.js" defer></script>Features:
- Click to toggle - Open/close individual items
- Auto-close others - Optional: close other items when opening one
- Smooth transitions - CSS transitions for expand/collapse
- ARIA attributes - Proper
aria-expandedstates
Styling States
Section titled “Styling States”/* Closed state */.faq-item { }
/* Open state */.faq-item.active { /* Answer visible */}
/* Icon rotation */.faq-icon { transition: transform 0.3s;}.faq-item.active .faq-icon { transform: rotate(180deg);}Styling
Section titled “Styling”Layout
Section titled “Layout”- Container: Max-width with padding
- Items: Stacked vertically with borders
- Spacing: Consistent gaps between items
Typography
Section titled “Typography”- Question: Bold, larger text (lg or xl)
- Answer: Regular weight, comfortable line height
- Color: Dark text on white background
Interactive Elements
Section titled “Interactive Elements”- Hover State: Subtle background color change
- Focus State: Visible outline for keyboard users
- Icon: Chevron or plus/minus that rotates when open
Categories
Section titled “Categories”Organize FAQs by category for better user experience:
Common Categories
Section titled “Common Categories”- General - About the organization
- Membership - How to join, benefits, fees
- Events - Event information, registration
- Contact - How to reach representatives
- Resources - Guides, documents, links
Using Categories
Section titled “Using Categories”Display category-specific FAQs on different pages:
<!-- On membership page --><FAQAccordion category="membership" />
<!-- On events page --><FAQAccordion category="events" />
<!-- On general FAQ page (all categories) --><FAQAccordion />Accessibility
Section titled “Accessibility”- Keyboard Navigation: Tab through questions, Enter/Space to toggle
- ARIA Labels:
aria-expanded="true|false"on buttonsaria-controlslinking button to content
- Semantic HTML:
<h3>or<h4>for questions<button>for interactive triggers
- Focus Management: Visible focus indicators
SEO Benefits
Section titled “SEO Benefits”Structured Data
Section titled “Structured Data”Combine with the StructuredData component for rich snippets:
---import FAQAccordion from '@/components/FAQAccordion.astro';import StructuredData from '@/components/StructuredData.astro';import { faqData } from '@/data/faqData';---
<!-- FAQ Page with structured data --><StructuredData type="FAQPage" data={faqData} /><FAQAccordion />This creates FAQ rich snippets in Google search results.
Content Benefits
Section titled “Content Benefits”- Answers user questions directly
- Reduces support inquiries
- Improves search rankings for question keywords
- Increases time on page
Common Modifications
Section titled “Common Modifications”Changing Default Behavior
Section titled “Changing Default Behavior”Keep all items closed by default:
// In faq-accordion.jsconst defaultOpen = false; // Change from trueAllow multiple items open:
// In faq-accordion.jsconst allowMultiple = true; // Change from falseCustomizing Appearance
Section titled “Customizing Appearance”Change icon:
<!-- Replace chevron with plus/minus --><svg class="faq-icon"> <path d="M12 5v14M5 12h14" /> <!-- Plus icon --></svg>Add answer formatting:
<div class="faq-answer" set:html={faq.answer} />This allows HTML in answers (use carefully).
Best Practices
Section titled “Best Practices”✅ Do:
- Keep questions concise (one sentence)
- Provide complete, helpful answers (2-4 sentences)
- Use categories to organize related questions
- Update FAQs based on common user questions
- Include links in answers when relevant
❌ Don’t:
- Create overly long answers (consider a dedicated page instead)
- Use technical jargon without explanation
- Duplicate information across multiple FAQs
- Leave outdated information
Common Issues
Section titled “Common Issues”FAQ Not Showing
Section titled “FAQ Not Showing”Cause: Typo in category name
Solution: Verify category matches exactly:
<FAQAccordion category="membership" /> // Must match data exactlyAccordion Not Working
Section titled “Accordion Not Working”Cause: JavaScript file not loading
Solution: Check that /js/faq-accordion.js exists and loads properly
Build Errors
Section titled “Build Errors”Cause: Invalid JSON in faqData.ts
Solution: Ensure proper TypeScript syntax with commas and quotes
Performance
Section titled “Performance”- Lazy Script Loading: Uses
deferattribute - No Heavy Dependencies: Pure JavaScript
- CSS Animations: Hardware-accelerated transforms
- Static Content: FAQ content is static, loads fast
Dependencies
Section titled “Dependencies”- faqData.ts - FAQ content configuration
- public/js/faq-accordion.js - Interactive functionality
Used On
Section titled “Used On”- FAQ Page (
/faq) - Primary usage - Membership Page - Membership-specific FAQs
- Event Pages - Event-specific questions
Related Components
Section titled “Related Components”- StructuredData - SEO markup for FAQs
Related Documentation
Section titled “Related Documentation”- FAQ Management Guide Essential for editors
- Data Configuration - Config file patterns
Source Code
Section titled “Source Code”View the complete implementation:
https://github.com/poncardasm/finan-website/blob/main/src/components/FAQAccordion.astrohttps://github.com/poncardasm/finan-website/blob/main/src/data/faqData.tshttps://github.com/poncardasm/finan-website/blob/main/public/js/faq-accordion.js