UNPKG

@spaced-out/ui-design-system

Version:
172 lines (170 loc) 5.94 kB
"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 _qa = require("../../utils/qa"); var _ConditionalWrapper = require("../ConditionalWrapper"); var _Icon = require("../Icon"); var _IconModule = _interopRequireDefault(require("../Icon/Icon.module.css")); var _typographyModule = _interopRequireDefault(require("../../styles/typography.module.css")); var _jsxRuntime = require("react/jsx-runtime"); 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); } 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, testId, 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__*/(0, _jsxRuntime.jsxs)(LinkComponent, { ...props, tabIndex: disabled ? -1 : tabIndex, ref: linkRef, testId: (0, _qa.generateTestId)({ base: testId, slot: 'root' }), 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, children: [!!iconLeftName && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: iconLeftName, size: iconLeftSize, type: iconLeftType, className: (0, _classify.default)(_IconModule.default[color], { [_typographyModule.default.disabled]: disabled }), testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon-left' }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_ConditionalWrapper.ConditionalWrapper, { condition: Boolean(iconLeftName || iconRightName), wrapper: children => /*#__PURE__*/(0, _jsxRuntime.jsx)("span", { className: (0, _classify.default)({ [_typographyModule.default.underline]: underline }), "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'text' }), children: children }), children: children }), !!iconRightName && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: iconRightName, size: iconRightSize, type: iconRightType, className: _IconModule.default[color], testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon-right' }) })] }); }); const DefaultLink = /*#__PURE__*/React.forwardRef((_ref2, ref) => { let { children, href, to, testId, ...props } = _ref2; const resolvedHref = to ?? href; return /*#__PURE__*/(0, _jsxRuntime.jsx)("a", { ...props, href: resolvedHref, ref: ref, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'root' }), children: children }); });