Skip to content

Commit 211676f

Browse files
committed
web: Add support for RTL context menu
This patch adds support for RTL context menu. The context menu will use RTL when the preferred language of the user is RTL. This patch will work only for browsers supporting Intl.Locale.getTextInfo, which sadly does not include Firefox.
1 parent c55d624 commit 211676f

File tree

1 file changed

+29
-5
lines changed
  • web/packages/core/src/internal/player

1 file changed

+29
-5
lines changed

web/packages/core/src/internal/player/inner.tsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,7 @@ export class InnerPlayer {
282282
this.contextMenuElement.addEventListener("contextmenu", preserveMenu);
283283
this.contextMenuElement.addEventListener("click", preserveMenu);
284284

285-
// TODO Add support for RTL context menu.
286-
// It should use the direction of the browser, not the document.
287-
// See <https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language>
288-
// See <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo>
289-
this.contextMenuElement.dir = 'ltr';
285+
this.contextMenuElement.dir = detectBrowserDirection();
290286

291287
document.documentElement.addEventListener(
292288
"pointerdown",
@@ -2328,3 +2324,31 @@ function parseAllowScriptAccess(
23282324
return null;
23292325
}
23302326
}
2327+
2328+
/**
2329+
* Detect direction (LTR/RTL) of the preferred language of the browser.
2330+
*
2331+
* @returns 'ltr' or 'rtl' depending on the detected direction
2332+
*/
2333+
function detectBrowserDirection(): string {
2334+
const browserLocale = new Intl.Locale(navigator.language);
2335+
2336+
let textInfo = null;
2337+
if ('getTextInfo' in browserLocale &&
2338+
typeof browserLocale.getTextInfo === 'function') {
2339+
textInfo = browserLocale.getTextInfo();
2340+
} else if ('textInfo' in browserLocale &&
2341+
typeof browserLocale.textInfo === 'object') {
2342+
textInfo = browserLocale.textInfo;
2343+
} else {
2344+
return 'ltr';
2345+
}
2346+
2347+
if (typeof textInfo === 'object' &&
2348+
'direction' in textInfo &&
2349+
typeof textInfo.direction === 'string') {
2350+
return textInfo.direction || 'ltr';
2351+
}
2352+
2353+
return 'ltr';
2354+
}

0 commit comments

Comments
 (0)