@redocly/graphql-docs
Version:
Redocly GraphQL docs
2 lines (1 loc) • 1.17 kB
JavaScript
import{createRef as S,memo as E,useCallback as C,useEffect as d,useMemo as B,useState as u}from"react";import{IS_BROWSER as R}from"@redocly/theme";import{useLocation as b}from"react-router-dom";function O({items:e,typeGroupId:I,renderItem:x,path:g,useVirtualList:o}){const f=b(),r=f.pathname.split("/").filter(Boolean).pop(),[l,m]=u(()=>{if(!o)return e;const n=e.findIndex(t=>t===r),a=Math.max(0,n?n-5:0),c=Math.min(e.length,n?n+6:10);return e.slice(a,c)}),[p,h]=u(!0);d(()=>{if(!o)return;const n=e.findIndex(t=>t===r),a=Math.max(0,n?n-5:0),c=Math.min(e.length,n?n+6:10);m(e.slice(a,c)),h(!0)},[r,e,e.length,f.pathname,o]);const s=C(n=>{const c=(n.detail?.url||window.location.pathname).split("/").filter(Boolean).pop(),t=e.findIndex(w=>w===c),v=Math.max(0,t?t-5:0),M=Math.min(e.length,t?t+6:10);m(e.slice(v,M)),h(!1)},[e]);d(()=>{if(R&&o)return window.addEventListener("locationChangeOnScroll",s),()=>{window.removeEventListener("locationChangeOnScroll",s)}},[s,o]);const i=B(()=>new Map(l.map(n=>[n,S()])),[l]);return d(()=>{if(!p)return;const n=i.get(r||"");n?.current&&n.current.scrollIntoView()},[r,i,p]),l.map(n=>x(n,g,I,i.get(n)))}const U=E(O);export{U as Items};