Skip to content

Commit

Permalink
sidebar toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Oct 14, 2023
1 parent fe8a2f0 commit 6992af5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
39 changes: 35 additions & 4 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ main {
}

#observablehq-sidebar {
display: none;
position: fixed;
background: var(--theme-background-alt);
color: var(--theme-foreground);
Expand All @@ -78,7 +77,7 @@ main {
z-index: 1;
top: 0;
bottom: 0;
left: 0;
left: calc(-240px - 2rem);
padding: 1rem;
}

Expand All @@ -92,6 +91,26 @@ main {
margin: 0;
}

#observablehq-sidebar-toggle {
position: fixed;
top: 0;
bottom: 0;
left: 0;
appearance: none;
display: flex;
align-items: center;
margin: 0;
cursor: pointer;
z-index: 2;
}

#observablehq-sidebar-toggle::before {
content: "≡";
padding: 2px;
color: var(--theme-foreground-faint);
font-size: 20px;
}

.observablehq-link a {
display: block;
padding: 0.5rem 1rem;
Expand Down Expand Up @@ -123,11 +142,23 @@ main {
color: var(--theme-foreground-focus);
}

#observablehq-sidebar-toggle:checked + #observablehq-sidebar {
left: 0;
box-shadow: 0 0 8px 8px rgba(0, 0, 0, 0.5);
}

#observablehq-sidebar {
transition: left 150ms ease;
}

@media (min-width: calc(640px + 4rem + 0.5rem + 240px + 2rem)) {
#observablehq-sidebar {
display: initial;
transition: none;
}
#observablehq-sidebar-toggle:checked + #observablehq-sidebar {
box-shadow: none;
}
#observablehq-center.observablehq--sidebar {
#observablehq-sidebar-toggle:checked ~ #observablehq-center {
padding-left: calc(240px + 3rem);
}
pre {
Expand Down
6 changes: 4 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ ${JSON.stringify(parseResult.data)}
}
${
showSidebar
? `<nav id="observablehq-sidebar">
? `<input id="observablehq-sidebar-toggle" type="checkbox">
<nav id="observablehq-sidebar">
<ol>${pages
?.map(
(p) => `
Expand All @@ -96,9 +97,10 @@ ${
.join("")}
</ol>
</nav>
<script>document.querySelector("#observablehq-sidebar-toggle").checked = matchMedia("(min-width: calc(640px + 4rem + 0.5rem + 240px + 2rem))").matches;</script>
`
: ""
}<div id="observablehq-center"${showSidebar ? ` class="observablehq--sidebar"` : ""}>
}<div id="observablehq-center">
<main class="observablehq">
${parseResult.html}</main>
</div>
Expand Down

0 comments on commit 6992af5

Please sign in to comment.