UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

178 lines (177 loc) 7.27 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnchorInstance = AnchorInstance; exports.opensNewTab = exports.default = void 0; exports.pickIcon = pickIcon; exports.scrollToHash = scrollToHash; var _withComponentMarkers = _interopRequireDefault(require("../../shared/helpers/withComponentMarkers.js")); var _react = _interopRequireDefault(require("react")); var _clsx = _interopRequireDefault(require("clsx")); var _Element = _interopRequireDefault(require("../../elements/Element.js")); var _Context = _interopRequireDefault(require("../../shared/Context.js")); var _componentHelper = require("../../shared/component-helper.js"); var _helpers = require("../../shared/helpers.js"); var _IconPrimary2 = _interopRequireDefault(require("../icon-primary/IconPrimary.js")); var _Tooltip = _interopRequireDefault(require("../tooltip/Tooltip.js")); var _index = require("../../icons/index.js"); var _jsxRuntime = require("react/jsx-runtime"); var _IconPrimary; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const defaultProps = { noAnimation: false, noStyle: false, noHover: false, noUnderline: false, noIcon: false, noLaunchIcon: false, disabled: false }; function AnchorInstance(localProps) { var _context$theme; const context = _react.default.useContext(_Context.default); const allProps = (0, _componentHelper.extendPropsWithContext)(localProps, defaultProps, { skeleton: context === null || context === void 0 ? void 0 : context.skeleton }, context === null || context === void 0 ? void 0 : context.getTranslation(localProps).Anchor, context === null || context === void 0 ? void 0 : context.Anchor); const fallbackRef = _react.default.useRef(null); if (!allProps.ref) { allProps.ref = fallbackRef; } const tooltipRef = _react.default.useRef(null); const { id, element, className, children, tooltip, icon, iconPosition = 'left', omitClass, ref: refProp, targetBlankTitle, noAnimation, noHover, noStyle, noUnderline, noIcon, noLaunchIcon, disabled, ...rest } = allProps; const attributes = rest; const internalId = id || 'id' + (0, _componentHelper.makeUniqueId)(); const as = element || 'a'; const isDisabled = disabled; const hasNoHover = noHover || isDisabled; const hasNoAnimation = noAnimation || isDisabled; const hasNoUnderline = noUnderline || isDisabled; const href = allProps.href || allProps.to; const _opensNewTab = opensNewTab(allProps.target, href); const showLaunchIcon = _opensNewTab && !isDisabled && !noIcon && !noLaunchIcon && !(className !== null && className !== void 0 && className.includes('dnb-anchor--no-icon')) && !(className !== null && className !== void 0 && className.includes('dnb-anchor--no-launch-icon')) && !omitClass; const showTooltip = (tooltip || _opensNewTab) && !allProps.title; if (_opensNewTab && !attributes.rel) { attributes.rel = 'noopener noreferrer'; } const iconNode = icon && getIcon(icon); const suffix = iconPosition === 'right' && iconNode || showLaunchIcon && (_IconPrimary || (_IconPrimary = (0, _jsxRuntime.jsx)(_IconPrimary2.default, { className: "dnb-anchor__launch-icon", icon: _index.launch }))); const prefix = iconPosition === 'left' && iconNode; const anchorRef = _react.default.useCallback(elem => { tooltipRef.current = elem; if (typeof refProp === 'function') { refProp(elem); } else if (refProp) { ; refProp.current = elem; } }, [refProp]); if (isDisabled) { attributes.disabled = true; if (as === 'a') { attributes.tabIndex = -1; attributes['aria-disabled'] = true; if (attributes.href) { delete attributes.href; } if (attributes.to) { delete attributes.to; } attributes.onClick = event => { event.preventDefault(); event.stopPropagation(); }; } } return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [(0, _jsxRuntime.jsxs)(_Element.default, { as: as, id: id, internalClass: as !== 'button', className: (0, _clsx.default)(className, omitClass !== true && (0, _clsx.default)('dnb-anchor', prefix && 'dnb-anchor--icon-left', suffix && 'dnb-anchor--icon-right', typeof children !== 'string' && 'dnb-anchor--was-node', hasNoAnimation && 'dnb-anchor--no-animation', hasNoHover && 'dnb-anchor--no-hover', noStyle && 'dnb-anchor--no-style', hasNoUnderline && 'dnb-anchor--no-underline', isDisabled && 'dnb-anchor--disabled', noIcon && !(className !== null && className !== void 0 && className.includes('dnb-anchor--no-icon')) && 'dnb-anchor--no-icon', noLaunchIcon && !(className !== null && className !== void 0 && className.includes('dnb-anchor--no-launch-icon')) && 'dnb-anchor--no-launch-icon', (context === null || context === void 0 || (_context$theme = context.theme) === null || _context$theme === void 0 ? void 0 : _context$theme.surface) === 'dark' && 'dnb-anchor--surface-dark')), ...attributes, ref: anchorRef, children: [prefix, children, suffix] }), showTooltip && (0, _jsxRuntime.jsx)(_Tooltip.default, { showDelay: 100, id: internalId + '-tooltip', targetElement: tooltipRef, tooltip: tooltip, children: allProps.title || targetBlankTitle })] }); } function Anchor(props) { return (0, _jsxRuntime.jsx)(AnchorInstance, { ...props }); } (0, _withComponentMarkers.default)(Anchor, { _supportsSpacingProps: true }); var _default = exports.default = Anchor; function scrollToHash(hash) { if (typeof document === 'undefined' || !hash || !hash.includes('#')) { return undefined; } const id = hash.split(/#/g).reverse()[0]; const anchorElem = document.getElementById(id); if (anchorElem instanceof HTMLElement) { try { const scrollPadding = parseFloat(window.getComputedStyle(document.documentElement).scrollPaddingTop); const top = (0, _helpers.getOffsetTop)(anchorElem) - scrollPadding || 0; window.scroll({ top }); return { element: anchorElem }; } catch (error) { console.error(error); } } return undefined; } function getIcon(icon) { return pickIcon(icon) || (0, _jsxRuntime.jsx)(_IconPrimary2.default, { icon: icon }); } function pickIcon(icon, className) { var _icon$props, _icon$props2; if (icon !== null && icon !== void 0 && (_icon$props = icon.props) !== null && _icon$props !== void 0 && _icon$props.icon || icon !== null && icon !== void 0 && (_icon$props2 = icon.props) !== null && _icon$props2 !== void 0 && (_icon$props2 = _icon$props2.className) !== null && _icon$props2 !== void 0 && _icon$props2.includes('dnb-icon')) { var _icon$props3; return _react.default.createElement(icon.type, { ...icon.props, key: 'button-icon-clone', className: (0, _clsx.default)((_icon$props3 = icon.props) === null || _icon$props3 === void 0 ? void 0 : _icon$props3.className, className) }); } return null; } const opensNewTab = (target, href) => target === '_blank' && !/^(mailto|tel|sms)/.test(href); exports.opensNewTab = opensNewTab; //# sourceMappingURL=Anchor.js.map