UNPKG

@redocly/asyncapi-docs

Version:

Async API docs for Redocly Realm

2 lines (1 loc) 935 B
import{useEffect as r}from"react";import{useLocation as a}from"react-router-dom";import{useActiveSectionId as c}from"@redocly/theme/core/openapi";import{IS_BROWSER as l}from"@redocly/theme/core/utils";import{tryDecodeURIComponent as m}from"../utils/index.js";let s=!0;function y(e){const t=a(),o=c(t,!1,!1);r(()=>{d({location:t,routingBasePath:e})},[t,e]),r(()=>{if(o&&s){s=!1;return}l&&window.location.pathname!==o&&window.history.replaceState({},"",o)},[o])}function d({location:e,routingBasePath:t}){if(e.pathname===t&&e.hash===""){window.scrollTo(0,0);return}const o=e.pathname,n=u(e.hash),i=f(n)||document.querySelector(`[data-section-id="${o}"]`);i?setTimeout(()=>{i.scrollIntoView()},0):window.scrollTo(0,0)}function u(e){const t=e.replace("#","");if(!t.includes("&m="))return t;const[o,n]=t.split("&m=");return n.includes("&")||n.includes("/")?t:o}function f(e){return document.getElementById(m(e))}export{y as useAutoScroll};