feat(web): fold the side menu to a strip of icons
- a button next to the logo folds the menu to icons and opens it again - folded, each link keeps its name for a screen reader and shows it as a tooltip, the group names stay for a screen reader, the logo becomes the mark - the choice is kept in this device's storage - no button on a phone, where the tabs replace the menu
This commit is contained in:
parent
0fb57fe4f0
commit
78f3f7ec9e
4 changed files with 106 additions and 13 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 }) {
|
|||
<NavLink
|
||||
to={item.to}
|
||||
end={item.end ?? false}
|
||||
title={collapsed ? t(item.key) : undefined}
|
||||
className={({ isActive }) =>
|
||||
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')}
|
||||
/>
|
||||
<span>{t(item.key)}</span>
|
||||
<span className={cn(collapsed && 'sr-only')}>{t(item.key)}</span>
|
||||
</>
|
||||
)}
|
||||
</NavLink>
|
||||
|
|
@ -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 (
|
||||
<nav
|
||||
aria-label={t('nav.main')}
|
||||
className="flex flex-[0_0_232px] flex-col border-r border-border px-5 py-8 max-sm:min-w-0 max-sm:flex-[1_1_100%] max-sm:border-r-0 max-sm:border-b max-sm:p-3"
|
||||
data-collapsed={collapsed ? '' : undefined}
|
||||
className={cn(
|
||||
'flex flex-col border-r border-border py-8 max-sm:min-w-0 max-sm:flex-[1_1_100%] max-sm:border-r-0 max-sm:border-b max-sm:p-3',
|
||||
collapsed ? 'flex-[0_0_76px] px-3' : 'flex-[0_0_232px] px-5',
|
||||
)}
|
||||
>
|
||||
<Logo name={t('app.name')} className="mx-3 mb-7 h-8 w-auto self-start max-sm:mb-0" />
|
||||
<div className={cn('mb-7 flex items-center max-sm:mb-0', collapsed ? 'flex-col gap-5' : 'justify-between pl-3')}>
|
||||
<Logo name={t('app.name')} mark={collapsed} className={cn('w-auto self-start max-sm:h-8', collapsed ? 'h-8 self-center' : 'h-8')} />
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={!collapsed}
|
||||
aria-label={t(collapsed ? 'nav.expand' : 'nav.collapse')}
|
||||
title={t(collapsed ? 'nav.expand' : 'nav.collapse')}
|
||||
onClick={() => setCollapsed(!collapsed)}
|
||||
className="flex size-10 items-center justify-center rounded-lg text-muted-foreground hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring max-sm:hidden"
|
||||
>
|
||||
<Toggle aria-hidden="true" strokeWidth={1.75} className="size-5" />
|
||||
</button>
|
||||
</div>
|
||||
{/* On a phone the links are the bar of tabs at the bottom; only the logo stays on top. */}
|
||||
<div className="flex min-h-0 flex-1 flex-col gap-5 overflow-y-auto max-sm:hidden">
|
||||
{groups.map((group) => (
|
||||
<div key={group.items[0].to} className="flex flex-col">
|
||||
{group.heading && (
|
||||
<p id={`nav-group-${group.heading}`} className="flex min-h-7 items-center pl-11 text-[13px] font-medium text-muted-foreground">
|
||||
<p
|
||||
id={`nav-group-${group.heading}`}
|
||||
className={cn(collapsed ? 'sr-only' : 'flex min-h-7 items-center pl-11 text-[13px] font-medium text-muted-foreground')}
|
||||
>
|
||||
{t(group.heading)}
|
||||
</p>
|
||||
)}
|
||||
<ul aria-labelledby={group.heading ? `nav-group-${group.heading}` : undefined} className="flex flex-col gap-1">
|
||||
{group.items.map((item) => (
|
||||
<Row key={item.to} item={item} />
|
||||
<Row key={item.to} item={item} collapsed={collapsed} />
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
))}
|
||||
<ul className="mt-auto flex flex-col gap-1">
|
||||
{foot.map((item) => (
|
||||
<Row key={item.to} item={item} />
|
||||
<Row key={item.to} item={item} collapsed={collapsed} />
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
<p className="px-3 pt-7 text-[13px] leading-snug text-muted-foreground max-sm:hidden">{t('app.privacy')}</p>
|
||||
{!collapsed && <p className="px-3 pt-7 text-[13px] leading-snug text-muted-foreground max-sm:hidden">{t('app.privacy')}</p>}
|
||||
</nav>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@
|
|||
"history": "History",
|
||||
"import": "Import",
|
||||
"more": "More",
|
||||
"collapse": "Collapse menu",
|
||||
"expand": "Expand menu",
|
||||
"tabs": "Tabs"
|
||||
},
|
||||
"common": {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@
|
|||
"history": "Cronologia",
|
||||
"import": "Importa",
|
||||
"more": "Altro",
|
||||
"collapse": "Comprimi il menu",
|
||||
"expand": "Espandi il menu",
|
||||
"tabs": "Schede"
|
||||
},
|
||||
"common": {
|
||||
|
|
|
|||
Loading…
Reference in a new issue