Skip to content

Open-source music player for YouTube Music's catalog, with genres, full albums and playlists. Windows, macOS, Linux, Android, iPhone and Docker. No account needed.

TypeScript

tv: focus trapped in dialogs/sheets (auto focus on open, restore on close, arrows stay inside), TV-sized sheet panel, Back goes up a screen

vaha7315eb826427
diff --git a/web/src/tv/tv.css b/web/src/tv/tv.css
index d0b5d0b..16d0b23 100644
--- a/web/src/tv/tv.css
+++ b/web/src/tv/tv.css
@@ -115,3 +115,32 @@ html[data-tv="1"] input[type="text"] {
font-size: 1.05em;
padding-block: 0.6em;
}
+
+/* ---- dialogs / sheets ----------------------------------------------------
+ On TV a sheet is a centred panel with its own scrolling; navigation is
+ trapped inside it by tvMode.ts while it is open. */
+html[data-tv="1"] [role="dialog"] {
+ max-width: min(1100px, 78vw) !important;
+ max-height: 80vh !important;
+ overflow-y: auto !important;
+ border-radius: 22px !important;
+ margin: auto !important;
+ box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6) !important;
+}
+
+html[data-tv="1"] [role="dialog"] h2 {
+ font-size: 1.35em;
+}
+
+html[data-tv="1"] [role="dialog"] li,
+html[data-tv="1"] [role="dialog"] button,
+html[data-tv="1"] [role="dialog"] [role="radio"] {
+ border-radius: 14px;
+}
+
+/* keep list entries apart so the focus ring has room */
+html[data-tv="1"] [role="dialog"] ul {
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+}
diff --git a/web/src/tv/tvMode.ts b/web/src/tv/tvMode.ts
index 850b429..657dd6d 100644
--- a/web/src/tv/tvMode.ts
+++ b/web/src/tv/tvMode.ts
@@ -38,8 +38,17 @@ function shown(el: HTMLElement): boolean {
return r.width >= 4 && r.height >= 4;
}
+/** The topmost open dialog/sheet, if any. While one is open the D-pad stays inside it. */
+function activeDialog(): HTMLElement | null {
+ const nodes = Array.from(
+ document.querySelectorAll<HTMLElement>('[role="dialog"], [aria-modal="true"], dialog[open]'),
+ ).filter(shown);
+ return nodes.length ? nodes[nodes.length - 1] : null;
+}
+
function focusables(): HTMLElement[] {
- return Array.from(document.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(shown);
+ const scope: ParentNode = activeDialog() ?? document;
+ return Array.from(scope.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(shown);
}
function box(el: HTMLElement) {
@@ -137,6 +146,31 @@ function activate(): boolean {
return true;
}
+let lastOutside: HTMLElement | null = null;
+
+/** Focus the first control of a freshly opened dialog, and put focus back when it closes. */
+function watchDialogs() {
+ let current: HTMLElement | null = null;
+ const sync = () => {
+ const dialog = activeDialog();
+ if (dialog === current) return;
+ if (dialog) {
+ lastOutside = document.activeElement instanceof HTMLElement ? document.activeElement : null;
+ current = dialog;
+ window.setTimeout(() => {
+ const first = focusables()[0];
+ if (first) first.focus({ preventScroll: true });
+ }, 60);
+ } else {
+ current = null;
+ if (lastOutside && document.contains(lastOutside)) lastOutside.focus({ preventScroll: true });
+ lastOutside = null;
+ }
+ };
+ new MutationObserver(sync).observe(document.body, { childList: true, subtree: true });
+ sync();
+}
+
let installed = false;
export function installTvMode() {
@@ -169,6 +203,15 @@ export function installTvMode() {
return;
}
+ if (key === "Escape" || key === "BrowserBack" || key === "GoBack") {
+ if (activeDialog()) return; // the app's own Escape handler closes it
+ if (window.history.length > 1) {
+ handled();
+ window.history.back();
+ }
+ return;
+ }
+
if ((key === "Enter" || key === " ") && !typing) {
if (document.activeElement instanceof HTMLButtonElement) return;
if (activate()) handled();
@@ -177,6 +220,8 @@ export function installTvMode() {
{ capture: true },
);
+ watchDialogs();
+
const focusFirst = () => {
const els = focusables();
if (document.activeElement === document.body && els.length) els[0].focus({ preventScroll: true });