UNPKG

react-responsive-menu-hook

Version:
9 lines (8 loc) 5.28 kB
import r from"react";function e(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function t(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),t.push.apply(t,n)}return t}function n(r){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?t(Object(o),!0).forEach((function(t){e(r,t,o[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(o,e))}))}return r}function o(r,e){if(null==r)return{};var t,n,o=function(r,e){if(null==r)return{};var t,n,o={},i=Object.keys(r);for(n=0;n<i.length;n++)t=i[n],e.indexOf(t)>=0||(o[t]=r[t]);return o}(r,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(r);for(n=0;n<i.length;n++)t=i[n],e.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(r,t)&&(o[t]=r[t])}return o}function i(r,e){return function(r){if(Array.isArray(r))return r}(r)||function(r,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(r)))return;var t=[],n=!0,o=!1,i=void 0;try{for(var u,a=r[Symbol.iterator]();!(n=(u=a.next()).done)&&(t.push(u.value),!e||t.length!==e);n=!0);}catch(r){o=!0,i=r}finally{try{n||null==a.return||a.return()}finally{if(o)throw i}}return t}(r,e)||function(r,e){if(!r)return;if("string"==typeof r)return u(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);"Object"===t&&r.constructor&&(t=r.constructor.name);if("Map"===t||"Set"===t)return Array.from(r);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return u(r,e)}(r,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}var a,c=(function(r){ /*! Copyright (c) 2018 Jed Watson. Licensed under the MIT License (MIT), see http://jedwatson.github.io/classnames */ !function(){var e={}.hasOwnProperty;function t(){for(var r=[],n=0;n<arguments.length;n++){var o=arguments[n];if(o){var i=typeof o;if("string"===i||"number"===i)r.push(o);else if(Array.isArray(o)){if(o.length){var u=t.apply(null,o);u&&r.push(u)}}else if("object"===i)if(o.toString===Object.prototype.toString)for(var a in o)e.call(o,a)&&o[a]&&r.push(a);else r.push(o.toString())}}return r.join(" ")}r.exports?(t.default=t,r.exports=t):window.classNames=t}()}(a={exports:{}},a.exports),a.exports),l=function(){};if("production"!==process.env.NODE_ENV){var f=function(r,e){var t=arguments.length;e=new Array(t>1?t-1:0);for(var n=1;n<t;n++)e[n-1]=arguments[n];var o=0,i="Warning: "+r.replace(/%s/g,(function(){return e[o++]}));"undefined"!=typeof console&&console.error(i);try{throw new Error(i)}catch(r){}};l=function(r,e,t){var n=arguments.length;t=new Array(n>2?n-2:0);for(var o=2;o<n;o++)t[o-2]=arguments[o];if(void 0===e)throw new Error("`warning(condition, format, ...args)` requires a warning message argument");r||f.apply(null,[e].concat(t))}}var s=l,p="function"==typeof Symbol&&Symbol.for?Symbol.for("react.fragment"):60107,y=function(r){return r.type===p},d={menuWrapper:{display:"flex",overflow:"hidden"}},v=function(e){var t=e.children,u=r.useRef(null),a=i(r.useState({}),2),l=a[0],f=a[1],p=i(r.useState(!1),2),v=p[0],b=p[1],g=r.useCallback((function(r){return b(r)}),[b]),m=r.useCallback((function(r){var e={};r.forEach((function(r){var t=r.target.dataset.targetid;r.isIntersecting?e[t]=!0:e[t]=!1})),f((function(r){return n(n({},r),e)}))}),[f]),h=r.useCallback((function(){var r=new IntersectionObserver(m,{root:u.current,threshold:1});return Array.from(u.current.children).forEach((function(e){return e.dataset.targetid&&r.observe(e)})),r}),[]),O=r.useCallback((function(r){var e=r.target;return u&&u.current&&!u.current.contains(e)&&g(!1)}),[g]),w=r.useMemo((function(){return Object.values(l).some((function(r){return!1===r}))}),[l]),j=function(r){return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.id,i=o(e,["id"]);return n({"data-targetid":t,className:c({visible:"menu"===r&&!!l[t]||"overflowMenu"===r&&!l[t],inVisible:"menu"===r&&!l[t]||"overflowMenu"===r&&!!l[t]}),onClick:function(){return"overflowMenu"===r&&g(!1)}},i)}},E=r.useCallback((function(r){return y(r)?r.props.children.filter((function(r){return r&&r.props["data-targetid"]})):r}),[]),S={getMenuProps:j("menu"),showEllipsis:w,children:r.Children.map(E(t({getMenuProps:j("overflowMenu")})),(function(e){return e&&e.props["data-targetid"]&&!l[e.props["data-targetid"]]?r.createElement(r.Fragment,{key:e},r.cloneElement(e,{onClick:function(){return g(!1)}})):null})),open:v,toggle:g};return r.useEffect((function(){var r=h();return document.addEventListener("click",O),function(){r.disconnect(),document.removeEventListener("click",O)}}),[]),r.useEffect((function(){s(y(t(S)),"Menu should wrapped inside fragment")}),[]),r.createElement("div",{ref:u,style:d.menuWrapper},t(S))},b=r.memo(v);export default b; //# sourceMappingURL=react-responsive-menu-hook.min.mjs.map