Skip to content

Commit

Permalink
feat: add accordion
Browse files Browse the repository at this point in the history
  • Loading branch information
artKozinets committed Nov 7, 2024
1 parent 5e4e984 commit d675e4c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
61 changes: 61 additions & 0 deletions blocks/accordion/accordion.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.accordion details {
border: 1px solid #dadada;
}

/* stylelint-disable-next-line no-descending-specificity */
.accordion details + details {
margin-top: 24px;
}

.accordion details p {
margin-bottom: 0.8em;
}

.accordion details summary {
position: relative;
padding: 0 16px;
padding-right: 46px;
cursor: pointer;
list-style: none;
overflow: auto;
transition: background-color 0.2s;
}

.accordion details[open] summary {
background-color: var(--light-color);
}

.accordion details summary:focus,
.accordion details summary:hover {
background-color: var(--light-color);
}

.accordion details summary::-webkit-details-marker {
display: none;
}

.accordion details summary::after {
content: '';
position: absolute;
top: 50%;
right: 18px;
transform: translateY(-50%) rotate(135deg);
width: 6px;
height: 6px;
border: 2px solid;
border-width: 2px 2px 0 0;
transition: transform 0.2s;
}

.accordion details[open] summary::after {
transform: translateY(-50%) rotate(-45deg);
}

.accordion details .accordion-item-body {
padding: 0 16px;
}

.accordion details[open] .accordion-item-body {
border-top: 1px solid #dadada;
background-color: var(--background-color);
}
23 changes: 23 additions & 0 deletions blocks/accordion/accordion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Accordion Block
* Recreate an accordion
* https://www.hlx.live/developer/block-collection/accordion
*/

export default function decorate(block) {
[...block.children].forEach((row) => {
// decorate accordion item label
const label = row.children[0];
const summary = document.createElement('summary');
summary.className = 'accordion-item-label';
summary.append(...label.childNodes);
// decorate accordion item body
const body = row.children[1];
body.className = 'accordion-item-body';
// decorate accordion item
const details = document.createElement('details');
details.className = 'accordion-item';
details.append(summary, body);
row.replaceWith(details);
});
}

0 comments on commit d675e4c

Please sign in to comment.