@atlaskit/menu
Version:
A list of options to help users navigate, or perform actions.
107 lines (105 loc) • 4.84 kB
JavaScript
/* link-item.tsx generated by @compiled/babel-plugin v0.36.1 */
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "href", "description", "iconAfter", "iconBefore", "isDisabled", "isSelected", "onClick", "testId", "onMouseDown", "shouldTitleWrap", "shouldDescriptionWrap", "className", "UNSAFE_shouldDisableRouterLink", "UNSAFE_isDraggable", "interactionName"];
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { forwardRef, memo, useCallback, useContext } from 'react';
import { useRouterLink } from '@atlaskit/app-provider';
import InteractionContext from '@atlaskit/interaction-context';
import MenuItemPrimitive from '../internal/components/menu-item-primitive';
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__*/memo( /*#__PURE__*/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 = _objectWithoutProperties(props, _excluded);
var onMouseDownHandler = onMouseDown;
var RouterLink = useRouterLink();
var interactionContext = useContext(InteractionContext);
var handleClick = 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, _extends({}, 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, _extends({
"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);
});
}));
export default LinkItem;