-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update device and browser check logics
- Loading branch information
1 parent
8f553fd
commit d541c91
Showing
2 changed files
with
17 additions
and
20 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
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 |
---|---|---|
@@ -1,21 +1,18 @@ | ||
import { detect } from 'detect-browser' | ||
export const isInstalledOnHomeScreen = window.matchMedia('(display-mode: standalone)').matches | ||
|
||
const browser = detect() | ||
export const isMobileBrowser = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | ||
|
||
export const isInstalledOnHomescreen = () => { | ||
// on Android and iOS, display mode is set to | ||
// standalone when the app is opened from home screen | ||
const displayIsStandalone = window.matchMedia('(display-mode: standalone)').matches | ||
export const isAppleMobile = /iPhone|iPad|iPod/i.test(navigator.userAgent) | ||
|
||
return displayIsStandalone | ||
} | ||
/** | ||
* vendor is deprecated but it's the only way to detect if the browser is belongs to Apple product (Safari) | ||
* There are variety of browsers that are based on Chromium (Chrome, Arc, Brave, etc.) but `navigator` object doesn't provide a unique way to detect them. | ||
* `userAgent` or other fields doesn't give us ability to check if it's Safari or not. | ||
* So, we need to check the vendor to make sure that the browser is Safari to make conditional rendering. | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vendor | ||
*/ | ||
const isAppleVendor = /Apple Computer, Inc./i.test(navigator.vendor) | ||
|
||
export const isMobileBrowser = () => /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | ||
export const isSafari = isAppleVendor && navigator.userAgent.match(/Safari/i) | ||
|
||
export const isIOS = () => /iPhone|iPad|iPod/i.test(navigator.userAgent) | ||
|
||
export const isChrome = () => | ||
browser?.name ? /chrome|crios|edge-chromium/i.test(browser?.name) : false | ||
|
||
export const isMobileButNotInstalledOnHomeScreen = () => | ||
isMobileBrowser() && !isInstalledOnHomescreen() | ||
export const isMobileButNotInstalledOnHomeScreen = isMobileBrowser && !isInstalledOnHomeScreen |