@carbon/react
Version:
React components for the Carbon Design System
77 lines (75 loc) • 2.59 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.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("../../internal/usePrefix.js");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let react_jsx_runtime = require("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.default.forwardRef(({ as: BaseComponent, children, className: customClassName, href, disabled = false, inline = false, visited = false, renderIcon: Icon, size, target, ...rest }, ref) => {
const prefix = require_usePrefix.usePrefix();
const linkProps = {
className: (0, classnames.default)(`${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__ */ (0, react_jsx_runtime.jsxs)(BaseComponentAsAny, {
ref,
...linkProps,
...rest,
onClick: handleOnClick,
children: [children, !inline && Icon && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: `${prefix}--link__icon`,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, {})
})]
});
});
Link.displayName = "Link";
Link.propTypes = {
as: prop_types.default.elementType,
children: prop_types.default.node,
className: prop_types.default.string,
disabled: prop_types.default.bool,
href: prop_types.default.string,
inline: prop_types.default.bool,
renderIcon: prop_types.default.oneOfType([prop_types.default.func, prop_types.default.object]),
size: prop_types.default.oneOf([
"sm",
"md",
"lg"
]),
visited: prop_types.default.bool
};
//#endregion
exports.default = Link;