Skip to content

Commit aae36ca

Browse files
committed
feat: add configurable sidebar width in settings
1 parent 149a99b commit aae36ca

5 files changed

Lines changed: 37 additions & 10 deletions

File tree

src/components/sidebar/index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "./style.scss";
22
import toast from "components/toast";
33
import Ref from "html-tag-js/ref";
44
import actionStack from "lib/actionStack";
5+
import appSettings from "lib/settings";
56
import auth, { loginEvents } from "lib/auth";
67
import constants from "lib/constants";
78

@@ -33,14 +34,14 @@ function create($container, $toggler) {
3334

3435
const START_THRESHOLD = constants.SIDEBAR_SLIDE_START_THRESHOLD_PX; //Point where to start swipe
3536
const MIN_WIDTH = 200; //Min width of the side bar
36-
const MAX_WIDTH = () => innerWidth * 0.7; //Max width of the side bar
37+
const MAX_WIDTH = () => innerWidth - 40; //Max width of the side bar
3738
const resizeBar = Ref();
3839
const userAvatar = Ref();
3940
const userContextMenu = Ref();
4041

4142
$container = $container || app;
4243
let mode = innerWidth > 600 ? "tab" : "phone";
43-
let width = +(localStorage.sideBarWidth || MIN_WIDTH);
44+
let width = Math.min(appSettings.value.sidebarWidth || MIN_WIDTH, MAX_WIDTH());
4445

4546
const eventOptions = { passive: false };
4647
const $el = (
@@ -92,6 +93,11 @@ function create($container, $toggler) {
9293
$toggler?.addEventListener("click", toggle);
9394
$container.addEventListener("touchstart", ontouchstart, eventOptions);
9495
window.addEventListener("resize", onWindowResize);
96+
97+
appSettings.on("update:sidebarWidth", () => {
98+
width = Math.min(appSettings.value.sidebarWidth || MIN_WIDTH, MAX_WIDTH());
99+
setWidth(width);
100+
});
95101

96102
if (mode === "tab" && localStorage.sidebarShown === "1") {
97103
show();
@@ -226,6 +232,8 @@ function create($container, $toggler) {
226232
localStorage.sidebarShown = 1;
227233
$el.activated = true;
228234
$el.onclick = null;
235+
236+
setWidth(width);
229237

230238
if (mode === "phone") {
231239
resizeBar.style.display = "none";
@@ -239,7 +247,6 @@ function create($container, $toggler) {
239247
action: hideMaster,
240248
});
241249
} else {
242-
setWidth(width);
243250
resizeBar.style.display = "block";
244251
app.append($el);
245252
$el.onclick = () => {
@@ -349,7 +356,8 @@ function create($container, $toggler) {
349356
if (newWidth <= MIN_WIDTH) width = MIN_WIDTH;
350357
else if (newWidth >= MAX_WIDTH()) width = MAX_WIDTH();
351358
else width = newWidth;
352-
localStorage.sideBarWidth = width;
359+
appSettings.value.sidebarWidth = width;
360+
appSettings.update(false);
353361
document.removeEventListener("touchmove", onMove, eventOptions);
354362
document.removeEventListener("mousemove", onMove, eventOptions);
355363
document.removeEventListener("touchend", onEnd, eventOptions);
@@ -468,9 +476,12 @@ function create($container, $toggler) {
468476
*/
469477
function setWidth(width) {
470478
$el.style.transition = "none";
479+
$el.style.width = width + "px";
471480
$el.style.maxWidth = width + "px";
472-
root.style.marginLeft = width + "px";
473-
root.style.width = `calc(100% - ${width}px)`;
481+
if (mode === "tab") {
482+
root.style.marginLeft = width + "px";
483+
root.style.width = `calc(100% - ${width}px)`;
484+
}
474485
clearTimeout(setWidthTimeout);
475486
setWidthTimeout = setTimeout(() => {
476487
editorManager?.editor?.resize(true);
@@ -492,8 +503,8 @@ function create($container, $toggler) {
492503
$el.toggle = toggle;
493504
$el.onshow = () => {};
494505
$el.getWidth = function () {
495-
const width = innerWidth * 0.7;
496-
return mode === "phone" ? (width >= 350 ? 350 : width) : MIN_WIDTH;
506+
const configuredWidth = appSettings.value.sidebarWidth || MIN_WIDTH;
507+
return mode === "phone" ? Math.min(configuredWidth, MAX_WIDTH()) : width;
497508
};
498509

499510
return $el;

src/components/sidebar/style.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ body.no-animation {
1313
position: fixed;
1414
left: 0;
1515
top: 0;
16-
width: 70vw;
17-
max-width: 350px;
16+
width: 280px;
1817
height: 100vh;
1918
display: flex;
2019
background-color: rgb(153, 153, 255);

src/lang/en-us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@
494494
"change theme": "Change Theme",
495495
"documentation": "Documentation",
496496
"open in terminal": "Open in Terminal",
497+
"sidebar width": "Sidebar width",
497498
"developer mode": "Developer Mode",
498499
"info-developermode": "Enable developer tools (Eruda) for debugging plugins and inspecting app state. Inspector will be initialized on app start.",
499500
"developer mode enabled": "Developer mode enabled. Use command palette to toggle inspector (Ctrl+Shift+I).",

src/lib/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class Settings {
175175
colorPreview: true,
176176
maxRetryCount: 3,
177177
showRetryToast: false,
178+
sidebarWidth: 280,
178179
showSideButtons: true,
179180
showSponsorSidebarApp: true,
180181
showAnnotations: false,

src/settings/appSettings.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ export default function otherSettings() {
8787
info: strings["settings-info-app-floating-button"],
8888
category: categories.interface,
8989
},
90+
{
91+
key: "sidebarWidth",
92+
text: strings["sidebar width"] || "Sidebar width",
93+
value: values.sidebarWidth,
94+
prompt: strings["sidebar width"] || "Sidebar width (pixels)",
95+
promptType: "number",
96+
promptOptions: {
97+
test(value) {
98+
value = Number.parseInt(value);
99+
return value >= 200 && value <= 1500;
100+
},
101+
},
102+
info: "Adjust the width of the sidebar (min 200px).",
103+
category: categories.interface,
104+
},
90105
{
91106
key: "showSideButtons",
92107
text: strings["show side buttons"],

0 commit comments

Comments
 (0)