# Adding an OnThisPage navigation to Swift-DocC-Render

**URL:** <https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806>\
**Category:** Swift-DocC\
**Created:** [August 22, 2022, 11:27am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806 "2022-08-22T11:27:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhristov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dhristov/32/24138_2.png) [@dhristov](https://forums.swift.org/u/dhristov)\
**Post date:** [August 22, 2022, 11:27am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/1 "2022-08-22T11:27:13Z")

</div>

Hi all!

I'm excited to pitch a new feature to Swift-DocC-Render, which I refer to as OnThisPage navigation. This is a new right sidebar component, that would visualise the current page's content headings and sections, as well as track where you are on the page, as you scroll.

The new element will be visible only for larger screens, where the content width is at least 1270px, so it does not take up valuable content space. This ensures there is enough space for the content and the sidebar on the right. This also works in tandem with the new adjustable sidebar navigator, so if you expand the navigator too much, the new sidebar will hide itself.

You can see a deployed preview [here](https://dobromir-hristov.github.io/slothcreator/documentation/slothcreator)  
DocC-Render PR - [here](https://github.com/apple/swift-docc-render/pull/416)

 ![Preview of onThisPage navigation](https://global.discourse-cdn.com/swift/original/3X/e/c/ecad01af0c987ec57439f356586e824b6a9715bd.png)

---

<div class="post-metadata">

**Author:** ![franklin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franklin/32/17924_2.png) [@franklin](https://forums.swift.org/u/franklin)\
**Post date:** [August 22, 2022, 2:09pm UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/2 "2022-08-22T14:09:52Z")

</div>

Looks great, @dhristov! In the same way we're showing h3s in the On This Page component, have you also considered showing topic group titles from Topics sections and See Also sections?

---

<div class="post-metadata">

**Author:** ![dhristov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dhristov/32/24138_2.png) [@dhristov](https://forums.swift.org/u/dhristov)\
**Post date:** [August 22, 2022, 2:13pm UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/3 "2022-08-22T14:13:44Z")

</div>

I thought about it, but I think those do not have anchors generated by docc, plus I thought it might start to look pretty busy in the nav. We can for sure add them, up to us to decide.

---

<div class="post-metadata">

**Author:** ![daniel-grumberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/daniel-grumberg/32/22029_2.png) [@daniel-grumberg](https://forums.swift.org/u/daniel-grumberg)\
**Post date:** [August 22, 2022, 4:05pm UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/4 "2022-08-22T16:05:41Z")

</div>

Maybe topic groups shouldn’t be clickable links in the right sidebar (with associated styling to show that)? They could act just a marker to remind the user of the curation of the topics in the right sidebar. I think they would be great context clues for topic links in this new sidebar.

---

<div class="post-metadata">

**Author:** ![ethankusters](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ethankusters/32/19078_2.png) [@ethankusters](https://forums.swift.org/u/ethankusters)\
**Post date:** [August 22, 2022, 5:25pm UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/5 "2022-08-22T17:25:31Z")

</div>

Looks great! Thanks for your work on this @dhristov. Happy to see this feature coming back to Swift-DocC-Render in a new and improved way.

> [@dhristov](#):
>
> The new element will be visible only for larger screens, where the content width is at least 1270px,

@dhristov How did you decide on this particular content width value? I noticed I can't get the "on this page" to appear on my 13inch MacBook Air which seems like it ought to be large enough for this.

* * *

> [@franklin](#):
>
> have you also considered showing topic group titles from Topics sections and See Also sections?

> [@dhristov](#):
>
> I thought about it, but I think those do not have anchors generated by docc, plus I thought it might start to look pretty busy in the nav.

I agree with @dhristov on this- the Topics are also already visible in the navigation sidebar on the left so duplicating them on the right seems pretty redundant.

* * *

@dhristov On the "looking pretty busy" topic, did you consider only enabling the "On this Page" when a page really needs it? I.e it has more than one or two headings or the subsections are sufficiently large to be moved off of one screen?

I've noticed with similar UI elements on other documentation engines that it ends up looking a little out-of-place for pages with only a single heading.

 ![Screenshot 2022-08-22 at 10.21.57 AM](https://global.discourse-cdn.com/swift/original/3X/8/9/8982d80c57d76034cb51a20fe3ee89fe16bb4bc8.jpeg)

Here for example, I'm not sure the "Declaration" ever needs an entry in the breadcrumbs. And then the "Topics" and "See Also" are autogenerated so they'll appear on nearly every page which also seems a little redundant.

 ![Screenshot 2022-08-22 at 10.22.53 AM](https://global.discourse-cdn.com/swift/original/3X/8/b/8b19d446f0392a602f2e383e18d4cfc4b89d1c4b.jpeg)

 ![Screenshot 2022-08-22 at 10.23.38 AM](https://global.discourse-cdn.com/swift/original/3X/3/1/3128d8c69ebb266d85f4fa338013fa163781baa1.jpeg)

I think my preference would be to only use the "on the page" for items that are part of the prose/body content, not the autogenerated links or declaration sections. And then ideally "on this page" would only be activated if that body content was sufficiently large enough to actually benefit from the extra navigation.

---

<div class="post-metadata">

**Author:** ![krilnon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krilnon/32/276_2.png) [@krilnon](https://forums.swift.org/u/krilnon)\
**Post date:** [August 22, 2022, 5:31pm UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/6 "2022-08-22T17:31:18Z")

</div>

I'm a bit curious if there's room for "on this page" elements in the sidebar. It'd help smaller viewports and might make sense for longer form content, like showing sections of a chapter. If you can link to headings from the "on this page" elements, it'd be nice if there was a way to do the same from a phone, for example.

---

<div class="post-metadata">

**Author:** ![dhristov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dhristov/32/24138_2.png) [@dhristov](https://forums.swift.org/u/dhristov)\
**Post date:** [August 23, 2022, 7:22am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/7 "2022-08-23T07:22:31Z")

</div>

> [@ethankusters](#):
>
> @dhristov How did you decide on this particular content width value? I noticed I can't get the "on this page" to appear on my 13inch MacBook Air which seems like it ought to be large enough for this.

At first glance yes, but if you look at the gutters and free space you will see there is barely room (green are gutter spaces, orange is free space). We would have to interfere with the gutter space, which generally we avoid.

 ![image](https://global.discourse-cdn.com/swift/original/3X/3/3/33bd2dab482d5c5052cbb224e92004f956f7d6fa.png)

It could work, though only if you have the navigator closed, and we reduce the sidebar width.

 ![image](https://global.discourse-cdn.com/swift/original/3X/c/6/c64e001ef17135fe996a5f83cd72d42cdc903e31.png)

> [@ethankusters](#):
>
> I think my preference would be to only use the "on the page" for items that are part of the prose/body content, not the autogenerated links or declaration sections. And then ideally "on this page" would only be activated if that body content was sufficiently large enough to actually benefit from the extra navigation.

That sounds pretty good, but detecting the "size" of a section might be too complex, as the size varies allot. (Images are async downloaded, space changes as you resize etc).

We can however have some checks, whether we have collected only auto generated headings (Declaration, See Also etc) or we also have some content headings in there as well, and decide whether the OnThisPage is even useful or not.

---

<div class="post-metadata">

**Author:** ![dhristov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dhristov/32/24138_2.png) [@dhristov](https://forums.swift.org/u/dhristov)\
**Post date:** [August 23, 2022, 7:24am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/8 "2022-08-23T07:24:20Z")

</div>

> [@krilnon](#):
>
> I'm a bit curious if there's room for "on this page" elements in the sidebar. It'd help smaller viewports and might make sense for longer form content, like showing sections of a chapter. If you can link to headings from the "on this page" elements, it'd be nice if there was a way to do the same from a phone, for example.

Do you mean to move the OnThisPage to the left, where the Tree navigator is? I think those are different UI elements and should probably stay separate 😄 As for whether it makes sense to have it on mobile or not, I dont think there is enough space to show it in a meaningful way anywhere 😕

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [August 23, 2022, 8:33am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/9 "2022-08-23T08:33:27Z")

</div>

Did you consider an "On This Page" pull-down menu?

[The Swift Programming Language](https://docs.swift.org/swift-book/GuidedTour/GuidedTour.html) book has a menu that works on smaller screens.

For example, 800px by 600px:

 ![TSPL](https://global.discourse-cdn.com/swift/original/3X/d/2/d28958662b7065f6b4f9a61a4e8a940de6c50665.png)

---

<div class="post-metadata">

**Author:** ![theMomax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/themomax/32/22719_2.png) [@theMomax](https://forums.swift.org/u/theMomax)\
**Post date:** [August 23, 2022, 8:38am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/10 "2022-08-23T08:38:21Z")

</div>

This looks awesome!

One tiny thing I noticed is that the bold effect for the active section can cause longer headlines to break differently when active as it does here:

 ![size_glitch](https://global.discourse-cdn.com/swift/original/3X/2/e/2e04e100925e7b99fecbfc2bf1294668c5b9865b.jpeg)

It's no biggie, of course, but the tiny glitch can be a little distracting. I guess this could be easily avoided by just coloring the active section black without the extra boldness or using a small arrow indicator.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [August 23, 2022, 12:46pm UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/11 "2022-08-23T12:46:05Z")

</div>

Really nice! The right-hand side looks a bit empty right now, so I'm glad this is coming back with improvements.

One thing though - when scrolling down a page, the box feels like it sits too close to the top of the page. I think it could do with some extra spacing between this content and the navigation bar.

See: [docc-onthispage-scroll · GitHub](https://gist.github.com/karwa/2e01d251fe96cdf9fad455dabdd7416d)

---

<div class="post-metadata">

**Author:** ![sofiaromorales](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sofiaromorales/32/43011_2.png) [@sofiaromorales](https://forums.swift.org/u/sofiaromorales)\
**Post date:** [August 26, 2022, 4:25am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/12 "2022-08-26T04:25:19Z")

</div>

This looks great! Thanks for building this @dhristov

As far as I could tell, the OnThisPage navigator only shows the autogenerated h2 headers, have you considered adding a deeper level that shows heading level 3 and `Topics` and `See Also` group titles? I've seen similar UIs that add indentation to distinguish between sections and subsections.

I find this very useful in articles, but it might be a little redundant for symbol pages, IMO most of them would not need this navigator because the content will not be that big unless they have extra content added via extension files. Not sure if it's currently possible from the docc-render level since all the content for each symbol ends up in a single file, but if there's a way to tell if the symbol had additional content added via an extension, we could use that information to decide if is useful to render the file navigator.

---

<div class="post-metadata">

**Author:** ![dhristov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dhristov/32/24138_2.png) [@dhristov](https://forums.swift.org/u/dhristov)\
**Post date:** [August 26, 2022, 6:53am UTC](https://forums.swift.org/t/adding-an-onthispage-navigation-to-swift-docc-render/59806/13 "2022-08-26T06:53:10Z")

</div>

It shows H2 and H3 from the content, as well as the main topics after the content. Subtopics are still in debate whether it makes sense to show them, since they are already in the Navigator.

Indentation is not a bad idea, but we have very little space to work with in the first place. I will have to discuss with the Design team.

In the latest PR, we added a minimum of 2 items in order to show the OnThisPage, which should help situations as the ones you described.
