3 files changed, +155, -48
655fea6Show a tab menu in the header when the tabs don't fit
author
mogi <git@paramogi.world> on 30-11-2026 03:01 UTC
committer
mogi <git@paramogi.world> on 30-11-2026 03:01 UTC
parentd9fffc0
src/html.rs
+116, -36
style.css
+20, -12
+116, -36
      @@ -33,11 +33,20 @@ const MARKDOWN_BOX: &str = "box markdown-body";
 33 33 
 34 34 pub const ARCHIVE_FORMATS: [&str; 2] = ["zip", "tar.gz"];
 35 35 
 36   -const REPO_FILES: [(&str, &str, [&str; 2]); 2] = [
 37   -    ("README", "book", README_NAMES),
 38   -    ("LICENSE", "law", ["LICENSE", "COPYING"]),
    36+const REPO_FILES: [(&str, &str, f32, [&str; 2]); 2] = [
    37+    ("README", "book", 3.717, README_NAMES),
    38+    ("LICENSE", "law", 3.874, ["LICENSE", "COPYING"]),
 39 39 ];
 40 40 
    41+const NAV_CSS: &str = include_str!("nav.css");
    42+const TAB_GAP: f32 = 1.1;
    43+const COUNT_GAP: f32 = 0.2;
    44+const DIGIT_WIDTH: f32 = 0.4875;
    45+const DOC_ICON: f32 = 1.0;
    46+const DOC_GAP: f32 = 1.0;
    47+const PAGE_MARGINS: f32 = 2.0;
    48+const SCROLLBAR: f32 = 1.0;
    49+
 41 50 const IMAGE_TYPES: [(&str, &str); 6] = [
 42 51     ("png", "image/png"),
 43 52     ("jpg", "image/jpeg"),
      @@ -119,6 +128,17 @@ impl Tab {
119128         }
120129     }
121130 
   131+    fn width(self) -> f32 {
   132+        match self {
   133+            Tab::Summary => 7.752,
   134+            Tab::Tree => 3.657,
   135+            Tab::Commits => 7.227,
   136+            Tab::Branches => 7.682,
   137+            Tab::Tags => 3.849,
   138+            Tab::Releases => 7.348,
   139+        }
   140+    }
   141+
122142     fn main_class(self) -> &'static str {
123143         match self {
124144             Tab::Summary => "",
      @@ -236,7 +256,15 @@ fn repo_file<'a>(dir: &'a Dir, names: &[&str]) -> Option<&'a TreeEntry> {
236256         .find_map(|name| dir.entry(name).filter(|e| !e.is_dir))
237257 }
238258 
239   -fn file_links(nav: &RepoNav, repo_root: &str) -> Vec<(bool, String)> {
   259+struct DocLink {
   260+    current: &'static str,
   261+    href: String,
   262+    label: &'static str,
   263+    icon: &'static str,
   264+    width: f32,
   265+}
   266+
   267+fn doc_links(nav: &RepoNav, repo_root: &str) -> Vec<DocLink> {
240268     let Some(git_ref) = find_ref(nav.repo, nav.git_ref) else {
241269         return Vec::new();
242270     };
      @@ -245,23 +273,20 @@ fn file_links(nav: &RepoNav, repo_root: &str) -> Vec<(bool, String)> {
245273     };
246274     REPO_FILES
247275         .iter()
248   -        .filter_map(|(label, icon_name, names)| {
249   -            let path = follow_links(git_ref, &repo_file(root, names)?.name)?;
   276+        .filter_map(|&(label, icon, width, names)| {
   277+            let path = follow_links(git_ref, &repo_file(root, &names)?.name)?;
250278             let href = path_href(git_ref, repo_root, &path)?;
251   -            let current = path == nav.path;
252   -            Some((
253   -                current,
254   -                format!(
255   -                    "<a class=\"doc-link\"{} href=\"{}\">{}{label}</a>",
256   -                    if current {
257   -                        " aria-current=\"page\""
258   -                    } else {
259   -                        ""
260   -                    },
261   -                    escape_str(&href),
262   -                    icon(icon_name)
263   -                ),
264   -            ))
   279+            Some(DocLink {
   280+                current: if path == nav.path {
   281+                    " aria-current=\"page\""
   282+                } else {
   283+                    ""
   284+                },
   285+                href: escape_str(&href),
   286+                label,
   287+                icon,
   288+                width,
   289+            })
265290         })
266291         .collect()
267292 }
      @@ -347,6 +372,7 @@ fn wrap_page(title: &str, nav: Option<&RepoNav>, body: &str) -> String {
347372         None => (String::new(), title.to_string()),
348373     };
349374     let mut header = String::from("<header class=\"topbar\">\n<div id=\"header\">\n");
   375+    let mut menu_style = String::new();
350376     let (main_class, footer) = match nav {
351377         Some(nav) => {
352378             let repo_root = "../".repeat(nav.depth);
      @@ -354,14 +380,16 @@ fn wrap_page(title: &str, nav: Option<&RepoNav>, body: &str) -> String {
354380             header.push_str(&format!(
355381                 "\
356382 <a class=\"brand\" href=\"{root}\">{BRAND}</a>\n\
357   -<h1 id=\"site_path\"><a class=\"truncate\" href=\"{}\" title=\"{name}\">{name}</a></h1>\n\
358   -</div>\n\
359   -<nav class=\"repo-tabs\" aria-label=\"repository\">\n\
   383+<h1 id=\"site_path\"><a href=\"{}\" title=\"{name}\">{}</a></h1>\n\
360384 ",
361   -                here(repo_root.clone())
   385+                here(repo_root.clone()),
   386+                fit_name(&name)
362387             ));
363   -            let files = file_links(nav, &repo_root);
364   -            let on_file = files.iter().any(|(current, _)| *current);
   388+            let docs = doc_links(nav, &repo_root);
   389+            let on_file = docs.iter().any(|doc| !doc.current.is_empty());
   390+            let mut tabs = String::new();
   391+            let mut items = String::new();
   392+            let mut width = -TAB_GAP;
365393             for tab in repo_tabs(nav.repo) {
366394                 let current = match (tab == nav.tab, on_file) {
367395                     (false, _) => "",
      @@ -376,26 +404,72 @@ fn wrap_page(title: &str, nav: Option<&RepoNav>, body: &str) -> String {
376404                     _ => None,
377405                 };
378406                 let label = tab.label();
379   -                header.push_str(&format!(
380   -                    "<a class=\"tab\"{current} href=\"{}\"><span class=\"tab-label\" data-label=\"{label}\">{label}</span>",
381   -                    here(format!("{repo_root}{}", tab.href(nav.git_ref)))
   407+                let href = here(format!("{repo_root}{}", tab.href(nav.git_ref)));
   408+                tabs.push_str(&format!(
   409+                    "<a class=\"tab\"{current} href=\"{href}\"><span class=\"tab-label\" data-label=\"{label}\">{label}</span>"
382410                 ));
   411+                items.push_str(&format!("<a href=\"{href}\"{current}><span>{label}</span>"));
   412+                width += TAB_GAP + tab.width();
383413                 if let Some(count) = count {
384   -                    header.push_str(&format!(
   414+                    tabs.push_str(&format!(
385415                         "<span class=\"tab-count\"><span class=\"vh\">, </span>{count}</span>"
386416                     ));
   417+                    items.push_str(&format!("<span>{count}</span>"));
   418+                    width += COUNT_GAP + DIGIT_WIDTH * count.to_string().len() as f32;
   419+                }
   420+                tabs.push_str("</a>\n");
   421+                items.push_str("</a>\n");
   422+            }
   423+            header.push_str(&format!(
   424+                "\
   425+<span class=\"nav-only\">/</span>\n\
   426+<button type=\"button\" class=\"nav-btn\" popovertarget=\"nav-menu\">{}{}</button>\n\
   427+<div class=\"ref-menu-list nav-menu\" id=\"nav-menu\" popover>\n{items}</div>\n\
   428+",
   429+                nav.tab.label(),
   430+                icon("chevron-down")
   431+            ));
   432+            if !docs.is_empty() {
   433+                header.push_str(&format!(
   434+                    "<nav class=\"nav-icons docs-{}\" aria-label=\"documents\">\n",
   435+                    docs.len()
   436+                ));
   437+                for doc in &docs {
   438+                    let DocLink {
   439+                        current,
   440+                        href,
   441+                        label,
   442+                        ..
   443+                    } = doc;
   444+                    header.push_str(&format!(
   445+                        "<a{current} href=\"{href}\" aria-label=\"{label}\" title=\"{label}\">{}<span>{label}</span></a>\n",
   446+                        icon(doc.icon)
   447+                    ));
387448                 }
388   -                header.push_str("</a>\n");
   449+                header.push_str("</nav>\n");
389450             }
390   -            header.push_str("</nav>\n");
391   -            if !files.is_empty() {
   451+            header.push_str(&format!(
   452+                "</div>\n<nav class=\"repo-tabs\" aria-label=\"repository\">\n{tabs}</nav>\n"
   453+            ));
   454+            if !docs.is_empty() {
392455                 header.push_str("<nav class=\"repo-docs\" aria-label=\"documents\">\n");
393   -                for (_, link) in &files {
394   -                    header.push_str(link);
395   -                    header.push('\n');
   456+                width -= DOC_GAP;
   457+                for doc in &docs {
   458+                    let DocLink {
   459+                        current,
   460+                        href,
   461+                        label,
   462+                        ..
   463+                    } = doc;
   464+                    header.push_str(&format!(
   465+                        "<a class=\"doc-link\"{current} href=\"{href}\">{}{label}</a>\n",
   466+                        icon(doc.icon)
   467+                    ));
   468+                    width += DOC_GAP + DOC_ICON + doc.width;
396469                 }
397470                 header.push_str("</nav>\n");
398471             }
   472+            menu_style = nav_style(width);
399473             (
400474                 nav.tab.main_class(),
401475                 "<footer class=\"site-footer\">powered by <a href=\"https://git.paramogi.world/wit/\">wit</a></footer>\n",
      @@ -424,6 +498,7 @@ fn wrap_page(title: &str, nav: Option<&RepoNav>, body: &str) -> String {
424498 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\
425499 <title>{}</title>\n\
426500 <link rel=\"stylesheet\" href=\"{root}style.css\">\n\
   501+{menu_style}\
427502 <link rel=\"icon\" href=\"{root}favicon.ico\">\n\
428503 </head>\n\
429504 <body>\n\
      @@ -440,6 +515,11 @@ fn wrap_page(title: &str, nav: Option<&RepoNav>, body: &str) -> String {
440515     )
441516 }
442517 
   518+fn nav_style(width: f32) -> String {
   519+    let below = ((width + PAGE_MARGINS + SCROLLBAR) * 100.0).ceil() / 100.0;
   520+    format!("<style media=\"(width < {below}rem)\">\n{NAV_CSS}</style>\n")
   521+}
   522+
443523 fn description(repo: &RepoInfo) -> (&'static str, String) {
444524     if repo.description.is_empty() {
445525         (" empty", NO_DESCRIPTION.to_string())
+19, -0
    @@ -0,0 +1,19 @@
   1+.topbar:has(> .repo-tabs) { padding-top: 1rem; margin-bottom: 1rem; }
   2+.topbar:has(> .repo-tabs) > :is(.repo-tabs, .repo-docs) { display: none; }
   3+.topbar #header { flex-wrap: nowrap; align-items: center; height: 2.1rem; }
   4+.topbar #site_path { flex: 0 1 auto; min-width: 0; align-items: center; }
   5+.topbar #site_path > a { display: flex; }
   6+.topbar:has(> .repo-tabs) :is(#site_path, a.brand) { font-size: var(--text-base); }
   7+.topbar:has(> .repo-tabs) #site_path::before, .topbar:has(> .repo-tabs) .nav-only { content: "/"; display: block; flex-shrink: 0; margin: 0 0.25rem; font-family: var(--font-ui); font-size: var(--text-base); font-weight: 300; font-style: normal; font-variation-settings: "MONO" 0; letter-spacing: 0; line-height: 1.5; color: var(--muted); }
   8+.nav-btn { display: flex; flex-shrink: 0; align-items: center; box-sizing: border-box; height: 2.1rem; gap: 0.25rem; padding: 0 0.35rem; font: inherit; font-size: var(--text-base); color: var(--fg); border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); cursor: pointer; }
   9+.nav-menu.ref-menu-list { min-width: 13rem; position-area: bottom span-left; }
  10+.nav-menu.ref-menu-list a { display: flex; gap: 1.5rem; }
  11+.nav-menu a > span:last-child:not(:first-child) { margin-left: auto; font-family: var(--font-mono); font-variation-settings: "MONO" 1; color: var(--muted); }
  12+.nav-icons { display: flex; flex: 1 1 0; justify-content: flex-end; column-gap: 0.45rem; min-width: 2.2rem; padding-left: 0.65rem; container-type: inline-size; }
  13+.nav-icons.docs-2 { min-width: 4.15rem; }
  14+.nav-icons a { display: flex; flex: none; align-items: center; box-sizing: border-box; height: 2.1rem; gap: 0.25rem; padding: 0 0.1rem; font-size: var(--text-sm); line-height: 1.25rem; --link: var(--muted); }
  15+.nav-icons a > svg { flex-shrink: 0; }
  16+@container (width < 5.15rem) { .docs-1 span { display: none; } .docs-1 svg { width: 21px; height: 21px; } }
  17+@container (width < 10.45rem) { .docs-2 span { display: none; } .docs-2 svg { width: 21px; height: 21px; } }
  18+.nav-icons a[aria-current] { --link: var(--fg); font-weight: 600; }
  19+.nav-icons a[aria-current] > svg { color: var(--fg); }
+20, -12
        @@ -2085,12 +2085,31 @@ header.subtitle h2 .count {
20852085   gap: 0.25rem 0;
20862086   margin-bottom: 2rem;
20872087   padding-top: 2rem;
    2088+  --fit-bg: var(--bg);
20882089 }
20892090 
20902091 .topbar:has(> .repo-tabs) {
20912092   margin-bottom: 1rem;
20922093 }
20932094 
    2095+#site_path > a {
    2096+  min-width: 0;
    2097+}
    2098+
    2099+.nav-only,
    2100+.nav-btn,
    2101+.nav-icons {
    2102+  display: none;
    2103+}
    2104+
    2105+.nav-menu > a:first-child {
    2106+  border-radius: var(--radius-inner) var(--radius-inner) 0 0;
    2107+}
    2108+
    2109+.nav-menu > a:last-child {
    2110+  border-radius: 0 0 var(--radius-inner) var(--radius-inner);
    2111+}
    2112+
20942113 .repo-docs {
20952114   display: flex;
20962115   gap: 1rem;
        @@ -2314,21 +2333,10 @@ a:is(.commit-id, .release-id) {
23142333 }
23152334 
23162335 @media only screen and (max-width: 600px) {
2317    -  a.brand[aria-current],
2318    -  #site_path,
2319    -  .tab {
    2336+  a.brand[aria-current] {
23202337     font-size: var(--text-xl);
23212338   }
23222339 
2323    -  a.brand,
2324    -  #site_path::before {
2325    -    font-size: var(--text-lg);
2326    -  }
2327    -
2328    -  .repo-tabs {
2329    -    flex-basis: auto;
2330    -  }
2331    -
23322340   .branch-row {
23332341     display: grid;
23342342     grid-template-columns: minmax(0, 1fr) max-content;