UNPKG

@redocly/graphql-docs

Version:

Redocly GraphQL docs

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