@anoki/fse-ui
Version:
FSE UI components library
3 lines (2 loc) • 3.07 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require('./components/ui/TableOfContents/TableOfContents.css');const s=require("./index.cjs277.js"),c=require("react");;/* empty css */const T=require("./index.cjs319.js"),f=require("./index.cjs279.js"),q=require("./index.cjs218.js"),C=require("./index.cjs19.js"),N=require("./index.cjs498.js"),I=({title:g="Indice",headings:l,titleTag:b="h1",modeAuthenticated:j=!1,isOpen:m,onToggle:p=()=>{}})=>{const[w,h]=c.useState(""),[R,v]=c.useState(0),i=T.useMediaQueryV2("(min-width: 768px)",!1),E=i||m,a=c.useRef(!1),u=c.useRef(null);c.useEffect(()=>{const e=()=>{const o=window.scrollY,t=document.documentElement.scrollHeight-window.innerHeight,r=t>0?Math.min(o/t*100,100):0;v(r)};return window.addEventListener("scroll",e),e(),()=>window.removeEventListener("scroll",e)},[]);const d=c.useCallback(e=>{if(window.innerWidth<768)return;const o=document.querySelector(`a[href="#${e}"]`),t=document.querySelector(".toc-scroll");if(o&&t){const r=t.getBoundingClientRect(),n=o.getBoundingClientRect();if(!(n.top>=r.top&&n.bottom<=r.bottom)){const y=n.top-r.top,S=t.scrollTop+y-r.height/2+n.height/2;t.scrollTo({top:S,behavior:"smooth"})}}},[i]);return c.useEffect(()=>{const e=new IntersectionObserver(o=>{if(a.current)return;const t=o.filter(n=>n.isIntersecting);if(t.length===0)return;const r=t.reduce((n,x)=>x.boundingClientRect.top<n.boundingClientRect.top?x:n);h(r.target.id),d(r.target.id)},{rootMargin:i?"-90px 0px -70% 0px":"-35% 0px -35% 0px",threshold:0});return l.forEach(({id:o})=>{const t=document.getElementById(o);t&&e.observe(t)}),()=>e.disconnect()},[l,d,i]),s.jsxRuntimeExports.jsxs("div",{className:f.clsx("toc-container",{"toc-container-authenticated":j}),children:[s.jsxRuntimeExports.jsx("button",{className:"ui-button-accordion-toc",onClick:p,type:"button",children:s.jsxRuntimeExports.jsxs(q.Row,{className:"ui-header-table-of-contents",children:[s.jsxRuntimeExports.jsx(b,{className:"text-slate-20 fw-semibold fs-7",children:g}),s.jsxRuntimeExports.jsx(C.ArrowDown,{color:"primary",className:f.clsx("ui-arrow-toc",{"ui-expanded-toc":m})})]})}),s.jsxRuntimeExports.jsx("div",{className:"scroll-progress-bar",children:s.jsxRuntimeExports.jsx("div",{className:"scroll-progress",style:{width:`${R}%`}})}),E&&s.jsxRuntimeExports.jsx(N.ScrollArea,{type:"always",scrollbars:"vertical",className:"toc-scroll fw-semibold fs-6",children:s.jsxRuntimeExports.jsx("div",{className:"toc-list base-col",children:l.map(e=>s.jsxRuntimeExports.jsx("div",{className:"toc-item",children:s.jsxRuntimeExports.jsx("a",{href:`#${e.id}`,className:`toc-link ${w===e.id?"active text-primary":"text-slate-28"}`,onClick:o=>{o.preventDefault(),h(e.id),a.current=!0,u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{a.current=!1},1e3),setTimeout(()=>{d(e.id)},100),p(),requestAnimationFrame(()=>{var t;(t=document.getElementById(e.id))==null||t.scrollIntoView({behavior:"smooth"})})},children:e.text})},e.id))})})]})};exports.TableOfContents=I;
//# sourceMappingURL=index.cjs252.js.map