@carbon/react
Version:
React components for the Carbon Design System
114 lines (109 loc) • 3.17 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import { usePrefix } from '../../internal/usePrefix.js';
// First create the component with basic types
const LinkBase = /*#__PURE__*/React.forwardRef(({
as: BaseComponent,
children,
className: customClassName,
href,
disabled = false,
inline = false,
visited = false,
renderIcon: Icon,
size,
target,
...rest
}, ref) => {
const prefix = usePrefix();
const className = cx(`${prefix}--link`, customClassName, {
[`${prefix}--link--disabled`]: disabled,
[`${prefix}--link--inline`]: inline,
[`${prefix}--link--visited`]: visited,
[`${prefix}--link--${size}`]: size
});
const rel = target === '_blank' ? 'noopener' : undefined;
const linkProps = {
className,
rel,
target
};
// Reference for disabled links:
// https://www.scottohara.me/blog/2021/05/28/disabled-links.html
if (!disabled) {
linkProps.href = href;
} else {
linkProps.role = 'link';
linkProps['aria-disabled'] = true;
}
const BaseComponentAsAny = BaseComponent ?? 'a';
const handleOnClick = event => {
if (disabled) {
event.preventDefault();
event.stopPropagation();
} else {
// If the link is not disabled, we allow the onClick event to propagate
// so that any parent handlers can also respond to the click.
if (rest.onClick) {
rest.onClick(event);
}
}
};
return /*#__PURE__*/React.createElement(BaseComponentAsAny, _extends({
ref: ref
}, linkProps, rest, {
onClick: handleOnClick
}), children, !inline && Icon && /*#__PURE__*/React.createElement("div", {
className: `${prefix}--link__icon`
}, /*#__PURE__*/React.createElement(Icon, null)));
});
const Link = LinkBase;
Link.displayName = 'Link';
Link.propTypes = {
/**
* Provide a custom element or component to render the top-level node for the
* component.
*/
as: PropTypes.elementType,
/**
* Provide the content for the Link
*/
children: PropTypes.node,
/**
* Provide a custom className to be applied to the containing `<a>` node
*/
className: PropTypes.string,
/**
* Specify if the control should be disabled, or not
*/
disabled: PropTypes.bool,
/**
* Provide the `href` attribute for the `<a>` node
*/
href: PropTypes.string,
/**
* Specify whether you want the inline version of this control
*/
inline: PropTypes.bool,
/**
* A component used to render an icon.
*/
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* Specify the size of the Link. Currently supports either `sm`, `md` (default) or `lg` as an option.
*/
size: PropTypes.oneOf(['sm', 'md', 'lg']),
/**
* Specify whether you want the link to receive visited styles after the link has been clicked
*/
visited: PropTypes.bool
};
export { Link as default };