Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions assets/js/sidebar-resizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

document.addEventListener("DOMContentLoaded", function () {
const resizer = document.getElementById("left-resizer");
const sidebar = document.getElementById("left-sidebar");

if (!resizer || !sidebar) return;
if (window.innerWidth < 768) return;


let isResizing = false;


const savedWidth = localStorage.getItem("leftSidebarWidth");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Directly accessing localStorage can throw a SecurityError in certain browser configurations (e.g., private browsing mode, blocked cookies, or specific security policies). It is recommended to wrap these calls in a try...catch block or check for availability to prevent the script from crashing.

if (savedWidth) {
sidebar.style.width = savedWidth;
sidebar.style.flex = "none";
sidebar.style.maxWidth = "35vw";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Hardcoding maxWidth = "35vw" in JavaScript is redundant because it is already defined in the SCSS file (_styles_project.scss). Removing this inline style ensures that the layout constraints are managed in a single place (CSS), making the code easier to maintain.

}


resizer.addEventListener("mousedown", function (e) {
isResizing = true;
document.body.style.cursor = "col-resize";
document.body.style.userSelect = "none";
});


document.addEventListener("mousemove", function (e) {
if (!isResizing) return;


const newWidth = e.clientX + "px";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The width calculation using e.clientX assumes the sidebar starts at the left edge of the viewport (x=0). However, on larger screens, the layout is centered with margin: auto, meaning the sidebar has a left offset. This will cause the sidebar to jump or resize incorrectly when dragging starts. Using the element's bounding rectangle ensures the width is calculated correctly relative to its start position.

Suggested change
const newWidth = e.clientX + "px";
const newWidth = (e.clientX - sidebar.getBoundingClientRect().left) + "px";

sidebar.style.width = newWidth;
sidebar.style.flex = "none"; // Override bootstrap grid flex
});


document.addEventListener("mouseup", function () {
if (isResizing) {
isResizing = false;
document.body.style.cursor = "";
document.body.style.userSelect = "";

// Save the final width
localStorage.setItem("leftSidebarWidth", sidebar.style.width);
}
});


resizer.addEventListener("dblclick", function () {
sidebar.classList.add("is-resetting"); // Add transition class

sidebar.style.width = "";
sidebar.style.flex = "";
localStorage.removeItem("leftSidebarWidth");


setTimeout(() => sidebar.classList.remove("is-resetting"), 300);
});

});

56 changes: 48 additions & 8 deletions assets/scss/_styles_project.scss
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ a:not([href]):not([class]):hover {
// Left sidebar
.td-sidebar-nav {
max-height: none;
overflow: hidden;
padding: 0px;

&__section-title {
Expand Down Expand Up @@ -818,15 +817,56 @@ html {
h1, h2, h3, h4, h5, h6 {
scroll-margin-top: 1rem;
}
/* Sidebar Resizer Handle */
/* Sidebar Resizer Handle */
@include media-breakpoint-up(md)
{
.sidebar-resizer {
width: 10px;
cursor: col-resize;
position: absolute;
top: 0;
right: 8px; /* Push it outside the scrollbar */
bottom: 0;
z-index: 100;
background-color: transparent;

/* Grip icon using a pseudo-element */
&::after {
content: "⋮";
position: sticky;
top: 40vh;
display: block;
text-align: center;
color: var(--secondary);
font-size: 18px;
opacity: 0.9;
}

@media (max-width: 767px) {
.td-navbar {
top: 0;
margin-top: 0;
padding-top: 0;
&:hover::after, &:active::after {
opacity: 1;
}
}
#left-sidebar {
min-width: 20vw;
max-width: 35vw;
}
#left-sidebar.is-resetting {
transition: width 0.3s ease, max-width 0.3s ease;
}

body {
padding-top: 0;

}
@include media-breakpoint-down(sm) {
#left-sidebar {
/* Force mobile width and override any JS inline styles */
width: 100% !important;
max-width: 100% !important;
flex: 100% !important;
}
}





11 changes: 10 additions & 1 deletion layouts/docs/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
<div class="container-fluid td-outer">
<div class="td-main">
<div class="row flex-xl-nowrap">
<aside class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none">
<aside class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none" id="left-sidebar">
{{ partial "sidebar.html" . }}
<!-- Add the resizer handle here! -->
<div class="sidebar-resizer" id="left-resizer"></div>
</aside>
<aside class="d-none d-xl-block col-xl-2 td-sidebar-toc d-print-none">
{{ partial "page-meta-links.html" . }}
Expand All @@ -38,6 +40,13 @@
</div>
{{ partial "scripts.html" . }}
{{ partial "image-modal.html" . }}

{{ with resources.Get "js/sidebar-resizer.js" }}
<script src="{{ .RelPermalink }}" defer></script>
{{ end }}
</body>

Comment on lines +47 to +48
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

A duplicate tag was accidentally introduced here. The original closing tag is still present on line 49.

</body>


</html>
Loading