-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(player): new keyboard action display in default layout
- Loading branch information
Showing
20 changed files
with
493 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/react/src/components/layouts/default/keyboard-action-display.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import * as React from 'react'; | ||
|
||
import { computed, effect, scoped, useContext } from 'maverick.js'; | ||
import { useReactScope, useSignal } from 'maverick.js/react'; | ||
import { camelToKebabCase } from 'maverick.js/std'; | ||
import { mediaContext, type DefaultLayoutTranslations } from 'vidstack'; | ||
|
||
import { useMediaState } from '../../../hooks/use-media-state'; | ||
import { DefaultLayoutContext, i18n } from './context'; | ||
import type { DefaultLayoutIcons } from './icons'; | ||
|
||
function DefaultVideoKeyboardActionDisplay() { | ||
const scope = useReactScope(), | ||
{ translations, Icons, noKeyboardActionDisplay } = React.useContext(DefaultLayoutContext), | ||
[visible, setVisible] = React.useState(false), | ||
[Icon, setIcon] = React.useState<any>(null), | ||
$lastKeyboardAction = useMediaState('lastKeyboardAction'); | ||
|
||
const actionDataAttr = React.useMemo(() => { | ||
const action = $lastKeyboardAction?.action; | ||
return action && visible ? camelToKebabCase(action) : null; | ||
}, [visible, $lastKeyboardAction]); | ||
|
||
const className = React.useMemo(() => `vds-kb-action${!visible ? ' hidden' : ''}`, [visible]); | ||
|
||
const $$text = React.useMemo(() => scoped(() => computed(getText), scope)!, [scope]), | ||
$text = useSignal($$text); | ||
|
||
const $$statusLabel = React.useMemo( | ||
() => scoped(() => computed(() => getStatusLabel(translations)), scope)!, | ||
[scope, translations], | ||
), | ||
$statusLabel = useSignal($$statusLabel); | ||
|
||
React.useEffect(() => { | ||
scoped(() => { | ||
effect(() => { | ||
const Icon = getIcon(Icons.KeyboardAction); | ||
setIcon(() => Icon); | ||
}); | ||
}, scope); | ||
}, [scope, Icons]); | ||
|
||
React.useEffect(() => { | ||
setVisible(!!$lastKeyboardAction); | ||
const id = setTimeout(() => setVisible(false), 500); | ||
return () => { | ||
setVisible(false); | ||
window.clearTimeout(id); | ||
}; | ||
}, [$lastKeyboardAction]); | ||
|
||
if (noKeyboardActionDisplay) return null; | ||
|
||
return ( | ||
<div className={className} data-action={actionDataAttr}> | ||
<div className="vds-kb-text-wrapper"> | ||
<div className="vds-kb-text">{$text}</div> | ||
</div> | ||
{Icon ? ( | ||
<div className="vds-kb-bezel" role="status" aria-label={$statusLabel}> | ||
<div className="vds-kb-icon"> | ||
<Icon /> | ||
</div> | ||
</div> | ||
) : null} | ||
</div> | ||
); | ||
} | ||
|
||
DefaultVideoKeyboardActionDisplay.displayName = 'DefaultVideoKeyboardActionDisplay'; | ||
export { DefaultVideoKeyboardActionDisplay }; | ||
|
||
function getText() { | ||
const { $state } = useContext(mediaContext), | ||
action = $state.lastKeyboardAction()?.action; | ||
switch (action) { | ||
case 'toggleMuted': | ||
return $state.muted() ? '0%' : getVolumeText($state.volume()); | ||
case 'volumeUp': | ||
case 'volumeDown': | ||
return getVolumeText($state.volume()); | ||
default: | ||
return ''; | ||
} | ||
} | ||
|
||
function getVolumeText(volume: number) { | ||
return `${Math.round(volume * 100)}%`; | ||
} | ||
|
||
function getIcon(Icons?: DefaultLayoutIcons['KeyboardAction']) { | ||
const { $state } = useContext(mediaContext), | ||
action = $state.lastKeyboardAction()?.action; | ||
switch (action) { | ||
case 'togglePaused': | ||
return !$state.paused() ? Icons?.Play : Icons?.Pause; | ||
case 'toggleMuted': | ||
return $state.muted() || $state.volume() === 0 | ||
? Icons?.Mute | ||
: $state.volume() >= 0.5 | ||
? Icons?.VolumeUp | ||
: Icons?.VolumeDown; | ||
case 'toggleFullscreen': | ||
return $state.fullscreen() ? Icons?.EnterFullscreen : Icons?.ExitFullscreen; | ||
case 'togglePictureInPicture': | ||
return $state.pictureInPicture() ? Icons?.EnterPiP : Icons?.ExitPiP; | ||
case 'toggleCaptions': | ||
return $state.hasCaptions() | ||
? $state.textTrack() | ||
? Icons?.CaptionsOn | ||
: Icons?.CaptionsOff | ||
: null; | ||
case 'volumeUp': | ||
return Icons?.VolumeUp; | ||
case 'volumeDown': | ||
return Icons?.VolumeDown; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
function getStatusLabel(translations?: DefaultLayoutTranslations | null) { | ||
const text = getStatusText(translations); | ||
return text ? i18n(translations, text) : null; | ||
} | ||
|
||
function getStatusText(translations?: DefaultLayoutTranslations | null): any { | ||
const { $state } = useContext(mediaContext), | ||
action = $state.lastKeyboardAction()?.action; | ||
switch (action) { | ||
case 'togglePaused': | ||
return !$state.paused() ? 'Play' : 'Pause'; | ||
case 'toggleFullscreen': | ||
return $state.fullscreen() ? 'Enter Fullscreen' : 'Exit Fullscreen'; | ||
case 'togglePictureInPicture': | ||
return $state.pictureInPicture() ? 'Enter PiP' : 'Exit PiP'; | ||
case 'toggleCaptions': | ||
return $state.textTrack() ? 'Closed-Captions On' : 'Closed-Captions Off'; | ||
case 'toggleMuted': | ||
case 'volumeUp': | ||
case 'volumeDown': | ||
return $state.muted() || $state.volume() === 0 | ||
? 'Mute' | ||
: `${Math.round($state.volume() * 100)}% ${i18n(translations, 'Volume')}`; | ||
default: | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.