{"version":3,"file":"useRevealActiveRow.cjs","sources":["../../../../src/components/navbar/useRevealActiveRow.ts"],"sourcesContent":["import { RefObject, useEffect, useRef } from \"react\";\n\n/**\n * Brings the current page's row into view when the page changes: a click, a\n * route change, or the first render after a reload deep in a long menu. It\n * waits until the groups and sections above the row have opened, which is\n * why it also re-runs on `signals`.\n *\n * It never moves the list for anything else. Opening or closing a group or\n * section while scrolled away from the current page used to centre that\n * page's row, which threw the user back to the top of the menu.\n */\nexport const useRevealActiveRow = (\n  box: RefObject<HTMLElement | null>,\n  activeId: string | null,\n  signals: unknown[]\n) => {\n  const pending = useRef(false);\n  const lastId = useRef<string | null>(null);\n\n  useEffect(() => {\n    if (activeId !== lastId.current) {\n      lastId.current = activeId;\n      pending.current = activeId !== null;\n    }\n    if (!pending.current) return;\n\n    const frame = window.requestAnimationFrame(() => {\n      const list = box.current;\n      const row = list?.querySelector<HTMLElement>('[aria-current=\"page\"]');\n      // The row renders once its ancestors are open; try again on the next\n      // signal until it exists.\n      if (!list || !row) return;\n      pending.current = false;\n\n      const rowRect = row.getBoundingClientRect();\n      const boxRect = list.getBoundingClientRect();\n      const above = rowRect.top < boxRect.top;\n      const below = rowRect.bottom > boxRect.bottom;\n      if (!above && !below) return;\n\n      const hidden = rowRect.bottom <= boxRect.top || rowRect.top >= boxRect.bottom;\n      // A row the user can already partly see nudges in; one out of sight\n      // lands in the middle.\n      row.scrollIntoView({ block: hidden ? \"center\" : \"nearest\" });\n    });\n    return () => window.cancelAnimationFrame(frame);\n  }, [activeId, ...signals]); // eslint-disable-line react-hooks/exhaustive-deps\n};\n"],"names":["box","activeId","signals","pending","useRef","lastId","useEffect","current","frame","window","requestAnimationFrame","list","row","querySelector","rowRect","getBoundingClientRect","boxRect","above","top","below","bottom","hidden","scrollIntoView","block","cancelAnimationFrame","concat","_toConsumableArray"],"mappings":"mLAYkC,SAChCA,EACAC,EACAC,GAEA,IAAMC,EAAUC,UAAO,GACjBC,EAASD,SAAsB,MAErCE,EAAAA,UAAU,WAKR,GAJIL,IAAaI,EAAOE,UACtBF,EAAOE,QAAUN,EACjBE,EAAQI,QAAuB,OAAbN,GAEfE,EAAQI,QAAb,CAEA,IAAMC,EAAQC,OAAOC,sBAAsB,WACzC,IAAMC,EAAOX,EAAIO,QACXK,EAAMD,aAAAA,EAAAA,EAAME,cAA2B,yBAG7C,GAAKF,GAASC,EAAd,CACAT,EAAQI,SAAU,EAElB,IAAMO,EAAUF,EAAIG,wBACdC,EAAUL,EAAKI,wBACfE,EAAQH,EAAQI,IAAMF,EAAQE,IAC9BC,EAAQL,EAAQM,OAASJ,EAAQI,OACvC,GAAKH,GAAUE,EAAf,CAEA,IAAME,EAASP,EAAQM,QAAUJ,EAAQE,KAAOJ,EAAQI,KAAOF,EAAQI,OAGvER,EAAIU,eAAe,CAAEC,MAAOF,EAAS,SAAW,WAL1B,CAPH,CAarB,GACA,OAAO,WAAA,OAAMZ,OAAOe,qBAAqBhB,EAAM,CArBzB,GAsBvB,CAAGP,GAAQwB,OAAAC,EAAAA,kBAAKxB,IACnB"}