UNPKG

v8-ui-atoms

Version:

A library of common base components for building ui

75 lines 2.92 kB
import * as React from "react"; import { AnchorNoStyle } from "./Link.styles"; import { isAbsoluteUrl, startsWithSpecialChar, } from "../../helpers/isAbsoluteUrl"; import SharedContext from "../../Context"; const LinkComponent = (props) => { const context = React.useContext(SharedContext); const [state, setState] = React.useState({ clicked: false }); try { const constructUrl = () => { const shoppingMode = props.shoppingMode ? props.shoppingMode : context.shoppingMode; const retailerStoreId = props.retailerStoreId ? props.retailerStoreId : context.retailerStoreId; if (props.isAbsolute || isAbsoluteUrl(props.href) || typeof props.href !== "string") { return props.href; } if (startsWithSpecialChar(props.href)) { return `/sm/${shoppingMode}/rsid/${retailerStoreId}${props.href}`; } return `/sm/${shoppingMode}/rsid/${retailerStoreId}/${props.href}`; }; const manageClick = (e) => { if (state.clicked) { // disallow double click e.stopPropagation(); e.preventDefault(); return; } setState({ clicked: true }); setTimeout(() => setState({ clicked: false }), 1000); if (props.handleClick) { props.handleClick(e); } }; const tagElement = { ["aria-label"]: state.clicked ? `${props.ariaLabel || "link"} clicked` : props.ariaLabel, href: constructUrl(), onBlur: props.handleBlur, onClick: manageClick, onMouseDown: props.handleMouseDown, onMouseEnter: props.handleMouseEnter, id: props.pageObjectId, role: props.role, target: props.target, type: props.buttonType, title: props.title, noStyles: !!props.noStyles, tabIndex: props.tabIndex, theme: !!props.theme ? props.theme : context.theme, ["data-testid"]: !!props["data-testid"] ? props["data-testid"] : LinkTestId, }; if (props.target === "_blank") { tagElement.rel = "noopener noreferrer"; } return React.createElement(AnchorNoStyle, Object.assign({}, tagElement), props.children); } catch (e) { const logger = !!context.logger ? context.logger : console; logger.error("Error: V8Atoms.Link", e, { props, }); return null; } }; export default LinkComponent; export const LinkTestId = "LinkTestId"; //# sourceMappingURL=index.js.map