244 lines
7.9 KiB
JavaScript
244 lines
7.9 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
exports.__esModule = true;
|
|
exports.withPrefix = withPrefix;
|
|
exports.withAssetPrefix = withAssetPrefix;
|
|
exports.navigateTo = exports.replace = exports.push = exports.navigate = exports.default = void 0;
|
|
|
|
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
|
|
|
|
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
|
|
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var _react = _interopRequireDefault(require("react"));
|
|
|
|
var _router = require("@reach/router");
|
|
|
|
var _parsePath = require("./parse-path");
|
|
|
|
exports.parsePath = _parsePath.parsePath;
|
|
|
|
function withPrefix(path) {
|
|
return normalizePath([typeof __BASE_PATH__ !== "undefined" ? __BASE_PATH__ : __PATH_PREFIX__, path].join("/"));
|
|
}
|
|
|
|
function withAssetPrefix(path) {
|
|
return [__PATH_PREFIX__].concat([path.replace(/^\//, "")]).join("/");
|
|
}
|
|
|
|
function normalizePath(path) {
|
|
return path.replace(/\/+/g, "/");
|
|
}
|
|
|
|
var NavLinkPropTypes = {
|
|
activeClassName: _propTypes.default.string,
|
|
activeStyle: _propTypes.default.object,
|
|
partiallyActive: _propTypes.default.bool // Set up IntersectionObserver
|
|
|
|
};
|
|
|
|
var handleIntersection = function handleIntersection(el, cb) {
|
|
var io = new window.IntersectionObserver(function (entries) {
|
|
entries.forEach(function (entry) {
|
|
if (el === entry.target) {
|
|
// Check if element is within viewport, remove listener, destroy observer, and run link callback.
|
|
// MSEdge doesn't currently support isIntersecting, so also test for an intersectionRatio > 0
|
|
if (entry.isIntersecting || entry.intersectionRatio > 0) {
|
|
io.unobserve(el);
|
|
io.disconnect();
|
|
cb();
|
|
}
|
|
}
|
|
});
|
|
}); // Add element to the observer
|
|
|
|
io.observe(el);
|
|
};
|
|
|
|
var GatsbyLink =
|
|
/*#__PURE__*/
|
|
function (_React$Component) {
|
|
(0, _inheritsLoose2.default)(GatsbyLink, _React$Component);
|
|
|
|
function GatsbyLink(props) {
|
|
var _this;
|
|
|
|
_this = _React$Component.call(this, props) || this; // Default to no support for IntersectionObserver
|
|
|
|
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "defaultGetProps", function (_ref) {
|
|
var isPartiallyCurrent = _ref.isPartiallyCurrent,
|
|
isCurrent = _ref.isCurrent;
|
|
|
|
if (_this.props.partiallyActive ? isPartiallyCurrent : isCurrent) {
|
|
return {
|
|
className: [_this.props.className, _this.props.activeClassName].filter(Boolean).join(" "),
|
|
style: (0, _extends2.default)({}, _this.props.style, _this.props.activeStyle)
|
|
};
|
|
}
|
|
|
|
return null;
|
|
});
|
|
var IOSupported = false;
|
|
|
|
if (typeof window !== "undefined" && window.IntersectionObserver) {
|
|
IOSupported = true;
|
|
}
|
|
|
|
_this.state = {
|
|
IOSupported: IOSupported
|
|
};
|
|
_this.handleRef = _this.handleRef.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
|
|
return _this;
|
|
}
|
|
|
|
var _proto = GatsbyLink.prototype;
|
|
|
|
_proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
|
|
// Preserve non IO functionality if no support
|
|
if (this.props.to !== prevProps.to && !this.state.IOSupported) {
|
|
___loader.enqueue((0, _parsePath.parsePath)(this.props.to).pathname);
|
|
}
|
|
};
|
|
|
|
_proto.componentDidMount = function componentDidMount() {
|
|
// Preserve non IO functionality if no support
|
|
if (!this.state.IOSupported) {
|
|
___loader.enqueue((0, _parsePath.parsePath)(this.props.to).pathname);
|
|
}
|
|
};
|
|
|
|
_proto.handleRef = function handleRef(ref) {
|
|
var _this2 = this;
|
|
|
|
if (this.props.innerRef && this.props.innerRef.hasOwnProperty("current")) {
|
|
this.props.innerRef.current = ref;
|
|
} else if (this.props.innerRef) {
|
|
this.props.innerRef(ref);
|
|
}
|
|
|
|
if (this.state.IOSupported && ref) {
|
|
// If IO supported and element reference found, setup Observer functionality
|
|
handleIntersection(ref, function () {
|
|
___loader.enqueue((0, _parsePath.parsePath)(_this2.props.to).pathname);
|
|
});
|
|
}
|
|
};
|
|
|
|
_proto.render = function render() {
|
|
var _this3 = this;
|
|
|
|
var _this$props = this.props,
|
|
to = _this$props.to,
|
|
_this$props$getProps = _this$props.getProps,
|
|
getProps = _this$props$getProps === void 0 ? this.defaultGetProps : _this$props$getProps,
|
|
_onClick = _this$props.onClick,
|
|
_onMouseEnter = _this$props.onMouseEnter,
|
|
$activeClassName = _this$props.activeClassName,
|
|
$activeStyle = _this$props.activeStyle,
|
|
$innerRef = _this$props.innerRef,
|
|
partiallyActive = _this$props.partiallyActive,
|
|
state = _this$props.state,
|
|
replace = _this$props.replace,
|
|
rest = (0, _objectWithoutPropertiesLoose2.default)(_this$props, ["to", "getProps", "onClick", "onMouseEnter", "activeClassName", "activeStyle", "innerRef", "partiallyActive", "state", "replace"]);
|
|
var LOCAL_URL = /^\/(?!\/)/;
|
|
|
|
if (process.env.NODE_ENV !== "production" && !LOCAL_URL.test(to)) {
|
|
console.warn("External link " + to + " was detected in a Link component. Use the Link component only for internal links. See: https://gatsby.dev/internal-links");
|
|
}
|
|
|
|
var prefixedTo = withPrefix(to);
|
|
return _react.default.createElement(_router.Link, (0, _extends2.default)({
|
|
to: prefixedTo,
|
|
state: state,
|
|
getProps: getProps,
|
|
innerRef: this.handleRef,
|
|
onMouseEnter: function onMouseEnter(e) {
|
|
if (_onMouseEnter) {
|
|
_onMouseEnter(e);
|
|
}
|
|
|
|
___loader.hovering((0, _parsePath.parsePath)(to).pathname);
|
|
},
|
|
onClick: function onClick(e) {
|
|
if (_onClick) {
|
|
_onClick(e);
|
|
}
|
|
|
|
if (e.button === 0 && // ignore right clicks
|
|
!_this3.props.target && // let browser handle "target=_blank"
|
|
!e.defaultPrevented && // onClick prevented default
|
|
!e.metaKey && // ignore clicks with modifier keys...
|
|
!e.altKey && !e.ctrlKey && !e.shiftKey) {
|
|
e.preventDefault(); // Make sure the necessary scripts and data are
|
|
// loaded before continuing.
|
|
|
|
navigate(to, {
|
|
state: state,
|
|
replace: replace
|
|
});
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}, rest));
|
|
};
|
|
|
|
return GatsbyLink;
|
|
}(_react.default.Component);
|
|
|
|
GatsbyLink.propTypes = (0, _extends2.default)({}, NavLinkPropTypes, {
|
|
onClick: _propTypes.default.func,
|
|
to: _propTypes.default.string.isRequired,
|
|
replace: _propTypes.default.bool
|
|
});
|
|
|
|
var showDeprecationWarning = function showDeprecationWarning(functionName, altFunctionName, version) {
|
|
return console.warn("The \"" + functionName + "\" method is now deprecated and will be removed in Gatsby v" + version + ". Please use \"" + altFunctionName + "\" instead.");
|
|
};
|
|
|
|
var _default = _react.default.forwardRef(function (props, ref) {
|
|
return _react.default.createElement(GatsbyLink, (0, _extends2.default)({
|
|
innerRef: ref
|
|
}, props));
|
|
});
|
|
|
|
exports.default = _default;
|
|
|
|
var navigate = function navigate(to, options) {
|
|
window.___navigate(withPrefix(to), options);
|
|
};
|
|
|
|
exports.navigate = navigate;
|
|
|
|
var push = function push(to) {
|
|
showDeprecationWarning("push", "navigate", 3);
|
|
|
|
window.___push(withPrefix(to));
|
|
};
|
|
|
|
exports.push = push;
|
|
|
|
var replace = function replace(to) {
|
|
showDeprecationWarning("replace", "navigate", 3);
|
|
|
|
window.___replace(withPrefix(to));
|
|
}; // TODO: Remove navigateTo for Gatsby v3
|
|
|
|
|
|
exports.replace = replace;
|
|
|
|
var navigateTo = function navigateTo(to) {
|
|
showDeprecationWarning("navigateTo", "navigate", 3);
|
|
return push(to);
|
|
};
|
|
|
|
exports.navigateTo = navigateTo; |