From fbf3d7b64bb137620b3c8270bd31bbfb69ff90e2 Mon Sep 17 00:00:00 2001 From: John Bowdre Date: Fri, 17 Dec 2021 15:49:08 -0600 Subject: [PATCH] implement collapsible table of contents at the top of the post body, remove it from the sidebar --- assets/js/custom.js | 13 ++++++++ assets/sass/_custom.sass | 59 ++++++++++++++++++++++++++++++++++- layouts/_default/single.html | 13 ++++++++ layouts/partials/sidebar.html | 6 ---- 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 assets/js/custom.js diff --git a/assets/js/custom.js b/assets/js/custom.js new file mode 100644 index 0000000..bf4c405 --- /dev/null +++ b/assets/js/custom.js @@ -0,0 +1,13 @@ +// Make the collapsible content accessible +let myLabels = document.querySelectorAll('.lbl-toggle'); + +Array.from(myLabels).forEach(label => { + label.addEventListener('keydown', e => { + // 32 === spacebar + // 13 === enter + if (e.which === 32 || e.which === 13) { + e.preventDefault(); + label.click(); + }; + }); +}); \ No newline at end of file diff --git a/assets/sass/_custom.sass b/assets/sass/_custom.sass index 558391f..1f62947 100644 --- a/assets/sass/_custom.sass +++ b/assets/sass/_custom.sass @@ -1,7 +1,64 @@ +// Offset scrolling to compensate for navbar so anchors work correctly html, body scroll-padding-top: 4.5rem +// Impose a max height on the site logo so it doesn't scale and look bad .nav &_brand img - max-height: 2rem \ No newline at end of file + max-height: 2rem + +// Magic for collapsing content, borrowed from https://www.digitalocean.com/community/tutorials/css-collapsible +input[type='checkbox'] + display: none + +.lbl-toggle + display: block + font-size: 125% + cursor: pointer + transition: all 0.25s ease-out + +.lbl-toggle::before + content: ' ' + display: inline-block + border-top: 5px solid transparent + border-bottom: 5px solid transparent + border-left: 5px solid currentColor + verticale-align: middle + margin-right: .7rem + transform: translateY(-2px) + transition: transform .2s ease-out + +.collapsible-content .content-inner + border-bottom-left-radius: 7px + border-bottom-right-radius: 7px + padding: .5rem 1rem + +.collapsible-content + max-height: 0px + overflow: hidden + transition: max-height .25s ease-in-out + +.toggle:checked + .lbl-toggle + .collapsible-content + max-height: 100vh + +.toggle:checked + .lbl-toggle::before + transform: rotate(90deg) translateX(-3px) + +.toggle:checked + .lbl-toggle + border-bottom-right-radius: 0 + border-bottom-left-radius: 0 + +// Make the ToC a bit more compact than other lists +.post_toc + a + color: inherit + ul, ol + padding-top: 0.0rem + padding-left: 0.5rem + padding-bottom: 0.0rem + li + padding-top: 0.01rem + padding-left: 0.5rem + padding-bottom: 0.01rem + font-size: 1rem \ No newline at end of file diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 1a675ee..e174b3f 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -15,6 +15,19 @@

{{ $t }}

{{- partial "post-meta" . }} {{- partial "image-feature" . }} + {{ if and (gt .WordCount 400 ) (gt (len .TableOfContents) 180) ($s.toc) }} +
+
+ + +
+
+ {{ .TableOfContents }} +
+
+
+
+ {{ end }}
{{- .Content }}
diff --git a/layouts/partials/sidebar.html b/layouts/partials/sidebar.html index 9ee5a6d..7bbf1e3 100644 --- a/layouts/partials/sidebar.html +++ b/layouts/partials/sidebar.html @@ -30,12 +30,6 @@ {{ $r }} {{- end }} {{- end }} - {{ if and (gt .WordCount 400 ) (gt (len .TableOfContents) 180) ($s.toc) }} -
-

{{ T "overview" }}

- {{ .TableOfContents }} -
- {{ end }} {{- $relatedInSidebar := true }} {{- if eq $s.showRelatedInSidebar false }} {{ $relatedInSidebar = false }}