@wordpress/components
Version:
UI components for WordPress.
1 lines • 3.03 kB
Source Map (JSON)
{"version":3,"file":"use-track-overflow.mjs","names":[],"sources":["../../src/tabs/use-track-overflow.ts"],"sourcesContent":["/* eslint-disable jsdoc/require-param */\nimport { useState, useEffect } from '@wordpress/element';\nimport { useEvent } from '@wordpress/compose';\n\n/**\n * Tracks if an element contains overflow and on which end by tracking the\n * first and last child elements with an `IntersectionObserver` in relation\n * to the parent element.\n *\n * Note that the returned value will only indicate whether the first or last\n * element is currently \"going out of bounds\" but not whether it happens on\n * the X or Y axis.\n */\nexport function useTrackOverflow(\n\tparent: HTMLElement | undefined | null,\n\tchildren: {\n\t\tfirst: HTMLElement | undefined | null;\n\t\tlast: HTMLElement | undefined | null;\n\t}\n) {\n\tconst [ first, setFirst ] = useState( false );\n\tconst [ last, setLast ] = useState( false );\n\tconst [ observer, setObserver ] = useState< IntersectionObserver >();\n\n\tconst callback: IntersectionObserverCallback = useEvent( ( entries ) => {\n\t\tfor ( const entry of entries ) {\n\t\t\tif ( entry.target === children.first ) {\n\t\t\t\tsetFirst( ! entry.isIntersecting );\n\t\t\t}\n\t\t\tif ( entry.target === children.last ) {\n\t\t\t\tsetLast( ! entry.isIntersecting );\n\t\t\t}\n\t\t}\n\t} );\n\n\tuseEffect( () => {\n\t\tif ( ! parent || ! window.IntersectionObserver ) {\n\t\t\treturn;\n\t\t}\n\t\tconst newObserver = new IntersectionObserver( callback, {\n\t\t\troot: parent,\n\t\t\tthreshold: 0.9,\n\t\t} );\n\t\tsetObserver( newObserver );\n\n\t\treturn () => newObserver.disconnect();\n\t}, [ callback, parent ] );\n\n\tuseEffect( () => {\n\t\tif ( ! observer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( children.first ) {\n\t\t\tobserver.observe( children.first );\n\t\t}\n\t\tif ( children.last ) {\n\t\t\tobserver.observe( children.last );\n\t\t}\n\n\t\treturn () => {\n\t\t\tif ( children.first ) {\n\t\t\t\tobserver.unobserve( children.first );\n\t\t\t}\n\t\t\tif ( children.last ) {\n\t\t\t\tobserver.unobserve( children.last );\n\t\t\t}\n\t\t};\n\t}, [ children.first, children.last, observer ] );\n\n\treturn { first, last };\n}\n/* eslint-enable jsdoc/require-param */\n"],"mappings":";;;;;;;;;;;;AAaA,SAAgB,iBACf,QACA,UAIC;CACD,MAAM,CAAE,OAAO,YAAa,SAAU,KAAM;CAC5C,MAAM,CAAE,MAAM,WAAY,SAAU,KAAM;CAC1C,MAAM,CAAE,UAAU,eAAgB,SAAiC;CAEnE,MAAM,WAAyC,UAAY,YAAa;EACvE,KAAM,MAAM,SAAS,SAAU;GAC9B,IAAK,MAAM,WAAW,SAAS,OAC9B,SAAU,CAAE,MAAM,cAAe;GAElC,IAAK,MAAM,WAAW,SAAS,MAC9B,QAAS,CAAE,MAAM,cAAe;EAElC;CACD,CAAE;CAEF,gBAAiB;EAChB,IAAK,CAAE,UAAU,CAAE,OAAO,sBACzB;EAED,MAAM,cAAc,IAAI,qBAAsB,UAAU;GACvD,MAAM;GACN,WAAW;EACZ,CAAE;EACF,YAAa,WAAY;EAEzB,aAAa,YAAY,WAAW;CACrC,GAAG,CAAE,UAAU,MAAO,CAAE;CAExB,gBAAiB;EAChB,IAAK,CAAE,UACN;EAGD,IAAK,SAAS,OACb,SAAS,QAAS,SAAS,KAAM;EAElC,IAAK,SAAS,MACb,SAAS,QAAS,SAAS,IAAK;EAGjC,aAAa;GACZ,IAAK,SAAS,OACb,SAAS,UAAW,SAAS,KAAM;GAEpC,IAAK,SAAS,MACb,SAAS,UAAW,SAAS,IAAK;EAEpC;CACD,GAAG;EAAE,SAAS;EAAO,SAAS;EAAM;CAAS,CAAE;CAE/C,OAAO;EAAE;EAAO;CAAK;AACtB"}