UNPKG

carbon-react

Version:

A library of reusable React components for easily building user interfaces.

2 lines (1 loc) 4.39 kB
import{jsx as e,jsxs as n,Fragment as t}from"react/jsx-runtime";import r,{useState as o,useContext as i,useRef as l,useCallback as a,useMemo as s,useEffect as c}from"react";import p from"../icon/icon.component.js";import u from"../menu/__internal__/menu.context.js";import{StyledLink as d,StyledContent as b}from"./link.style.js";import f from"../../__internal__/utils/helpers/tags/tags.js";import m from"../../hooks/__internal__/useLocale/useLocale.js";import y from"../batch-selection/__internal__/batch-selection.context.js";import k from"../../__internal__/utils/logger/index.js";function v(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function O(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{},r=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(t).filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})))),r.forEach((function(n){v(e,n,t[n])}))}return e}function g(e,n){return n=null!=n?n:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):function(e){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);n.push.apply(n,t)}return n}(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})),e}let h=!1,j=!1,w=!1,_=!1;const P={default:!1,neutral:!1},L={default:"typical",neutral:"subtle"},D=r.forwardRef(((v,D)=>{var{children:S,onKeyDown:M,href:x,onClick:A,onMouseDown:B,icon:T,iconAlign:C="left",isSkipLink:E,disabled:I=!1,underline:N="always",ariaLabel:z,rel:K,tooltipMessage:F,tooltipPosition:$,target:R,variant:W="typical",isDarkBackground:q,inverse:G,removeAriaLabelOnIcon:H,className:J,linkSize:Q="medium",download:U}=v,V=function(e,n){if(null==e)return{};var t,r,o=function(e,n){if(null==e)return{};var t,r,o={},i=Object.keys(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||(o[t]=e[t]);return o}(e,n);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)t=i[r],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(o[t]=e[t])}return o}(v,["children","onKeyDown","href","onClick","onMouseDown","icon","iconAlign","isSkipLink","disabled","underline","ariaLabel","rel","tooltipMessage","tooltipPosition","target","variant","isDarkBackground","inverse","removeAriaLabelOnIcon","className","linkSize","download"]);const[X,Y]=o(!1),Z=m(),{inMenu:ee}=i(u),{batchSelectionDisabled:ne}=i(y),te=I||ne,re=l(null);!h&&I&&(h=!0,k.deprecate("The 'disabled' prop in Link is deprecated and will soon be removed.")),!w&&F&&(w=!0,k.deprecate("The 'tooltipMessage' prop in Link is deprecated and will soon be removed.")),!j&&$&&(j=!0,k.deprecate("The 'tooltipPosition' prop in Link is deprecated and will soon be removed.")),!_&&q&&(_=!0,k.deprecate("The 'isDarkBackground' prop in Link is deprecated and will soon be removed. Please use 'inverse' prop instead.")),!P[W]&&L[W]&&(P[W]=!0,k.deprecate(`The value '${W}' for the variant prop is deprecated and will soon be removed. Please use value '${L[W]}' instead.`));const oe=null!=G?G:q;var ie;const le=null!==(ie=L[W])&&void 0!==ie?ie:W,ae=a((e=>{re.current=e,D&&("function"!=typeof D?"object"!=typeof D||(D.current=e):D(e))}),[D]),se=a((e=>{var n,t;null===(t=re.current)||void 0===t||null===(n=t.focus)||void 0===n||n.call(t,{preventScroll:!0}),null==A||A(e)}),[A]),ce=(n="left")=>T&&C===n?e(p,{type:T,disabled:te,ariaLabel:H?void 0:z,tooltipMessage:F,tooltipPosition:$}):null,pe=s((()=>{const e=V;return Object.keys(e).filter((e=>e.startsWith("aria"))).reduce(((n,t)=>(n[t]=e[t],n)),{})}),[V]),ue=g(O({onKeyDown:M,onMouseDown:B,onClick:se,disabled:te,target:R,ref:ae,href:x,rel:K,"aria-label":z},pe),{onFocus:()=>Y(!0),onBlur:()=>Y(!1)}),de={type:"button"};return c((()=>{(I||!x&&!A)&&Y(!1)}),[I,x,A]),e(d,g(O({isSkipLink:E,disabled:te,underline:N,iconAlign:C,className:J,hasContent:Boolean(S),variant:le,inverse:oe,isMenuItem:ee},f("link",V),E&&{"data-element":"skip-link"}),{hasFocus:X,linkSize:Q,children:(()=>{let o="a";return A&&!x&&(o="button"),r.createElement(o,"button"===o?O({},ue,de):g(O({},ue),{download:U,"data-role":"link-anchor"}),n(t,{children:[ce(),e(b,{children:E?Z.link.skipLinkLabel():S}),ce("right")]}))})()}))}));D.displayName="Link";export{D as Link,D as default};