Skip to content

Commit 3404beb

Browse files
committed
Add sidebar to news posts (#561)
This PR adds a sidebar using [Zolas TOC](https://www.getzola.org/documentation/content/table-of-contents/). In desktop first two levels are shown, in mobile just the top level headers. **DESKTOP** ![image](https://user-images.githubusercontent.com/188612/222731870-76394e64-797d-4523-b9f6-7d0ef0febaa6.png) **MOBILE** ![image](https://user-images.githubusercontent.com/188612/222724600-56e5bafa-f4ef-4d72-8a7f-dc38f8232f18.png)
1 parent be9da37 commit 3404beb

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

sass/components/_page-with-menu.scss

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.page-with-menu {
22
$margin-top: 24px;
3+
$sidebar-width: 250px;
34

45
display: grid;
56
grid-gap: 24px;
@@ -9,7 +10,7 @@
910

1011
@media #{$bp-tablet-landscape-up} {
1112
margin-top: $margin-top;
12-
grid-template-columns: 250px 1fr;
13+
grid-template-columns: $sidebar-width 1fr;
1314
grid-template-areas: 'menu content';
1415
}
1516

@@ -28,7 +29,7 @@
2829

2930
position: sticky;
3031
top: calc(var(--header-height) + #{$margin-top});
31-
height: calc(100vh - var(--header-height) - #{$margin-top});
32+
height: calc(100vh - var(--header-height) - #{$margin-top * 2});
3233
overflow-y: auto;
3334
padding-bottom: $margin-top;
3435
}
@@ -37,4 +38,11 @@
3738
grid-area: content;
3839
overflow-x: hidden;
3940
}
41+
42+
&--news {
43+
@media #{$bp-tablet-landscape-up} {
44+
grid-template-columns: 1fr $sidebar-width;
45+
grid-template-areas: 'content menu';
46+
}
47+
}
4048
}

templates/layouts/page-with-menu.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{% endblock %}
1717

1818
{% block content %}
19-
<div class="page-with-menu">
19+
<div class="page-with-menu {% block page_with_menu_extra_class %}{% endblock %}">
2020
<div class="page-with-menu__menu-wrapper">
2121
<div class="page-with-menu__menu">
2222
{% block page_menu %}{% endblock %}

templates/macros/news.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{% macro news_menu_row(header, max_levels, level) %}
2+
{% set id = 'news-menu-' ~ level ~ '-' ~ header.title | slugify %}
3+
{% set label_class = "tree-menu__label" %}
4+
{% set total_children = header.children | length %}
5+
{% set has_children = total_children > 0 %}
6+
{% set show_children = has_children and level < max_levels %}
7+
8+
{% if show_children %}
9+
{% set label_class = label_class ~ " tree-menu__label--with-chevron" %}
10+
<input id="{{id}}" class="tree-menu__state" type="checkbox" checked>
11+
{% endif %}
12+
13+
<li class="tree-menu__item">
14+
<div class="{{label_class}}">
15+
<a class="tree-menu__link" href="{{ header.permalink | safe }}">{{ header.title }}</a>
16+
{% if show_children %}
17+
<label class="tree-menu__toggle" for="{{id}}">
18+
<img class="tree-menu__chevron" src="/assets/icon-chevron-down.svg">
19+
</label>
20+
{% endif %}
21+
</div>
22+
{% if show_children %}
23+
<ul class="tree-menu">
24+
{% for sub_header in header.children %}
25+
{{ self::news_menu_row(header=sub_header, max_levels=max_levels, level=level + 1)}}
26+
{% endfor %}
27+
</ul>
28+
{% endif %}
29+
</li>
30+
{% endmacro %}
31+
32+
{% macro news_menu(toc, max_levels) %}
33+
{% if toc %}
34+
<ul class="tree-menu">
35+
{% for header in toc %}
36+
{{ self::news_menu_row(header=header, max_levels=max_levels, level=1)}}
37+
{% endfor %}
38+
</ul>
39+
{% endif %}
40+
{% endmacro %}

templates/news-page.html

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
{% extends "layouts/base.html" %}
1+
{% extends "layouts/page-with-menu.html" %}
2+
{% import "macros/news.html" as news_macros %}
23

3-
{% block content %}
4-
<div class="padded-content">
4+
{% block page_with_menu_extra_class %}page-with-menu--news{% endblock %}
5+
6+
{% block page_name %}Post{% endblock %}
7+
8+
{% block mobile_page_menu %}
9+
{{news_macros::news_menu(toc=page.toc, max_levels=1)}}
10+
{% endblock %}
11+
12+
{% block page_menu %}
13+
{{news_macros::news_menu(toc=page.toc, max_levels=2)}}
14+
{% endblock %}
15+
16+
{% block page_content %}
517
<h1 class="news-title">
618
{{ page.title }}
719
</h1>
@@ -44,5 +56,4 @@ <h2 class="news-subtitle">Posted on {{ page.date | date(format="%B %d, %Y") }} b
4456
</div>
4557
{% endif %}
4658
<div class="media-content news-content">{{ page.content | safe }}</div>
47-
</div>
4859
{% endblock content %}

0 commit comments

Comments
 (0)