@spaced-out/ui-design-system
Version:
Sense UI components library
143 lines (141 loc) • 5.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Link = exports.LINK_AS = exports.ANCHOR_TARGET = exports.ANCHOR_REL = void 0;
var React = _interopRequireWildcard(require("react"));
var _typography = require("../../types/typography");
var _classify = _interopRequireDefault(require("../../utils/classify"));
var _ConditionalWrapper = require("../ConditionalWrapper");
var _Icon = require("../Icon");
var _typographyModule = _interopRequireDefault(require("../../styles/typography.module.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const LINK_AS = exports.LINK_AS = Object.freeze({
bodyLarge: 'bodyLarge',
bodyMedium: 'bodyMedium',
bodySmall: 'bodySmall',
buttonTextExtraSmall: 'buttonTextExtraSmall',
buttonTextMedium: 'buttonTextMedium',
buttonTextSmall: 'buttonTextSmall',
formInputMedium: 'formInputMedium',
formInputSmall: 'formInputSmall',
formLabelMedium: 'formLabelMedium',
formLabelSmall: 'formLabelSmall',
jumboMedium: 'jumboMedium',
subTitleExtraSmall: 'subTitleExtraSmall',
subTitleLarge: 'subTitleLarge',
subTitleMedium: 'subTitleMedium',
subTitleSmall: 'subTitleSmall',
titleMedium: 'titleMedium'
});
const ANCHOR_REL = exports.ANCHOR_REL = Object.freeze({
alternate: 'alternate',
author: 'author',
bookmark: 'bookmark',
external: 'external',
help: 'help',
license: 'license',
next: 'next',
nofollow: 'nofollow',
noopener: 'noopener',
noreferrer: 'noreferrer',
search: 'search',
tag: 'tag'
});
const ANCHOR_TARGET = exports.ANCHOR_TARGET = Object.freeze({
_blank: '_blank',
_self: '_self',
_parent: '_parent',
_top: '_top',
framename: 'framename'
});
const Link = exports.Link = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
color = _typography.TEXT_COLORS.clickable,
children,
className,
as = 'buttonTextExtraSmall',
underline = true,
tabIndex = 0,
disabled,
onClick,
linkComponent: LinkComponent = DefaultLink,
iconLeftName,
iconLeftSize = _Icon.ICON_SIZE.small,
iconLeftType,
iconRightName,
iconRightSize = _Icon.ICON_SIZE.small,
iconRightType,
...props
} = _ref;
const linkRef = React.useRef(null);
React.useImperativeHandle(ref, () => linkRef.current);
React.useEffect(() => {
if (disabled) {
linkRef.current?.blur();
}
}, [disabled]);
const handleClick = event => {
if (disabled) {
event.preventDefault();
return;
}
onClick?.(event);
};
/**
* By spec anchor tag wont call onClick on enter key press when the element is focussed
* as a workaround we would need to listen to key press event and call onClick
* manually, one workaround to avoid this is to have empty href along with onClick
* but that would break accessibility
*/
const handleKeyPress = event => {
if (event.key === 'Enter' && onClick) {
handleClick(event);
}
};
return /*#__PURE__*/React.createElement(LinkComponent, _extends({}, props, {
tabIndex: disabled ? -1 : tabIndex,
ref: linkRef,
"data-testid": "Link",
className: (0, _classify.default)(_typographyModule.default.link, _typographyModule.default[as], _typographyModule.default[color], {
[_typographyModule.default.underline]: underline && !(iconLeftName || iconRightName),
[_typographyModule.default.disabled]: disabled
}, className),
onClick: handleClick,
onKeyPress: handleKeyPress
}), !!iconLeftName && /*#__PURE__*/React.createElement(_Icon.Icon, {
name: iconLeftName,
size: iconLeftSize,
type: iconLeftType,
className: (0, _classify.default)(_typographyModule.default[color], {
[_typographyModule.default.disabled]: disabled
})
}), /*#__PURE__*/React.createElement(_ConditionalWrapper.ConditionalWrapper, {
condition: Boolean(iconLeftName || iconRightName),
wrapper: children => /*#__PURE__*/React.createElement("span", {
className: (0, _classify.default)({
[_typographyModule.default.underline]: underline
})
}, children)
}, children), !!iconRightName && /*#__PURE__*/React.createElement(_Icon.Icon, {
name: iconRightName,
size: iconRightSize,
type: iconRightType,
className: _typographyModule.default[color]
}));
});
const DefaultLink = /*#__PURE__*/React.forwardRef((_ref2, ref) => {
let {
children,
href,
to,
...props
} = _ref2;
const resolvedHref = to ?? href;
return /*#__PURE__*/React.createElement("a", _extends({}, props, {
href: resolvedHref,
ref: ref
}), children);
});