Skip to content

Commit fa8c68a

Browse files
committed
refactor: update captions toggle and create generic toggle interface
1 parent 58bc002 commit fa8c68a

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

src/components/media/audio/sound.cy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('Sound', () => {
9595
captions={captionsPath}
9696
/>
9797
)
98-
cy.get('[data-cy=captions-toggle]').click()
98+
cy.get('[data-cy=captionsOn-button]').click()
9999
cy.get('track').should('exist').should('have.attr', 'src', captionsPath)
100100
})
101101
it(`should display an error message when no valid sources are found on ${viewport} screen`, () => {

src/components/media/audio/sound.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import './sound.css'
22

3-
import ClosedCaptionDisabledRoundedIcon from '@mui/icons-material/ClosedCaptionDisabledRounded'
4-
import ClosedCaptionRoundedIcon from '@mui/icons-material/ClosedCaptionRounded'
53
import { useMediaQuery, useTheme } from '@mui/material'
64
import Alert from '@mui/material/Alert'
75
import CircularProgress from '@mui/material/CircularProgress'
8-
import IconButton from '@mui/material/IconButton'
96
import Typography from '@mui/material/Typography'
107
import { Box } from '@mui/system'
118
import React, { useEffect, useRef, useState } from 'react'
129

1310
import type { AudioFormat } from '../../types'
1411
import {
12+
CaptionsToggle,
1513
MoveTenSecondsButton,
1614
PausePlayToggle,
1715
PlaybackRateButton,
@@ -76,17 +74,10 @@ export default function Sound(props: AudioFormat) {
7674
function renderCaptionsToggle() {
7775
if (props.captions && props.captions.length > 0) {
7876
return (
79-
<IconButton
80-
onClick={() => setAreCaptionsShown(!areCaptionsShown)}
81-
aria-label={areCaptionsShown ? 'Hide captions' : 'Show captions'}
82-
data-cy="captions-toggle"
83-
>
84-
{areCaptionsShown ? (
85-
<ClosedCaptionRoundedIcon color="primary" />
86-
) : (
87-
<ClosedCaptionDisabledRoundedIcon color="primary" />
88-
)}
89-
</IconButton>
77+
<CaptionsToggle
78+
active={areCaptionsShown}
79+
setActive={() => setAreCaptionsShown(!areCaptionsShown)}
80+
/>
9081
)
9182
}
9283
}
@@ -119,8 +110,8 @@ export default function Sound(props: AudioFormat) {
119110
if (props.transcript) {
120111
return (
121112
<TranscriptToggle
122-
isTranscriptShown={isTranscriptShown}
123-
setIsTranscriptShown={() => setIsTranscriptShown(!isTranscriptShown)}
113+
active={isTranscriptShown}
114+
setActive={() => setIsTranscriptShown(!isTranscriptShown)}
124115
/>
125116
)
126117
}

src/components/media/controls/controls.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ interface MediaIconButtonProps {
2626
| 'pictureInPictureExit'
2727
| 'fullscreen'
2828
| 'fullscreenExit'
29+
| 'captionsOn'
30+
| 'captionsOff'
2931
className?: string
3032
color?: string
3133
}
@@ -35,9 +37,9 @@ interface MediaControls {
3537
color?: string
3638
}
3739

38-
interface TranscriptToggleProps {
39-
isTranscriptShown: boolean
40-
setIsTranscriptShown: () => void
40+
interface Toggle {
41+
active: boolean
42+
setActive: () => void
4143
color?: string
4244
}
4345

@@ -70,6 +72,8 @@ export function MediaIconButton(props: MediaIconButtonProps) {
7072
},
7173
fullscreen: { symbol: 'fullscreen', label: 'fullscreen' },
7274
fullscreenExit: { symbol: 'fullscreen_exit', label: 'exit fullscreen' },
75+
captionsOn: { symbol: 'closed_caption', label: 'show captions' },
76+
captionsOff: { symbol: 'closed_caption_disabled', label: 'hide captions' },
7377
}
7478
return (
7579
<IconButton
@@ -207,18 +211,22 @@ export function VolumeSettings(props: MediaControls) {
207211
)
208212
}
209213

210-
export function TranscriptToggle(props: TranscriptToggleProps) {
211-
const Icon = props.isTranscriptShown
212-
? KeyboardArrowUpIcon
213-
: KeyboardArrowDownIcon
214+
export function CaptionsToggle(props: Toggle) {
215+
const action = props.active ? 'captionsOff' : 'captionsOn'
214216

215-
const buttonText = `${props.isTranscriptShown ? 'Hide' : 'Show'} Transcript`
217+
return <MediaIconButton onClick={props.setActive} action={action} />
218+
}
219+
220+
export function TranscriptToggle(props: Toggle) {
221+
const Icon = props.active ? KeyboardArrowUpIcon : KeyboardArrowDownIcon
222+
223+
const buttonText = `${props.active ? 'Hide' : 'Show'} Transcript`
216224

217225
return (
218226
<Button
219227
className="rustic-transcript-toggle"
220228
data-cy="transcript-toggle"
221-
onClick={props.setIsTranscriptShown}
229+
onClick={props.setActive}
222230
endIcon={<Icon sx={{ color: props.color }} />}
223231
>
224232
<Typography variant="overline" sx={{ color: props.color }}>

src/components/video/video.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ export default function Video(props: VideoFormat) {
132132
if (props.transcript && !isFullscreen) {
133133
return (
134134
<TranscriptToggle
135-
isTranscriptShown={isTranscriptShown}
136-
setIsTranscriptShown={() => setIsTranscriptShown(!isTranscriptShown)}
135+
active={isTranscriptShown}
136+
setActive={() => setIsTranscriptShown(!isTranscriptShown)}
137137
color={color}
138138
/>
139139
)

0 commit comments

Comments
 (0)