vcc-ui
Version:
A React library for building user interfaces at Volvo Cars
53 lines (50 loc) • 1.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isTouchDevice = exports.getLinkRel = void 0;
/**
* Detect type of device
* From stackoverflow: https://stackoverflow.com/a/4819886
*
* @returns bool
*/
const isTouchDevice = () => {
const prefixes = ' -webkit- -moz- -o- -ms- '.split(' ');
const matchedQueries = query => {
return window.matchMedia(query).matches;
};
if ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch) {
return true;
}
// include the 'heartz' as a way to have a non matching MQ to help terminate the join
// https://git.io/vznFH
const query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('');
return matchedQueries(query);
};
/**
* Takes link component props and returns a recommended `rel` value.
*
* @see https://developers.google.com/web/tools/lighthouse/audits/noopener
*/
exports.isTouchDevice = isTouchDevice;
const getLinkRel = _ref => {
let {
href,
target,
rel
} = _ref;
if (!href || target !== '_blank') {
return rel;
}
let isCrossOrigin;
try {
// Fails in environments without URL support (like IE11 and in SSR)
isCrossOrigin = new URL(href).host !== window.location.host;
} catch {
isCrossOrigin = /^http/i.test(href);
}
const needsNoopener = isCrossOrigin && !/\bopener\b/.test(rel || '') && !/no(opener|referrer)/.test(rel || '');
return needsNoopener ? `${rel || ''} noopener`.trim() : rel;
};
exports.getLinkRel = getLinkRel;
;