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

329 multicolour accordion #344

Merged
merged 15 commits into from
Aug 10, 2023
72 changes: 72 additions & 0 deletions blocks/multicolor-accordion/multicolor-accordion.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
.accordion-item {
padding: 0 16px;
margin-bottom: 20px;
}

.accordion-trigger {
display: flex;
align-items: center;
cursor: pointer;
line-height: 40px;
font-size: 34px;
height: 80px;
color: var(--c-white);
font-weight: 700;
}

.multicolor-accordion>div:nth-child(odd) {
background: var(--c-dark-plum);
}

.multicolor-accordion>div:nth-child(even) {
background: var(--c-dark-blue);
}

.accordion-trigger .details-button {
background: var(--c-light-teal);
color: var(--c-dark-plum);
border-radius: 16px;
font-weight: 600;
text-transform: uppercase;
padding: 8px 0;
cursor: pointer;
width: 9%;
font-size: 15px;
border: 1px solid var(--c-light-teal);
transition: 0.5s;
mihai-buricea-net marked this conversation as resolved.
Show resolved Hide resolved
}

.accordion-trigger .details-button:hover {
border: 1px solid var(--c-light-teal);
mihai-buricea-net marked this conversation as resolved.
Show resolved Hide resolved
background: none;
color: var(--c-light-teal);
}

.accordion-trigger .close-button {
border: 1px solid var(--c-light-teal);
background: none;
color: var(--c-light-teal);
border-radius: 16px;
font-weight: 600;
text-transform: uppercase;
padding: 8px 0;
cursor: pointer;
width: 9%;
font-size: 15px;
}

.accordion-trigger > p {
display: inline-flex;
width: calc(100% - 40px);
}

.accordion-content {
transition: max-height .3333s;
max-height: 0;
overflow: hidden;
color: var(--c-white);
}

[aria-expanded] .accordion-content {
max-height: 1000px;
}
53 changes: 53 additions & 0 deletions blocks/multicolor-accordion/multicolor-accordion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export default async function decorate(block) {
const accordionItems = block.querySelectorAll(':scope > div > div');
accordionItems.forEach((accordionItem) => {
const nodes = accordionItem.children;

const titleText = nodes[0];
sonaldhekale marked this conversation as resolved.
Show resolved Hide resolved
const rest = Array.prototype.slice.call(nodes, 1);
sonaldhekale marked this conversation as resolved.
Show resolved Hide resolved

const titleDiv = document.createElement('div');
const detailsBtn = document.createElement('button');
detailsBtn.innerText = 'Details';
sonaldhekale marked this conversation as resolved.
Show resolved Hide resolved
detailsBtn.className = 'details-button';
titleDiv.append(titleText, detailsBtn);

titleDiv.classList.add('accordion-trigger');

const content = document.createElement('div');
content.classList.add('accordion-content');
rest.forEach((elem) => {
content.appendChild(elem);
});

const newItem = document.createElement('div');
newItem.appendChild(titleDiv);
newItem.appendChild(content);

newItem.classList.add('accordion-item');

accordionItem.replaceWith(newItem);
});

const triggers = block.querySelectorAll('.accordion-trigger');
sonaldhekale marked this conversation as resolved.
Show resolved Hide resolved
triggers.forEach((trigger) => {
trigger.addEventListener('click', () => {
const openAttribute = 'aria-expanded';
sonaldhekale marked this conversation as resolved.
Show resolved Hide resolved
const wasOpen = trigger.parentElement.hasAttribute(openAttribute);

triggers.forEach((_trigger) => {
const getDetailsBtn = _trigger.children[1];
sonaldhekale marked this conversation as resolved.
Show resolved Hide resolved
getDetailsBtn.className = 'details-button';
getDetailsBtn.innerText = 'Details';
_trigger.parentElement.removeAttribute('aria-expanded');
});

if (!wasOpen) {
const closeBtn = trigger.children[1];
closeBtn.className = 'close-button';
closeBtn.innerText = 'close';
trigger.parentElement.setAttribute('aria-expanded', '');
}
});
});
}