From 606845ae7250d075aadb7fcc6d2a64212becbf97 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 15:22:57 +0100 Subject: [PATCH] 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. --- web/src/components/VolumeControl.test.tsx | 61 +++++++++++++++++++++++ web/src/components/VolumeControl.tsx | 38 ++++++++++++++ web/src/i18n/locales/en.json | 5 +- web/src/i18n/locales/it.json | 5 +- 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 web/src/components/VolumeControl.test.tsx create mode 100644 web/src/components/VolumeControl.tsx diff --git a/web/src/components/VolumeControl.test.tsx b/web/src/components/VolumeControl.test.tsx new file mode 100644 index 0000000..ebf7a8f --- /dev/null +++ b/web/src/components/VolumeControl.test.tsx @@ -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() + 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() + 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() + 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() + 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() + 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() + expect(slider().className).toContain('max-sm:hidden') + expect(screen.getByRole('button', { name: 'Mute' }).className).not.toContain('max-sm:hidden') +}) diff --git a/web/src/components/VolumeControl.tsx b/web/src/components/VolumeControl.tsx new file mode 100644 index 0000000..7eb1311 --- /dev/null +++ b/web/src/components/VolumeControl.tsx @@ -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 ( +
+ + setVolume(Number(e.target.value) / 100)} + className="h-11 w-24 cursor-pointer accent-primary max-sm:hidden" + /> +
+ ) +} diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index f395c88..a407f7b 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -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", diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index 589427b..f08d7aa 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -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",