Skip to content
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

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
949 changes: 508 additions & 441 deletions src/components/bcl-accordion/__snapshots__/accordion.test.js.snap

Large diffs are not rendered by default.

131 changes: 85 additions & 46 deletions src/components/bcl-accordion/accordion.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#}
Expand All @@ -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 %}
Expand All @@ -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">
Copy link
Collaborator

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

Copy link
Collaborator Author

@tibi2303 tibi2303 Jan 29, 2025

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

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

{% 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 %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and _expand_button is empty why is this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 %}

Expand Down
7 changes: 7 additions & 0 deletions src/data/accordion/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ module.exports = {
},
],
open_item_id: 1,
expand_button: {
label: "Expand section",
},
collapse_button: {
label: "Collapse section",
variant: "secondary",
},
};
52 changes: 52 additions & 0 deletions src/themes/default/src/js/accordion-toggle/accordion-toggle.js
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;
2 changes: 2 additions & 0 deletions src/themes/default/src/js/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Dropdown from "@openeuropa/bcl-bootstrap/js/src/dropdown";
import Gallery from "@openeuropa/bcl-theme-default/src/js/gallery/gallery";
import Modal from "@openeuropa/bcl-bootstrap/js/src/modal";
import AccessibleToggle from "@openeuropa/bcl-theme-default/src/js/accessible-toggle/accessible-toggle";
import AccordionToggle from "@openeuropa/bcl-theme-default/src/js/accordion-toggle/accordion-toggle";
import Offcanvas from "@openeuropa/bcl-bootstrap/js/src/offcanvas";
import Popover from "@openeuropa/bcl-bootstrap/js/src/popover";
import ScrollSpyV2 from "@openeuropa/bcl-bootstrap/js/src/scrollspy";
Expand All @@ -30,6 +31,7 @@ export {
Gallery,
Modal,
AccessibleToggle,
AccordionToggle,
Offcanvas,
Popover,
ScrollSpyV2,
Expand Down
2 changes: 2 additions & 0 deletions src/themes/default/src/js/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Dropdown from "@openeuropa/bcl-bootstrap/js/src/dropdown";
import Gallery from "@openeuropa/bcl-theme-default/src/js/gallery/gallery";
import Modal from "@openeuropa/bcl-bootstrap/js/src/modal";
import AccessibleToggle from "@openeuropa/bcl-theme-default/src/js/accessible-toggle/accessible-toggle";
import AccordionToggle from "@openeuropa/bcl-theme-default/src/js/accordion-toggle/accordion-toggle";
import Offcanvas from "@openeuropa/bcl-bootstrap/js/src/offcanvas";
import Popover from "@openeuropa/bcl-bootstrap/js/src/popover";
import ScrollSpyV2 from "@openeuropa/bcl-bootstrap/js/src/scrollspy";
Expand All @@ -30,6 +31,7 @@ export default {
Gallery,
Modal,
AccessibleToggle,
AccordionToggle,
Offcanvas,
Popover,
ScrollSpyV2,
Expand Down
2 changes: 2 additions & 0 deletions src/themes/joinup/src/js/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Dropdown from "@openeuropa/bcl-bootstrap/js/src/dropdown";
import Gallery from "@openeuropa/bcl-theme-default/src/js/gallery/gallery";
import Modal from "@openeuropa/bcl-bootstrap/js/src/modal";
import AccessibleToggle from "@openeuropa/bcl-theme-default/src/js/accessible-toggle/accessible-toggle";
import AccordionToggle from "@openeuropa/bcl-theme-default/src/js/accordion-toggle/accordion-toggle";
import Offcanvas from "@openeuropa/bcl-bootstrap/js/src/offcanvas";
import Popover from "@openeuropa/bcl-bootstrap/js/src/popover";
import ScrollSpyV2 from "@openeuropa/bcl-bootstrap/js/src/scrollspy";
Expand All @@ -30,6 +31,7 @@ export {
Gallery,
Modal,
AccessibleToggle,
AccordionToggle,
Offcanvas,
Popover,
ScrollSpyV2,
Expand Down
2 changes: 2 additions & 0 deletions src/themes/joinup/src/js/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Dropdown from "@openeuropa/bcl-bootstrap/js/src/dropdown";
import Gallery from "@openeuropa/bcl-theme-default/src/js/gallery/gallery";
import Modal from "@openeuropa/bcl-bootstrap/js/src/modal";
import AccessibleToggle from "@openeuropa/bcl-theme-default/src/js/accessible-toggle/accessible-toggle";
import AccordionToggle from "@openeuropa/bcl-theme-default/src/js/accordion-toggle/accordion-toggle";
import Offcanvas from "@openeuropa/bcl-bootstrap/js/src/offcanvas";
import Popover from "@openeuropa/bcl-bootstrap/js/src/popover";
import ScrollSpyV2 from "@openeuropa/bcl-bootstrap/js/src/scrollspy";
Expand All @@ -30,6 +31,7 @@ export default {
Gallery,
Modal,
AccessibleToggle,
AccordionToggle,
Offcanvas,
Popover,
ScrollSpyV2,
Expand Down
2 changes: 2 additions & 0 deletions src/themes/ucpkn/src/js/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Dropdown from "@openeuropa/bcl-bootstrap/js/src/dropdown";
import Gallery from "@openeuropa/bcl-theme-default/src/js/gallery/gallery";
import Modal from "@openeuropa/bcl-bootstrap/js/src/modal";
import AccessibleToggle from "@openeuropa/bcl-theme-default/src/js/accessible-toggle/accessible-toggle";
import AccordionToggle from "@openeuropa/bcl-theme-default/src/js/accordion-toggle/accordion-toggle";
import Offcanvas from "@openeuropa/bcl-bootstrap/js/src/offcanvas";
import Popover from "@openeuropa/bcl-bootstrap/js/src/popover";
import ScrollSpyV2 from "@openeuropa/bcl-bootstrap/js/src/scrollspy";
Expand All @@ -30,6 +31,7 @@ export {
Gallery,
Modal,
AccessibleToggle,
AccordionToggle,
Offcanvas,
Popover,
ScrollSpyV2,
Expand Down
2 changes: 2 additions & 0 deletions src/themes/ucpkn/src/js/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Dropdown from "@openeuropa/bcl-bootstrap/js/src/dropdown";
import Gallery from "@openeuropa/bcl-theme-default/src/js/gallery/gallery";
import Modal from "@openeuropa/bcl-bootstrap/js/src/modal";
import AccessibleToggle from "@openeuropa/bcl-theme-default/src/js/accessible-toggle/accessible-toggle";
import AccordionToggle from "@openeuropa/bcl-theme-default/src/js/accordion-toggle/accordion-toggle";
import Offcanvas from "@openeuropa/bcl-bootstrap/js/src/offcanvas";
import Popover from "@openeuropa/bcl-bootstrap/js/src/popover";
import ScrollSpyV2 from "@openeuropa/bcl-bootstrap/js/src/scrollspy";
Expand All @@ -30,6 +31,7 @@ export default {
Gallery,
Modal,
AccessibleToggle,
AccordionToggle,
Offcanvas,
Popover,
ScrollSpyV2,
Expand Down
Loading