Skip to content

Commit

Permalink
Show table of contents on large viewports (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkudrna committed Nov 16, 2020
1 parent f3a36ea commit 2c89a51
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"browser": true
},
"globals": {
"gtag": false
"gtag": false,
"tocbot": false
},
"parserOptions": {
"ecmaVersion": 10,
Expand Down
38 changes: 38 additions & 0 deletions assets/js/table-of-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const TOC_ID = 'obsah';

const generateToc = () => {
tocbot.init({
activeLinkClass: 'is-active',
collapsibleClass: 'is-collapsible',
contentSelector: '.js-table-of-contents-source',
headingSelector: 'h1, h2, h3',
isCollapsedClass: 'is-collapsed',
linkClass: 'toc__link',
listClass: 'toc__list',
listItemClass: 'toc__list__item',
scrollSmooth: false,
tocSelector: `#${TOC_ID}`,
});

document.getElementById(TOC_ID).classList.add('toc--active');
};

const destroyToc = () => {
document.getElementById(TOC_ID).classList.remove('toc--active');
tocbot.destroy();
};

const updateToc = () => {
const tocEl = document.getElementById(TOC_ID);

if (window.getComputedStyle(tocEl).display !== 'none') {
if (!tocEl.innerHTML) {
generateToc();
}
} else if (tocEl.innerHTML) {
destroyToc();
}
};

window.addEventListener('load', () => updateToc());
window.addEventListener('resize', () => updateToc());
1 change: 1 addition & 0 deletions assets/scss/02-settings/_toc.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$toc-breakpoint: xl;
36 changes: 36 additions & 0 deletions assets/scss/07-components/_toc.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.toc {
display: none;

@include breakpoint-up($toc-breakpoint) {
@include transition('visibility, opacity');

position: sticky;
top: 0;
right: 0;
display: block;
visibility: hidden;
opacity: 0;
width: calc((100vw - #{$container-article-width}) / 2);
height: 0;
padding: spacing(2) $container-padding-sm;
line-height: map-get($typography-line-height-values, dense);
will-change: visibility, opacity;
}
}

.toc__list {
@include breakpoint-up($toc-breakpoint) {
@include typography(small);
}
}

.toc__list .toc__list {
font-size: inherit;
}

.toc--active {
@include breakpoint-up($toc-breakpoint) {
visibility: visible;
opacity: 1;
}
}
2 changes: 2 additions & 0 deletions assets/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@import "02-settings/grid";
@import "02-settings/modularscale";
@import "02-settings/tables";
@import "02-settings/toc";
@import "02-settings/typography";

// 3. Tools
Expand Down Expand Up @@ -75,6 +76,7 @@
@import "07-components/post-card";
@import "07-components/share";
@import "07-components/text-field";
@import "07-components/toc";

// 8. Editor
// User content from CMS.
Expand Down
13 changes: 12 additions & 1 deletion post.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@

</header>

{{^has tag="galerie"}}
<div class="toc__holder">
<div id="obsah" class="toc" hidden></div>
</div>
{{/has}}

<section class="section section--dense">
<div class="container container--article">

<div class="editor js-nbsp js-external-links js-heading-anchors">
<div class="editor js-nbsp js-external-links js-heading-anchors js-table-of-contents-source">
{{content}}
</div>

Expand Down Expand Up @@ -156,6 +162,11 @@
{{/contentFor}}

{{#contentFor "scripts"}}
{{!-- Table of Contents --}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.12.0/tocbot.min.js"></script>
<script src="{{asset "js/table-of-contents.js"}}"></script>

{{!-- Code syntax highlighting with Prism.js --}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/plugins/autoloader/prism-autoloader.min.js"></script>

Expand Down

0 comments on commit 2c89a51

Please sign in to comment.