@xzar90/react-router-hash-link
Version:
Hash link scroll functionality for React Router v6
3 lines (2 loc) • 3.67 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("react-router-dom");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=r(e);function o(e){var t={exports:{}};return e(t,t.exports),t.exports}var l=o((function(e,t){var r=60103;if(t.Fragment=60107,"function"==typeof Symbol&&Symbol.for){var o=Symbol.for;r=o("react.element"),t.Fragment=o("react.fragment")}var l=n.default.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,s=Object.prototype.hasOwnProperty,i={key:!0,ref:!0,__self:!0,__source:!0};function c(e,t,n){var o,c={},a=null,u=null;for(o in void 0!==n&&(a=""+n),void 0!==t.key&&(a=""+t.key),void 0!==t.ref&&(u=t.ref),t)s.call(t,o)&&!i.hasOwnProperty(o)&&(c[o]=t[o]);if(e&&e.defaultProps)for(o in t=e.defaultProps)void 0===c[o]&&(c[o]=t[o]);return{$$typeof:r,type:e,key:a,ref:u,props:c,_owner:l.current}}t.jsx=c,t.jsxs=c})),s=o((function(e){e.exports=l}));var i=new class{constructor(e){this.observer=null,this.asyncTimerId=null,this.hashFragment="",this.scrollFunction=null,this.mutate(e)}mutate(e){Object.keys(e).map((t=>{const r=t;this[t]=e[r]}))}}({});const c=i;function a(e,t){return!(""===c.hashFragment||e.defaultPrevented||0!==e.button||(null==t?void 0:t.target)&&"_self"!==(null==t?void 0:t.target)||e.metaKey||e.altKey||e.ctrlKey||e.shiftKey)}function u(e){return"string"==typeof e.to&&e.to.includes("#")?`#${e.to.split("#").slice(1).join("#")}`:"object"==typeof e.to&&"string"==typeof e.to.hash?e.to.hash:""}const d=i;function f(){d.hashFragment="",null!==d.observer&&d.observer.disconnect(),null!==d.asyncTimerId&&(window.clearTimeout(d.asyncTimerId),d.asyncTimerId=null)}function h(e){return["BUTTON","INPUT","SELECT","TEXTAREA"].includes(e.tagName)&&!e.hasAttribute("disabled")||["A","AREA"].includes(e.tagName)&&e.hasAttribute("href")}const m=i;function v(){let e=null;if("#"===m.hashFragment)e=document.body;else{const t=m.hashFragment.replace("#","");e=document.getElementById(t),null===e&&"#top"===m.hashFragment&&(e=document.body)}if(null!==e){m.scrollFunction&&m.scrollFunction(e);let t=e.getAttribute("tabindex");return null!==t||h(e)||e.setAttribute("tabindex","-1"),e.focus({preventScroll:!0}),null!==t||h(e)||(e.blur(),e.removeAttribute("tabindex")),f(),!0}return!1}const p=i;const b=i;function y(e,t,r){b.scrollFunction=t||(t=>e?t.scrollIntoView({behavior:"smooth"}):t.scrollIntoView()),function(e){window.setTimeout((()=>{!1===v()&&(null===p.observer&&(p.observer=new MutationObserver(v)),p.observer.observe(document,{attributes:!0,childList:!0,subtree:!0}),p.asyncTimerId=window.setTimeout((()=>{f()}),null!=e?e:1e4))}),0)}(r)}const g=i;var _=e=>{const r=t.useNavigate(),{scroll:o,smooth:l,timeout:i,elementId:c,onClick:d,children:h,to:m,...v}=e,p=u({...e,children:h});var b=n.default.Children.only(h),_=n.default.cloneElement(b,{onClick:e=>{b.props.onClick&&b.props.onClick(e),(e=>{f(),g.hashFragment=c?`#${c}`:p,d&&d(e),a(e)&&(r(m),y(l,o,i))})(e)}});return s.jsx(s.Fragment,{children:_},void 0)};const k=i;var F=n.default.forwardRef(((e,r)=>{const{scroll:n,smooth:o,timeout:l,elementId:i,children:c,onClick:d,...h}=e,m=u(e);return s.jsx(t.Link,{ref:r,onClick:t=>{f(),k.hashFragment=i?`#${i}`:m,d&&d(t);a(t,e)&&y(o,n,l)},...h,children:c},void 0)}));const T=i;var w=n.default.forwardRef(((e,r)=>{const{scroll:n,smooth:o,timeout:l,elementId:i,children:c,onClick:d,...h}=e,m=u(e);return s.jsx(t.NavLink,{ref:r,onClick:t=>{f(),T.hashFragment=i?`#${i}`:m,d&&d(t);a(t,e)&&y(o,n,l)},...h,children:c},void 0)}));exports.GenericHashLink=_,exports.HashLink=F,exports.NavHashLink=w;
//# sourceMappingURL=xzar90-react-router-hash-link.cjs.production.js.map