Skip to content

Commit 343b6a7

Browse files
committed
sticky sidebar, command-b; fix #33, fix #34
1 parent c0b35cf commit 343b6a7

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

public/client.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,22 @@ export function open({hash} = {}) {
186186
socket.send(JSON.stringify(message));
187187
}
188188
}
189+
190+
{
191+
const toggle = document.querySelector("#observablehq-sidebar-toggle");
192+
let indeterminate = toggle.indeterminate;
193+
toggle.onclick = () => {
194+
const matches = matchMedia("(min-width: calc(640px + 4rem + 0.5rem + 240px + 2rem))").matches;
195+
if (indeterminate) (toggle.checked = !matches), (indeterminate = false);
196+
else if (toggle.checked === matches) indeterminate = true;
197+
toggle.indeterminate = indeterminate;
198+
if (indeterminate) localStorage.removeItem("observablehq-sidebar");
199+
else localStorage.setItem("observablehq-sidebar", toggle.checked);
200+
};
201+
addEventListener("keypress", (event) => {
202+
if (event.key === "b" && event.metaKey && !event.ctrlKey) {
203+
toggle.click();
204+
event.preventDefault();
205+
}
206+
});
207+
}

src/render.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,9 @@ ${
9090
</nav>
9191
<script>{
9292
const toggle = document.querySelector("#observablehq-sidebar-toggle");
93-
let indeterminate = toggle.indeterminate = true;
94-
toggle.onclick = () => {
95-
const matches = matchMedia("(min-width: calc(640px + 4rem + 0.5rem + 240px + 2rem))").matches;
96-
if (indeterminate) toggle.checked = !matches, indeterminate = false;
97-
else if (toggle.checked === matches) toggle.indeterminate = indeterminate = true;
98-
};
93+
const initialState = localStorage.getItem("observablehq-sidebar");
94+
if (initialState) toggle.checked = initialState === "true";
95+
else toggle.indeterminate = true;
9996
}</script>
10097
`
10198
: ""

0 commit comments

Comments
 (0)