UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com's products.

95 lines (94 loc) 2.96 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from "react"; import cx from "clsx"; import { TYPE_OPTIONS, SIZE_OPTIONS } from "./consts"; import createRel from "../primitives/ButtonPrimitive/common/createRel"; import { sizeClasses, typeClasses } from "./helpers/twClasses"; function filterAriaDataProps(props) { return Object.keys(props).reduce((acc, key) => { if (key.startsWith("data-") || key.startsWith("aria-")) { acc[key] = props[key]; } return acc; }, {}); } const getComponent = (href, asComponent) => { if (asComponent) return asComponent; if (href) return "a"; return "button"; }; const IconContainer = ({ children, size }) => { if (!children) return null; return /*#__PURE__*/React.createElement("span", { className: cx("flex items-center", { "[&_svg]:w-icon-large [&_svg]:h-icon-large": size === SIZE_OPTIONS.LARGE, "[&_svg]:w-icon-small [&_svg]:h-icon-small": size === SIZE_OPTIONS.SMALL, "[&_svg]:w-icon-medium [&_svg]:h-icon-medium": !size || size !== SIZE_OPTIONS.SMALL && size !== SIZE_OPTIONS.LARGE }) }, children); }; const TextLink = ({ ariaCurrent, type = TYPE_OPTIONS.PRIMARY, size, children, href, external = false, rel, iconLeft, iconRight, onClick, dataTest, download, id, tabIndex, asComponent, stopPropagation = false, title, standAlone, noUnderline, ref, ...props }) => { const onClickHandler = ev => { if (stopPropagation) { ev.stopPropagation(); } if (onClick) { // @ts-expect-error We can't infer the correct event type here, but it works at runtime onClick(ev); } }; const filteredAriaDataProps = filterAriaDataProps(props); const Component = getComponent(href, asComponent); return /*#__PURE__*/React.createElement(Component, _extends({ ref: ref, "aria-current": ariaCurrent, id: id, href: href, target: external ? "_blank" : undefined, rel: createRel({ href, external, rel }), onClick: onClickHandler, "data-test": dataTest, tabIndex: tabIndex, type: Component === "button" ? "button" : undefined, role: !href && onClick ? "button" : undefined, title: title, download: download, className: cx("orbit-text-link font-base duration-fast inline-flex cursor-pointer items-center font-medium transition-colors delay-0 ease-in-out hover:no-underline hover:outline-none active:no-underline active:outline-none", type === "secondary" && "orbit-text-link--secondary", standAlone && "h-form-box-normal", typeClasses[type], size != null && sizeClasses[size], noUnderline ? "no-underline" : "underline") }, filteredAriaDataProps), /*#__PURE__*/React.createElement(IconContainer, { size: size }, iconLeft), children, /*#__PURE__*/React.createElement(IconContainer, { size: size }, iconRight)); }; TextLink.displayName = "TextLink"; export default TextLink;