Do you embed Amplenote content into a blog or help center? Would you like your content to have a sidebar list of all the sections in your main content, that allows users to remain oriented within their help page, even for long & complex pages like the GitClear Encyclopedia of Developer Analytics?
By consuming events passed in from the Amplenote embed, you can implement your own custom sidebar table of contents. Below, we present the code that GitClear uses to implement a scrolling sidebar table of contents, just like the ones shown when navigating pages from the Amplenote Help Center (like the Amplecap help docs).
linkFunctionality demo
For a live demo, please visit the aforementioned Encyclopedia of Developer Metrics, where this component is utilized to render some 70+ sections. You can also click around pages in the GitClear Help Section or GitClear Blog, where you'll find the sidebar embedded in pages with varying numbers of headers.
linkFunctional React component: AmplenoteSidebarToc
Here is the functional React component that GitClear is using to load a table of contents alongside its Amplenote page embed:
First, amplenote-sidebar-toc.js. Note that lodash is not strictly necessary, but scroll events fire a lot, so your page could experience some amount of slowdown if you don't use some flavor of "debounce" or "throttle" for handling these noisy events:
Then, amplenote-sidebar-toc.scss. Note that this code presumes that your initial React component is mounted in a div with data-react-class="AmplenoteSidebarToc", which must be a direct child of the parent div that contains your Amplenote embedded content. More on that in the next section.
linkAdding the sidebar component to your HTML
To implement the sidebar component, it must be a child of a div that allows it to enact its position: sticky CSS property. On GitClear we structure the sidebar as follows:
Note how the React component (the div with data-react-class="AmplenoteSidebarToc" that is mounted as a React component on page load) is a sibling of the div that holds the Amplenote content embed.
Finally, a tablespoon of CSS to ensure that the sticky-positioned React component stays visible & anchored atop page:
Upon mounting the React component, you should get all the bells & whistles of an auto-scrolling sidebar table of contents.