Skip to content

Commit 0a97f54

Browse files
feat: add common browser detection utils (#3062) (#3063)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent 62df481 commit 0a97f54

11 files changed

Lines changed: 45 additions & 21 deletions

File tree

‎packages/app-layout/src/detect-ios-navbar.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
7+
18
export const _detectIosNavbar = function () {
29
/* c8 ignore next 11 */
3-
if (window.navigator.userAgent.match(/iPhone|iPad/i)) {
10+
if (isIOS) {
411
const innerHeight = window.innerHeight;
512
const innerWidth = window.innerWidth;
613
const landscape = innerWidth > innerHeight;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
7+
export const isAndroid = /android/i.test(navigator.userAgent);
8+
9+
export const isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
10+
11+
export const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
12+
13+
export const isIOS =
14+
(/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) ||
15+
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
16+
17+
export const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

‎packages/component-base/src/virtualizer-iron-list-adapter.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { animationFrame, timeOut } from './async.js';
7+
import { isSafari } from './browser-utils.js';
78
import { Debouncer, flush } from './debounce.js';
89
import { ironList } from './iron-list-core.js';
910

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

28-
this.__safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
29-
3029
this.timeouts = {
3130
SCROLL_REORDER: 500,
3231
IGNORE_WHEEL: 500
@@ -452,7 +451,7 @@ export class IronListAdapter {
452451
// Due to a rendering bug, reordering the rows can make parts of the scroll target disappear
453452
// on Safari when using sticky positioning in case the scroll target is inside a flexbox.
454453
// This issue manifests with grid (the header can disappear if grid is used inside a flexbox)
455-
if (this.__safari) {
454+
if (isSafari) {
456455
const { transform } = this.scrollTarget.style;
457456
this.scrollTarget.style.transform = 'translateZ(0)';
458457
setTimeout(() => (this.scrollTarget.style.transform = transform));

‎packages/context-menu/src/vaadin-contextmenu-event.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { prevent, register } from '@polymer/polymer/lib/utils/gestures.js';
7+
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
78

89
register({
910
name: 'vaadin-contextmenu',
@@ -16,10 +17,7 @@ register({
1617
emits: ['vaadin-contextmenu'],
1718

1819
info: {
19-
sourceEvent: null,
20-
_ios:
21-
(/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) ||
22-
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
20+
sourceEvent: null
2321
},
2422

2523
reset: function () {
@@ -50,7 +48,7 @@ register({
5048
this._timerId = setTimeout(() => {
5149
const ct = e.changedTouches[0];
5250
if (!e.shiftKey) {
53-
if (this.info._ios) {
51+
if (isIOS) {
5452
this._fired = true;
5553
this.fire(t, ct.clientX, ct.clientY);
5654
}

‎packages/custom-field/src/vaadin-custom-field.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
77
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
8+
import { isChrome } from '@vaadin/component-base/src/browser-utils.js';
89
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
910
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
1011
import { FieldMixin } from '@vaadin/field-base/src/field-mixin.js';
@@ -279,7 +280,6 @@ class CustomField extends FieldMixin(FocusMixin(ThemableMixin(ElementMixin(Polym
279280

280281
/** @private */
281282
__fixChromeFocus() {
282-
const isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
283283
this.addEventListener('keydown', (e) => {
284284
if (e.keyCode === 9) {
285285
// FIXME(yuriy): remove this workaround once this issue is fixed:

‎packages/date-picker/src/vaadin-date-picker-mixin.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { addListener } from '@polymer/polymer/lib/utils/gestures.js';
7+
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
78
import { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
89
import { DelegateFocusMixin } from '@vaadin/field-base/src/delegate-focus-mixin.js';
910
import { InputMixin } from '@vaadin/field-base/src/input-mixin.js';
@@ -307,7 +308,7 @@ export const DatePickerMixin = (subclass) =>
307308
/** @private */
308309
_ios: {
309310
type: Boolean,
310-
value: navigator.userAgent.match(/iP(?:hone|ad;(?: U;)? CPU) OS (\d+)/)
311+
value: isIOS
311312
},
312313

313314
/** @private */

‎packages/date-picker/src/vaadin-infinite-scroller.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
77
import { templatize } from '@polymer/polymer/lib/utils/templatize.js';
88
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
99
import { timeOut } from '@vaadin/component-base/src/async.js';
10+
import { isFirefox } from '@vaadin/component-base/src/browser-utils.js';
1011
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
1112

1213
/**
@@ -137,7 +138,6 @@ class InfiniteScroller extends PolymerElement {
137138

138139
// Firefox interprets elements with overflow:auto as focusable
139140
// https://bugzilla.mozilla.org/show_bug.cgi?id=1069739
140-
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
141141
if (isFirefox) {
142142
this.$.scroller.tabIndex = -1;
143143
}

‎packages/grid/src/vaadin-grid.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import './vaadin-grid-column.js';
77
import './vaadin-grid-styles.js';
88
import { beforeNextRender } from '@polymer/polymer/lib/utils/render-status.js';
99
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
10+
import { isAndroid, isFirefox, isIOS, isSafari } from '@vaadin/component-base/src/browser-utils.js';
1011
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1112
import { processTemplates } from '@vaadin/component-base/src/templates.js';
1213
import { Virtualizer } from '@vaadin/component-base/src/virtualizer.js';
@@ -326,27 +327,25 @@ class Grid extends ElementMixin(
326327
/** @private */
327328
_safari: {
328329
type: Boolean,
329-
value: /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
330+
value: isSafari
330331
},
331332

332333
/** @private */
333334
_ios: {
334335
type: Boolean,
335-
value:
336-
(/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) ||
337-
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
336+
value: isIOS
338337
},
339338

340339
/** @private */
341340
_firefox: {
342341
type: Boolean,
343-
value: navigator.userAgent.toLowerCase().indexOf('firefox') > -1
342+
value: isFirefox
344343
},
345344

346345
/** @private */
347346
_android: {
348347
type: Boolean,
349-
value: /android/i.test(navigator.userAgent)
348+
value: isAndroid
350349
},
351350

352351
/** @private */

‎packages/notification/src/vaadin-notification.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
77
import { render } from 'lit';
88
import { isTemplateResult } from 'lit/directive-helpers.js';
9+
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
910
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1011
import { processTemplates } from '@vaadin/component-base/src/templates.js';
1112
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
@@ -128,7 +129,7 @@ class NotificationContainer extends ThemableMixin(ElementMixin(PolymerElement))
128129
constructor() {
129130
super();
130131

131-
if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
132+
if (isIOS) {
132133
this._boundIosResizeListener = () => this._detectIosNavbar();
133134
}
134135
}

‎packages/rich-text-editor/src/vaadin-rich-text-editor.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import './vaadin-rich-text-editor-toolbar-styles.js';
1212
import { resetMouseCanceller } from '@polymer/polymer/lib/utils/gestures.js';
1313
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
1414
import { timeOut } from '@vaadin/component-base/src/async.js';
15+
import { isFirefox } from '@vaadin/component-base/src/browser-utils.js';
1516
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
1617
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1718
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
@@ -566,7 +567,7 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
566567
this.__patchKeyboard();
567568

568569
/* istanbul ignore if */
569-
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
570+
if (isFirefox) {
570571
this.__patchFirefoxFocus();
571572
}
572573

0 commit comments

Comments
 (0)