|
1 | 1 | import clsx from 'clsx'; |
2 | 2 | import { GessoComponent } from 'gesso'; |
3 | | -import { useEffect, useId, useRef } from 'react'; |
| 3 | +import { useEffect, useId, useRef, useState } from 'react'; |
4 | 4 | import styles from './accordion.module.css'; |
5 | 5 | import stylesAccordionItem from './accordion-item.module.css'; |
6 | 6 | import getCssVar from '../../06-utility/getCssVar'; |
7 | | -import { slideCollapse, slideExpand } from '../../06-utility/slide'; |
8 | 7 | import { KEYCODE } from '../../00-config/constants'; |
9 | 8 | import AccordionItem, { AccordionItemProps } from './AccordionItem'; |
10 | 9 |
|
11 | 10 | interface AccordionProps extends GessoComponent { |
12 | | - accordionItems?: AccordionItemProps[]; |
| 11 | + accordionItems: AccordionItemProps[]; |
| 12 | + accordionSpeed?: string; |
13 | 13 | allowMultiple?: boolean; |
14 | 14 | allowToggle?: boolean; |
15 | 15 | } |
16 | 16 |
|
17 | 17 | function Accordion({ |
18 | 18 | accordionItems, |
| 19 | + accordionSpeed = getCssVar('duration-standard'), |
19 | 20 | allowMultiple, |
20 | 21 | allowToggle, |
21 | 22 | modifierClasses, |
22 | 23 | }: AccordionProps): JSX.Element { |
23 | 24 | const accordionId = useId(); |
24 | 25 | const accordionRef = useRef(null); |
| 26 | + const [accordionItemsStatus, setAccordionItemsStatus] = useState( |
| 27 | + accordionItems.map((item, index) => ({ |
| 28 | + ...item, |
| 29 | + id: `${accordionId}-${index}`, |
| 30 | + })), |
| 31 | + ); |
25 | 32 |
|
26 | | - useEffect(() => { |
27 | | - const ACCORDION_TOGGLE_CLASS = stylesAccordionItem.toggle; |
28 | | - const ACCORDION_SPEED = getCssVar('duration-standard'); |
29 | | - |
30 | | - const accordion = document.getElementById(accordionId); |
31 | | - const multipleAllowed = allowMultiple; |
| 33 | + const openAccordionItem = (items: AccordionItemProps[], index: number) => { |
| 34 | + return items.with(index, { |
| 35 | + ...items[index], |
| 36 | + isOpen: true, |
| 37 | + }); |
| 38 | + }; |
| 39 | + |
| 40 | + const closeAccordionItem = (items: AccordionItemProps[], index: number) => { |
| 41 | + return items.with(index, { |
| 42 | + ...items[index], |
| 43 | + isOpen: false, |
| 44 | + }); |
| 45 | + }; |
| 46 | + |
| 47 | + const handleClick = (id: string, isOpen = false) => { |
32 | 48 | const toggleAllowed = allowMultiple ? true : allowToggle; |
| 49 | + const active = accordionItemsStatus.findIndex(item => item.isOpen); |
| 50 | + const itemIndex = accordionItemsStatus.findIndex(item => item.id === id); |
| 51 | + let itemStatusUpdated = [...accordionItemsStatus]; |
33 | 52 |
|
34 | | - const openAccordion = (button: Element | HTMLElement | null) => { |
35 | | - if (button && button.getAttribute('aria-expanded') === 'false') { |
36 | | - button.setAttribute('aria-expanded', 'true'); |
37 | | - const accordionSectionId = button.getAttribute( |
38 | | - 'aria-controls', |
39 | | - ) as string; |
40 | | - const accordionSection = document.getElementById(accordionSectionId); |
| 53 | + // Without allowMultiple, close the open accordion |
| 54 | + if (!allowMultiple && active !== -1 && active !== itemIndex) { |
| 55 | + itemStatusUpdated = closeAccordionItem(itemStatusUpdated, active); |
| 56 | + } |
41 | 57 |
|
42 | | - if (accordionSection) { |
43 | | - accordionSection.setAttribute('aria-expanded', 'true'); |
44 | | - slideExpand(accordionSection, ACCORDION_SPEED); |
45 | | - } |
46 | | - } |
47 | | - }; |
| 58 | + if (!isOpen) { |
| 59 | + itemStatusUpdated = openAccordionItem(itemStatusUpdated, itemIndex); |
| 60 | + } else if (toggleAllowed && isOpen) { |
| 61 | + itemStatusUpdated = closeAccordionItem(itemStatusUpdated, itemIndex); |
| 62 | + } |
48 | 63 |
|
49 | | - const closeAccordion = (button: Element | HTMLElement | null) => { |
50 | | - if (button && button.getAttribute('aria-expanded') === 'true') { |
51 | | - button.setAttribute('aria-expanded', 'false'); |
52 | | - const accordionSectionId = button.getAttribute( |
53 | | - 'aria-controls', |
54 | | - ) as string; |
55 | | - const accordionSection = document.getElementById(accordionSectionId); |
| 64 | + return setAccordionItemsStatus(itemStatusUpdated); |
| 65 | + }; |
56 | 66 |
|
57 | | - if (accordionSection) { |
58 | | - accordionSection.setAttribute('aria-expanded', 'false'); |
59 | | - slideCollapse(accordionSection, ACCORDION_SPEED); |
60 | | - } |
61 | | - } |
62 | | - }; |
| 67 | + const handleKeydown = (event: KeyboardEvent) => { |
| 68 | + const currentTarget = event.target as HTMLElement; |
| 69 | + const accordion = accordionRef.current as HTMLElement | null; |
| 70 | + const ACCORDION_TOGGLE_CLASS = stylesAccordionItem.toggle; |
63 | 71 |
|
64 | | - const handleClick = (event: MouseEvent) => { |
65 | | - const currentTarget = event.target as HTMLElement; |
| 72 | + // Create the array of toggle elements for the accordion group |
| 73 | + const triggers = Array.prototype.slice.call( |
| 74 | + accordion ? accordion.querySelectorAll(`.${ACCORDION_TOGGLE_CLASS}`) : [], |
| 75 | + ); |
66 | 76 |
|
67 | | - // Set target differently depending on click vs. keydown |
68 | | - // because the <span> inside <button> screws things up |
| 77 | + // Is this coming from an accordion header? |
| 78 | + if (currentTarget.tagName === 'BUTTON') { |
| 79 | + // Up/ Down arrow and Control + Page Up/ Page Down keyboard operations |
69 | 80 | if ( |
70 | | - currentTarget && |
71 | | - (currentTarget.tagName === 'BUTTON' || |
72 | | - (currentTarget.parentElement && |
73 | | - currentTarget.parentElement.tagName === 'BUTTON')) |
| 81 | + event.code === KEYCODE.UP || |
| 82 | + event.code === KEYCODE.DOWN || |
| 83 | + event.code === KEYCODE.PAGEDOWN || |
| 84 | + event.code === KEYCODE.UP |
74 | 85 | ) { |
75 | | - let target; |
76 | | - // Set target based on click or keydown |
77 | | - if (currentTarget.tagName === 'BUTTON') { |
78 | | - target = currentTarget; |
| 86 | + const index = triggers.indexOf(currentTarget); |
| 87 | + let direction; |
| 88 | + if (event.code === KEYCODE.DOWN || event.code === KEYCODE.PAGEDOWN) { |
| 89 | + direction = 1; |
79 | 90 | } else { |
80 | | - target = currentTarget.parentElement; |
81 | | - } |
82 | | - // Check if the current toggle is expanded. |
83 | | - const isExpanded = target |
84 | | - ? target.getAttribute('aria-expanded') === 'true' |
85 | | - : false; |
86 | | - const active = accordion |
87 | | - ? accordion.querySelector('[aria-expanded="true"]') |
88 | | - : null; |
89 | | - |
90 | | - // without allowMultiple, close the open accordion |
91 | | - if (!multipleAllowed && active && active !== target) { |
92 | | - closeAccordion(active); |
| 91 | + direction = -1; |
93 | 92 | } |
94 | | - |
95 | | - if (!isExpanded) { |
96 | | - openAccordion(target); |
97 | | - } else if (toggleAllowed && isExpanded) { |
98 | | - closeAccordion(target); |
99 | | - } |
100 | | - |
| 93 | + const triggerLength = triggers.length; |
| 94 | + const newIndex = (index + triggerLength + direction) % triggerLength; |
| 95 | + triggers[newIndex].focus(); |
101 | 96 | event.preventDefault(); |
102 | | - } |
103 | | - }; |
104 | | - |
105 | | - const handleKeydown = (event: KeyboardEvent) => { |
106 | | - const currentTarget = event.target as HTMLElement; |
107 | | - |
108 | | - // Create the array of toggle elements for the accordion group |
109 | | - const triggers = Array.prototype.slice.call( |
110 | | - accordion |
111 | | - ? accordion.querySelectorAll(`.${ACCORDION_TOGGLE_CLASS}`) |
112 | | - : [], |
113 | | - ); |
114 | | - |
115 | | - // Is this coming from an accordion header? |
116 | | - if (currentTarget.tagName === 'BUTTON') { |
117 | | - // Up/ Down arrow and Control + Page Up/ Page Down keyboard operations |
118 | | - if ( |
119 | | - event.code === KEYCODE.UP || |
120 | | - event.code === KEYCODE.DOWN || |
121 | | - event.code === KEYCODE.PAGEDOWN || |
122 | | - event.code === KEYCODE.UP |
123 | | - ) { |
124 | | - const index = triggers.indexOf(currentTarget); |
125 | | - let direction; |
126 | | - if (event.code === KEYCODE.DOWN || event.code === KEYCODE.PAGEDOWN) { |
127 | | - direction = 1; |
128 | | - } else { |
129 | | - direction = -1; |
130 | | - } |
131 | | - const triggerLength = triggers.length; |
132 | | - const newIndex = (index + triggerLength + direction) % triggerLength; |
133 | | - triggers[newIndex].focus(); |
134 | | - event.preventDefault(); |
135 | | - } else if (event.code === KEYCODE.HOME || event.code === KEYCODE.END) { |
136 | | - switch (event.code) { |
137 | | - // Go to first accordion |
138 | | - case KEYCODE.HOME: |
139 | | - triggers[0].focus(); |
140 | | - break; |
141 | | - // Go to last accordion |
142 | | - case KEYCODE.END: |
143 | | - triggers[triggers.length - 1].focus(); |
144 | | - break; |
145 | | - default: |
146 | | - triggers[0].focus(); |
147 | | - break; |
148 | | - } |
149 | | - event.preventDefault(); |
| 97 | + } else if (event.code === KEYCODE.HOME || event.code === KEYCODE.END) { |
| 98 | + switch (event.code) { |
| 99 | + // Go to first accordion |
| 100 | + case KEYCODE.HOME: |
| 101 | + triggers[0].focus(); |
| 102 | + break; |
| 103 | + // Go to last accordion |
| 104 | + case KEYCODE.END: |
| 105 | + triggers[triggers.length - 1].focus(); |
| 106 | + break; |
| 107 | + default: |
| 108 | + triggers[0].focus(); |
| 109 | + break; |
150 | 110 | } |
| 111 | + event.preventDefault(); |
151 | 112 | } |
152 | | - }; |
| 113 | + } |
| 114 | + }; |
153 | 115 |
|
154 | | - // Initiate accordions on page load |
| 116 | + useEffect(() => { |
| 117 | + const accordion = accordionRef.current as HTMLElement | null; |
155 | 118 | if (accordion) { |
156 | | - accordion.addEventListener('click', handleClick); |
157 | 119 | accordion.addEventListener('keydown', handleKeydown); |
158 | | - |
159 | | - const accordionItems = accordion.querySelectorAll( |
160 | | - `.${stylesAccordionItem.accordionItem}`, |
161 | | - ); |
162 | | - accordionItems.forEach(item => { |
163 | | - const toggle = item.querySelector(`.${ACCORDION_TOGGLE_CLASS}`); |
164 | | - console.log(toggle); |
165 | | - // Close all accordion items that are not 'default-open' |
166 | | - if ( |
167 | | - !item.hasAttribute('data-accordion-open') || |
168 | | - item.getAttribute('data-accordion-open') === 'false' |
169 | | - ) { |
170 | | - closeAccordion(toggle); |
171 | | - } |
172 | | - // Update toggle tabindex |
173 | | - if (toggle) { |
174 | | - toggle.removeAttribute('tabindex'); |
175 | | - } |
176 | | - // Add attribute 'processed' |
177 | | - item.setAttribute('data-accordion-processed', ''); |
178 | | - }); |
179 | 120 | } |
180 | | - }, [accordionId, allowMultiple, allowToggle]); |
| 121 | + }); |
181 | 122 |
|
182 | 123 | return ( |
183 | 124 | <> |
184 | | - {accordionItems && ( |
185 | | - <div |
186 | | - ref={accordionRef} |
187 | | - className={clsx(styles.accordion, modifierClasses)} |
188 | | - id={accordionId} |
189 | | - > |
190 | | - <div className={styles.content}> |
191 | | - {accordionItems.map((item, index) => { |
192 | | - return <AccordionItem key={index} {...item} />; |
193 | | - })} |
194 | | - </div> |
| 125 | + <div |
| 126 | + ref={accordionRef} |
| 127 | + className={clsx(styles.accordion, modifierClasses)} |
| 128 | + id={accordionId} |
| 129 | + > |
| 130 | + <div className={styles.content}> |
| 131 | + {accordionItemsStatus.map(item => { |
| 132 | + return ( |
| 133 | + <AccordionItem |
| 134 | + key={item.id} |
| 135 | + {...item} |
| 136 | + accordionSpeed={accordionSpeed} |
| 137 | + handleClick={() => handleClick(item.id, item.isOpen)} |
| 138 | + /> |
| 139 | + ); |
| 140 | + })} |
195 | 141 | </div> |
196 | | - )} |
| 142 | + </div> |
197 | 143 | </> |
198 | 144 | ); |
199 | 145 | } |
|
0 commit comments