@redocly/graphql-docs
Version:
Redocly GraphQL docs
2 lines (1 loc) • 1.13 kB
JavaScript
import{useEffect as c}from"react";import{IS_BROWSER as d,addLeadingSlash as p,useActiveSectionId as m}from"@redocly/theme";import{useLocation as u}from"./use-location.js";import{normalizePath as w}from"../components/hooks/use-is-expanded.js";import{DATA_SECTION_ID as f}from"../constants.js";import{joinPath as S}from"../utils/join.js";let r=!0;const h=o=>new CustomEvent("locationChangeOnScroll",{detail:{url:o}});function C(o){const e=u(),t=m(e,!1,!1,150);c(()=>{if(t&&r){r=!1;return}if(d&&!window.location.pathname.includes(t)&&!window.location.hash.includes(t)){const n=window.history.replaceState;window.history.replaceState=function(l,s,a){n.apply(this,[l,s,a]),window.dispatchEvent(h(a))};const i=p(t);window.history.replaceState({},"",i)}},[t]),c(()=>{g({location:e,basePath:o})},[e,o])}function g({location:o,basePath:e}){const t=document.querySelector(`[${f}="${decodeURIComponent(w(S(e,o.pathname)))}"]`),n=t?.getAttribute("data-testid")==="group-title";if(t&&(n||window.location.pathname===e)){const i=t.getBoundingClientRect().top+window.scrollY-120;window.scrollTo({top:i})}}export{C as useAutomaticScrollByLocation};