Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
fix of screen.orientation bug 🪲 (#164)
Browse files Browse the repository at this point in the history
* Just a quick remainder that you made the world a little better just by being yourself. Also a fix of screen.orientation bug

* Comment about the solution

* Prettier
  • Loading branch information
pkrucz00 committed May 10, 2023
1 parent 6a02683 commit fa6d702
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions assets/src/features/shared/hooks/useSmartphoneViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@ const useSmartphoneViewport = (): ScreenInfo => {

useEffect(() => {
if (typeof window !== "undefined") {
// The screen.orientation.type is widely available on most of the devices but it was recently added to mobile safari.
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation
// At the day of writing this comment (May 8th 2023) some web apps like Chrome on iOS does NOT implement this.
// The `windows.matchMedia` is used as a fallback for such devices.
const isLandscape =
screen.orientation?.type.includes("landscape") || window.matchMedia("(orientation: landscape)").matches;

const updateIsSmartphoneState = () => {
if (!window.visualViewport) return;

const isCoarse = matchMedia("(pointer:coarse)").matches;
const hasMobileWidth = window.visualViewport.width <= MOBILE_WIDTH_BREAKPOINT;

const isLandscapedSmartphone =
screen.orientation.type.includes("landscape") && window.visualViewport.width <= MAX_MOBILE_WIDTH_BREAKPOINT; // iPhone 12 max viewpor width
const isLandscapedSmartphone = isLandscape && window.visualViewport.width <= MAX_MOBILE_WIDTH_BREAKPOINT; // iPhone 12 max viewpor width
setIsSmartphone(isCoarse && (hasMobileWidth || isLandscapedSmartphone));
};

const updateIsHorizontalOrientationState = () => {
if (!screen) return;

setIsHorizontal(screen.orientation.type.includes("landscape"));
setIsHorizontal(isLandscape);
};

const updateState = () => {
Expand Down

0 comments on commit fa6d702

Please sign in to comment.