Script
var toggle = new Toggle({
buttonClassExpanded: 'toggle-btn-active',
buttonSelector: 'a',
parentClass: 'toggle-parent',
targetSelector: '.menu ul ul'
})
HTML
<nav class="menu">
<ul>
<li>
<a href="#">Pie</a>
<ul>
<li><a href="#">Apple Pie</a></li>
<li>
<a href="#">Cherry Pie</a>
<ul>
<li><a href="#">With Pastry Crust</a></li>
<li><a href="#">With Graham Cracker Crust</a></li>
</ul>
</li>
<li><a href="#">Pumpkin Pie</a></li>
<li><a href="#">Chocolate Pie</a></li>
</ul>
</li>
<li>
<a href="#">Cake</a>
<ul>
<li>
<a href="#">Vanilla Cake</a>
<ul>
<li><a href="#">With Vanilla Icing</a></li>
<li><a href="#">With Chocolate Icing</a></li>
<li><a href="#">With Sprinkles</a></li>
</ul>
</li>
<li><a href="#">Chocolate Cake</a></li>
<li><a href="#">Fruit Cake</a></li>
</ul>
</li>
<li><a href="#">Ice Cream</a></li>
</ul>
</nav>
CSS
/* Hide collapsed */
[aria-expanded="false"] {
display: none;
}
/* Add icon to parents */
.toggle-parent > a::after {
content: '\f107'; /* Font Awesome 'angle-down' */
display: inline-block;
font-family: 'FontAwesome';
margin-left: 0.5rem;
transition: transform 0.3s ease;
}
/* Flip the icon */
.toggle-parent > a.toggle-btn-active::after {
transform: rotate(180deg);
}