Skip to content

Commit 581b1ab

Browse files
feat(): Side menu and sidebar
1 parent c06d423 commit 581b1ab

File tree

20 files changed

+335
-219
lines changed

20 files changed

+335
-219
lines changed

js/feedback/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const COMPONENT_NAME = 'data-feedback'
2+
const COMPONENT_SELECTOR = `[${COMPONENT_NAME}]`
3+
const HELPFUL_BUTTON_SELECTOR = `[data-ref="feedback[helpful]"]`
4+
const UNHELPFUL_BUTTON_SELECTOR = `[data-ref="feedback[unhelpful]"]`
5+
6+
const DEFAULTS = {
7+
message: {
8+
success: 'Thanks for your feedback!'
9+
}
10+
}
11+
12+
export default function () {
13+
const components = document.querySelectorAll(COMPONENT_SELECTOR)
14+
15+
for (let i = 0; i < components.length; i++) {
16+
const options = JSON.parse(components[i].getAttribute(COMPONENT_NAME) || '{}')
17+
const settings = Object.assign({}, DEFAULTS, options)
18+
19+
const helpfulButton = components[i].querySelector(HELPFUL_BUTTON_SELECTOR)
20+
const unhelpfulButton = components[i].querySelector(UNHELPFUL_BUTTON_SELECTOR)
21+
22+
helpfulButton.addEventListener('click', (event) => {
23+
event.preventDefault()
24+
25+
components[i].innerText = settings.message.success
26+
27+
window.analytics.track('Docs Rated', {
28+
title: '{{title}}',
29+
helpful: true,
30+
url: document.URL
31+
})
32+
})
33+
34+
unhelpfulButton.addEventListener('click', (event) => {
35+
event.preventDefault()
36+
37+
components[i].innerText = settings.message.success
38+
39+
window.analytics.track('Docs Rated', {
40+
title: '{{title}}',
41+
helpful: false,
42+
url: document.URL
43+
})
44+
})
45+
}
46+
}

js/headings-anchors/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const COMPONENT_SELECTOR = '[data-headings-anchors]'
2+
const HEADINGS_SELECTOR = 'h1, h2, h3, h4, h5, h6'
3+
4+
export default function () {
5+
const components = document.querySelectorAll(COMPONENT_SELECTOR)
6+
7+
for (let i = 0; i < components.length; i++) {
8+
const headings = components[i].querySelectorAll(HEADINGS_SELECTOR)
9+
10+
for (let i = 0; i < headings.length; i++) {
11+
headings[i].addEventListener('click', (event) => {
12+
location.hash = headings[i].getAttribute('id')
13+
})
14+
}
15+
}
16+
}

js/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
console.log('hello world');
1+
import feedback from './feedback'
2+
import headingsAnchors from './headings-anchors'
3+
4+
feedback()
5+
headingsAnchors()

src/_includes/feedback-footer.html

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Loading
Lines changed: 3 additions & 3 deletions
Loading

src/_includes/sidebar/feedback.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<div class="widget" data-feedback>
2+
<p class="widget__heading">Was this page helpful?</p>
3+
4+
<div class="flex">
5+
<div class="flex__column flex__column--6">
6+
<button class="button button--small button-fill button-fill--white" data-ref="feedback[helpful]">
7+
<span class="button__icon">
8+
{% include icons/symbols/thumb-up.svg %}
9+
</span>
10+
11+
<span>Yes</span>
12+
</button>
13+
</div>
14+
15+
<div class="flex__column flex__column--6">
16+
<button class="button button--small button-fill button-fill--white" data-ref="feedback[unhelpful]">
17+
<span class="button__icon">
18+
{% include icons/symbols/thumb-down.svg %}
19+
</span>
20+
21+
<span>No</span>
22+
</button>
23+
</div>
24+
</div>
25+
</div>

src/_includes/sidebar/menu-side.html

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{% capture tocWorkspace %}
2+
{% comment %}
3+
Version 1.0.8
4+
https://github.com/allejo/jekyll-toc
5+
6+
"...like all things liquid - where there's a will, and ~36 hours to spare, there's usually a/some way" ~jaybe
7+
8+
Usage:
9+
{% include toc.html html=content sanitize=true class="inline_toc" id="my_toc" h_min=2 h_max=3 %}
10+
11+
Parameters:
12+
* html (string) - the HTML of compiled markdown generated by kramdown in Jekyll
13+
14+
Optional Parameters:
15+
* sanitize (bool) : false - when set to true, the headers will be stripped of any HTML in the TOC
16+
* class (string) : '' - a CSS class assigned to the TOC
17+
* id (string) : '' - an ID to assigned to the TOC
18+
* h_min (int) : 1 - the minimum TOC header level to use; any header lower than this value will be ignored
19+
* h_max (int) : 6 - the maximum TOC header level to use; any header greater than this value will be ignored
20+
* ordered (bool) : false - when set to true, an ordered list will be outputted instead of an unordered list
21+
* item_class (string) : '' - add custom class(es) for each list item; has support for '%level%' placeholder, which is the current heading level
22+
* baseurl (string) : '' - add a base url to the TOC links for when your TOC is on another page than the actual content
23+
* anchor_class (string) : '' - add custom class(es) for each anchor element
24+
25+
Output:
26+
An ordered or unordered list representing the table of contents of a markdown block. This snippet will only
27+
generate the table of contents and will NOT output the markdown given to it
28+
{% endcomment %}
29+
30+
{% capture my_toc %}{% endcapture %}
31+
{% assign orderedList = include.ordered | default: false %}
32+
{% assign minHeader = include.h_min | default: 1 %}
33+
{% assign maxHeader = include.h_max | default: 6 %}
34+
{% assign nodes = include.html | split: '<h' %}
35+
{% assign firstHeader = true %}
36+
37+
{% capture listModifier %}{% if orderedList %}1.{% else %}-{% endif %}{% endcapture %}
38+
39+
{% for node in nodes %}
40+
{% if node == "" %}
41+
{% continue %}
42+
{% endif %}
43+
44+
{% assign headerLevel = node | replace: '"', '' | slice: 0, 1 | times: 1 %}
45+
46+
{% if headerLevel < minHeader or headerLevel > maxHeader %}
47+
{% continue %}
48+
{% endif %}
49+
50+
{% if firstHeader %}
51+
{% assign firstHeader = false %}
52+
{% assign minHeader = headerLevel %}
53+
{% endif %}
54+
55+
{% assign indentAmount = headerLevel | minus: minHeader | add: 1 %}
56+
{% assign _workspace = node | split: '</h' %}
57+
58+
{% assign _idWorkspace = _workspace[0] | split: 'id="' %}
59+
{% assign _idWorkspace = _idWorkspace[1] | split: '"' %}
60+
{% assign html_id = _idWorkspace[0] %}
61+
62+
{% assign _classWorkspace = _workspace[0] | split: 'class="' %}
63+
{% assign _classWorkspace = _classWorkspace[1] | split: '"' %}
64+
{% assign html_class = _classWorkspace[0] %}
65+
66+
{% if html_class contains "no_toc" %}
67+
{% continue %}
68+
{% endif %}
69+
70+
{% capture _hAttrToStrip %}{{ _workspace[0] | split: '>' | first }}>{% endcapture %}
71+
{% assign header = _workspace[0] | replace: _hAttrToStrip, '' %}
72+
73+
{% assign space = '' %}
74+
{% for i in (1..indentAmount) %}
75+
{% assign space = space | prepend: ' ' %}
76+
{% endfor %}
77+
78+
{% unless include.item_class == blank %}
79+
{% capture listItemClass %}{:.{{ include.item_class | replace: '%level%', headerLevel }}}{% endcapture %}
80+
{% endunless %}
81+
82+
{% capture heading_body %}{% if include.sanitize %}{{ header | strip_html }}{% else %}{{ header }}{% endif %}{% endcapture %}
83+
{% capture my_toc %}{{ my_toc }}
84+
{{ space }}{{ listModifier }} {{ listItemClass }} [{{ heading_body | replace: "|", "\|" }}]({% if include.baseurl %}{{ include.baseurl }}{% endif %}#{{ html_id }}){% if include.anchor_class %}{:.{{ include.anchor_class }}}{% endif %}{% endcapture %}
85+
{% endfor %}
86+
87+
{% if include.class %}
88+
{% capture my_toc %}{:.{{ include.class }}}
89+
{{ my_toc | lstrip }}{% endcapture %}
90+
{% endif %}
91+
92+
{% if include.id %}
93+
{% capture my_toc %}{: #{{ include.id }}}
94+
{{ my_toc | lstrip }}{% endcapture %}
95+
{% endif %}
96+
{% endcapture %}{% assign tocWorkspace = '' %}{{ my_toc | markdownify | strip }}

src/_includes/toc.html

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/_layouts/default.html

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,17 @@
6464
<div class="docs__sidebar stage__sidebar">
6565
{% include navbar/nav.html %}
6666

67-
{%- unless page.hide_toc -%}
68-
<div>
69-
<p>On this page</p>
70-
71-
{% include toc.html html=content h_max=2 %}
72-
</div>
73-
{%- endunless -%}
74-
75-
{% if page.feedback != false %}
76-
<div id="feedback">
77-
{% include feedback-footer.html %}
78-
</div>
79-
{% endif %}
67+
<div class="sidebar">
68+
{%- unless page.hide_toc -%}
69+
<div data-menu-side>
70+
{% include sidebar/menu-side.html class="menu-side" anchor_class="menu-side__link" html=content h_min=2 h_max=2 %}
71+
</div>
72+
{%- endunless -%}
73+
74+
{% if page.feedback != false %}
75+
{% include sidebar/feedback.html %}
76+
{% endif %}
77+
</div>
8078
</div>
8179
</div>
8280

0 commit comments

Comments
 (0)