UNPKG

@mmcodemark/fuselage-hooks

Version:

React hooks for Fuselage, Rocket.Chat's design system and UI toolkit

3 lines (2 loc) 18.3 kB
var e,t;e=this,t=function(e,t){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t),o=function(e,t){if(!e[t])return"contentBoxSize"===t?{inlineSize:e.contentRect.width,blockSize:e.contentRect.height}:{inlineSize:Math.round(e.target.getBoundingClientRect().width),blockSize:Math.round(e.target.getBoundingClientRect().height)};var n=e[t],r=Array.isArray(n)?n[0]:n;return{blockSize:r.blockSize,inlineSize:r.inlineSize}},i=function(e){return o(e,"contentBoxSize")},u=function(e){return o(e,"borderBoxSize")},c=function(e,n,r){var o=t.useMemo((function(){return e}),r),i=t.useRef(),u=t.useRef(),c=t.useCallback((function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];i.current=function(){o.apply(void 0,e)},clearTimeout(u.current),u.current=setTimeout(i.current,n)}),[o,n]),a=t.useCallback((function(){var e;clearTimeout(u.current),null===(e=i.current)||void 0===e||e.call(i)}),[]),f=t.useCallback((function(){clearTimeout(u.current)}),[]);return t.useEffect((function(){return function(){f()}}),[f]),t.useMemo((function(){return Object.assign(c,{flush:a,cancel:f})}),[c,a,f])},a="undefined"!=typeof window?t.useLayoutEffect:t.useEffect,f=function(){return f=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},f.apply(this,arguments)};function s(e,t,n,r){return new(n||(n=Promise))((function(o,i){function u(e){try{a(r.next(e))}catch(e){i(e)}}function c(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,c)}a((r=r.apply(e,t||[])).next())}))}function l(e,t){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){u.label=c[1];break}if(6===c[0]&&u.label<o[1]){u.label=o[1],o=c;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(c);break}o[2]&&u.ops.pop(),u.trys.pop();continue}c=t.call(e,u)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function d(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}var v=[{name:"xs",minViewportWidth:null,columns:4,gutterWidth:16},{name:"sm",minViewportWidth:600,columns:8,gutterWidth:16},{name:"md",minViewportWidth:768,columns:8,gutterWidth:24},{name:"lg",minViewportWidth:1024,columns:12,gutterWidth:24},{name:"xl",minViewportWidth:1280,columns:12,gutterWidth:24},{name:"xxl",minViewportWidth:1600,columns:12,gutterWidth:24},{name:"xxxl",minViewportWidth:1920,columns:12,gutterWidth:24}],p={exports:{}},h={},m=r.default,g="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},S=m.useState,y=m.useEffect,b=m.useLayoutEffect,w=m.useDebugValue;function E(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!g(e,n)}catch(e){return!0}}var z="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,t){return t()}:function(e,t){var n=t(),r=S({inst:{value:n,getSnapshot:t}}),o=r[0].inst,i=r[1];return b((function(){o.value=n,o.getSnapshot=t,E(o)&&i({inst:o})}),[e,n,t]),y((function(){return E(o)&&i({inst:o}),e((function(){E(o)&&i({inst:o})}))}),[e]),w(n),n};h.useSyncExternalStore=void 0!==m.useSyncExternalStore?m.useSyncExternalStore:z;var R={};"production"!==process.env.NODE_ENV&&function(){"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error);var e=r.default,t=e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function n(e){for(var n=arguments.length,r=new Array(n>1?n-1:0),o=1;o<n;o++)r[o-1]=arguments[o];!function(e,n,r){var o=t.ReactDebugCurrentFrame.getStackAddendum();""!==o&&(n+="%s",r=r.concat([o]));var i=r.map((function(e){return String(e)}));i.unshift("Warning: "+n),Function.prototype.apply.call(console[e],console,i)}("error",e,r)}var o="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},i=e.useState,u=e.useEffect,c=e.useLayoutEffect,a=e.useDebugValue,f=!1,s=!1;function l(e){var t=e.getSnapshot,n=e.value;try{var r=t();return!o(n,r)}catch(e){return!0}}var d="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,t,n){return t()}:function(t,r,d){f||void 0!==e.startTransition&&(f=!0,n("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var v=r();if(!s){var p=r();o(v,p)||(n("The result of getSnapshot should be cached to avoid an infinite loop"),s=!0)}var h=i({inst:{value:v,getSnapshot:r}}),m=h[0].inst,g=h[1];return c((function(){m.value=v,m.getSnapshot=r,l(m)&&g({inst:m})}),[t,v,r]),u((function(){return l(m)&&g({inst:m}),t((function(){l(m)&&g({inst:m})}))}),[t]),a(v),v},v=void 0!==e.useSyncExternalStore?e.useSyncExternalStore:d;R.useSyncExternalStore=v,"undefined"!=typeof __REACT_DEVTOOLS_GLOBAL_HOOK__&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error)}(),"production"===process.env.NODE_ENV?p.exports=h:p.exports=R;var _=function(e,t,n){return function(e,t,n){return!Array.isArray(t)||e.current!==t&&(e.current.length!==t.length||e.current.some((function(e,r){return!n(e,t[r])})))}(e,t,n)&&(e.current=t),e.current},O=function(e,n){void 0===n&&(n=Object.is);var r=t.useRef(e);return _(r,e,n)},x=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var r=O(e),o=t.useMemo((function(){return function(e){if("undefined"==typeof window||"function"!=typeof window.matchMedia){var t=Array.from({length:e.length},(function(){return!1}));return[function(){return function(){}},function(){return t}]}var n=e.map((function(e){return window.matchMedia(e)})),r=n.map((function(e){return e.matches}));return[function(e){var t=function(){r=n.map((function(e){return e.matches})),e()};return n.forEach((function(e){"function"!=typeof e.addEventListener?e.addListener(t):e.addEventListener("change",t)})),function(){n.forEach((function(e){"function"!=typeof e.removeEventListener?e.removeListener(t):e.removeEventListener("change",t)}))}},function(){return r}]}(r)}),[r]),i=o[0],u=o[1];return p.exports.useSyncExternalStore(i,u)},L=v.slice(1).map((function(e){return"(min-width: ".concat(e.minViewportWidth,"px)")})),k=function(e){var n=t.useRef(e),r=t.useRef((function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=n.current).call.apply(e,d([void 0],t,!1))}));return a((function(){n.current=e})),r.current},B=function(e){return!!x.apply(void 0,e?[e]:[])[0]},T=function(e){return B("(prefers-color-scheme: ".concat(e||"light",")"))};function C(e,t){var n=e[0],r=e[1];return[n,c(r,t,[])]}function A(e,n){return C(t.useState(e),n)}var D=Symbol("empty"),M="undefined"!=typeof window&&window.document?function(e){var n=t.useRef(D);return t.useLayoutEffect((function(){n.current===D&&(n.current=e())}),[e]),n}:function(e){return t.useRef(e())};function V(e){var n=e[0],r=e[1],o=t.useRef(r);t.useEffect((function(){return function(){o.current=void 0}}),[]);var i=t.useCallback((function(e){var t=o.current;null==t||t(e)}),[]);return[n,i]}function W(e,n){t.useEffect((function(){var t=e.current;if(t){var r=function(){e.current&&n()};r();var o=new ResizeObserver(r);o.observe(t),window.addEventListener("resize",r);var i=function(e){for(var t=[],n=e.parentElement;n&&n!==document.documentElement;n=n.parentElement)t.push(n);return t}(t);return i.forEach((function(e){return e.addEventListener("scroll",r,{passive:!0})})),function(){o.disconnect(),window.removeEventListener("resize",r),i.forEach((function(e){return e.removeEventListener("scroll",r)}))}}}),[n,e])}var I={top:["t","b","r","l"],bottom:["b","t","r","l"],right:["r","l","t","b"],left:["l","r","b","t"]},j={start:["s","e","m"],middle:["m","s","e"],end:["e","s","m"]},H={t:"top",b:"bottom",l:"left",r:"right",s:"start",e:"end",m:"middle"},N={style:{position:"fixed",visibility:"hidden"}},P=function(e){var t=e.split("-"),n=t[0],r=t[1];return[n,void 0===r?"middle":r]};function G(e){var t,n=e.placement,r=e.targetRect,o=e.containerRect,i=e.targetBoundaries,u=e.variantBoundaries,c=e.margin,a=void 0===c?0:c;if(!i)return N;for(var s=o.top,l=o.left,d=o.bottom,v=o.right,p=P(n),h=p[0],m=p[1],g=I[h],S=j[m],y=0,b=g;y<b.length;y++){var w=b[y],E=["t","b"].includes(w),z=E?["top","left"]:["left","top"],R=z[0],_=z[1],O=i[w],x=E?[r.height,r.width]:[r.width,r.height],L=x[1],k=E?[d,v]:[v,d],B=k[1],T=E?[s,l]:[l,s],C=T[1];if(!(O<T[0]||O+x[0]>k[0]))for(var A=0,D=S;A<D.length;A++){var M=D[A],V=u["".concat(E?"v":"h").concat(M)];if(!(V<C||V+L>B))return{style:(t={},t[R]="".concat(O,"px"),t[_]="".concat(V,"px"),t.position="fixed",t.zIndex=9999,t.opacity=1,t),placement:"".concat(H[w],"-").concat(H[M])}}}var W=g[0],G=["t","b"].includes(W),F=i[W],K=u["".concat(G?"v":"h").concat(S[0])];return{style:f(f({top:"".concat(F,"px"),left:"".concat(K,"px"),position:"fixed"},d<r.height+F&&{bottom:"".concat(a,"px"),overflowY:"auto"}),{zIndex:"9999"}),placement:"".concat(H[W],"-").concat(H[S[0]])}}var F,K,U=Symbol("once"),J=Symbol("evts"),Y=function(){function e(){this[F]=new Map,this[K]=new WeakMap}return e.prototype.events=function(){return Array.from(this[J].keys())},e.prototype.has=function(e){return this[J].has(e)},e.prototype.on=function(e,t){var n,r=this,o=null!==(n=this[J].get(e))&&void 0!==n?n:[];return o.push(t),this[J].set(e,o),function(){return r.off(e,t)}},e.prototype.once=function(e,t){var n=this[U].get(t)||0;return this[U].set(t,n+1),this.on(e,t)},e.prototype.off=function(e,t){var n,r=this[J].get(e);if(r){var o=null!==(n=this[U].get(t))&&void 0!==n?n:0;o>1?this[U].set(t,o-1):this[U].delete(t),1!==r.length?r.splice(r.findIndex((function(e){return e===t}))>>>0,1):this[J].delete(e)}},e.prototype.emit=function(e){for(var t,n=this,r=[],o=1;o<arguments.length;o++)r[o-1]=arguments[o];var i=r[0];(function(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))})([],null!==(t=this[J].get(e))&&void 0!==t?t:[],!0).forEach((function(t){t(i),n[U].get(t)&&n.off(e,t)}))},e}();function q(e,n){var r=void 0;"undefined"!=typeof window&&(r="function"==typeof e?e():e);var o=function(e){return"fuselage-".concat(n,"-").concat(e)},i=new Y;return function(e,n){var u=t.useRef(n);u.current=n;var c=t.useState((function(){if(!r)return n;var t=r.getItem(o(e));return t?JSON.parse(t):n})),a=c[0],f=c[1],s=t.useCallback((function(t){f((function(n){var u=t instanceof Function?t(n):t;return null==r||r.setItem(o(e),JSON.stringify(u)),i.emit(e,u),u}))}),[e]);return t.useEffect((function(){var t=function(t){t.key===o(e)&&f(t.newValue?JSON.parse(t.newValue):u.current)},n=function(e){f(e)};return i.on(e,n),window.addEventListener("storage",t),function(){i.off(e,n),window.removeEventListener("storage",t)}}),[e]),[a,s]}}F=J,K=U;var Q=q((function(){return window.localStorage}),"localStorage"),X=q((function(){return window.sessionStorage}),"sessionStorage");e.getPositionStyle=G,e.useAutoFocus=function(e,n){void 0===e&&(e=!0);var r=t.useRef(null),o=(n||{}).preventScroll;return t.useEffect((function(){e&&r.current&&r.current.focus({preventScroll:o})}),[r,e,o]),r},e.useBorderBoxSize=function(e,n){var r=(void 0===n?{}:n).debounceDelay,o=void 0===r?0:r,i=t.useState((function(){var t,n,r,o;return{inlineSize:null!==(n=null===(t=e.current)||void 0===t?void 0:t.offsetWidth)&&void 0!==n?n:0,blockSize:null!==(o=null===(r=e.current)||void 0===r?void 0:r.offsetHeight)&&void 0!==o?o:0}})),f=i[0],s=i[1],l=c(s,o);return a((function(){var t=e.current;if(t){var n=new ResizeObserver((function(e){if(0!==e.length){var t=u(e[0]);l((function(e){return e.inlineSize===t.inlineSize&&e.blockSize===t.blockSize?e:{inlineSize:t.inlineSize,blockSize:t.blockSize}}))}}));return n.observe(t),s({inlineSize:t.offsetWidth,blockSize:t.offsetHeight}),function(){n.unobserve(t)}}}),[l]),f},e.useBreakpoints=function(){var e=x.apply(void 0,L);return t.useMemo((function(){return e.reduce((function(e,t,n){return t?d(d([],e,!0),[v[n+1].name],!1):e}),[v[0].name])}),[e])},e.useClipboard=function(e,n){var r=void 0===n?{}:n,o=r.clearTime,i=void 0===o?2e3:o,u=r.onCopySuccess,c=void 0===u?function(){}:u,a=r.onCopyError,f=void 0===a?function(){}:a,d=t.useState(!1),v=d[0],p=d[1],h=k((function(t){return s(void 0,void 0,void 0,(function(){var n;return l(this,(function(r){switch(r.label){case 0:null==t||t.preventDefault(),r.label=1;case 1:return r.trys.push([1,3,,4]),[4,navigator.clipboard.writeText(e)];case 2:return r.sent(),c(t),p(!0),[3,4];case 3:if((n=r.sent())instanceof Error)return f(n),[2];throw n;case 4:return[2]}}))}))}));return t.useEffect((function(){if(v){var e=setTimeout((function(){p(!1)}),i);return function(){return clearTimeout(e)}}}),[v,i]),{copy:h,hasCopied:v}},e.useContentBoxSize=function(e,n){var r,o,u,f,s=(void 0===n?{}:n).debounceDelay,l=void 0===s?0:s,d=t.useState({inlineSize:null!==(o=null===(r=e.current)||void 0===r?void 0:r.clientWidth)&&void 0!==o?o:0,blockSize:null!==(f=null===(u=e.current)||void 0===u?void 0:u.clientHeight)&&void 0!==f?f:0}),v=d[0],p=d[1],h=c(p,l);return a((function(){var t=e.current;if(t){var n=new ResizeObserver((function(e){if(0!==e.length){var t=i(e[0]);h((function(e){return e.inlineSize===t.inlineSize&&e.blockSize===t.blockSize?e:{inlineSize:t.inlineSize,blockSize:t.blockSize}}))}}));return n.observe(t),p({inlineSize:t.clientWidth,blockSize:t.clientHeight}),function(){n.unobserve(t)}}}),[h]),v},e.useDarkMode=function(e){var t=T("dark");return void 0!==e?e:t},e.useDebouncedCallback=c,e.useDebouncedReducer=function(e,n,r,o){return C(void 0!==r?t.useReducer(e,n,r):t.useReducer(e,n),o)},e.useDebouncedState=A,e.useDebouncedUpdates=C,e.useDebouncedValue=function(e,n){var r=t.useState((function(){return e})),o=r[0],i=r[1];return t.useEffect((function(){var t=setTimeout((function(){i((function(){return e}))}),n);return function(){clearTimeout(t)}}),[e,n]),o},e.useIsomorphicLayoutEffect=a,e.useLazyRef=M,e.useLocalStorage=Q,e.useMediaQueries=x,e.useMediaQuery=B,e.useMergedRefs=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var r=t.useRef(e);return a((function(){r.current=e})),t.useCallback((function(e){r.current.filter(Boolean).forEach((function(t){"function"!=typeof t?function(e){return"object"==typeof e}(t)&&(t.current=e):t(e)}))}),[])},e.useMutableCallback=k,e.useOutsideClick=function(e,n){var r=k((function(t){if(e.every((function(e){return t&&e.current&&!e.current.contains(t.target)})))return n(t)}));t.useEffect((function(){return document.addEventListener("mousedown",r),function(){document.removeEventListener("mousedown",r)}}),[r])},e.usePosition=function(e,n,r){var o=void 0===r?{}:r,i=o.margin,u=void 0===i?8:i,a=o.placement,f=void 0===a?"bottom-start":a,s=o.container,l=void 0===s?document.body:s,d=V(t.useState(N)),v=d[0],p=d[1],h=t.useRef(l);t.useEffect((function(){h.current=l}),[l]);var m=c(k((function(){var t=n.current,r=e.current,o=null==t?void 0:t.parentElement;if(t&&r&&o){var i=t.cloneNode(!0);i.style.bottom="",i.id="clone",o.appendChild(i);var c=i.getBoundingClientRect();o.removeChild(i);var a=r.getBoundingClientRect(),s=function(e){var t=e.anchorRect,n=e.targetRect,r=e.margin,o=void 0===r?0:r;return{t:t.top-n.height-o,b:t.bottom+o,r:t.right+o,l:t.left-n.width-o}}({anchorRect:a,targetRect:c,margin:u}),d=function(e){var t=e.anchorRect,n=e.targetRect;return{vm:-n.width/2+(t.left+t.width/2),vs:t.left,ve:t.left+t.width-n.width,hs:t.bottom-t.height,he:t.bottom-n.height,hm:t.bottom-t.height/2-n.height/2}}({anchorRect:a,targetRect:c});p(G({placement:f,containerRect:l.getBoundingClientRect(),targetBoundaries:s,variantBoundaries:d,targetRect:c,margin:u}))}})),30);return W(n,m),W(e,m),W(h,m),v},e.usePrefersColorScheme=T,e.usePrefersReducedData=function(){return B("(prefers-reduced-data: reduce)")},e.usePrefersReducedMotion=function(){return B("(prefers-reduced-motion: reduce)")},e.usePrevious=function(e){var n=t.useRef();return t.useEffect((function(){n.current=e})),n.current},e.useResizeObserver=function(e){var n=(void 0===e?{}:e).debounceDelay,r=void 0===n?0:n,o=t.useRef(null),c=A({borderBoxSize:{inlineSize:void 0,blockSize:void 0},contentBoxSize:{inlineSize:void 0,blockSize:void 0}},r),a=c[0],f=a.borderBoxSize,s=a.contentBoxSize,l=c[1];return t.useEffect((function(){var e=new ResizeObserver((function(e){var t=e[0];l({contentBoxSize:i(t),borderBoxSize:u(t)})}));return o.current&&e.observe(o.current),function(){e.disconnect()}}),[l]),{ref:o,contentBoxSize:s,borderBoxSize:f}},e.useSafely=V,e.useSessionStorage=X,e.useStableArray=O,e.useToggle=function(e){var n=t.useState((function(){return"function"==typeof e?!!e():!!e})),r=n[0],o=n[1],i=k((function(e){o((function(){return"boolean"==typeof e?e:"function"==typeof e?e(r):!r}))}));return[r,i]},e.useUniqueId=function(){var e=t.useRef(),n=t.useMemo((function(){return e.current||(e.current=Math.random().toString(36).slice(2)),e.current}),[]);return t.useDebugValue(n),n},Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).FuselageHooks={},e.React); //# sourceMappingURL=index.umd.js.map