Skip to content

Commit f1bb4ea

Browse files
Add more mobile device detection for hiding TryExamples buttons
1 parent af7f8cd commit f1bb4ea

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

jupyterlite_sphinx/jupyterlite_sphinx.js

+66
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,42 @@ var tryExamplesGlobalMinHeight = 0;
149149
*/
150150
var tryExamplesConfigLoaded = false;
151151

152+
// This function is used to check if the current device is a mobile device.
153+
// We assume the authenticity of the user agent string is enough to
154+
// determine that, and we also check the window size as a fallback.
155+
window.isMobileDevice = () => {
156+
const mobilePatterns = [
157+
/Android/i,
158+
/webOS/i,
159+
/iPhone/i,
160+
/iPad/i,
161+
/iPod/i,
162+
/BlackBerry/i,
163+
/IEMobile/i,
164+
/Windows Phone/i,
165+
/Opera Mini/i,
166+
/SamsungBrowser/i,
167+
/UC.*Browser|UCWEB/i,
168+
/MiuiBrowser/i,
169+
/Mobile/i,
170+
/Tablet/i,
171+
];
172+
173+
const isMobileByUA = mobilePatterns.some((pattern) =>
174+
pattern.test(navigator.userAgent),
175+
);
176+
const isMobileBySize = window.innerWidth <= 480 || window.innerHeight <= 480;
177+
const isLikelyMobile = isMobileByUA || isMobileBySize;
178+
179+
if (isLikelyMobile) {
180+
console.log(
181+
"Mobile device detected, disabling interactive example buttons to conserve bandwidth.",
182+
);
183+
}
184+
185+
return isLikelyMobile;
186+
};
187+
152188
// A config loader with improved error handling + request deduplication
153189
const ConfigLoader = (() => {
154190
// setting a private state for managing requests and errors
@@ -173,6 +209,15 @@ const ConfigLoader = (() => {
173209
};
174210

175211
const loadConfig = async (configFilePath) => {
212+
if (window.isMobileDevice()) {
213+
const buttons = document.getElementsByClassName("try_examples_button");
214+
for (let i = 0; i < buttons.length; i++) {
215+
buttons[i].classList.add("hidden");
216+
}
217+
tryExamplesConfigLoaded = true; // mock it
218+
return;
219+
}
220+
176221
if (tryExamplesConfigLoaded) {
177222
return;
178223
}
@@ -254,6 +299,27 @@ const ConfigLoader = (() => {
254299
};
255300
})();
256301

302+
// Add a resize handler that will update the buttons' visibility on
303+
// orientation changes
304+
let resizeTimeout;
305+
window.addEventListener("resize", () => {
306+
clearTimeout(resizeTimeout);
307+
resizeTimeout = setTimeout(() => {
308+
if (!tryExamplesConfigLoaded) return; // since we won't interfere if the config isn't loaded
309+
310+
const buttons = document.getElementsByClassName("try_examples_button");
311+
const shouldHide = window.isMobileDevice();
312+
313+
for (let i = 0; i < buttons.length; i++) {
314+
if (shouldHide) {
315+
buttons[i].classList.add("hidden");
316+
} else {
317+
buttons[i].classList.remove("hidden");
318+
}
319+
}
320+
}, 250);
321+
});
322+
257323
window.loadTryExamplesConfig = ConfigLoader.loadConfig;
258324

259325
window.toggleTryExamplesButtons = () => {

0 commit comments

Comments
 (0)