feat(web): a button for browser full screen
- one button in the player bar and one in the full-screen player puts the whole page in full screen and takes it out - it follows the browser: Esc and F11 flip it back, a refused request leaves the page as it was - left out where the browser has no Fullscreen API (for example iPhone Safari) - English and Italian labels
This commit is contained in:
parent
2d478055f2
commit
075e000fd8
9 changed files with 228 additions and 8 deletions
22
web/src/components/FullscreenButton.tsx
Normal file
22
web/src/components/FullscreenButton.tsx
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { Maximize, Minimize } from 'lucide-react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { useFullscreen } from '@/lib/fullscreen'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
/** Puts the browser in full screen, or takes it out. Not shown where the browser has no full screen. */
|
||||
export default function FullscreenButton({ className }: { className?: string }) {
|
||||
const { t } = useTranslation()
|
||||
const { supported, active, toggle } = useFullscreen()
|
||||
if (!supported) return null
|
||||
return (
|
||||
<Button
|
||||
variant="ghost"
|
||||
className={cn('size-11 text-muted-foreground', className)}
|
||||
aria-label={active ? t('fullscreen.exit') : t('fullscreen.enter')}
|
||||
onClick={() => void toggle()}
|
||||
>
|
||||
{active ? <Minimize /> : <Maximize />}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ import { useEffect } from 'react'
|
|||
import { useTranslation } from 'react-i18next'
|
||||
import { Link } from 'react-router-dom'
|
||||
import Cover from '@/components/Cover'
|
||||
import FullscreenButton from '@/components/FullscreenButton'
|
||||
import Scrubber from '@/components/Scrubber'
|
||||
import TrackRow from '@/components/TrackRow'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
|
@ -36,14 +37,12 @@ export default function NowPlaying() {
|
|||
>
|
||||
{track && (
|
||||
<>
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="absolute top-4 right-4 size-11"
|
||||
aria-label={t('player.close')}
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
<X />
|
||||
</Button>
|
||||
<div className="absolute top-4 right-4 flex gap-1">
|
||||
<FullscreenButton />
|
||||
<Button variant="ghost" className="size-11" aria-label={t('player.close')} onClick={() => setOpen(false)}>
|
||||
<X />
|
||||
</Button>
|
||||
</div>
|
||||
<DialogDescription className="sr-only">{t('player.nowPlaying')}</DialogDescription>
|
||||
|
||||
<div className="mx-auto flex w-full max-w-5xl flex-wrap items-center justify-center gap-x-16 gap-y-8 pt-10">
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import TrackRow from '@/components/TrackRow'
|
|||
import { initI18n } from '@/i18n'
|
||||
import { usePlayer } from '@/player/store'
|
||||
import { useNowPlayingUi } from '@/player/ui'
|
||||
import { fakeFullscreen } from '@/test/fullscreen'
|
||||
import PlayerBar from './PlayerBar'
|
||||
|
||||
function track(n: number, over: Partial<Track> = {}): Track {
|
||||
|
|
@ -249,6 +250,27 @@ test('Up next shows the next three tracks and a click plays one of them', async
|
|||
expect(usePlayer.getState().index).toBe(2)
|
||||
})
|
||||
|
||||
test('the bar and the full-screen player both have a button for browser full screen, where the browser has one', async () => {
|
||||
const env = fakeFullscreen()
|
||||
try {
|
||||
act(() => usePlayer.getState().playTracks([a]))
|
||||
renderBar()
|
||||
expect(screen.getByRole('button', { name: 'Enter full screen' })).toBeInTheDocument()
|
||||
const { dialog } = await openNowPlaying()
|
||||
expect(within(dialog).getByRole('button', { name: 'Enter full screen' })).toBeInTheDocument()
|
||||
} finally {
|
||||
env.restore()
|
||||
}
|
||||
})
|
||||
|
||||
test('without the Fullscreen API neither the bar nor the full-screen player shows the button', async () => {
|
||||
act(() => usePlayer.getState().playTracks([a]))
|
||||
renderBar()
|
||||
const { dialog } = await openNowPlaying()
|
||||
expect(screen.queryByRole('button', { name: 'Enter full screen' })).not.toBeInTheDocument()
|
||||
expect(within(dialog).queryByRole('button', { name: 'Enter full screen' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('a track with no album and no artists opens in the full-screen player', async () => {
|
||||
act(() => usePlayer.getState().playTracks([track(9, { album: null, artists: [] })]))
|
||||
renderBar()
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { useEffect } from 'react'
|
|||
import { useTranslation } from 'react-i18next'
|
||||
import { Link } from 'react-router-dom'
|
||||
import Cover from '@/components/Cover'
|
||||
import FullscreenButton from '@/components/FullscreenButton'
|
||||
import Scrubber from '@/components/Scrubber'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { formatTime } from '@/lib/time'
|
||||
|
|
@ -85,6 +86,7 @@ export default function PlayerBar() {
|
|||
</div>
|
||||
|
||||
<div className="flex flex-[1_1_120px] justify-end">
|
||||
<FullscreenButton />
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="size-11 text-muted-foreground"
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@
|
|||
"notFound": "This page does not exist",
|
||||
"backHome": "Back to Home"
|
||||
},
|
||||
"fullscreen": {
|
||||
"enter": "Enter full screen",
|
||||
"exit": "Exit full screen"
|
||||
},
|
||||
"player": {
|
||||
"label": "Player",
|
||||
"play": "Play",
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@
|
|||
"notFound": "Questa pagina non esiste",
|
||||
"backHome": "Torna alla Home"
|
||||
},
|
||||
"fullscreen": {
|
||||
"enter": "Schermo intero",
|
||||
"exit": "Esci dallo schermo intero"
|
||||
},
|
||||
"player": {
|
||||
"label": "Player",
|
||||
"play": "Riproduci",
|
||||
|
|
|
|||
97
web/src/lib/fullscreen.test.tsx
Normal file
97
web/src/lib/fullscreen.test.tsx
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import { act, render, renderHook, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
|
||||
import FullscreenButton from '@/components/FullscreenButton'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { fakeFullscreen } from '@/test/fullscreen'
|
||||
import { useFullscreen } from './fullscreen'
|
||||
|
||||
let env: ReturnType<typeof fakeFullscreen> | null = null
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
env?.restore()
|
||||
env = null
|
||||
})
|
||||
|
||||
test('a browser without the Fullscreen API is reported as not supporting it', () => {
|
||||
const { result } = renderHook(() => useFullscreen())
|
||||
expect(result.current.supported).toBe(false)
|
||||
expect(result.current.active).toBe(false)
|
||||
})
|
||||
|
||||
test('a browser that has it is reported as supporting it, and starts out of full screen', () => {
|
||||
env = fakeFullscreen()
|
||||
const { result } = renderHook(() => useFullscreen())
|
||||
expect(result.current.supported).toBe(true)
|
||||
expect(result.current.active).toBe(false)
|
||||
})
|
||||
|
||||
test('a page that is not allowed to go full screen is reported as not supporting it', () => {
|
||||
env = fakeFullscreen({ enabled: false })
|
||||
const { result } = renderHook(() => useFullscreen())
|
||||
expect(result.current.supported).toBe(false)
|
||||
})
|
||||
|
||||
test('toggling out of full screen puts the whole page in full screen', async () => {
|
||||
env = fakeFullscreen()
|
||||
const { result } = renderHook(() => useFullscreen())
|
||||
await act(() => result.current.toggle())
|
||||
expect(env.request).toHaveBeenCalledTimes(1)
|
||||
expect(env.request.mock.contexts[0]).toBe(document.documentElement)
|
||||
expect(result.current.active).toBe(true)
|
||||
})
|
||||
|
||||
test('toggling in full screen leaves it', async () => {
|
||||
env = fakeFullscreen()
|
||||
const { result } = renderHook(() => useFullscreen())
|
||||
await act(() => result.current.toggle())
|
||||
await act(() => result.current.toggle())
|
||||
expect(env.exit).toHaveBeenCalledTimes(1)
|
||||
expect(result.current.active).toBe(false)
|
||||
})
|
||||
|
||||
test('leaving full screen with Esc or F11 is noticed', async () => {
|
||||
env = fakeFullscreen()
|
||||
const { result } = renderHook(() => useFullscreen())
|
||||
await act(() => result.current.toggle())
|
||||
expect(result.current.active).toBe(true)
|
||||
act(() => env!.leave())
|
||||
expect(result.current.active).toBe(false)
|
||||
})
|
||||
|
||||
test('a browser that refuses full screen leaves the page as it was, without an error', async () => {
|
||||
env = fakeFullscreen({ deny: true })
|
||||
const { result } = renderHook(() => useFullscreen())
|
||||
await act(() => result.current.toggle())
|
||||
expect(result.current.active).toBe(false)
|
||||
})
|
||||
|
||||
test('the button is not there where full screen is not available', () => {
|
||||
render(<FullscreenButton />)
|
||||
expect(screen.queryByRole('button')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the button offers full screen, then offering to leave it', async () => {
|
||||
env = fakeFullscreen()
|
||||
render(<FullscreenButton />)
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Enter full screen' }))
|
||||
expect(env.request).toHaveBeenCalledTimes(1)
|
||||
expect(screen.getByRole('button', { name: 'Exit full screen' })).toBeInTheDocument()
|
||||
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Exit full screen' }))
|
||||
expect(env.exit).toHaveBeenCalledTimes(1)
|
||||
expect(screen.getByRole('button', { name: 'Enter full screen' })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the button follows the browser when full screen is left with Esc', async () => {
|
||||
env = fakeFullscreen()
|
||||
render(<FullscreenButton />)
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Enter full screen' }))
|
||||
act(() => env!.leave())
|
||||
expect(screen.getByRole('button', { name: 'Enter full screen' })).toBeInTheDocument()
|
||||
})
|
||||
27
web/src/lib/fullscreen.ts
Normal file
27
web/src/lib/fullscreen.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { useSyncExternalStore } from 'react'
|
||||
|
||||
function subscribe(onChange: () => void): () => void {
|
||||
document.addEventListener('fullscreenchange', onChange)
|
||||
return () => document.removeEventListener('fullscreenchange', onChange)
|
||||
}
|
||||
|
||||
const isActive = () => (document.fullscreenElement ?? null) !== null
|
||||
|
||||
/**
|
||||
* Browser full screen for the whole page. `supported` is false where the browser or the page does not
|
||||
* allow it (iPhone Safari, an embedded frame); `active` follows the browser, so it also notices Esc and F11.
|
||||
*/
|
||||
export function useFullscreen(): { supported: boolean; active: boolean; toggle: () => Promise<void> } {
|
||||
const active = useSyncExternalStore(subscribe, isActive, () => false)
|
||||
|
||||
async function toggle() {
|
||||
try {
|
||||
if (document.fullscreenElement) await document.exitFullscreen()
|
||||
else await document.documentElement.requestFullscreen()
|
||||
} catch {
|
||||
// The browser refused (no user gesture, a policy): the page stays as it was.
|
||||
}
|
||||
}
|
||||
|
||||
return { supported: document.fullscreenEnabled === true, active, toggle }
|
||||
}
|
||||
43
web/src/test/fullscreen.ts
Normal file
43
web/src/test/fullscreen.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { vi } from 'vitest'
|
||||
|
||||
/**
|
||||
* Gives the page a working Fullscreen API: jsdom has none. `leave()` is what the browser does when the
|
||||
* listener presses Esc or F11. Call `restore()` (or let the test file's afterEach do it) to take it away again.
|
||||
*/
|
||||
export function fakeFullscreen(options: { enabled?: boolean; deny?: boolean } = {}) {
|
||||
const enabled = options.enabled ?? true
|
||||
let element: Element | null = null
|
||||
|
||||
const change = () => document.dispatchEvent(new Event('fullscreenchange'))
|
||||
// Only the page itself is ever put in full screen; the tests read `request.mock.contexts` to check it.
|
||||
const request = vi.fn(async () => {
|
||||
if (options.deny) throw new TypeError('Permissions check failed')
|
||||
element = document.documentElement
|
||||
change()
|
||||
})
|
||||
const exit = vi.fn(async () => {
|
||||
element = null
|
||||
change()
|
||||
})
|
||||
|
||||
Object.defineProperty(document, 'fullscreenEnabled', { configurable: true, get: () => enabled })
|
||||
Object.defineProperty(document, 'fullscreenElement', { configurable: true, get: () => element })
|
||||
Object.defineProperty(document.documentElement, 'requestFullscreen', { configurable: true, value: request })
|
||||
Object.defineProperty(document, 'exitFullscreen', { configurable: true, value: exit })
|
||||
|
||||
return {
|
||||
request,
|
||||
exit,
|
||||
leave() {
|
||||
element = null
|
||||
change()
|
||||
},
|
||||
restore() {
|
||||
const doc = document as unknown as Record<string, unknown>
|
||||
delete doc.fullscreenEnabled
|
||||
delete doc.fullscreenElement
|
||||
delete doc.exitFullscreen
|
||||
delete (document.documentElement as unknown as Record<string, unknown>).requestFullscreen
|
||||
},
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue