Skip to content

Commit

Permalink
fix(player): hide chapters menu button when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Mar 20, 2024
1 parent edddf3b commit 9e96b33
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/vidstack/player/styles/default/menus.css
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@
overflow: hidden;
}

.vds-menu-button[aria-disabled='true'],
.vds-menu-item[aria-disabled='true'],
.vds-menu-item[data-disabled] {
display: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export class ChaptersRadioGroup extends Component<

if (activeCueIndex >= 0) {
requestScopedAnimationFrame(() => {
if (!this.connectScope) return;

const cue = this._cues()[activeCueIndex],
radio = this.el!.querySelector(`[aria-checked='true']`),
cueStartTime = Math.max(startTime, cue.startTime),
Expand Down
17 changes: 14 additions & 3 deletions packages/vidstack/src/core/tracks/text/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Dispose } from 'maverick.js';
import { getScope, onDispose, scoped, type Dispose } from 'maverick.js';
import { isString, listenEvent } from 'maverick.js/std';
import type { VTTCue } from 'media-captions';

Expand All @@ -22,7 +22,8 @@ export function watchActiveTextTrack(
kind: TextTrackKind | TextTrackKind[],
onChange: (track: TextTrack | null) => void,
): Dispose {
let currentTrack: TextTrack | null = null;
let currentTrack: TextTrack | null = null,
scope = getScope();

function onModeChange() {
const kinds = isString(kind) ? [kind] : kind,
Expand All @@ -42,7 +43,17 @@ export function watchActiveTextTrack(
onChange(track);
} else {
onChange(null);
track.addEventListener('load', () => onChange(track), { once: true });
scoped(() => {
const off = listenEvent(
track,
'load',
() => {
onChange(track);
off();
},
{ once: true },
);
}, scope);
}

currentTrack = track;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { isFunction, unwrap } from 'maverick.js/std';
import { useDefaultLayoutContext } from '../../../../../../components/layouts/default/context';
import type { MenuPlacement } from '../../../../../../components/ui/menu/menu-items';
import type { TooltipPlacement } from '../../../../../../components/ui/tooltip/tooltip-content';
import { useMediaState } from '../../../../../../core/api/media-context';
import { TextTrack, watchActiveTextTrack } from '../../../../../../core';
import { useMediaContext, useMediaState } from '../../../../../../core/api/media-context';
import { $signal } from '../../../../../lit/directives/signal';
import { IconSlot } from '../../slots';
import { $i18n } from '../utils';
Expand All @@ -20,7 +21,8 @@ export function DefaultChaptersMenu({
placement: MenuPlacement | ReadSignal<MenuPlacement | null>;
tooltip: TooltipPlacement | ReadSignal<TooltipPlacement>;
}) {
const { viewType } = useMediaState(),
const { textTracks } = useMediaContext(),
{ viewType, clipStartTime, clipEndTime } = useMediaState(),
{
translations,
thumbnails,
Expand All @@ -29,7 +31,23 @@ export function DefaultChaptersMenu({
menuGroup,
smallWhen: smWhen,
} = useDefaultLayoutContext(),
$placement = computed(() =>
$disabled = computed(() => {
const $startTime = clipStartTime(),
$endTime = clipEndTime() || Infinity,
$track = signal<TextTrack | null>(null);

watchActiveTextTrack(textTracks, 'chapters', $track.set);

const cues = $track()?.cues.filter(
(cue) => cue.startTime <= $endTime && cue.endTime >= $startTime,
);

return !cues?.length;
});

if ($disabled()) return null;

const $placement = computed(() =>
noModal() ? unwrap(placement) : !smWhen() ? unwrap(placement) : null,
),
$offset = computed(() =>
Expand Down

0 comments on commit 9e96b33

Please sign in to comment.