@atlaskit/menu
Version:
A list of options to help users navigate, or perform actions.
116 lines (113 loc) • 5.97 kB
JavaScript
/* link-item.tsx generated by @compiled/babel-plugin v0.36.1 */
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _runtime = require("@compiled/react/runtime");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _appProvider = require("@atlaskit/app-provider");
var _interactionContext = _interopRequireDefault(require("@atlaskit/interaction-context"));
var _menuItemPrimitive = _interopRequireDefault(require("../internal/components/menu-item-primitive"));
var _excluded = ["children", "href", "description", "iconAfter", "iconBefore", "isDisabled", "isSelected", "onClick", "testId", "onMouseDown", "shouldTitleWrap", "shouldDescriptionWrap", "className", "UNSAFE_shouldDisableRouterLink", "UNSAFE_isDraggable", "interactionName"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var IS_EXTERNAL_LINK_REGEX = /^(?:(http|https):\/\/)/;
var IS_NON_HTTP_BASED = /^(((mailto|tel|sms):)|(#))/;
var preventEvent = function preventEvent(e) {
e.preventDefault();
};
/**
* __Link item__
*
* A link item is used to populate a menu with items that are links.
*
* - [Examples](https://atlaskit.atlassian.com/packages/design-system/menu/docs/link-item)
* - [Code](https://atlaskit.atlassian.com/packages/design-system/menu)
*/
var LinkItem = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
var children = props.children,
href = props.href,
description = props.description,
iconAfter = props.iconAfter,
iconBefore = props.iconBefore,
_props$isDisabled = props.isDisabled,
isDisabled = _props$isDisabled === void 0 ? false : _props$isDisabled,
_props$isSelected = props.isSelected,
isSelected = _props$isSelected === void 0 ? false : _props$isSelected,
onClick = props.onClick,
testId = props.testId,
onMouseDown = props.onMouseDown,
shouldTitleWrap = props.shouldTitleWrap,
shouldDescriptionWrap = props.shouldDescriptionWrap,
UNSAFE_className = props.className,
UNSAFE_shouldDisableRouterLink = props.UNSAFE_shouldDisableRouterLink,
UNSAFE_isDraggable = props.UNSAFE_isDraggable,
interactionName = props.interactionName,
rest = (0, _objectWithoutProperties2.default)(props, _excluded);
var onMouseDownHandler = onMouseDown;
var RouterLink = (0, _appProvider.useRouterLink)();
var interactionContext = (0, _react.useContext)(_interactionContext.default);
var handleClick = (0, _react.useCallback)(function (e) {
interactionContext === null || interactionContext === void 0 || interactionContext.tracePress(interactionName, e.timeStamp);
onClick === null || onClick === void 0 || onClick(e);
}, [onClick, interactionContext, interactionName]);
if (!children) {
return null;
}
var isExternal = typeof href === 'string' && IS_EXTERNAL_LINK_REGEX.test(href);
var isNonHttpBased = typeof href === 'string' && IS_NON_HTTP_BASED.test(href);
var isEmptyHref = href == null || href === '';
/**
* Renders a router link if:
*
* - a link component is set in the app provider
* - it's not an external link (starting with `http://` or `https://`)
* - it's not a non-HTTP-based link (e.g. emails, phone numbers, hash links etc.)
* - it doesn't have an empty href (e.g. href="" or href={undefined})
*/
var isRouterLink = !UNSAFE_shouldDisableRouterLink && RouterLink && !isExternal && !isNonHttpBased && !isEmptyHref;
var Component = isRouterLink ? RouterLink : 'a';
return /*#__PURE__*/React.createElement(_menuItemPrimitive.default, (0, _extends2.default)({}, rest, {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: UNSAFE_className,
iconBefore: iconBefore,
iconAfter: iconAfter,
isSelected: isSelected,
isDisabled: isDisabled,
isTitleHeading: false,
description: description,
shouldTitleWrap: shouldTitleWrap,
shouldDescriptionWrap: shouldDescriptionWrap,
title: children,
testId: testId && "".concat(testId, "--primitive")
}), function (_ref) {
var children = _ref.children,
className = _ref.className;
return /*#__PURE__*/React.createElement(Component, (0, _extends2.default)({
"data-testid": testId,
"data-is-router-link": testId ? isRouterLink ? 'true' : 'false' : undefined,
"data-vc": "link-item"
}, rest, {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: className
// @ts-expect-error
,
href: isDisabled ? undefined : href
}, UNSAFE_isDraggable ? {} : {
draggable: false,
onDragStart: preventEvent
}, {
onMouseDown: isDisabled ? preventEvent : onMouseDownHandler,
onClick: isDisabled ? preventEvent : handleClick,
"aria-current": isSelected ? 'page' : undefined,
"aria-disabled": isDisabled,
ref: ref
}), children);
});
}));
var _default = exports.default = LinkItem;