Skip to content

Commit 75536a9

Browse files
committed
WIP: Update
Signed-off-by: Khusika Dhamar Gusti <[email protected]>
1 parent e9a9c6d commit 75536a9

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

assets/css/_page/_single.scss

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
@import "../_partial/_single/toc";
2+
@import '../_variables'; // Import theme variables to resolve the error.
23

34
/* Add styles for the new Related Posts panel, matching the TOC */
45
.related {
56
position: absolute;
67
padding: .5rem .5rem .5rem 1rem;
7-
width: 240px;
8-
visibility: hidden;
8+
width: clamp(180px, 15vw, 280px);
99
border-left: 3px solid $global-link-color;
1010

1111
[theme=dark] & {
@@ -58,6 +58,24 @@
5858
}
5959
}
6060

61+
.page-aside-left {
62+
// Hide by default on all screens
63+
display: none;
64+
}
65+
66+
// This new media query controls the layout for screens 1200px and wider.
67+
@media (min-width: 1200px) {
68+
.page-aside-left {
69+
// Show the panel on wide screens
70+
display: block;
71+
position: absolute;
72+
// Symmetrically position the panel to the left of the main content.
73+
// This calculation is robust and uses the globally available CSS variable.
74+
left: calc((100vw - var(--single-max-width)) / 2 - 280px - 1rem);
75+
width: 280px;
76+
}
77+
}
78+
6179
/* Style for the inline "details" version to match the inline TOC */
6280
.details.related {
6381
.details-summary {

assets/js/theme.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -394,46 +394,42 @@ class Theme {
394394
}
395395

396396
initRelated() {
397-
const $relatedStatic = document.getElementById('related-static');
398-
if (!$relatedStatic) return;
399-
400-
if (this.util.isTocStatic() || $relatedStatic.getAttribute('data-kept') === 'true') {
401-
if (this._relatedOnScroll) this.scrollEventSet.delete(this._relatedOnScroll);
402-
return;
403-
}
404-
405397
const $relatedAuto = document.getElementById('related-auto');
406-
const $relatedContent = document.getElementById('related-content-static');
407-
408-
if ($relatedContent && $relatedAuto) {
409-
$relatedAuto.querySelector('.related-content').appendChild($relatedContent);
410-
} else {
411-
return;
398+
if (!$relatedAuto) return;
399+
400+
const $relatedContentStatic = document.getElementById('related-content-static');
401+
if ($relatedContentStatic) {
402+
$relatedAuto.querySelector('.related-content').appendChild($relatedContentStatic);
412403
}
413404

414405
const $toc = document.getElementById('toc-auto');
415-
416-
if ($toc && getComputedStyle($toc).visibility !== 'hidden' && getComputedStyle($toc).display !== 'none') {
417-
// Sychronize position with the TOC
418-
const tocRect = $toc.getBoundingClientRect();
419-
$relatedAuto.style.top = `${window.scrollY + tocRect.top}px`;
420-
$relatedAuto.style.left = `20px`;
421-
$relatedAuto.style.visibility = 'visible';
422-
} else {
423-
$relatedAuto.style.visibility = 'hidden';
424-
return;
425-
}
426-
427-
const headerIsFixed = document.body.getAttribute('data-header-desktop') !== 'normal';
428-
const headerHeight = document.getElementById('header-desktop').offsetHeight;
429-
const TOP_SPACING = 20 + (headerIsFixed ? headerHeight : 0);
430-
const minRelatedTop = $relatedAuto.offsetTop;
431-
const minScrollTop = minRelatedTop - TOP_SPACING + (headerIsFixed ? 0 : headerHeight);
406+
if (!$toc) return;
432407

408+
// This function ONLY handles vertical scrolling adjustments.
433409
this._relatedOnScroll = this._relatedOnScroll || (() => {
410+
// If the panel's container is hidden by CSS, do nothing.
411+
if (getComputedStyle($relatedAuto.parentElement).display === 'none') {
412+
return;
413+
}
414+
415+
const headerIsFixed = document.body.getAttribute('data-header-desktop') !== 'normal';
416+
const headerHeight = document.getElementById('header-desktop').offsetHeight;
417+
const TOP_SPACING = 20 + (headerIsFixed ? headerHeight : 0);
418+
419+
// Use the TOC's position as the reliable source of truth for vertical alignment.
420+
const minRelatedTop = $toc.offsetTop;
421+
const minScrollTop = minRelatedTop - TOP_SPACING;
422+
434423
const footerTop = document.getElementById('post-footer').offsetTop;
435424
const maxRelatedTop = footerTop - $relatedAuto.getBoundingClientRect().height;
436-
const maxScrollTop = maxRelatedTop - TOP_SPACING + (headerIsFixed ? 0 : headerHeight);
425+
const maxScrollTop = maxRelatedTop - TOP_SPACING;
426+
427+
// Set initial position before scrolling begins
428+
if ($relatedAuto.style.position !== 'fixed') {
429+
$relatedAuto.style.position = 'absolute';
430+
$relatedAuto.style.top = `${minRelatedTop}px`;
431+
}
432+
437433
if (this.newScrollTop < minScrollTop) {
438434
$relatedAuto.style.position = 'absolute';
439435
$relatedAuto.style.top = `${minRelatedTop}px`;
@@ -445,8 +441,12 @@ class Theme {
445441
$relatedAuto.style.top = `${TOP_SPACING}px`;
446442
}
447443
});
444+
445+
// Run once on load and add to the scroll listener.
448446
this._relatedOnScroll();
449447
this.scrollEventSet.add(this._relatedOnScroll);
448+
// Also run on resize to catch layout shifts.
449+
this.resizeEventSet.add(this._relatedOnScroll);
450450
}
451451

452452
initToc() {

layouts/posts/single.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
{{- /* Auto Related (Left Panel) - Only show if related pages exist */ -}}
2323
{{- if and (ne $related.enable false) (gt (len $relatedPages) 0) -}}
24-
<div class="related" id="related-auto">
25-
<h2 class="related-title">{{ T "related" | default "Related" }}</h2>
26-
<div class="related-content{{ if eq $related.auto false }} always-active{{ end }}" id="related-content-auto"></div>
24+
<div class="page-aside-left">
25+
<div class="related" id="related-auto">
26+
<h2 class="related-title">{{ T "related" | default "Related" }}</h2>
27+
<div class="related-content{{ if eq $related.auto false }} always-active{{ end }}" id="related-content-auto"></div>
28+
</div>
2729
</div>
2830
{{- end -}}
2931

0 commit comments

Comments
 (0)