Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sidebar: "hidden" #701

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft

sidebar: "hidden" #701

wants to merge 15 commits into from

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Feb 7, 2024

For #696. Note: as I was writing the documentation I thought that "closed" would be a better word. Rather than "hide" and "show" the sidebar, we'd open and close it. wdyt?

@Fil Fil requested a review from mbostock February 7, 2024 11:17
@Fil Fil mentioned this pull request Feb 7, 2024
docs/config.md Outdated Show resolved Hide resolved
Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

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

Lots of fiddly details…

src/client/sidebar-init.ts Outdated Show resolved Hide resolved
src/config.ts Outdated
@@ -107,7 +106,7 @@ export async function normalizeConfig(spec: any = {}, defaultRoot = "docs"): Pro
let {title, pages = await readPages(root), pager = true, toc = true} = spec;
if (title !== undefined) title = String(title);
pages = Array.from(pages, normalizePageOrSection);
sidebar = sidebar === undefined ? pages.length > 0 : Boolean(sidebar);
const {sidebar = "auto"} = spec;
Copy link
Member

Choose a reason for hiding this comment

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

This needs to validate the allowed values of sidebar (either "auto" or "hidden" or boolean). Meaning you should call maybeSidebar here, earlier.

Also, I think we should still default to false if pages.length === 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops yes, that's an oversight!

src/render.ts Outdated
@@ -59,7 +59,7 @@ type RenderInternalOptions =

async function render(parseResult: ParseResult, options: RenderOptions & RenderInternalOptions): Promise<string> {
const {root, path, pages, title, preview} = options;
const sidebar = parseResult.data?.sidebar !== undefined ? Boolean(parseResult.data.sidebar) : options.sidebar;
const sidebar = maybeSidebar(parseResult.data?.sidebar ?? options.sidebar, pages);
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn’t use ?? for optional arguments because it would mean that sidebar: null is ignored. You have to check strictly for undefined.

src/render.ts Outdated Show resolved Hide resolved
src/render.ts Outdated
}

function maybeSidebar(sidebar: string | boolean, pages): "hidden" | boolean {
if (sidebar === "auto") sidebar = pages.length > 0;
Copy link
Member

Choose a reason for hiding this comment

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

At this point sidebar isn’t normalized, so the "auto" check will miss e.g. "AUTO".

src/render.ts Outdated

function maybeSidebar(sidebar: string | boolean, pages): "hidden" | boolean {
if (sidebar === "auto") sidebar = pages.length > 0;
return typeof sidebar === "boolean" ? sidebar : (keyword(sidebar, "sidebar", ["hidden"]) as "hidden");
Copy link
Member

Choose a reason for hiding this comment

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

This will throw an error on sidebar: null, but I think we want to coerce any non-string value to a boolean. Something like:

Suggested change
return typeof sidebar === "boolean" ? sidebar : (keyword(sidebar, "sidebar", ["hidden"]) as "hidden");
return typeof sidebar === "string" ? keyword(sidebar, "sidebar", ["auto", "hidden"]) : Boolean(sidebar);

And I think we can have TypeScript assert that keyword returns one of the passed-in string literals using generics.

Fil and others added 4 commits February 9, 2024 11:31
@Fil
Copy link
Contributor Author

Fil commented Feb 9, 2024

ptal

Base automatically changed from mbostock/sidebar-option to main February 10, 2024 19:48
Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

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

We don’t need the explicit auto option (nor likewise auto-hidden if you want to combine auto and hidden); we can just have the default be true or false as before, and have hidden if you want an initially-hidden sidebar. Simpler! 👍

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

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

Hmm, in further testing, this doesn’t seem to persist the sidebar state in the session: if you visit a page and toggle the sidebar open, and then reload, the sidebar disappears; likewise if you navigate between pages, the sidebar keeps disappearing. I think that’s because we clear the session state if the sidebar would be initially visible (ignoring the hidden setting). In other words, the client needs to check the data-hidden attribute when deciding whether to persist the sidebar state in session storage.

@mbostock mbostock marked this pull request as draft April 9, 2024 21:14
@mbostock
Copy link
Member

mbostock commented Apr 9, 2024

Converting to draft given the needed changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants