Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(docs): add mobile navigation menu (#1498)

Corentin Thomasset0af44eae9e3c
diff --git a/apps/website/src/docs/components/DocMobileNav.astro b/apps/website/src/docs/components/DocMobileNav.astro
new file mode 100644
index 00000000..f5e135d6
--- /dev/null
+++ b/apps/website/src/docs/components/DocMobileNav.astro
@@ -0,0 +1,121 @@
+---
+import type { Locale } from '../../i18n/i18n.constants';
+import { useI18n } from '../../i18n/i18n';
+import { docCategories } from '../docs.navigations';
+import DocNavSections from './DocNavSections.astro';
+
+type Props = {
+ currentDocId?: string;
+ locale: Locale;
+};
+
+const { currentDocId, locale } = Astro.props;
+const { t } = useI18n({ locale });
+const categories = docCategories.filter((category) =>
+ category.sections.some((section) => section.items.length > 0),
+);
+---
+
+<docs-mobile-nav class="block md:hidden">
+ <details class="relative">
+ <summary
+ aria-controls="docs-mobile-navigation"
+ class="h-48px px-6 flex items-center justify-between gap-3 cursor-pointer font-medium hover:text-primary transition"
+ >
+ <span class="flex items-center gap-2">
+ <span class="i-tabler-menu-2 size-5" aria-hidden="true"></span>
+ {t('docs.menu-label')}
+ </span>
+ <span class="docs-menu-chevron i-tabler-chevron-down size-5" aria-hidden="true"></span>
+ </summary>
+
+ <nav
+ id="docs-mobile-navigation"
+ aria-label={t('docs.menu-label')}
+ class="absolute top-full inset-x-0 border-y bg-#121212 shadow-lg overflow-y-auto overscroll-contain pl-2 pr-4 pb-4"
+ >
+ {categories.map((category) => (
+ <section class="pt-4">
+ <h2 class="px-4 text-sm font-semibold text-primary">{t(category.titleKey)}</h2>
+ <DocNavSections sections={category.sections} currentDocId={currentDocId} locale={locale} />
+ </section>
+ ))}
+ </nav>
+ </details>
+</docs-mobile-nav>
+
+<script>
+ class DocsMobileNav extends HTMLElement {
+ private controller?: AbortController;
+
+ connectedCallback() {
+ this.controller?.abort();
+ this.controller = new AbortController();
+ const { signal } = this.controller;
+ const details = this.querySelector('details')!;
+ const summary = this.querySelector('summary')!;
+ const nav = this.querySelector('nav')!;
+ const desktop = window.matchMedia('(min-width: 768px)');
+
+ this.addEventListener('keydown', (event) => {
+ if (event.key === 'Escape' && details.open) {
+ event.preventDefault();
+ details.open = false;
+ summary.focus();
+ }
+ }, { signal });
+
+ // Don't leave the panel covering page content when keyboard focus moves on.
+ this.addEventListener('focusout', (event) => {
+ if (!(event.relatedTarget instanceof Node) || !this.contains(event.relatedTarget)) {
+ details.open = false;
+ }
+ }, { signal });
+
+ nav.addEventListener('click', (event) => {
+ if (event.target instanceof Element && event.target.closest('a[href]')) {
+ details.open = false;
+ }
+ }, { signal });
+
+ const closeOnDesktop = () => {
+ if (desktop.matches) {
+ details.open = false;
+ }
+ };
+ desktop.addEventListener('change', closeOnDesktop, { signal });
+ closeOnDesktop();
+ }
+
+ disconnectedCallback() {
+ this.controller?.abort();
+ }
+ }
+
+ customElements.define('docs-mobile-nav', DocsMobileNav);
+</script>
+
+<style>
+ summary {
+ list-style: none;
+ }
+
+ summary::-webkit-details-marker {
+ display: none;
+ }
+
+ summary:focus-visible {
+ outline: 2px solid hsl(var(--ring));
+ outline-offset: -4px;
+ }
+
+ details[open] .docs-menu-chevron {
+ transform: rotate(180deg);
+ }
+
+ nav {
+ /* The closed disclosure preserves the desktop header's 113px height. */
+ max-height: calc(100vh - 113px);
+ max-height: calc(100dvh - 113px);
+ }
+</style>
diff --git a/apps/website/src/docs/components/DocNavSections.astro b/apps/website/src/docs/components/DocNavSections.astro
new file mode 100644
index 00000000..1af2ac18
--- /dev/null
+++ b/apps/website/src/docs/components/DocNavSections.astro
@@ -0,0 +1,56 @@
+---
+import { getCollection } from 'astro:content';
+import type { Locale } from '../../i18n/i18n.constants';
+import { useI18n } from '../../i18n/i18n';
+import { cn } from '../../utils/cn';
+import { getDocUrl, resolveDoc } from '../docs.models';
+import type { DocsSection } from '../docs.types';
+
+export type Props = {
+ sections: DocsSection[];
+ currentDocId?: string;
+ locale: Locale;
+};
+
+const { sections, currentDocId, locale } = Astro.props;
+const { t } = useI18n({ locale });
+const docs = await getCollection('docs');
+const augmentedSections = sections.map((section) => ({
+ ...section,
+ items: section.items.map((item) => ({
+ ...item,
+ title: resolveDoc({ docs, docId: item.docId, locale }).entry.data.title,
+ })),
+}));
+---
+
+{
+ augmentedSections.map((section, index) => (
+ <div class={cn('py-4', index !== 0 ? 'border-t' : '')}>
+ <div class="text-13px font-medium mb-2 pl-4">{t(section.titleKey)}</div>
+ <ul>
+ {section.items.map((item) => (
+ <li>
+ <a
+ href={getDocUrl({ docId: item.docId, locale })}
+ aria-current={item.docId === currentDocId ? 'page' : undefined}
+ class={cn(
+ 'text-sm text-muted-foreground hover:(bg-muted text-foreground) transition flex items-center min-h-11 md:min-h-0 px-4 rounded-lg py-1.2 my-1px',
+ item.docId === currentDocId ? 'bg-muted text-foreground' : '',
+ )}
+ >
+ {item.title}
+ </a>
+ </li>
+ ))}
+ </ul>
+ </div>
+ ))
+}
+
+<style>
+ a:focus-visible {
+ outline: 2px solid hsl(var(--ring));
+ outline-offset: 2px;
+ }
+</style>
diff --git a/apps/website/src/docs/components/DocSidenav.astro b/apps/website/src/docs/components/DocSidenav.astro
index 60ee02a2..1606a7ce 100644
--- a/apps/website/src/docs/components/DocSidenav.astro
+++ b/apps/website/src/docs/components/DocSidenav.astro
@@ -1,10 +1,8 @@
---
-import { cn } from '../../utils/cn';
-import { getDocUrl, resolveDoc } from '../docs.models';
import type { DocsSection } from '../docs.types';
import type { Locale } from '../../i18n/i18n.constants';
import { useI18n } from '../../i18n/i18n';
-import { getCollection } from 'astro:content';
+import DocNavSections from './DocNavSections.astro';
export type Props = {
sections: DocsSection[];
@@ -14,31 +12,8 @@ export type Props = {
const { sections, currentDocId, locale } = Astro.props;
const { t } = useI18n({ locale });
-const docs = await getCollection('docs');
-const augmentedSections = sections.map((section) => ({
- ...section,
- items: section.items.map((item) => ({
- ...item,
- title: resolveDoc({ docs, docId: item.docId, locale }).entry.data.title,
- })),
-}));
---
-<nav aria-label={t('docs.sidebar-label')} class="border-r max-w-300px w-full hidden md:block overflow-y-auto pl-2 pr-4 pt-2 flex-shrink-0 sticky top-113px h-[calc(100vh-113px)] overflow-y-auto">
- {
- augmentedSections.map((section, index) => (
- <div class={cn('py-4', index !== 0 ? 'border-t' : '')}>
- <div class="text-13px font-medium mb-2 pl-4">{t(section.titleKey)}</div>
- <ul>
- {section.items.map((item) => (
- <li>
- <a href={getDocUrl({ docId: item.docId, locale })} aria-current={item.docId === currentDocId ? 'page' : undefined} class={cn("text-sm text-muted-foreground hover:(bg-muted text-foreground) transition py-1 block px-4 rounded-lg py-1.2 my-1px", item.docId === currentDocId ? "bg-muted text-foreground" : "")}>
- {item.title}
- </a>
- </li>
- ))}
- </ul>
- </div>
- ))
- }
+<nav aria-label={t('docs.sidebar-label')} class="border-r max-w-300px w-full hidden md:block overflow-y-auto pl-2 pr-4 pt-2 flex-shrink-0 sticky top-113px h-[calc(100vh-113px)]">
+ <DocNavSections sections={sections} currentDocId={currentDocId} locale={locale} />
</nav>
diff --git a/apps/website/src/layouts/DocsLayout.astro b/apps/website/src/layouts/DocsLayout.astro
index b6a17d29..1b8d5375 100644
--- a/apps/website/src/layouts/DocsLayout.astro
+++ b/apps/website/src/layouts/DocsLayout.astro
@@ -11,6 +11,7 @@ import type { Locale } from '../i18n/i18n.constants';
import { useI18n } from '../i18n/i18n';
import DocSidenav from '../docs/components/DocSidenav.astro';
import DocSearch from '../docs/components/DocSearch.astro';
+import DocMobileNav from '../docs/components/DocMobileNav.astro';
type Props = {
title?: string;
@@ -86,7 +87,9 @@ const fullTitle = title ? `${title} - ${titleSuffix}` : titleSuffix;
</div>
</div>
- <nav aria-label={t('docs.categories-label')} class="max-w-1400px mx-auto px-6 overflow-x-auto">
+ <DocMobileNav currentDocId={docId} locale={locale} />
+
+ <nav aria-label={t('docs.categories-label')} class="hidden md:block max-w-1400px mx-auto px-6 overflow-x-auto">
<div class="flex items-center gap-8 overflow-x-auto">
{
docCategories.map((category) => {
diff --git a/apps/website/src/locales/en.ts b/apps/website/src/locales/en.ts
index 47d855f5..7c503cef 100644
--- a/apps/website/src/locales/en.ts
+++ b/apps/website/src/locales/en.ts
@@ -364,6 +364,7 @@ export const translations = {
'docs.skip-to-content': 'Skip to content',
'docs.categories-label': 'Documentation categories',
'docs.sidebar-label': 'Documentation sidebar',
+ 'docs.menu-label': 'Documentation menu',
'docs.on-this-page': 'On this page',
'docs.pagination-label': 'Previous and next pages',
'docs.previous': 'Previous',
diff --git a/apps/website/src/locales/fr.ts b/apps/website/src/locales/fr.ts
index 6bb3fcd5..cc6a83d4 100644
--- a/apps/website/src/locales/fr.ts
+++ b/apps/website/src/locales/fr.ts
@@ -355,6 +355,7 @@ export const translations: Partial<TranslationsDictionary> = {
'Dernière chance : {{percentage}}% de réduction à vie - Expire le 31 décembre 2025 !',
// Documentation
+ 'docs.menu-label': 'Menu de la documentation',
'docs.edit-this-page': 'Modifier cette page',
// Pagination