Skip to content

Commit 194f6a0

Browse files
authored
Fix control + k keyboard shortcut (#1571)
* Fix control + k keyboard shortcut * Update src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
1 parent 3381187 commit 194f6a0

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ var addEventListenerForSearchKeyboard = () => {
241241
!event.shiftKey &&
242242
!event.altKey &&
243243
// On Mac use ⌘, all other OS use Ctrl
244-
(isMac
244+
(useCommandKey
245245
? event.metaKey && !event.ctrlKey
246246
: !event.metaKey && event.ctrlKey) &&
247247
// Case-insensitive so the shortcut still works with caps lock
@@ -260,28 +260,23 @@ var addEventListenerForSearchKeyboard = () => {
260260
};
261261

262262
/**
263-
* Find out if we're on a Mac
263+
* If the user is on a Mac, use command (⌘) instead of control (ctrl) key
264+
*
265+
* Note: `navigator.platform` is deprecated; however MDN still recommends using
266+
* it for the one specific use case of detecting whether a keyboard shortcut
267+
* should use control or command:
268+
* https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#examples
264269
*/
265-
var isMac = (navigator) => {
266-
var platform = "";
267-
if (
268-
typeof navigator.userAgentData !== "undefined" &&
269-
navigator.userAgentData != null
270-
) {
271-
platform = navigator.userAgentData.platform;
272-
} else if (typeof navigator.platform !== "undefined") {
273-
platform = navigator.platform;
274-
}
275-
return /mac.?os/.test(platform.toLowerCase());
276-
};
270+
var useCommandKey =
271+
navigator.platform.indexOf("Mac") === 0 || navigator.platform === "iPhone";
277272

278273
/**
279274
* Change the search hint to `meta key` if we are a Mac
280275
*/
281276

282277
var changeSearchShortcutKey = () => {
283278
let shortcuts = document.querySelectorAll(".search-button__kbd-shortcut");
284-
if (isMac(window.navigator)) {
279+
if (useCommandKey) {
285280
shortcuts.forEach(
286281
(f) => (f.querySelector("kbd.kbd-shortcut__modifier").innerText = "⌘")
287282
);

0 commit comments

Comments
 (0)