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: add common browser detection utils #3062

Merged
merged 1 commit into from
Nov 16, 2021
Merged
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
9 changes: 8 additions & 1 deletion packages/app-layout/src/detect-ios-navbar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/**
* @license
* Copyright (c) 2021 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';

export const _detectIosNavbar = function () {
/* c8 ignore next 11 */
if (window.navigator.userAgent.match(/iPhone|iPad/i)) {
if (isIOS) {
const innerHeight = window.innerHeight;
const innerWidth = window.innerWidth;
const landscape = innerWidth > innerHeight;
Expand Down
17 changes: 17 additions & 0 deletions packages/component-base/src/browser-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @license
* Copyright (c) 2021 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/

export const isAndroid = /android/i.test(navigator.userAgent);

export const isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);

export const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

export const isIOS =
(/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);

export const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
5 changes: 2 additions & 3 deletions packages/component-base/src/virtualizer-iron-list-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { animationFrame, timeOut } from './async.js';
import { isSafari } from './browser-utils.js';
import { Debouncer, flush } from './debounce.js';
import { ironList } from './iron-list-core.js';

Expand All @@ -25,8 +26,6 @@ export class IronListAdapter {
// Iron-list uses this value to determine how many pages of elements to render
this._maxPages = 1.3;

this.__safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

this.timeouts = {
SCROLL_REORDER: 500,
IGNORE_WHEEL: 500
Expand Down Expand Up @@ -452,7 +451,7 @@ export class IronListAdapter {
// Due to a rendering bug, reordering the rows can make parts of the scroll target disappear
// on Safari when using sticky positioning in case the scroll target is inside a flexbox.
// This issue manifests with grid (the header can disappear if grid is used inside a flexbox)
if (this.__safari) {
if (isSafari) {
const { transform } = this.scrollTarget.style;
this.scrollTarget.style.transform = 'translateZ(0)';
setTimeout(() => (this.scrollTarget.style.transform = transform));
Expand Down
8 changes: 3 additions & 5 deletions packages/context-menu/src/vaadin-contextmenu-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { prevent, register } from '@polymer/polymer/lib/utils/gestures.js';
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';

register({
name: 'vaadin-contextmenu',
Expand All @@ -16,10 +17,7 @@ register({
emits: ['vaadin-contextmenu'],

info: {
sourceEvent: null,
_ios:
(/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
sourceEvent: null
},

reset: function () {
Expand Down Expand Up @@ -50,7 +48,7 @@ register({
this._timerId = setTimeout(() => {
const ct = e.changedTouches[0];
if (!e.shiftKey) {
if (this.info._ios) {
if (isIOS) {
this._fired = true;
this.fire(t, ct.clientX, ct.clientY);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/custom-field/src/vaadin-custom-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { isChrome } from '@vaadin/component-base/src/browser-utils.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
import { FieldMixin } from '@vaadin/field-base/src/field-mixin.js';
Expand Down Expand Up @@ -279,7 +280,6 @@ class CustomField extends FieldMixin(FocusMixin(ThemableMixin(ElementMixin(Polym

/** @private */
__fixChromeFocus() {
const isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
this.addEventListener('keydown', (e) => {
if (e.keyCode === 9) {
// FIXME(yuriy): remove this workaround once this issue is fixed:
Expand Down
3 changes: 2 additions & 1 deletion packages/date-picker/src/vaadin-date-picker-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { addListener } from '@polymer/polymer/lib/utils/gestures.js';
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
import { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
import { DelegateFocusMixin } from '@vaadin/field-base/src/delegate-focus-mixin.js';
import { InputMixin } from '@vaadin/field-base/src/input-mixin.js';
Expand Down Expand Up @@ -307,7 +308,7 @@ export const DatePickerMixin = (subclass) =>
/** @private */
_ios: {
type: Boolean,
value: navigator.userAgent.match(/iP(?:hone|ad;(?: U;)? CPU) OS (\d+)/)
value: isIOS
},

/** @private */
Expand Down
2 changes: 1 addition & 1 deletion packages/date-picker/src/vaadin-infinite-scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import { templatize } from '@polymer/polymer/lib/utils/templatize.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { timeOut } from '@vaadin/component-base/src/async.js';
import { isFirefox } from '@vaadin/component-base/src/browser-utils.js';
import { Debouncer } from '@vaadin/component-base/src/debounce.js';

/**
Expand Down Expand Up @@ -137,7 +138,6 @@ class InfiniteScroller extends PolymerElement {

// Firefox interprets elements with overflow:auto as focusable
// https://bugzilla.mozilla.org/show_bug.cgi?id=1069739
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
if (isFirefox) {
this.$.scroller.tabIndex = -1;
}
Expand Down
11 changes: 5 additions & 6 deletions packages/grid/src/vaadin-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import './vaadin-grid-column.js';
import './vaadin-grid-styles.js';
import { beforeNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { isAndroid, isFirefox, isIOS, isSafari } from '@vaadin/component-base/src/browser-utils.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { processTemplates } from '@vaadin/component-base/src/templates.js';
import { Virtualizer } from '@vaadin/component-base/src/virtualizer.js';
Expand Down Expand Up @@ -326,27 +327,25 @@ class Grid extends ElementMixin(
/** @private */
_safari: {
type: Boolean,
value: /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
value: isSafari
},

/** @private */
_ios: {
type: Boolean,
value:
(/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
value: isIOS
},

/** @private */
_firefox: {
type: Boolean,
value: navigator.userAgent.toLowerCase().indexOf('firefox') > -1
value: isFirefox
},

/** @private */
_android: {
type: Boolean,
value: /android/i.test(navigator.userAgent)
value: isAndroid
},

/** @private */
Expand Down
3 changes: 2 additions & 1 deletion packages/notification/src/vaadin-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { render } from 'lit';
import { isTemplateResult } from 'lit/directive-helpers.js';
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { processTemplates } from '@vaadin/component-base/src/templates.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
Expand Down Expand Up @@ -128,7 +129,7 @@ class NotificationContainer extends ThemableMixin(ElementMixin(PolymerElement))
constructor() {
super();

if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
if (isIOS) {
this._boundIosResizeListener = () => this._detectIosNavbar();
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/rich-text-editor/src/vaadin-rich-text-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import './vaadin-rich-text-editor-toolbar-styles.js';
import { resetMouseCanceller } from '@polymer/polymer/lib/utils/gestures.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { timeOut } from '@vaadin/component-base/src/async.js';
import { isFirefox } from '@vaadin/component-base/src/browser-utils.js';
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
Expand Down Expand Up @@ -566,7 +567,7 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
this.__patchKeyboard();

/* istanbul ignore if */
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
if (isFirefox) {
this.__patchFirefoxFocus();
}

Expand Down
3 changes: 2 additions & 1 deletion packages/vaadin-overlay/src/vaadin-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nod
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
import { templatize } from '@polymer/polymer/lib/utils/templatize.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { FocusablesHelper } from './vaadin-focusables-helper.js';
Expand Down Expand Up @@ -338,7 +339,7 @@ class OverlayElement extends ThemableMixin(DirMixin(PolymerElement)) {
this._boundIronOverlayCanceledListener = this._ironOverlayCanceled.bind(this);

/* c8 ignore next 3 */
if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
if (isIOS) {
this._boundIosResizeListener = () => this._detectIosNavbar();
}
}
Expand Down