-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mobile phones on the dev pages
- Loading branch information
Showing
5 changed files
with
135 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** Based on https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent */ | ||
export function isTouchDevice() { | ||
var hasTouchScreen = false | ||
if ("maxTouchPoints" in navigator) { | ||
hasTouchScreen = navigator.maxTouchPoints > 0 | ||
} else if ("msMaxTouchPoints" in navigator) { | ||
// @ts-ignore | ||
hasTouchScreen = navigator.msMaxTouchPoints > 0 | ||
} else { | ||
var mQ = window.matchMedia && matchMedia("(pointer:coarse)") | ||
if (mQ && mQ.media === "(pointer:coarse)") { | ||
hasTouchScreen = !!mQ.matches | ||
} else if ("orientation" in window) { | ||
hasTouchScreen = true // deprecated, but good fallback | ||
} else { | ||
// Only as a last resort, fall back to user agent sniffing | ||
// @ts-ignore | ||
var UA = navigator.userAgent | ||
hasTouchScreen = | ||
/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) || | ||
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA) | ||
} | ||
} | ||
return hasTouchScreen | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/typescriptlang-org/src/components/SuppressWhenTouch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { useEffect } from "react" | ||
import { isTouchDevice } from "../../lib/utils/isTouchDevice" | ||
|
||
/** | ||
* A React component which will remove its children (at runtime!) | ||
* from the hierarchy if we're on a touch device | ||
*/ | ||
export const SuppressWhenTouch = ({ children, hideOnTouch }: any) => { | ||
|
||
useEffect(() => { | ||
if (isTouchDevice()) { | ||
// It's touch, so let's kill the content in the child and | ||
// replace it with a message that this section isn't good for mobile | ||
const suppressible = document.getElementById("touch-suppressible")! | ||
while (suppressible.firstChild) { | ||
suppressible.removeChild(suppressible.firstChild) | ||
} | ||
|
||
if (hideOnTouch) return | ||
|
||
const h4 = document.createElement("h4") | ||
h4.textContent = "Section best on a computer" | ||
|
||
const p = document.createElement("p") | ||
p.textContent = "This part of the site does not run well on a touch-oriented browser. We recommend switching to a computer to carry on." | ||
|
||
suppressible.appendChild(h4) | ||
suppressible.appendChild(p) | ||
} | ||
|
||
}) | ||
return ( | ||
<div id="touch-suppressible"> | ||
{children} | ||
</div>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters