JSFiddle - React, Tailwind, and code Playground
by AnthonyPaulO
HTML
<html>
<head>
</head>
<body>
<div id="app">
<div id="header">
<img id="logo" src="https://i0.wp.com/news.northeastern.edu/wp-content/uploads/2022/05/Quantum_1400.jpg?w=1400&ssl=1" height="50px" />
<div id="headerBody">HEADER BODY</div>
</div>
<div id="grid">
<div id="gridHeader">
<span class="toggler active" data-toggle="grid"><span class="entypo-layout"></span></span>
<span class="toggler" data-toggle="list"><span class="entypo-list"></span></span>
</div>
<div id="gridBody">
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
<div>ITEM</div>
</div>
</div>
</div>
</body>
</html>
SCSS
@import url(https://weloveiconfonts.com/api/?family=entypo);
/* entypo */
[class*="entypo-"]:before {
font-family: 'entypo', sans-serif;
}
$primary-text-color: #000;
$secondary-text-color: #A1A1A4;
.toggler {
color: $secondary-text-color;
font-size: 1.25em;
margin-left: 8px;
text-align: center;
cursor: pointer;
&.active {
color: $primary-text-color;
}
}
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
background: orange;
overflow-y: hidden;
}
#app {
height: 100%;
width: 100%;
background-color: cyan;
}
#header {
position: sticky;
top: 0;
display: grid;
grid-template-columns: auto auto;
background-color: red;
}
#logo {
grid-column: 1;
}
#headerBody {
grid-column: 2;
}
#grid {
background-color: blue;
display: grid;
grid-template-rows: 25px auto;
height: 100%;
}
#gridHeader {
background-color: green;
position: sticky;
top: 50px;
text-align: right;
padding: 0 4px;
}
#gridBody {
background-color: lime;
overflow-y: scroll;
}
JavaScript
const categoryTree = [
{
title: "Programming",
children: [
{
title: "Programming (Beginner)",
children: [
{ title: "The Rules of Programming Languages" },
{ title: "Working with Data " },
{ title: "Managing Program Flow" },
{ title: "Making Things Modular" },
{ title: "Programming in the Real World" },
],
}
],
},
{
title: "Electronics",
children: [
{
title: "Arduino (Beginner)",
children: [
{ title: "Programming Arduino" },
{ title: "Arduino Hardware" },
{ title: "Working with Sensors and Shields" },
],
}
],
},
{
title: "Body Transformation",
children: [
{
title: "Weight Training (Beginner)",
children: [
{ title: "Day 1" },
{ title: "Day 2" },
{ title: "Day 3" },
],
}
],
},
];
const data = categoryTree.flatMap((cat, catIndex) => cat.children.flatMap((subCat, subCatIndex) => subCat.children.map((leaf, leafIndex) => ({ cat: cat.title, subCat: subCat.title, title: leaf.title, length: catIndex * 100 + subCatIndex * 10 + leafIndex }))));
document.querySelectorAll(`[data-toggle]`)
.forEach(toggle => toggle.addEventListener('click', ({ currentTarget: target }) => {
target.classList.add('active');
const toggle = target.getAttribute('data-toggle');
[...target.parentNode.querySelectorAll(`:scope > *[data-toggle]`)].filter((child) => child !== target).forEach(sibling => sibling.classList.remove('active'));
const survey = document.querySelector(`.surveys`);
survey.classList.remove('grid', 'list');
survey.classList.add(toggle);
}));