@@ -5,30 +5,17 @@ const { title, description, id, last } = Astro.props;
55<!--
66Child component: Alpine.js logic
77
8- 1. Each child observes the `activeId` state from the `faqs` store by using the reactive `visible` property.
9- 2. When clicking on a child component, the global `activeId` state in the store is updated:
10- - If accordion is open (in child: `visible` === `true`), it sets `activeId` to `null` and closes.
11- - If accordion is closed (in child: `visible` === `false`), it replaces the `activeId` value with its own `id` value and opens.
12- 3. When `activeId` changes, the child's classes related to its visibility are automatically updated.
8+ 1. Each accordion receives a unique `id` that identifies it.
9+ 2. When clicking on an accordion:
10+ 2.1 The `toggle(id)` method defined in the parent component is called.
11+ 2.2 The global state `activeId` is updated with the new ID.
12+ 3. Each accordion checks if its ID matches `activeId` using the `isVisible` method:
13+ - If it matches, the accordion expands (`isVisible` is `true`).
14+ - If it doesn't match, it remains collapsed (`isVisible` is `false`).
1315
1416This way, each child component can respond reactively to the `activeId` global state.
1517-->
16- <div
17- x-data =`{
18- get visible() {
19- return Alpine.store && Alpine.store('faqs')?.isVisible(${id });
20- },
21-
22- toggle(id){
23- if ($store.faqs.activeId === id) {
24- $store.faqs.setActiveId(null);
25- } else {
26- $store.faqs.setActiveId(id);
27- };
28-
29- }
30- }`
31-
18+ <div
3219 class:list ={ [
3320 " bg-white py-6 accordion-transition overflow-hidden" ,
3421 {" border-b-0" : last === id },
@@ -38,15 +25,15 @@ This way, each child component can respond reactively to the `activeId` global s
3825 <div
3926 @click =`toggle(${id })`
4027 class =" flex flex-row items-center text-lg font-semibold cursor-pointer"
41- :class =" visible && 'text-blue-500'"
28+ :class =`isVisible(${ id }) && 'text-blue-500'`
4229 >
4330 <div class =" w-[95%] px-4" >
4431 { title }
4532 </div >
4633 <div
4734 x-transition
4835 class =" w-fit"
49- :class =" visible ? 'transform rotate-180 accordion-transition' : 'transform rotate-0 accordion-transition'"
36+ :class =`isVisible(${ id }) ? 'transform rotate-180 accordion-transition' : 'transform rotate-0 accordion-transition'`
5037 >
5138 <Fragment set:html ={ downIcon } />
5239 </div >
@@ -55,7 +42,7 @@ This way, each child component can respond reactively to the `activeId` global s
5542 <div
5643 x-transition
5744 class =" accordion-transition overflow-hidden"
58- :class =" visible ? 'max-h-72 opacity-100' : 'max-h-0 opacity-0'"
45+ :class =`isVisible(${ id }) ? 'max-h-72 opacity-100' : 'max-h-0 opacity-0'`
5946 >
6047 <p class =" w-[90%] text-gray-600 text-base mt-2 px-4 overflow-hidden" >
6148 { description }
0 commit comments