UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

165 lines (164 loc) 5.59 kB
"use client"; var _IconPrimary; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import React from 'react'; import clsx from 'clsx'; import E from "../../elements/Element.js"; import Context from "../../shared/Context.js"; import { makeUniqueId, extendPropsWithContext } from "../../shared/component-helper.js"; import { getOffsetTop } from "../../shared/helpers.js"; import IconPrimary from "../icon-primary/IconPrimary.js"; import Tooltip from "../tooltip/Tooltip.js"; import { launch as launchIcon } from "../../icons/index.js"; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; const defaultProps = { noAnimation: false, noStyle: false, noHover: false, noUnderline: false, noIcon: false, noLaunchIcon: false, disabled: false }; export function AnchorInstance(localProps) { const context = React.useContext(Context); const allProps = extendPropsWithContext(localProps, defaultProps, { skeleton: context?.skeleton }, context?.getTranslation(localProps).Anchor, context?.Anchor); const fallbackRef = React.useRef(null); if (!allProps.ref) { allProps.ref = fallbackRef; } const tooltipRef = React.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' + 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?.includes('dnb-anchor--no-icon') && !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 = _jsx(IconPrimary, { className: "dnb-anchor__launch-icon", icon: launchIcon }))); const prefix = iconPosition === 'left' && iconNode; const anchorRef = React.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 _jsxs(_Fragment, { children: [_jsxs(E, { as: as, id: id, internalClass: as !== 'button', className: clsx(className, omitClass !== true && clsx('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?.includes('dnb-anchor--no-icon') && 'dnb-anchor--no-icon', noLaunchIcon && !className?.includes('dnb-anchor--no-launch-icon') && 'dnb-anchor--no-launch-icon', context?.theme?.surface === 'dark' && 'dnb-anchor--surface-dark')), ...attributes, ref: anchorRef, children: [prefix, children, suffix] }), showTooltip && _jsx(Tooltip, { showDelay: 100, id: internalId + '-tooltip', targetElement: tooltipRef, tooltip: tooltip, children: allProps.title || targetBlankTitle })] }); } function Anchor(props) { return _jsx(AnchorInstance, { ...props }); } withComponentMarkers(Anchor, { _supportsSpacingProps: true }); export default Anchor; export 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 = getOffsetTop(anchorElem) - scrollPadding || 0; window.scroll({ top }); return { element: anchorElem }; } catch (error) { console.error(error); } } return undefined; } function getIcon(icon) { return pickIcon(icon) || _jsx(IconPrimary, { icon: icon }); } export function pickIcon(icon, className) { if (icon?.props?.icon || icon?.props?.className?.includes('dnb-icon')) { return React.createElement(icon.type, { ...icon.props, key: 'button-icon-clone', className: clsx(icon.props?.className, className) }); } return null; } export const opensNewTab = (target, href) => target === '_blank' && !/^(mailto|tel|sms)/.test(href); //# sourceMappingURL=Anchor.js.map