@equinor/amplify-component-lib 9.14.0-beta.0 → 9.14.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t}from"react/jsx-runtime";import{createContext as e,useState as r,useRef as n,useMemo as o,useEffect as i,useCallback as s,useContext as
|
|
1
|
+
import{jsx as t}from"react/jsx-runtime";import{createContext as e,useState as r,useRef as n,useMemo as o,useEffect as i,useCallback as s,useContext as c}from"react";import{useNavigate as l,useLocation as u}from"@tanstack/react-router";import{useOnScreenMultiple as a}from"../atoms/hooks/useOnScreenMultiple.js";import{getValues as h}from"./TableOfContentsProvider.utils.js";const m=e(void 0);function d(){const t=c(m);if(void 0===t)throw new Error("'useTableOfContents' must be used within provider");return t}const f=({items:e,children:c,hashNavigation:d})=>{const f=l(),{hash:v}=u(),[p,b]=r(e[0]?.value),[g,I]=r([]),w=n(!1),O=o(()=>e.flatMap(t=>h([],t)),[e]);i(()=>{I(O.map(t=>document.getElementById(t)))},[O]),i(()=>{v.length||(w.current=!0)},[v]);const k=s(t=>{if(t.value===p)return!0;if(t.children&&p){return h([],t).includes(p)}return!1},[p]),C=a(g),M=n(-1),j=s((t,e)=>{const r=O.findIndex(e=>e===t);if(d)return b(O[r]),void f({to:".",hash:`#${O[r]}`,hashScrollIntoView:{block:"start",behavior:e?.shouldInstantlyJumpOnMount?"instant":e?.behavior??"smooth"},replace:!0});const n=g[r];if(n){const t=e?.behavior??"smooth";n.scrollIntoView({block:"start",behavior:t}),M.current=r;let o=1/0,i=0;const s=()=>{const t=n?.getBoundingClientRect().top;if(t===o){if(i+=1,i>1)return b(O[r]),void(M.current=-1)}else i=0,o=t;requestAnimationFrame(s)};requestAnimationFrame(s)}},[g,d,f,O]);return i(()=>{if(v&&!w.current){w.current=!0;const t=decodeURIComponent(v.replace("#",""));if(!t.length||!O.includes(t))return;j(t,{behavior:"instant",shouldInstantlyJumpOnMount:!0})}},[j,v,O]),i(()=>{if(0===C.length||C.length!==O.length||-1!==M.current||!w.current)return;let t=-1;for(let e=C.length-1;e>=0;e--)C[e]&&(t=e);-1!==t&&void 0!==O.at(t)&&j(O[t])},[j,O,C]),t(m.Provider,{value:{items:e,selected:p,setSelected:j,isActive:k},children:c})};export{f as TableOfContentsProvider,d as useTableOfContents};
|