diff --git a/web/src/components/LinkRows.tsx b/web/src/components/LinkRows.tsx new file mode 100644 index 0000000..cdd228f --- /dev/null +++ b/web/src/components/LinkRows.tsx @@ -0,0 +1,19 @@ +import { Link } from 'react-router-dom' + +/** A list of places, each a row of words that leads there, separated by a line. */ +export default function LinkRows({ links }: { links: { to: string; label: string }[] }) { + return ( + + ) +} diff --git a/web/src/components/layout/PhoneTabs.test.tsx b/web/src/components/layout/PhoneTabs.test.tsx new file mode 100644 index 0000000..c7684ec --- /dev/null +++ b/web/src/components/layout/PhoneTabs.test.tsx @@ -0,0 +1,68 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { render, screen, within } from '@testing-library/react' +import { MemoryRouter } from 'react-router-dom' +import { beforeEach, expect, test, vi } from 'vitest' +import { initI18n } from '@/i18n' +import { mockFetch } from '@/test/mockFetch' +import PhoneTabs from './PhoneTabs' + +beforeEach(async () => { + vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') + await initI18n() +}) + +function renderTabs(path = '/', friends: { id: number; name: string }[] = []) { + mockFetch({ 'GET /api/v1/friends': () => ({ friends }) }) + render( + + + + + , + ) + return screen.getByRole('navigation', { name: 'Tabs' }) +} + +test('the tabs are Home, Search, Library and More, and only on a phone', () => { + const nav = renderTabs() + for (const name of ['Home', 'Search', 'Library', 'More']) expect(within(nav).getByRole('link', { name })).toBeInTheDocument() + expect(within(nav).queryByRole('link', { name: 'Friends' })).not.toBeInTheDocument() + expect(nav).toHaveClass('sm:hidden') +}) + +test('each tab is tall enough for a thumb', () => { + const nav = renderTabs() + for (const link of within(nav).getAllByRole('link')) expect(link).toHaveClass('min-h-14') +}) + +test('the tab of the page that is open is the current one', () => { + const nav = renderTabs('/search') + expect(within(nav).getByRole('link', { name: 'Search' })).toHaveAttribute('aria-current', 'page') + expect(within(nav).getByRole('link', { name: 'Home' })).not.toHaveAttribute('aria-current') +}) + +test.each(['/library', '/artists', '/albums/4', '/songs', '/mixes', '/playlists/2', '/smart-playlists', '/discover'])( + 'Library is the current tab on %s, one of the pages it leads to', + (path) => { + const nav = renderTabs(path) + expect(within(nav).getByRole('link', { name: 'Library' })).toHaveAttribute('aria-current', 'page') + }, +) + +test.each(['/settings', '/admin/users'])('More is the current tab on %s', (path) => { + const nav = renderTabs(path) + expect(within(nav).getByRole('link', { name: 'More' })).toHaveAttribute('aria-current', 'page') +}) + +test('with a friend there is a Friends tab, marked in the friend colour', async () => { + const nav = renderTabs('/friends/3', [{ id: 3, name: 'Anna' }]) + const friends = await within(nav).findByRole('link', { name: 'Friends' }) + expect(friends).toHaveAttribute('aria-current', 'page') + expect(friends.querySelector('[data-bar]')).toHaveClass('bg-friend') + expect(within(nav).getByRole('link', { name: 'Home' }).querySelector('[data-bar]')).toHaveClass('bg-transparent') +}) + +test('the bar of the current tab is the colour of this server', () => { + const nav = renderTabs('/') + expect(within(nav).getByRole('link', { name: 'Home' }).querySelector('[data-bar]')).toHaveClass('bg-primary') +}) diff --git a/web/src/components/layout/PhoneTabs.tsx b/web/src/components/layout/PhoneTabs.tsx new file mode 100644 index 0000000..408c3d3 --- /dev/null +++ b/web/src/components/layout/PhoneTabs.tsx @@ -0,0 +1,51 @@ +import { useTranslation } from 'react-i18next' +import { Link, useLocation } from 'react-router-dom' +import { useFriends } from '@/api/friends' +import { cn } from '@/lib/utils' + +/** The pages the Library tab leads to: it is the current tab on all of them. */ +const LIBRARY = ['/library', '/artists', '/albums', '/songs', '/mixes', '/playlists', '/smart-playlists', '/discover'] +const MORE = ['/more', '/settings', '/admin'] + +const under = (path: string, prefixes: string[]) => prefixes.some((p) => path === p || path.startsWith(`${p}/`)) + +/** + * The bar of tabs at the bottom of a phone, under the player: Home, Search, Library, Friends (only when there is a + * friend to browse) and More. The menu on the left of a wide screen does not show on a phone, and this does not show on + * a wide screen. The mark on top of the current tab is the colour of this server; the Friends tab has the friend colour. + */ +export default function PhoneTabs() { + const { t } = useTranslation() + const { pathname } = useLocation() + const hasFriends = (useFriends().data?.length ?? 0) > 0 + const tabs = [ + { to: '/', label: t('nav.home'), current: pathname === '/' }, + { to: '/search', label: t('nav.search'), current: under(pathname, ['/search']) }, + { to: '/library', label: t('nav.library'), current: under(pathname, LIBRARY) }, + ...(hasFriends ? [{ to: '/friends', label: t('nav.friends'), current: under(pathname, ['/friends']), friend: true }] : []), + { to: '/more', label: t('nav.more'), current: under(pathname, MORE) }, + ] + + return ( + + ) +} diff --git a/web/src/components/layout/Shell.test.tsx b/web/src/components/layout/Shell.test.tsx index e83f748..09f6be1 100644 --- a/web/src/components/layout/Shell.test.tsx +++ b/web/src/components/layout/Shell.test.tsx @@ -156,6 +156,8 @@ test.each([ ['/songs', 'Songs'], ['/discover', 'Discover'], ['/settings', 'Settings'], + ['/library', 'Library'], + ['/more', 'More'], ])('the route %s opens a page headed %s', async (path, heading) => { await renderAt(path) expect(screen.getByRole('heading', { level: 1, name: heading })).toBeInTheDocument() diff --git a/web/src/components/layout/Shell.tsx b/web/src/components/layout/Shell.tsx index 267684d..61cd4e7 100644 --- a/web/src/components/layout/Shell.tsx +++ b/web/src/components/layout/Shell.tsx @@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next' import { Outlet } from 'react-router-dom' import { usePlayerEngine } from '@/player/usePlayer' import UpdateNotice from '@/pwa/UpdateNotice' +import PhoneTabs from './PhoneTabs' import PlayerBar from './PlayerBar' import Sidebar from './Sidebar' @@ -38,6 +39,7 @@ export default function Shell() { {/* Sticky here, not on the bar: a sticky element only sticks inside its parent. */}
+
diff --git a/web/src/components/layout/Sidebar.test.tsx b/web/src/components/layout/Sidebar.test.tsx index 8f03d14..18ab387 100644 --- a/web/src/components/layout/Sidebar.test.tsx +++ b/web/src/components/layout/Sidebar.test.tsx @@ -79,3 +79,8 @@ test('the dot of Friends is the friend colour also when it is the page that is o expect(link).toHaveAttribute('aria-current', 'page') expect(link.querySelector('[data-dot]')).toHaveClass('bg-friend') }) + +test('on a phone the links are left to the bar of tabs: only the logo stays', () => { + const nav = renderSidebar() + expect(within(nav).getByRole('list')).toHaveClass('max-sm:hidden') +}) diff --git a/web/src/components/layout/Sidebar.tsx b/web/src/components/layout/Sidebar.tsx index 70c8e70..d76fe1e 100644 --- a/web/src/components/layout/Sidebar.tsx +++ b/web/src/components/layout/Sidebar.tsx @@ -23,7 +23,7 @@ const LINKS: { to: string; key: string; end: boolean; adminOnly: boolean; friend /** * The main menu: words, one for each page, and a dot that says which one 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. On a phone the menu is a row of links that scrolls sideways. + * people's music is told apart before it is opened. On a phone the links are left to the bar of tabs (PhoneTabs). */ export default function Sidebar() { const { t } = useTranslation() @@ -35,13 +35,13 @@ export default function Sidebar() { return (