@blocklet/payment-react
Version:
Reusable react components for payment kit v2
59 lines (58 loc) • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createLink = createLink;
exports.getLinkProps = getLinkProps;
exports.handleNavigation = handleNavigation;
exports.isInPaymentKit = isInPaymentKit;
var _ufo = require("ufo");
var _util = require("./util");
function isInPaymentKit() {
const componentId = (window.blocklet?.componentId || "").split("/").pop();
return componentId === _util.PAYMENT_KIT_DID;
}
function createLink(path, external = false) {
const isAbsoluteUrl = /^https?:\/\//.test(path);
const isExternal = external || isAbsoluteUrl;
const url = isExternal ? path : (0, _ufo.joinURL)((0, _util.getPrefix)(), path);
return {
url,
external: isExternal,
path
};
}
function getLinkProps(link, target = "_self") {
const props = {
href: link.url
};
if (link.external) {
props.target = target;
props.rel = target === "_blank" ? "noopener noreferrer" : void 0;
}
return props;
}
function handleNavigation(e, link, navigate, options = {}) {
if (e) {
e.preventDefault();
}
const {
replace = false,
target = "_self"
} = options;
if (link.external || target === "_blank") {
window.open(link.url, target);
return;
}
if (isInPaymentKit() && navigate) {
navigate(link.path, {
replace
});
return;
}
if (replace) {
window.location.replace(link.url);
} else {
window.location.href = link.url;
}
}