@atlaskit/menu
Version:
A list of options to help users navigate, or perform actions.
104 lines (102 loc) • 3.88 kB
JavaScript
/* link-item.tsx generated by @compiled/babel-plugin v0.39.1 */
import _extends from "@babel/runtime/helpers/extends";
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';
const IS_EXTERNAL_LINK_REGEX = /^(?:(http|https):\/\/)/;
const IS_NON_HTTP_BASED = /^(((mailto|tel|sms):)|(#))/;
const 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)
*/
const LinkItem = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, ref) => {
const {
children,
href,
description,
iconAfter,
iconBefore,
isDisabled = false,
isSelected = false,
onClick,
testId,
onMouseDown,
shouldTitleWrap,
shouldDescriptionWrap,
className: UNSAFE_className,
UNSAFE_shouldDisableRouterLink,
UNSAFE_isDraggable,
interactionName,
...rest
} = props;
const onMouseDownHandler = onMouseDown;
const RouterLink = useRouterLink();
const interactionContext = useContext(InteractionContext);
const handleClick = useCallback(e => {
interactionContext === null || interactionContext === void 0 ? void 0 : interactionContext.tracePress(interactionName, e.timeStamp);
onClick === null || onClick === void 0 ? void 0 : onClick(e);
}, [onClick, interactionContext, interactionName]);
if (!children) {
return null;
}
const isExternal = typeof href === 'string' && IS_EXTERNAL_LINK_REGEX.test(href);
const isNonHttpBased = typeof href === 'string' && IS_NON_HTTP_BASED.test(href);
const 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})
*/
const isRouterLink = !UNSAFE_shouldDisableRouterLink && RouterLink && !isExternal && !isNonHttpBased && !isEmptyHref;
const 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 && `${testId}--primitive`
}), ({
children,
className
}) => /*#__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;