@vve/react-router
Version:
react router for react-helper & with keep-alive
56 lines (54 loc) • 2.25 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["aria-current", "activeClassName", "activeStyle", "className", "exact", "isActive", "location", "strict", "style", "to"];
import React from 'react';
import Link from "./link";
import Route from "./route";
function joinClassnames() {
for (var _len = arguments.length, classnames = new Array(_len), _key = 0; _key < _len; _key++) {
classnames[_key] = arguments[_key];
}
return classnames.filter(i => i).join(' ');
}
/**
* A <Link> wrapper that knows if it's "active" or not.
*/
function NavLink(_ref) {
var _ref$ariaCurrent = _ref['aria-current'],
ariaCurrent = _ref$ariaCurrent === void 0 ? 'page' : _ref$ariaCurrent,
_ref$activeClassName = _ref.activeClassName,
activeClassName = _ref$activeClassName === void 0 ? 'active' : _ref$activeClassName,
activeStyle = _ref.activeStyle,
classNameProp = _ref.className,
exact = _ref.exact,
isActiveProp = _ref.isActive,
location = _ref.location,
strict = _ref.strict,
styleProp = _ref.style,
to = _ref.to,
rest = _objectWithoutProperties(_ref, _excluded);
var path = typeof to === 'object' ? to.pathname : to;
// Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, '\\$1');
return /*#__PURE__*/React.createElement(Route, {
path: escapedPath,
exact: exact,
strict: strict,
location: location
}, _ref2 => {
var location = _ref2.location,
match = _ref2.match;
var isActive = !!(isActiveProp ? isActiveProp(match, location) : match);
var className = isActive ? joinClassnames(classNameProp, activeClassName) : classNameProp;
var style = isActive ? _objectSpread(_objectSpread({}, styleProp), activeStyle) : styleProp;
return /*#__PURE__*/React.createElement(Link, _extends({
"aria-current": isActive && ariaCurrent || null,
className: className,
style: style,
to: to
}, rest));
});
}
export default NavLink;
//# sourceMappingURL=nav-link.js.map