Skip to content

Instantly share code, notes, and snippets.

@nektro
Created October 11, 2017 00:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nektro/b9f499afba0bb1e2b1ad1b4fad98abe0 to your computer and use it in GitHub Desktop.
Save nektro/b9f499afba0bb1e2b1ad1b4fad98abe0 to your computer and use it in GitHub Desktop.
Bootstrap 4 Accordion Custom Element w/ Corgi Example
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script defer src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script defer src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<template id="template-bs4-accordion-item">
<div class="card">
<div class="card-header" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"><span></span></a>
</h5>
</div>
<div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<span></span>
</div>
</div>
</div>
</template>
<script>
(function() {
const doc = document.currentScript.ownerDocument;
customElements.define('bs4-accordion', class BootstrapFourAccordion extends HTMLElement {
constructor(){
super();
const items = Array.from(this.children).map(x => x);
this.innerHTML = `<div id="accordion" role="tablist></div>`;
const templ = doc.getElementById('template-bs4-accordion-item');
items.forEach((v,i) => {
const sp = (templ.content.querySelectorAll('span'));
sp[0].innerHTML = v.children[0].innerHTML;
sp[0].parentElement.setAttribute('aria-controls', `collapse${i}`);
sp[0].parentElement.setAttribute('href', `#collapse${i}`);
sp[0].parentElement.parentElement.parentElement.setAttribute('id', `heading${i}`);
sp[1].innerHTML = v.children[1].innerHTML;
sp[1].parentElement.parentElement.setAttribute('id', `collapse${i}`);
sp[1].parentElement.parentElement.setAttribute('aria-labelledby', `heading${i}`);
this.appendChild(templ.content.cloneNode(true));
});
}
});
})();
</script>
(
bs4-accordion(
div(
h5("Collapsible Group Item #1")
div(
p("Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.")
)
)
div(
h5("Collapsible Group Item #2")
div(
p("Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.")
)
)
div(
h5("Collapsible Group Item #3")
div(
p("Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.")
)
)
)
)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Bootstrap 4 Corgi Example</title>
<link rel="import" href="bs4-accordion.html">
<script defer src="https://rawgit.com/Nektro/WebUtils/7576aed/src/Router.js"></script>
<script defer src="https://unpkg.com/mantle.js/mantle.js"></script>
<script defer src="https://unpkg.com/corgi-lang/corgi.js"></script>
<script defer>
(function() {
window.addEventListener('load', function() {
fetch('index.corgi')
.then(x => x.text())
.then(x => document.body.innerHTML += corgi.compile(x));
});
})();
</script>
</head>
<body>
<!-- -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment