diff --git a/web/src/components/layout/Sidebar.test.tsx b/web/src/components/layout/Sidebar.test.tsx index 768cbae..98eea66 100644 --- a/web/src/components/layout/Sidebar.test.tsx +++ b/web/src/components/layout/Sidebar.test.tsx @@ -1,5 +1,6 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { render, screen, within } from '@testing-library/react' +import { cleanup, render, screen, within } from '@testing-library/react' +import userEvent from '@testing-library/user-event' import { MemoryRouter } from 'react-router-dom' import { beforeEach, expect, test, vi } from 'vitest' import { sessionStore } from '@/auth/session' @@ -34,10 +35,48 @@ test('the menu has a link for each page, and the page that is open is the curren expect(within(nav).getByRole('link', { name: 'Artists' })).not.toHaveAttribute('aria-current') }) -test('there is nothing to fold: the menu always shows its words', () => { +test('the menu starts open, with a button that folds it', () => { const nav = renderSidebar() expect(nav).not.toHaveAttribute('data-collapsed') - expect(within(nav).queryByRole('button')).not.toBeInTheDocument() + const button = within(nav).getByRole('button', { name: 'Collapse menu' }) + expect(button).toHaveAttribute('aria-expanded', 'true') +}) + +test('folding leaves the icons: the words stay as the names of the links, and the logo becomes the mark', async () => { + const nav = renderSidebar() + await userEvent.click(within(nav).getByRole('button', { name: 'Collapse menu' })) + expect(nav).toHaveAttribute('data-collapsed') + const button = within(nav).getByRole('button', { name: 'Expand menu' }) + expect(button).toHaveAttribute('aria-expanded', 'false') + const albums = within(nav).getByRole('link', { name: 'Albums' }) + expect(albums).toHaveAttribute('title', 'Albums') + expect(albums.querySelector('[data-icon]')).toBeInTheDocument() + expect(albums.querySelector('span')).toHaveClass('sr-only') + expect(within(nav).getAllByRole('img', { name: 'Sonda' })).toHaveLength(1) + expect(within(nav).queryByText(/Nothing leaves this server/)).not.toBeInTheDocument() +}) + +test('when open the links have no tooltip: the words are there', () => { + const nav = renderSidebar() + expect(within(nav).getByRole('link', { name: 'Albums' })).not.toHaveAttribute('title') +}) + +test('the group names stay for a screen reader when the menu is folded', async () => { + const nav = renderSidebar() + await userEvent.click(within(nav).getByRole('button', { name: 'Collapse menu' })) + expect(within(nav).getByRole('list', { name: 'Library' })).toBeInTheDocument() + expect(within(nav).getByText('Library')).toHaveClass('sr-only') +}) + +test('the choice is kept on this device and applies the next time', async () => { + const first = renderSidebar() + await userEvent.click(within(first).getByRole('button', { name: 'Collapse menu' })) + expect(localStorage.getItem('sonda.sidebar')).toBe('collapsed') + cleanup() + const again = renderSidebar() + expect(again).toHaveAttribute('data-collapsed') + await userEvent.click(within(again).getByRole('button', { name: 'Expand menu' })) + expect(localStorage.getItem('sonda.sidebar')).toBe('open') }) test('every link has an icon, and the words stay: the icon is decoration, not the name', () => { @@ -100,7 +139,8 @@ test('the icon of Friends is the friend colour also when it is the page that is test('on a phone the links are left to the bar of tabs: only the logo stays', () => { const nav = renderSidebar() - expect(nav.querySelector(':scope > div')).toHaveClass('max-sm:hidden') + expect(nav.querySelector(':scope > div:nth-of-type(2)')).toHaveClass('max-sm:hidden') + expect(within(nav).getByRole('button', { name: 'Collapse menu' })).toHaveClass('max-sm:hidden') }) const names = (nav: HTMLElement) => within(nav).getAllByRole('link').map((a) => a.textContent) diff --git a/web/src/components/layout/Sidebar.tsx b/web/src/components/layout/Sidebar.tsx index 45d014a..dde745d 100644 --- a/web/src/components/layout/Sidebar.tsx +++ b/web/src/components/layout/Sidebar.tsx @@ -1,4 +1,5 @@ -import { Blend, Compass, Disc3, History, House, Inbox, ListMusic, MicVocal, Music, Search, Settings, ShieldCheck, Upload, Users, WandSparkles } from 'lucide-react' +import { useState } from 'react' +import { Blend, Compass, Disc3, History, House, Inbox, ListMusic, MicVocal, Music, PanelLeftClose, PanelLeftOpen, Search, Settings, ShieldCheck, Upload, Users, WandSparkles } from 'lucide-react' import type { LucideIcon } from 'lucide-react' import { NavLink } from 'react-router-dom' import { useTranslation } from 'react-i18next' @@ -61,7 +62,31 @@ const FOOT: Item[] = [ { to: '/admin', key: 'nav.admin', icon: ShieldCheck, adminOnly: true }, ] -function Row({ item }: { item: Item }) { +const KEY = 'sonda.sidebar' + +/** Whether this device folded the menu: a choice of the device, like the theme, kept in its storage. */ +function useCollapsed(): [boolean, (collapsed: boolean) => void] { + const [collapsed, set] = useState(() => { + try { + return localStorage.getItem(KEY) === 'collapsed' + } catch { + return false + } + }) + return [ + collapsed, + (value) => { + set(value) + try { + localStorage.setItem(KEY, value ? 'collapsed' : 'open') + } catch { + // The choice still applies for this page view. + } + }, + ] +} + +function Row({ item, collapsed }: { item: Item; collapsed: boolean }) { const { t } = useTranslation() const Icon = item.icon return ( @@ -69,9 +94,11 @@ function Row({ item }: { item: Item }) { cn( 'flex min-h-10 items-center gap-3 rounded-lg px-3 font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring', + collapsed && 'justify-center px-0', isActive ? 'bg-card font-bold text-foreground' : 'text-muted-foreground hover:text-foreground', ) } @@ -84,7 +111,7 @@ function Row({ item }: { item: Item }) { strokeWidth={1.75} className={cn('size-5 shrink-0', item.friends ? 'text-friend' : isActive && 'text-primary')} /> - {t(item.key)} + {t(item.key)} )} @@ -101,41 +128,63 @@ function Row({ item }: { item: Item }) { export default function Sidebar() { const { t } = useTranslation() const { user } = useSession() + const [collapsed, setCollapsed] = useCollapsed() // Friends is in the menu only when there is a friend to browse (federation on, and a friendship both admins agreed to). const hasFriends = (useFriends().data?.length ?? 0) > 0 const shown = (item: Item) => (!item.adminOnly || user?.is_admin) && (!item.friends || hasFriends) const groups = GROUPS.map((g) => ({ ...g, items: g.items.filter(shown) })).filter((g) => g.items.length > 0) const foot = FOOT.filter(shown) + const Toggle = collapsed ? PanelLeftOpen : PanelLeftClose + return ( ) } diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 9159d77..9626687 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -24,6 +24,8 @@ "history": "History", "import": "Import", "more": "More", + "collapse": "Collapse menu", + "expand": "Expand menu", "tabs": "Tabs" }, "common": { diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index 87b6301..4acdda5 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -24,6 +24,8 @@ "history": "Cronologia", "import": "Importa", "more": "Altro", + "collapse": "Comprimi il menu", + "expand": "Espandi il menu", "tabs": "Schede" }, "common": {