Skip to content

Commit 3954f62

Browse files
authored
fix(layout): balance topics and content scrolling (#33)
1 parent 72fc92e commit 3954f62

3 files changed

Lines changed: 41 additions & 15 deletions

File tree

build.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,16 @@ function buildBreadcrumb(pagePath) {
272272
function noteMain(pagePath, prefix, contentHtml) {
273273
return (
274274
`<div class="overlay" id="overlay"></div>\n` +
275-
` <div class="layout">\n` +
275+
` <div class="page-scroll" id="pageScroll">\n` +
276+
` <div class="layout">\n` +
276277
` <aside class="sidebar" id="sidebar" aria-label="Topics">\n` +
277278
`${buildSidebar(pagePath, prefix)}\n` +
278279
` </aside>\n` +
279280
` <main class="content">\n` +
280281
` <article class="article">${contentHtml.trim()}</article>\n` +
281282
` </main>\n` +
282283
`${buildContents(contentHtml)}\n` +
284+
` </div>\n` +
283285
` </div>`
284286
);
285287
}
@@ -302,7 +304,7 @@ function homeMain() {
302304
})
303305
.join("\n");
304306
return (
305-
`<main class="home-main">\n` +
307+
`<main class="home-main" id="pageScroll">\n` +
306308
` <div class="home">\n` +
307309
` <img class="home-logo" src="knowledge_base_logo.png" alt="" width="76" height="76" />\n` +
308310
` <h1>knowledge base</h1>\n` +

src/shell.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,18 @@
214214
if (sidebar) window.requestAnimationFrame(centerActiveSidebarLink);
215215

216216
var toTop = document.getElementById("toTop");
217-
if (toTop) {
217+
var pageScroll = document.getElementById("pageScroll");
218+
if (toTop && pageScroll) {
218219
var onScroll = function () {
219-
var doc = document.documentElement;
220-
var nearBottom = window.innerHeight + window.scrollY >= doc.scrollHeight - 160;
221-
toTop.hidden = !(window.scrollY > 320 && nearBottom);
220+
var nearBottom =
221+
pageScroll.clientHeight + pageScroll.scrollTop >= pageScroll.scrollHeight - 160;
222+
toTop.hidden = !(pageScroll.scrollTop > 320 && nearBottom);
222223
};
223-
window.addEventListener("scroll", onScroll, { passive: true });
224+
pageScroll.addEventListener("scroll", onScroll, { passive: true });
224225
window.addEventListener("resize", onScroll, { passive: true });
225226
onScroll();
226227
toTop.addEventListener("click", function () {
227-
window.scrollTo({ top: 0, behavior: "smooth" });
228+
pageScroll.scrollTo({ top: 0, behavior: "smooth" });
228229
});
229230
}
230231
})();

src/styles.css

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
box-sizing: border-box;
1919
}
2020
html {
21+
height: 100%;
2122
scroll-behavior: smooth;
2223
overscroll-behavior-y: none;
2324
}
2425

2526
body {
27+
display: flex;
28+
flex-direction: column;
29+
height: 100vh;
2630
margin: 0;
31+
overflow: hidden;
2732
font-family: var(--sans);
2833
font-size: 17px;
2934
line-height: 1.6;
@@ -42,6 +47,7 @@ a {
4247
.site-header {
4348
position: sticky;
4449
top: 0;
50+
flex: none;
4551
z-index: 40;
4652
height: var(--header-h);
4753
background: #fff;
@@ -158,17 +164,26 @@ a {
158164
height: 18px;
159165
}
160166

167+
.page-scroll {
168+
flex: 1;
169+
min-height: 0;
170+
overflow-y: auto;
171+
overscroll-behavior-y: contain;
172+
scrollbar-gutter: stable;
173+
}
174+
161175
.layout {
162176
display: grid;
163177
grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--toc-w);
178+
min-height: 100%;
164179
max-width: 1320px;
165180
margin: 0 auto;
166181
align-items: start;
167182
}
168183

169184
.sidebar {
170185
position: sticky;
171-
top: var(--header-h);
186+
top: 0;
172187
height: calc(100vh - var(--header-h));
173188
overflow-y: auto;
174189
padding: 24px 14px 48px 20px;
@@ -282,7 +297,7 @@ a {
282297

283298
.toc {
284299
position: sticky;
285-
top: calc(var(--header-h) + 28px);
300+
top: 28px;
286301
max-height: calc(100vh - var(--header-h) - 56px);
287302
overflow-y: auto;
288303
margin: 28px 20px 0 0;
@@ -378,12 +393,12 @@ h2 {
378393
margin: 48px 0 4px;
379394
padding-top: 20px;
380395
border-top: 1px solid var(--border);
381-
scroll-margin-top: calc(var(--header-h) + 12px);
396+
scroll-margin-top: 12px;
382397
}
383398
h3 {
384399
font-size: 19px;
385400
margin: 32px 0 4px;
386-
scroll-margin-top: calc(var(--header-h) + 12px);
401+
scroll-margin-top: 12px;
387402
}
388403
h4 {
389404
font-size: 16px;
@@ -638,9 +653,14 @@ footer.doc-footer {
638653
}
639654

640655
.home-main {
641-
max-width: 920px;
656+
flex: 1;
657+
min-height: 0;
658+
width: 100%;
642659
margin: 0 auto;
643660
padding: 56px 24px 96px;
661+
overflow-y: auto;
662+
overscroll-behavior-y: contain;
663+
scrollbar-gutter: stable;
644664
}
645665
.home {
646666
max-width: 820px;
@@ -664,8 +684,8 @@ footer.doc-footer {
664684
}
665685
.topic-grid {
666686
display: grid;
667-
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
668-
align-items: start;
687+
grid-template-columns: repeat(2, minmax(0, 1fr));
688+
align-items: stretch;
669689
gap: 16px;
670690
text-align: left;
671691
}
@@ -855,6 +875,9 @@ footer.doc-footer {
855875
.content {
856876
padding: 20px 16px;
857877
}
878+
.topic-grid {
879+
grid-template-columns: minmax(0, 1fr);
880+
}
858881
.toc {
859882
margin: 14px 16px 2px;
860883
}

0 commit comments

Comments
 (0)