deprecated
2 weeks ago
block.json
1 day ago
edit.js
2 months ago
icon.svg
2 months ago
index.js
2 months ago
index.php
2 months ago
save.js
2 months ago
view.js
1 day ago
view.js
66 lines
| 1 | const vkFaq2Container = document.getElementsByClassName('vk_faq-accordion'); |
| 2 | |
| 3 | const FAQ2ToggleLoop = (i) => { |
| 4 | const titleElement = vkFaq2Container[i].querySelector('.vk_faq_title'); |
| 5 | const contentElement = vkFaq2Container[i].querySelector('.vk_faq_content'); |
| 6 | |
| 7 | // Assign unique ID to content element for aria-controls / コンテンツ要素にユニークIDを付与(aria-controls のため) |
| 8 | const contentId = `vk-faq2-content-${i}`; |
| 9 | contentElement.setAttribute('id', contentId); |
| 10 | |
| 11 | titleElement.setAttribute('tabindex', '0'); |
| 12 | titleElement.setAttribute('role', 'button'); |
| 13 | titleElement.setAttribute('aria-controls', contentId); |
| 14 | // デフォルト値として false を設定し、open クラスがあれば true に上書きする |
| 15 | titleElement.setAttribute('aria-expanded', 'false'); |
| 16 | |
| 17 | if (vkFaq2Container[i].classList.contains('vk_faq-accordion-open')) { |
| 18 | contentElement.classList.add('vk_faq_content-accordion-open'); |
| 19 | titleElement.setAttribute('aria-expanded', 'true'); |
| 20 | } |
| 21 | |
| 22 | if (vkFaq2Container[i].classList.contains('vk_faq-accordion-close')) { |
| 23 | contentElement.classList.add('vk_faq_content-accordion-close'); |
| 24 | titleElement.setAttribute('aria-expanded', 'false'); |
| 25 | } |
| 26 | |
| 27 | const handleToggle = () => { |
| 28 | if (vkFaq2Container[i].classList.contains('vk_faq-accordion-open')) { |
| 29 | vkFaq2Container[i].classList.remove('vk_faq-accordion-open'); |
| 30 | vkFaq2Container[i].classList.add('vk_faq-accordion-close'); |
| 31 | |
| 32 | contentElement.classList.remove('vk_faq_content-accordion-open'); |
| 33 | contentElement.classList.add('vk_faq_content-accordion-close'); |
| 34 | titleElement.setAttribute('aria-expanded', 'false'); |
| 35 | } else if ( |
| 36 | vkFaq2Container[i].classList.contains('vk_faq-accordion-close') |
| 37 | ) { |
| 38 | vkFaq2Container[i].classList.remove('vk_faq-accordion-close'); |
| 39 | vkFaq2Container[i].classList.add('vk_faq-accordion-open'); |
| 40 | |
| 41 | contentElement.classList.remove('vk_faq_content-accordion-close'); |
| 42 | contentElement.classList.add('vk_faq_content-accordion-open'); |
| 43 | titleElement.setAttribute('aria-expanded', 'true'); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | titleElement.addEventListener('click', handleToggle, false); |
| 48 | |
| 49 | // Add keyboard support (Enter / Space) for toggle / Enter / Space キーで開閉できるようにキーボードイベントを追加 |
| 50 | titleElement.addEventListener( |
| 51 | 'keydown', |
| 52 | (e) => { |
| 53 | if (e.key === 'Enter' || e.key === ' ') { |
| 54 | // Prevent default Space key scroll behavior / Space キーはページスクロールを防ぐ |
| 55 | e.preventDefault(); |
| 56 | handleToggle(); |
| 57 | } |
| 58 | }, |
| 59 | false |
| 60 | ); |
| 61 | }; |
| 62 | |
| 63 | for (let i = 0; i < vkFaq2Container.length; i++) { |
| 64 | FAQ2ToggleLoop(i); |
| 65 | } |
| 66 |