Skip to content

Commit

Permalink
♻️ Show all active sections in table of contents
Browse files Browse the repository at this point in the history
Show all active sections as this will mean we can also show headings in our side navigation and the highlighted sections will be aligned.
It removes the need to handle clicks when there are small sections at the bottom that have their anchor tags clicked.

Related thread: https://centrapay.slack.com/archives/C04B93QUDSM/p1696994426728949

Story: ✨ [Migrate API Documentation](https://www.notion.so/centrapay/Lanterns-d784e8a5f9fa44328a5d0ae1fd85d37e?p=c6d64de29a2245358a5e9b7eed5edbcc&pm=s)

Test plan: Expect multiple sections to be highlighted in TOC when they are all visible.
  • Loading branch information
MeganSteenkamp committed Oct 11, 2023
1 parent cc48c0b commit 61defb7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
25 changes: 11 additions & 14 deletions src/components/TocNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
>
<a
:href="`#${heading.slug}`"
@click="handleTocClick(heading.slug)"
>
<h3
class="border-l-2 p-squish-2 hover:text-content-primary hover:border-content-primary"
class="border-l-2 p-squish-2 hover:text-content-primary hover:border-content-primary transition"
:class="[
visibleHeadingId === heading.slug ? 'text-content-primary border-brand-accent': 'font-normal text-content-tertiary',
visibleHeadingIds.includes(heading.slug) ?
'text-content-primary border-brand-accent delay-200 duration-1000 ease-in-out':
'font-normal text-content-tertiary',
heading.depth === 3 ? 'pl-5' : ''
]"
>
Expand All @@ -47,30 +48,30 @@ const props = defineProps({
headings: { type: Object, required: true },
});
const visibleHeadingId = ref('');
const visibleHeadingIds = ref([]);
const visibleHeadings = props.headings.filter(heading => heading.depth <= 3);
let observer = null;
onMounted(() => {
const observerOptions = {
threshold: 0,
rootMargin: '-20% 0px -80% 0px',
};
observer = new IntersectionObserver(contentSections => {
contentSections.forEach(contentSection => {
const headingId = contentSection.target.firstElementChild.id;
if (contentSection.isIntersecting) {
visibleHeadingId.value = headingId;
visibleHeadingIds.value.push(headingId);
} else {
const index = visibleHeadingIds.value.indexOf(headingId);
if (index > -1) {
visibleHeadingIds.value.splice(index, 1);
}
}
});
}, observerOptions);
const sections = document.querySelectorAll('section');
if (sections.length) {
const firstHeadingId = sections[0].firstElementChild.id;
visibleHeadingId.value = firstHeadingId;
}
sections.forEach(contentSection => {
observer.observe(contentSection);
});
Expand All @@ -80,8 +81,4 @@ onUnmounted(() => {
observer.disconnect();
});
function handleTocClick (headingId) {
visibleHeadingId.value = headingId;
};
</script>
4 changes: 2 additions & 2 deletions src/content/guides/creating-digital-tokens.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ nav:
Digital tokens are assets that can be exchanged for specific products or deals. Think a "Free coffee" coupon or a "50% off" voucher.
Unlike assets such as gift cards or currency, tokens can only be spent in their entirety, all at once.

### Token Collection
## Token Collection

A collection is a grouping of tokens that share the same attributes and redemption conditions, e.g.

Expand All @@ -18,7 +18,7 @@ A collection is a grouping of tokens that share the same attributes and redempti
- Expiry period
- Maximum redemption value

### Redemption Conditions
## Redemption Conditions

A redemption condition allows a [Merchant](https://docs.centrapay.com/api/merchants) to accept tokens from a token collection.
It contains redemption details specific to that merchant, e.g. redeemable product SKUs.
Expand Down

0 comments on commit 61defb7

Please sign in to comment.