-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OEL-3547: Added collapse/expand buttons in accordion. #632
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
stay_open (boolean) (default: false) | ||
}, | ||
] | ||
- expand_button: (button object) (default: {}) | ||
- collapse_button: (button object) (default: {}) | ||
- open_item_id (int) (default: 0) | ||
- attributes (drupal attrs) | ||
#} | ||
|
@@ -27,6 +29,8 @@ | |
{% set _id = id|default(random(1000)) %} | ||
{% set _flush = flush ?? false %} | ||
{% set _items = items|default([]) %} | ||
{% set _expand_button = expand_button|default({}) %} | ||
{% set _collapse_button = collapse_button|default({}) %} | ||
{% set _open_item_id = open_item_id|default(0) %} | ||
{% set _classes = ['accordion'] %} | ||
{% if _flush %} | ||
|
@@ -43,57 +47,92 @@ | |
<div | ||
{{ attributes }} | ||
> | ||
{%- if _title is not empty -%} | ||
{% include '@oe-bcl/bcl-heading/heading.html.twig' with { | ||
title: _title, | ||
title_tag: _title_tag, | ||
title_link: _title_link, | ||
attributes: _title_attributes, | ||
} only %} | ||
{%- endif -%} | ||
{%- if _title is not empty -%} | ||
{% include '@oe-bcl/bcl-heading/heading.html.twig' with { | ||
title: _title, | ||
title_tag: _title_tag, | ||
title_link: _title_link, | ||
attributes: _title_attributes, | ||
} only %} | ||
{%- endif -%} | ||
|
||
{% for _item in _items %} | ||
{% set _open_item = _open_item_id == loop.index %} | ||
{% set _button_classes = ['accordion-button'] %} | ||
{% if not _open_item %} | ||
{% set _button_classes = _button_classes|merge(['collapsed']) %} | ||
{% endif %} | ||
<div class="accordion-item"> | ||
{%- set _item_title_tag = _item.title_tag|default('h2') %} | ||
<{{ _item_title_tag }} | ||
class="accordion-header" | ||
id="heading-{{ _id }}-{{ loop.index }}" | ||
{% if _items|length > 1 and (_expand_button is not empty or _collapse_button is not empty) %} | ||
{% set wrapper_attributes = create_attribute().addClass(['d-flex', 'justify-content-end', 'gap-3', 'mb-3']) %} | ||
<div | ||
{{ wrapper_attributes }} | ||
> | ||
{% set button_attributes = create_attribute() | ||
.addClass(_button_classes) | ||
.setAttribute('data-bs-toggle', 'collapse') | ||
.setAttribute('autocomplete', 'off') | ||
.setAttribute('data-bs-target', '#collapse-' ~ _id ~ '-' ~ loop.index) | ||
.setAttribute('aria-controls', 'collapse-' ~ _id ~ '-' ~ loop.index) | ||
.setAttribute('aria-expanded', open_item ? 'true' : 'false') | ||
%} | ||
{% include '@oe-bcl/bcl-button/button.html.twig' with { | ||
label: _item.title, | ||
clean_class: true, | ||
attributes: button_attributes | ||
} only %} | ||
</{{ _item_title_tag }}> | ||
<div | ||
id="collapse-{{ _id }}-{{ loop.index }}" | ||
class="accordion-collapse collapse{{ _open_item ? ' show' }}" | ||
aria-labelledby="heading-{{ _id }}-{{ loop.index }}" | ||
role="region" | ||
{% if not _item.stay_open %} | ||
data-bs-parent="#accordion-{{ _id }}" | ||
{% if _expand_button is not empty %} | ||
{% if _expand_button.attributes is empty %} | ||
{% set _expand_button = _expand_button|merge({ | ||
attributes: create_attribute() | ||
}) | ||
%} | ||
{% endif %} | ||
{% include '@oe-bcl/bcl-button/button.html.twig' with _expand_button|merge({ | ||
attributes: _expand_button.attributes | ||
.setAttribute('data-target', 'accordion-' ~ _id) | ||
.setAttribute('data-action', 'expand') | ||
}) only %} | ||
{% endif %} | ||
> | ||
<div class="accordion-body"> | ||
{%- set _content = _item.content|default('') %} | ||
{%- block content _content -%} | ||
{% if _collapse_button is not empty %} | ||
{% if _collapse_button.attributes is empty %} | ||
{% set _collapse_button = _collapse_button|merge({ | ||
attributes: create_attribute() | ||
}) | ||
%} | ||
{% endif %} | ||
{% include '@oe-bcl/bcl-button/button.html.twig' with _collapse_button|merge({ | ||
attributes: _collapse_button.attributes | ||
.setAttribute('data-target', 'accordion-' ~ _id) | ||
.setAttribute('data-action', 'collapse') | ||
}) only %} | ||
{% endif %} | ||
</div> | ||
{% endif %} | ||
<div class="accordion-items-wrapper"> | ||
{% for _item in _items %} | ||
{% set _open_item = _open_item_id == loop.index %} | ||
{% set _button_classes = ['accordion-button'] %} | ||
{% if not _open_item %} | ||
{% set _button_classes = _button_classes|merge(['collapsed']) %} | ||
{% endif %} | ||
<div class="accordion-item"> | ||
{%- set _item_title_tag = _item.title_tag|default('h2') %} | ||
<{{ _item_title_tag }} | ||
class="accordion-header" | ||
id="heading-{{ _id }}-{{ loop.index }}" | ||
> | ||
{% set button_attributes = create_attribute() | ||
.addClass(_button_classes) | ||
.setAttribute('data-bs-toggle', 'collapse') | ||
.setAttribute('autocomplete', 'off') | ||
.setAttribute('data-bs-target', '#collapse-' ~ _id ~ '-' ~ loop.index) | ||
.setAttribute('aria-controls', 'collapse-' ~ _id ~ '-' ~ loop.index) | ||
.setAttribute('aria-expanded', open_item ? 'true' : 'false') | ||
drishu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
%} | ||
{% include '@oe-bcl/bcl-button/button.html.twig' with { | ||
label: _item.title, | ||
clean_class: true, | ||
attributes: button_attributes | ||
} only %} | ||
</{{ _item_title_tag }}> | ||
<div | ||
id="collapse-{{ _id }}-{{ loop.index }}" | ||
class="accordion-collapse collapse{{ _open_item ? ' show' }}" | ||
aria-labelledby="heading-{{ _id }}-{{ loop.index }}" | ||
role="region" | ||
{% if not _item.stay_open and _expand_button is empty %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because this attribute helps the accordion to close other 'tab' when another is opened. And when expand_button is clicked, the behaviour of the accordion does not know how to open all of them, only one because of this attribute. For the collapse_button i dont care, because it only closses all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when this attribute is not present, all the tabs can be opened separately |
||
data-bs-parent="#accordion-{{ _id }}" | ||
{% endif %} | ||
> | ||
<div class="accordion-body"> | ||
{%- set _content = _item.content|default('') %} | ||
{%- block content _content -%} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
{% endfor %} | ||
</div> | ||
{% endif %} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-disable prefer-destructuring */ | ||
import Collapse from "@openeuropa/bcl-bootstrap/js/src/collapse"; | ||
import EventHandler from "@openeuropa/bcl-bootstrap/js/src/dom/event-handler"; | ||
import SelectorEngine from "@openeuropa/bcl-bootstrap/js/src/dom/selector-engine"; | ||
|
||
class AccordionToggle { | ||
static isInitialized = false; | ||
|
||
constructor(buttonElement) { | ||
this.buttonElement = buttonElement; | ||
this.targetAccordionId = buttonElement.getAttribute("data-target"); | ||
this.action = buttonElement.getAttribute("data-action"); | ||
|
||
this.accordionElement = SelectorEngine.findOne(`#${this.targetAccordionId}`); | ||
this.accordionItems = SelectorEngine.find(".accordion-collapse", this.accordionElement); | ||
|
||
this.addEventListeners(); | ||
} | ||
|
||
addEventListeners() { | ||
EventHandler.on(this.buttonElement, "click", (event) => this.handleAccordionAction(event)); | ||
} | ||
|
||
handleAccordionAction(event) { | ||
const item = event.target; | ||
const action = item.getAttribute('data-action'); | ||
const accordionItems = this.accordionItems; | ||
|
||
accordionItems.forEach((accordionItem) => { | ||
const collapseInstance = Collapse.getOrCreateInstance(accordionItem, { toggle: false }); | ||
|
||
if (action === 'expand') { | ||
collapseInstance.show(); | ||
} else if (action === 'collapse') { | ||
collapseInstance.hide(); | ||
} | ||
}); | ||
} | ||
|
||
static init() { | ||
if (AccordionToggle.isInitialized) { | ||
return; | ||
} | ||
|
||
const toggleButtons = SelectorEngine.find('[data-action][data-target]'); | ||
toggleButtons.forEach(buttonElement => new AccordionToggle(buttonElement)); | ||
|
||
AccordionToggle.isInitialized = true; | ||
} | ||
} | ||
|
||
export default AccordionToggle; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested and we can drop this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is for the default bootstrap styles
![image](https://private-user-images.githubusercontent.com/36895476/407914876-f11fabde-bd5b-46d6-8b1e-f14be1291cc3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzOTU2MzIsIm5iZiI6MTczOTM5NTMzMiwicGF0aCI6Ii8zNjg5NTQ3Ni80MDc5MTQ4NzYtZjExZmFiZGUtYmQ1Yi00NmQ2LThiMWUtZjE0YmUxMjkxY2MzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDIxMjIxMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWE4NTFkNzVkZDAwYTc4M2ViZjlkMTI2OTAwYTUzNTViM2E1MzA3ZjY3MTlmYTk1NTU4ZGNjODlmNjA3ZTE2MTcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.k_qOvgRakJ1Uguj1Beh8TWkXmjMePbVwE8tYS4u00kM)
on :first-of-type on accordion items it adds a border. When we have the buttons, it no longer adds the border styles because it is not :first-of-type, so i encaspsulate them in a div to have the styles for border