Skip to content

Commit

Permalink
feat: use MC media-error-dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Dec 5, 2024
1 parent b762184 commit 5001fb0
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 354 deletions.
1 change: 1 addition & 0 deletions examples/vanilla-ts-esm/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h1><a href="/">Elements</a></h1>
<li><a href="./mux-video.html" class="video">&lt;mux-video&gt;</a></li>
<li><a href="./mux-audio.html" class="audio">&lt;mux-audio&gt;</a></li>
<li><a href="./mux-player.html" class="player">&lt;mux-player&gt;</a></li>
<li><a href="./mux-player-error.html" class="player">&lt;mux-player&gt; (error)</a></li>
<li><a href="./mux-player-audio.html" class="player">&lt;mux-player audio&gt;</a></li>
<li><a href="./mux-player-cuepoints.html" class="player">&lt;mux-player&gt; (cuePoints)</a></li>
<li><a href="./mux-player-chapters.html" class="player">&lt;mux-player&gt; (chapters)</a></li>
Expand Down
63 changes: 63 additions & 0 deletions examples/vanilla-ts-esm/public/mux-player-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>&lt;mux-player&gt; example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<link rel="stylesheet" href="./styles.css">
<script type="module" src="./dist/mux-player.js"></script>
<style>
mux-player {
display: block;
width: 100%;
margin: 1rem 0 2rem;
background-color: #000;
}

mux-player:not([audio]) {
aspect-ratio: 16 / 9;
}
</style>
</head>
<body>
<header>
<div class="left-header">
<a class="mux-logo" href="https://www.mux.com/player" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/360826/233653989-11cd8603-c20f-4008-8bf7-dc15b743c52b.svg">
<source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/360826/233653583-50dda726-cbe7-4182-a113-059a91ae83e6.svg">
<img alt="Mux Logo" src="https://user-images.githubusercontent.com/360826/233653583-50dda726-cbe7-4182-a113-059a91ae83e6.svg">
</picture>
</a>
<h1><a href="/">Elements</a></h1>
</div>
<div class="right-header">
<a class="github-logo" href="https://github.com/muxinc/elements" target="_blank">
<img width="32" height="32" src="./images/github-logo.svg" alt="Github logo">
</a>
</div>
</header>

<!-- Correct playbackid: Sc89iWAyNkhJ3P1rQ02nrEdCFTnfT01CZ2KmaEcxXfB008 -->
<mux-player
stream-type="on-demand"
playback-id="Sc89iWAyNkhJ3P1rQ02nrEdCFTnfT01CZ2KmaEcxXfB00"
></mux-player>

<button id="load-video-btn">Load new playback id</button>
<br>
<br>

<!-- Load new playback id -->
<script>
const player = document.querySelector('mux-player');
const loadVideoBtn = document.getElementById('load-video-btn');

loadVideoBtn.addEventListener('click', () => {
player.playbackId = 'Sc89iWAyNkhJ3P1rQ02nrEdCFTnfT01CZ2KmaEcxXfB008';
});
</script>

<a href="../">Browse Elements</a>
</body>
</html>
55 changes: 0 additions & 55 deletions packages/mux-player/src/dialog.ts

This file was deleted.

49 changes: 42 additions & 7 deletions packages/mux-player/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { globalThis, document } from './polyfills';
import { MediaController } from 'media-chrome';
import { MediaController, MediaErrorDialog } from 'media-chrome';
import { Attributes as MediaControllerAttributes } from 'media-chrome/dist/media-container.js';
import { MediaUIAttributes } from 'media-chrome/dist/constants.js';
import 'media-chrome/dist/experimental/index.js';
Expand Down Expand Up @@ -179,6 +179,34 @@ function getProps(el: MuxPlayerElement, state?: any): MuxTemplateProps {
return props;
}

MediaErrorDialog.formatErrorMessage = (error: { code?: number; message?: string }) => {
if (error instanceof MediaError) {
const { dialog } = getErrorLogs(error, false);
return `
${dialog?.title ? `<h3>${dialog.title}</h3>` : ''}
${
dialog?.message || dialog?.linkUrl
? `<p>
${dialog?.message}
${
dialog?.linkUrl
? `<a
href="${dialog.linkUrl}"
target="_blank"
rel="external noopener"
aria-label="${dialog.linkText ?? ''} ${i18n(`(opens in a new window)`)}"
>${dialog.linkText ?? dialog.linkUrl}</a
>`
: ''
}
</p>`
: ''
}
`;
}
return error.message ?? '';
};

function getThemeTemplate(el: MuxPlayerElement) {
let themeName = el.theme;

Expand Down Expand Up @@ -234,7 +262,6 @@ export const playerSoftwareVersion = getPlayerVersion();
export const playerSoftwareName = 'mux-player';

const initialState = {
dialog: undefined,
isDialogOpen: false,
};

Expand Down Expand Up @@ -289,10 +316,18 @@ class MuxPlayerElement extends VideoApiElement implements MuxPlayerElement {
#hotkeys = new AttributeTokenList(this, 'hotkeys');
#state: Partial<MuxTemplateProps> = {
...initialState,
onCloseErrorDialog: () => this.#setState({ dialog: undefined, isDialogOpen: false }),
onInitFocusDialog: (e) => {
onCloseErrorDialog: (event) => {
const localName = (event.composedPath()[0] as HTMLElement)?.localName;
if (localName !== 'media-error-dialog') return;

this.#setState({ isDialogOpen: false });
},
onFocusInErrorDialog: (event) => {
const localName = (event.composedPath()[0] as HTMLElement)?.localName;
if (localName !== 'media-error-dialog') return;

const isFocusedElementInPlayer = containsComposedNode(this, document.activeElement);
if (!isFocusedElementInPlayer) e.preventDefault();
if (!isFocusedElementInPlayer) event.preventDefault();
},
};

Expand Down Expand Up @@ -467,7 +502,7 @@ class MuxPlayerElement extends VideoApiElement implements MuxPlayerElement {
return;
}

const { dialog, devlog } = getErrorLogs(error, false);
const { devlog } = getErrorLogs(error, false);

if (devlog.message) {
logger.devlog(devlog);
Expand All @@ -478,7 +513,7 @@ class MuxPlayerElement extends VideoApiElement implements MuxPlayerElement {
logger.error(`${error.name} data:`, error.data);
}

this.#setState({ isDialogOpen: true, dialog });
this.#setState({ isDialogOpen: true });
};

// Keep this event listener on mux-player instead of calling onError directly
Expand Down
Loading

0 comments on commit 5001fb0

Please sign in to comment.