Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(selectors): SA/Add isWebView #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Available selectors:
| isConsole | bool | returns true if device type is `console` |
| isEmbedded | bool | returns true if device type is `embedded` |
| isAndroid | bool | returns true if os type is `Android` |
| isWebView | bool | returns true if is WebView `mobile` |
| isWinPhone | bool | returns true if os type is `Windows Phone` |
| isIOS | bool | returns true if os type is `iOS` |
| isChrome | bool | returns true if browser is `Chrome` |
Expand Down
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare module "react-device-detect" {
export import isChromium = ReactDeviceDetect.isChromium;
export import isMobileOnly = ReactDeviceDetect.isMobileOnly;
export import isAndroid = ReactDeviceDetect.isAndroid;
export import isWebView = ReactDeviceDetect.isWebView;
export import isWinPhone = ReactDeviceDetect.isWinPhone;
export import isIOS = ReactDeviceDetect.isIOS;
export import isChrome = ReactDeviceDetect.isChrome;
Expand Down Expand Up @@ -188,8 +189,10 @@ declare namespace ReactDeviceDetect {
export const isChromium: boolean;

export const isMobileOnly: boolean;

export const isAndroid: boolean;

export const isWebView: boolean;

export const isWinPhone: boolean;

Expand Down
11 changes: 11 additions & 0 deletions src/components/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isSmartTV,
isMobileOnly,
isWearable,
isWebView,
isConsole,
isTablet,
} from '../lib/selectors';
Expand Down Expand Up @@ -119,6 +120,16 @@ export const ConsoleView = ({ renderWithFragment, children, ...props }) => {
) : null;
};

export const WebView = ({ renderWithFragment, children, ...props }) => {
return isWebView ? (
renderWithFragment ? (
<Fragment>{children}</Fragment>
) : (
<div {...props}>{children}</div>
)
) : null;
};

export const WearableView = ({ renderWithFragment, children, ...props }) => {
return isWearable ? (
renderWithFragment ? (
Expand Down
1 change: 1 addition & 0 deletions src/lib/buildSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function buildSelectorsObject(options) {
isBrowser: types.isBrowserType(device),
isDesktop: types.isBrowserType(device),
isAndroid: types.isAndroidType(os),
isWebView: types.isWebViewType(),
isWinPhone: types.isWinPhoneType(os),
isIOS: types.isIOSType(os) || types.getIPad13(),
isChrome: types.isChromeType(browser),
Expand Down
1 change: 1 addition & 0 deletions src/lib/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const isBrowser = types.isBrowserType(UAHelper.device);
export const isDesktop = types.isBrowserType(UAHelper.device);
export const isAndroid = types.isAndroidType(UAHelper.os);
export const isWinPhone = types.isWinPhoneType(UAHelper.os);
export const isWebView = types.isWebViewType();
export const isIOS = types.isIOSType(UAHelper.os) || types.getIPad13();
export const isChrome = types.isChromeType(UAHelper.browser);
export const isFirefox = types.isFirefoxType(UAHelper.browser);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export const isElectronType = () => {
};
export const isEdgeChromiumType = (ua) => typeof ua === 'string' && ua.indexOf('Edg/') !== -1;

export const isWebViewType = () => {
const nav = getNavigatorInstance();
const ua = nav && nav.userAgent ? nav.userAgent : '';

return /WebView|(iPhone|iPod|iPad)(?!.*Safari)/i.test(ua);
};

export const getIOS13 = () => {
const nav = getNavigatorInstance();
return (
Expand Down