UNPKG

@randy.tarampi/jsx

Version:

Some common JSX components for www.randytarampi.ca

68 lines (62 loc) 3.03 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { augmentUrlWithTrackingParams } from "@randy.tarampi/js"; import PropTypes from "prop-types"; import React from "react"; import { CampaignContext } from "../../contexts"; import { InternalLink } from "./internal"; import { Link } from "./link"; export var CampaignLink = (_ref) => { var { useBranding, href, source, medium, name, term, content } = _ref, props = _objectWithoutProperties(_ref, ["useBranding", "href", "source", "medium", "name", "term", "content"]); return /*#__PURE__*/React.createElement(CampaignContext.Consumer, null, campaignContext => { var { source: contextSource, medium: contextMedium, name: contextName, term: contextTerm, content: contextContent } = campaignContext || {}; var LinkComponent = Link; var actualHref = augmentUrlWithTrackingParams(href, { source: source || contextSource, medium: medium || contextMedium, name: name || contextName, term: term || contextTerm || props.text, content: content || contextContent }); if (typeof window !== "undefined" && href && href.startsWith(window.location.origin)) { LinkComponent = InternalLink; actualHref = href.replace(window.location.origin, ""); } return /*#__PURE__*/React.createElement(LinkComponent, _extends({}, props, { className: ["link--campaign", useBranding ? "" : "link--no-branding", props.className].join(" ").trim(), href: actualHref, text: props.text || href })); }); }; CampaignLink.propTypes = { useBranding: PropTypes.bool, className: PropTypes.string, href: PropTypes.string, text: PropTypes.string, source: PropTypes.string, medium: PropTypes.string, name: PropTypes.string, term: PropTypes.string, content: PropTypes.string }; CampaignLink.defaultProps = { useBranding: true }; export default CampaignLink;