feat(web): a volume control with a mute button and a slider

The slider shows zero while muted and moving it unmutes; on a phone the slider is hidden, since the device has its own volume, and the button stays.
This commit is contained in:
tgiachi 2026-11-12 15:22:57 +01:00
commit 606845ae72
4 changed files with 107 additions and 2 deletions

View file

@ -0,0 +1,61 @@
import { act, fireEvent, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, expect, test, vi } from 'vitest'
import { initI18n } from '@/i18n'
import { usePlayer } from '@/player/store'
import VolumeControl from './VolumeControl'
beforeEach(async () => {
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
await initI18n()
localStorage.clear()
usePlayer.setState({ volume: 0.5, muted: false })
})
const slider = () => screen.getByRole('slider', { name: 'Volume' })
test('it has a mute button and a slider that shows the level in percent', () => {
render(<VolumeControl />)
expect(screen.getByRole('button', { name: 'Mute' })).toBeInTheDocument()
expect(slider()).toHaveValue('50')
expect(slider()).toHaveAttribute('aria-valuetext', '50%')
expect(slider()).toHaveAttribute('min', '0')
expect(slider()).toHaveAttribute('max', '100')
})
test('moving the slider sets the volume', () => {
render(<VolumeControl />)
fireEvent.change(slider(), { target: { value: '30' } })
expect(usePlayer.getState().volume).toBe(0.3)
})
test('the slider follows the volume when it is changed elsewhere', () => {
render(<VolumeControl />)
act(() => usePlayer.getState().setVolume(0.8))
expect(slider()).toHaveValue('80')
})
test('the mute button mutes, says so, and the slider falls to zero without losing the level', async () => {
render(<VolumeControl />)
await userEvent.click(screen.getByRole('button', { name: 'Mute' }))
expect(usePlayer.getState()).toMatchObject({ muted: true, volume: 0.5 })
expect(screen.getByRole('button', { name: 'Unmute' })).toBeInTheDocument()
expect(slider()).toHaveValue('0')
expect(slider()).toHaveAttribute('aria-valuetext', '0%')
await userEvent.click(screen.getByRole('button', { name: 'Unmute' }))
expect(usePlayer.getState()).toMatchObject({ muted: false, volume: 0.5 })
expect(slider()).toHaveValue('50')
})
test('moving the slider while muted unmutes', () => {
usePlayer.setState({ muted: true })
render(<VolumeControl />)
fireEvent.change(slider(), { target: { value: '70' } })
expect(usePlayer.getState()).toMatchObject({ muted: false, volume: 0.7 })
})
test('on a phone the slider is hidden, since the device has its own volume, and the button stays', () => {
render(<VolumeControl />)
expect(slider().className).toContain('max-sm:hidden')
expect(screen.getByRole('button', { name: 'Mute' }).className).not.toContain('max-sm:hidden')
})

View file

@ -0,0 +1,38 @@
import { Volume1, Volume2, VolumeX } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
import { usePlayer } from '@/player/store'
/**
* A mute button and a volume slider. While muted the slider shows zero, and moving it unmutes. On a phone
* the slider is hidden (the device has its own volume) and the button stays.
*/
export default function VolumeControl({ className }: { className?: string }) {
const { t } = useTranslation()
const volume = usePlayer((s) => s.volume)
const muted = usePlayer((s) => s.muted)
const setVolume = usePlayer((s) => s.setVolume)
const toggleMute = usePlayer((s) => s.toggleMute)
const percent = muted ? 0 : Math.round(volume * 100)
const Icon = percent === 0 ? VolumeX : percent < 50 ? Volume1 : Volume2
return (
<div className={cn('flex items-center gap-1', className)}>
<Button variant="ghost" className="size-11 text-muted-foreground" aria-label={muted ? t('player.unmute') : t('player.mute')} onClick={toggleMute}>
<Icon aria-hidden="true" />
</Button>
<input
type="range"
min={0}
max={100}
step={1}
value={percent}
aria-label={t('player.volume')}
aria-valuetext={`${percent}%`}
onChange={(e) => setVolume(Number(e.target.value) / 100)}
className="h-11 w-24 cursor-pointer accent-primary max-sm:hidden"
/>
</div>
)
}

View file

@ -43,7 +43,10 @@
"close": "Close",
"upNext": "Up next",
"queue": "Queue",
"nowPlaying": "Now playing"
"nowPlaying": "Now playing",
"volume": "Volume",
"mute": "Mute",
"unmute": "Unmute"
},
"settings": {
"account": "Account",

View file

@ -43,7 +43,10 @@
"close": "Chiudi",
"upNext": "Prossimi brani",
"queue": "Coda",
"nowPlaying": "In riproduzione"
"nowPlaying": "In riproduzione",
"volume": "Volume",
"mute": "Silenzia",
"unmute": "Riattiva l'audio"
},
"settings": {
"account": "Account",