Hi,
I am a beginner at kirby and php and so i wasn’t able to follow the kirby cookbook for ToC, it felt a bit over my head.
Instead, i managed to create one for my pages by finding the heading from the layout blocks and further organizing them based on the heading level, h2 (main menu item) and h4 (sub menu item)
I am trying to add in the onActive() and class=”active” i see in the kirby menu references, but it isn’t working with the structure i have. When i add it in, all of the items get the active class instead of just the item that is active/clicked. Ideally, i want the menu item to be styled when clicked and also when a user vertically scrolls to corresponding page section. I would be happy though just to have the menu item styled when clicked.
Thank you in advance!
snip for the menu
<?php foreach ($page->layout()->toLayouts() as $layout): ?>
<?php foreach ($layout->columns() as $column): ?>
<!-- access to blocks -->
<?php foreach ($column->blocks() as $projectBlock): ?>
<!-- if block type is heading -->
<?php if ($projectBlock->type() === 'heading'): ?>
<?php $id = Str::slug(Str::unhtml($projectBlock->content()->text())) ?>
<!-- add class depending on heading level -->
<li <?php if ($projectBlock->content()->level() == 'h2'): ?>
class="menu--item" <?php else: ?>
class="menu--subitem"
<?php endif ?>>
<a href="#<?= $id ?>">
<?= $projectBlock->content()->text() ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endforeach ?>