Skip to content

Commit

Permalink
feat: add mobile view toc
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfux committed Sep 26, 2024
1 parent e6b87d2 commit 78e5ce0
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions _includes/overlay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="overlay"></div>
6 changes: 6 additions & 0 deletions _includes/topbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<i class="fas fa-search fa-fw"></i>
</button>

{% if page.layout == 'post' %}
<button type="button" id="overlay-trigger" class="btn btn-link">
<i class="fas fa-ellipsis-v fa-fw"></i>
</button>
{% endif %}

<search id="search" class="align-items-center ms-3 ms-lg-0">
<i class="fas fa-search fa-fw"></i>
<input
Expand Down
25 changes: 25 additions & 0 deletions _javascript/modules/components/overlay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function showOverlay() {
document.getElementById('overlay').style.display = 'block';
}

function hideOverlay() {
document.getElementById('overlay').style.display = 'none';
}

const overlay = document.getElementById('overlay');
const overlayTrigger = document.getElementById('overlay-trigger');

overlay.addEventListener('click', () => {
hideOverlay();
});

overlayTrigger.addEventListener('click', () => {
const toc = document.getElementById('toc-wrapper').innerHTML;
while (overlay.firstChild) {
overlay.removeChild(overlay.firstChild);
}
overlay.insertAdjacentHTML('afterbegin', toc);
showOverlay();
});

export { showOverlay, hideOverlay };
2 changes: 2 additions & 0 deletions _javascript/modules/components/toc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { showOverlay } from './overlay';

export function toc() {
if (document.querySelector('main h2, main h3')) {
// see: https://github.com/tscanlin/tocbot#usage
Expand Down
3 changes: 2 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
layout: compress
---

<!doctype html>

{% include origin-type.html %}
Expand Down Expand Up @@ -74,6 +73,8 @@
{% include_cached notification.html lang=lang %}
{% endif %}

{% include overlay.html %}

<!-- JavaScripts -->
{% include js-selector.html lang=lang %}

Expand Down
21 changes: 19 additions & 2 deletions _sass/addon/commons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,19 @@ $btn-mb: 0.5rem;
@include placeholder-focus;
}

#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.9);
z-index: 2;
cursor: pointer;
}

search {
display: flex;
width: 100%;
Expand All @@ -988,7 +1001,8 @@ search {
}

#sidebar-trigger,
#search-trigger {
#search-trigger,
#overlay-trigger {
display: none;
}

Expand Down Expand Up @@ -1320,6 +1334,8 @@ search {
#topbar-wrapper {
@include slide(top 0.2s ease);

position: sticky;
top: 0;
left: 0;
}

Expand All @@ -1330,7 +1346,8 @@ search {

#topbar-title,
#sidebar-trigger,
#search-trigger {
#search-trigger,
#overlay-trigger {
display: block;
}

Expand Down
2 changes: 1 addition & 1 deletion _sass/colors/typography-dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
--avatar-border-color: rgb(206, 206, 206, 0.9);

/* Topbar */
--topbar-bg: rgb(27, 27, 30, 0.64);
--topbar-bg: rgb(27, 27, 30, 1);
--topbar-text-color: var(--text-color);
--search-border-color: rgb(55, 55, 55);
--search-icon-color: rgb(100, 102, 105);
Expand Down
2 changes: 1 addition & 1 deletion _sass/colors/typography-light.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
--avatar-border-color: white;

/* Topbar */
--topbar-bg: rgb(255, 255, 255, 0.7);
--topbar-bg: rgb(255, 255, 255, 1);
--topbar-text-color: rgb(78, 78, 78);
--search-border-color: rgb(240, 240, 240);
--search-icon-color: #c2c6cc;
Expand Down

0 comments on commit 78e5ce0

Please sign in to comment.