@matt-block/react-native-in-app-browser
Version:
React Native in-app browser
109 lines (108 loc) • 3.75 kB
JavaScript
;
/**
* Copyright (c) 2018-present, Matei Bogdan Radu <opensource@mateiradu.dev>
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_native_1 = require("react-native");
var tinycolor2_1 = __importDefault(require("tinycolor2"));
/**
* Default settings.
*
* These values can be augmented throgh initialization.
*/
exports.defaultSettings = {
android: {},
ios: {}
};
/**
* Initializes the platform-specific settings for the in-app browser
* experience.
*
* This utility function is useful when `openInApp` is used in several
* portions of the application code base as it allows to provide the
* settings only once instead of specifing them with each call.
*/
function initialize(settings) {
// First, reset directly as `sanitize` will otherwise merge with
// previous defaults: it would not be possible to remove properties.
exports.defaultSettings.android = {};
exports.defaultSettings.ios = {};
exports.defaultSettings.android = sanitize("android", settings);
exports.defaultSettings.ios = sanitize("ios", settings);
}
exports.initialize = initialize;
/**
* Sanitize the settings based on the running OS.
*
* Provided settings will be merged with the default ones.
* Also, in case of same properties, provided ones have priority
* over defaults.
*/
function sanitize(os, settings) {
switch (os) {
case "android":
return sanitizeAndroid(settings ? settings.android : {});
case "ios":
return sanitizeIOS(settings ? settings.ios : {});
// Other platforms in the future.
default:
return {};
}
}
exports.sanitize = sanitize;
function sanitizeAndroid(settings) {
var sanitized = __assign({}, exports.defaultSettings.android);
if (!settings) {
return sanitized;
}
if (tinycolor2_1.default(settings.toolbarColor).isValid()) {
sanitized.toolbarColor = tinycolor2_1.default(settings.toolbarColor).toHexString();
}
if (typeof settings.showTitle === "boolean") {
sanitized.showTitle = settings.showTitle;
}
try {
sanitized.closeButtonIcon = react_native_1.Image.resolveAssetSource(settings.closeButtonIcon).uri;
}
catch (unusedError) {
// Given icon image is invalid.
// Silently fail and proceed without it.
}
if (typeof settings.addDefaultShareMenu === "boolean") {
sanitized.addDefaultShareMenu = settings.addDefaultShareMenu;
}
return sanitized;
}
function sanitizeIOS(settings) {
var sanitized = __assign({}, exports.defaultSettings.ios);
if (!settings) {
return sanitized;
}
var colors = ["preferredBarTintColor", "preferredControlTintColor"];
colors.forEach(function (color) {
if (tinycolor2_1.default(settings[color]).isValid()) {
sanitized[color] = tinycolor2_1.default(settings[color]).toHexString();
}
});
if (typeof settings.barCollapsingEnabled === "boolean") {
sanitized.barCollapsingEnabled = settings.barCollapsingEnabled;
}
return sanitized;
}