Skip to content

Commit 6844bc3

Browse files
feat: Remember Sidebar state
1 parent c597013 commit 6844bc3

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

EssentialCSharp.Web/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<header class="header-background">
9696
<div class="banner d-flex justify-content-between">
9797
<div class="d-flex align-items-center menu-position">
98-
<button class="menu-btn has-tooltip" v-on:click="sidebarShown = !sidebarShown">
98+
<button class="menu-btn has-tooltip" v-on:click="toggleSidebar">
9999
<i class="fa fa-bars fa-lg icon-light"></i>
100100
<span class="tooltip-text sidebar-tooltip-text">
101101
<b>Ctrl + M</b>

EssentialCSharp.Web/wwwroot/js/site.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,21 @@ const app = createApp({
179179
}
180180

181181
if (e.code == "KeyM" && e.ctrlKey) {
182-
sidebarShown.value = !sidebarShown.value;
182+
toggleSidebar();
183183
}
184184
});
185185

186-
const sidebarShown = ref(false);
186+
const sidebarShown = ref(localStorage.getItem('sidebarShown'));
187187

188188
const smallScreen = computed(() => {
189189
return (windowWidth.value || 0) < smallScreenSize;
190190
});
191191

192+
function toggleSidebar() {
193+
sidebarShown.value = !sidebarShown.value;
194+
localStorage.setItem('sidebarShown', sidebarShown.value);
195+
}
196+
192197
/** @type {import("vue").Ref<"toc" | "search">} */
193198
const sidebarTab = ref("toc");
194199

@@ -207,13 +212,16 @@ const app = createApp({
207212
watch(windowWidth, (newWidth, oldWidth) => {
208213
//+ 50 so that the side bar diappears before the css media class changes the sidebar to take
209214
// over the full screen
210-
if (newWidth < smallScreenSize) {
211-
sidebarShown.value = false;
212-
}
213-
// when making screen bigger reveal sidebar
214-
else {
215-
if (!sidebarShown.value) {
216-
sidebarShown.value = true;
215+
// If a setting is set in storage already, follow that
216+
if (sidebarShown.value === null) {
217+
if (newWidth < smallScreenSize) {
218+
sidebarShown.value = false;
219+
}
220+
// when making screen bigger reveal sidebar
221+
else {
222+
if (!sidebarShown.value) {
223+
sidebarShown.value = true;
224+
}
217225
}
218226
}
219227
});
@@ -230,8 +238,11 @@ const app = createApp({
230238
});
231239

232240
onMounted(() => {
233-
if (windowWidth.value > smallScreenSize) {
234-
sidebarShown.value = true;
241+
// If a setting is set in storage already, follow that
242+
if (sidebarShown.value === null) {
243+
if (windowWidth.value > smallScreenSize) {
244+
sidebarShown.value = true;
245+
}
235246
}
236247

237248
// Scroll the current selected page in the TOC into view of the TOC.
@@ -304,6 +315,7 @@ const app = createApp({
304315

305316
sidebarShown,
306317
sidebarTab,
318+
toggleSidebar,
307319

308320
smallScreen,
309321

0 commit comments

Comments
 (0)