Merge pull request 'An icon next to each link in the side menu' (#194) from feat/sidebar-icons into main
This commit is contained in:
commit
0fb57fe4f0
2 changed files with 56 additions and 33 deletions
|
|
@ -25,15 +25,13 @@ function renderSidebar(friends: { id: number; name: string }[] = [], path = '/al
|
|||
return screen.getByRole('navigation', { name: 'Main' })
|
||||
}
|
||||
|
||||
test('the menu is words, one for each page, and the page that is open is the current one', () => {
|
||||
test('the menu has a link for each page, and the page that is open is the current one', () => {
|
||||
const nav = renderSidebar()
|
||||
for (const name of ['Home', 'Search', 'Discover', 'Artists', 'Albums', 'Songs', 'Mixes', 'Playlists', 'Smart playlists', 'History', 'Import', 'Admin', 'Settings']) {
|
||||
expect(within(nav).getByRole('link', { name })).toBeInTheDocument()
|
||||
}
|
||||
expect(within(nav).getByRole('link', { name: 'Albums' })).toHaveAttribute('aria-current', 'page')
|
||||
expect(within(nav).getByRole('link', { name: 'Artists' })).not.toHaveAttribute('aria-current')
|
||||
// No icons, the words are enough: the only drawing in the menu is the logo.
|
||||
expect([...nav.querySelectorAll('svg')].filter((svg) => svg.getAttribute('role') !== 'img')).toHaveLength(0)
|
||||
})
|
||||
|
||||
test('there is nothing to fold: the menu always shows its words', () => {
|
||||
|
|
@ -42,11 +40,31 @@ test('there is nothing to fold: the menu always shows its words', () => {
|
|||
expect(within(nav).queryByRole('button')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('a dot marks the page that is open, in the colour of this server', () => {
|
||||
test('every link has an icon, and the words stay: the icon is decoration, not the name', () => {
|
||||
const nav = renderSidebar()
|
||||
const dot = (name: string) => within(nav).getByRole('link', { name }).querySelector('[data-dot]')
|
||||
expect(dot('Albums')).toHaveClass('bg-primary')
|
||||
expect(dot('Artists')).toHaveClass('bg-transparent')
|
||||
const links = within(nav).getAllByRole('link')
|
||||
expect(links.length).toBeGreaterThan(10)
|
||||
for (const link of links) {
|
||||
const icon = link.querySelector('[data-icon]')
|
||||
expect(icon, link.textContent ?? '').toBeInTheDocument()
|
||||
expect(icon).toHaveAttribute('aria-hidden', 'true')
|
||||
expect(link.textContent?.trim()).not.toBe('')
|
||||
}
|
||||
})
|
||||
|
||||
test('no two pages share an icon', () => {
|
||||
const nav = renderSidebar()
|
||||
const kinds = within(nav)
|
||||
.getAllByRole('link')
|
||||
.map((a) => a.querySelector('[data-icon]')?.getAttribute('class')?.match(/lucide-[\w-]+/)?.[0])
|
||||
expect(new Set(kinds).size).toBe(kinds.length)
|
||||
})
|
||||
|
||||
test('the icon of the page that is open is in the colour of this server', () => {
|
||||
const nav = renderSidebar()
|
||||
const icon = (name: string) => within(nav).getByRole('link', { name }).querySelector('[data-icon]')
|
||||
expect(icon('Albums')).toHaveClass('text-primary')
|
||||
expect(icon('Artists')).not.toHaveClass('text-primary')
|
||||
})
|
||||
|
||||
test('the Sonda logo is the name of the app', () => {
|
||||
|
|
@ -66,18 +84,18 @@ test('Friends is in the menu only when there is a friend to browse', async () =>
|
|||
expect(within(none).queryByRole('link', { name: 'Friends' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('with a friend the menu has a Friends link, with the dot of what comes from elsewhere', async () => {
|
||||
test('with a friend the menu has a Friends link, with the icon of what comes from elsewhere', async () => {
|
||||
const nav = renderSidebar([{ id: 3, name: 'Anna' }])
|
||||
const link = await within(nav).findByRole('link', { name: 'Friends' })
|
||||
expect(link).toHaveAttribute('href', '/friends')
|
||||
expect(link.querySelector('[data-dot]')).toHaveClass('bg-friend')
|
||||
expect(link.querySelector('[data-icon]')).toHaveClass('text-friend')
|
||||
})
|
||||
|
||||
test('the dot of Friends is the friend colour also when it is the page that is open', async () => {
|
||||
test('the icon of Friends is the friend colour also when it is the page that is open', async () => {
|
||||
const nav = renderSidebar([{ id: 3, name: 'Anna' }], '/friends/3')
|
||||
const link = await within(nav).findByRole('link', { name: 'Friends' })
|
||||
expect(link).toHaveAttribute('aria-current', 'page')
|
||||
expect(link.querySelector('[data-dot]')).toHaveClass('bg-friend')
|
||||
expect(link.querySelector('[data-icon]')).toHaveClass('text-friend')
|
||||
})
|
||||
|
||||
test('on a phone the links are left to the bar of tabs: only the logo stays', () => {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { Blend, Compass, Disc3, History, House, Inbox, ListMusic, MicVocal, Music, 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'
|
||||
import { useFriends } from '@/api/friends'
|
||||
|
|
@ -8,6 +10,7 @@ import { cn } from '@/lib/utils'
|
|||
interface Item {
|
||||
to: string
|
||||
key: string
|
||||
icon: LucideIcon
|
||||
end?: boolean
|
||||
adminOnly?: boolean
|
||||
friends?: boolean
|
||||
|
|
@ -26,40 +29,41 @@ interface Group {
|
|||
const GROUPS: Group[] = [
|
||||
{
|
||||
items: [
|
||||
{ to: '/', key: 'nav.home', end: true },
|
||||
{ to: '/search', key: 'nav.search' },
|
||||
{ to: '/discover', key: 'nav.discover' },
|
||||
{ to: '/requests', key: 'nav.requests', adminOnly: true },
|
||||
{ to: '/', key: 'nav.home', icon: House, end: true },
|
||||
{ to: '/search', key: 'nav.search', icon: Search },
|
||||
{ to: '/discover', key: 'nav.discover', icon: Compass },
|
||||
{ to: '/requests', key: 'nav.requests', icon: Inbox, adminOnly: true },
|
||||
],
|
||||
},
|
||||
{
|
||||
heading: 'nav.library',
|
||||
items: [
|
||||
{ to: '/artists', key: 'nav.artists' },
|
||||
{ to: '/albums', key: 'nav.albums' },
|
||||
{ to: '/songs', key: 'nav.songs' },
|
||||
{ to: '/playlists', key: 'nav.playlists' },
|
||||
{ to: '/smart-playlists', key: 'nav.smartPlaylists' },
|
||||
{ to: '/mixes', key: 'nav.mixes' },
|
||||
{ to: '/artists', key: 'nav.artists', icon: MicVocal },
|
||||
{ to: '/albums', key: 'nav.albums', icon: Disc3 },
|
||||
{ to: '/songs', key: 'nav.songs', icon: Music },
|
||||
{ to: '/playlists', key: 'nav.playlists', icon: ListMusic },
|
||||
{ to: '/smart-playlists', key: 'nav.smartPlaylists', icon: WandSparkles },
|
||||
{ to: '/mixes', key: 'nav.mixes', icon: Blend },
|
||||
],
|
||||
},
|
||||
{
|
||||
heading: 'nav.listening',
|
||||
items: [
|
||||
{ to: '/history', key: 'nav.history' },
|
||||
{ to: '/import', key: 'nav.import' },
|
||||
{ to: '/history', key: 'nav.history', icon: History },
|
||||
{ to: '/import', key: 'nav.import', icon: Upload },
|
||||
],
|
||||
},
|
||||
{ items: [{ to: '/friends', key: 'nav.friends', friends: true }] },
|
||||
{ items: [{ to: '/friends', key: 'nav.friends', icon: Users, friends: true }] },
|
||||
]
|
||||
|
||||
const FOOT: Item[] = [
|
||||
{ to: '/settings', key: 'nav.settings' },
|
||||
{ to: '/admin', key: 'nav.admin', adminOnly: true },
|
||||
{ to: '/settings', key: 'nav.settings', icon: Settings },
|
||||
{ to: '/admin', key: 'nav.admin', icon: ShieldCheck, adminOnly: true },
|
||||
]
|
||||
|
||||
function Row({ item }: { item: Item }) {
|
||||
const { t } = useTranslation()
|
||||
const Icon = item.icon
|
||||
return (
|
||||
<li>
|
||||
<NavLink
|
||||
|
|
@ -74,10 +78,11 @@ function Row({ item }: { item: Item }) {
|
|||
>
|
||||
{({ isActive }) => (
|
||||
<>
|
||||
<span
|
||||
data-dot
|
||||
<Icon
|
||||
data-icon
|
||||
aria-hidden="true"
|
||||
className={cn('size-2 shrink-0 rounded-full', item.friends ? 'bg-friend' : isActive ? 'bg-primary' : 'bg-transparent')}
|
||||
strokeWidth={1.75}
|
||||
className={cn('size-5 shrink-0', item.friends ? 'text-friend' : isActive && 'text-primary')}
|
||||
/>
|
||||
<span>{t(item.key)}</span>
|
||||
</>
|
||||
|
|
@ -88,9 +93,9 @@ function Row({ item }: { item: Item }) {
|
|||
}
|
||||
|
||||
/**
|
||||
* The main menu: words, in groups, and a dot that says which page is open. The dot is the colour of this server; for
|
||||
* Friends it is the colour of what comes from elsewhere, always, so that the one page that shows other people's music
|
||||
* is told apart before it is opened. Space tells the groups apart; the two that need a name have a quiet one. On a
|
||||
* The main menu: an icon and a word for each page, in groups. The icon of the page that is open is the colour of this
|
||||
* server; the icon of Friends is the colour of what comes from elsewhere, always, so that the one page that shows other
|
||||
* people's music is told apart before it is opened. Space tells the groups apart; the two that need a name have a quiet one. On a
|
||||
* phone the links are left to the bar of tabs (PhoneTabs).
|
||||
*/
|
||||
export default function Sidebar() {
|
||||
|
|
@ -113,7 +118,7 @@ export default function Sidebar() {
|
|||
{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-8 text-[13px] font-medium text-muted-foreground">
|
||||
<p id={`nav-group-${group.heading}`} className="flex min-h-7 items-center pl-11 text-[13px] font-medium text-muted-foreground">
|
||||
{t(group.heading)}
|
||||
</p>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Reference in a new issue