@carbon/react
Version:
React components for the Carbon Design System
73 lines (71 loc) • 2.15 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* 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 { usePrefix } from "../../internal/usePrefix.js";
import classNames from "classnames";
import React from "react";
import PropTypes from "prop-types";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/Link/Link.tsx
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const Link = React.forwardRef(({ as: BaseComponent, children, className: customClassName, href, disabled = false, inline = false, visited = false, renderIcon: Icon, size, target, ...rest }, ref) => {
const prefix = usePrefix();
const linkProps = {
className: classNames(`${prefix}--link`, customClassName, {
[`${prefix}--link--disabled`]: disabled,
[`${prefix}--link--inline`]: inline,
[`${prefix}--link--visited`]: visited,
[`${prefix}--link--${size}`]: size
}),
rel: target === "_blank" ? "noopener" : void 0,
target
};
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 (rest.onClick) rest.onClick(event);
};
return /* @__PURE__ */ jsxs(BaseComponentAsAny, {
ref,
...linkProps,
...rest,
onClick: handleOnClick,
children: [children, !inline && Icon && /* @__PURE__ */ jsx("span", {
className: `${prefix}--link__icon`,
children: /* @__PURE__ */ jsx(Icon, {})
})]
});
});
Link.displayName = "Link";
Link.propTypes = {
as: PropTypes.elementType,
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
href: PropTypes.string,
inline: PropTypes.bool,
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
size: PropTypes.oneOf([
"sm",
"md",
"lg"
]),
visited: PropTypes.bool
};
//#endregion
export { Link as default };