UNPKG

@mmcodemark/fuselage-hooks

Version:

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

3 lines (2 loc) 17.8 kB
import n,{useRef as t,useEffect as e,useMemo as r,useCallback as o,useLayoutEffect as i,useState as u,useReducer as c,useDebugValue as a}from"react";var f=function(n,r){void 0===n&&(n=!0);var o=t(null),i=(r||{}).preventScroll;return e((function(){n&&o.current&&o.current.focus({preventScroll:i})}),[o,n,i]),o},l=function(n,t){if(!n[t])return"contentBoxSize"===t?{inlineSize:n.contentRect.width,blockSize:n.contentRect.height}:{inlineSize:Math.round(n.target.getBoundingClientRect().width),blockSize:Math.round(n.target.getBoundingClientRect().height)};var e=n[t],r=Array.isArray(e)?e[0]:e;return{blockSize:r.blockSize,inlineSize:r.inlineSize}},s=function(n){return l(n,"contentBoxSize")},d=function(n){return l(n,"borderBoxSize")},v=function(n,i,u){var c=r((function(){return n}),u),a=t(),f=t(),l=o((function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];a.current=function(){c.apply(void 0,n)},clearTimeout(f.current),f.current=setTimeout(a.current,i)}),[c,i]),s=o((function(){var n;clearTimeout(f.current),null===(n=a.current)||void 0===n||n.call(a)}),[]),d=o((function(){clearTimeout(f.current)}),[]);return e((function(){return function(){d()}}),[d]),r((function(){return Object.assign(l,{flush:s,cancel:d})}),[l,s,d])},h="undefined"!=typeof window?i:e,p=function(n,t){var e=(void 0===t?{}:t).debounceDelay,r=void 0===e?0:e,o=u((function(){var t,e,r,o;return{inlineSize:null!==(e=null===(t=n.current)||void 0===t?void 0:t.offsetWidth)&&void 0!==e?e:0,blockSize:null!==(o=null===(r=n.current)||void 0===r?void 0:r.offsetHeight)&&void 0!==o?o:0}})),i=o[0],c=o[1],a=v(c,r);return h((function(){var t=n.current;if(t){var e=new ResizeObserver((function(n){if(0!==n.length){var t=d(n[0]);a((function(n){return n.inlineSize===t.inlineSize&&n.blockSize===t.blockSize?n:{inlineSize:t.inlineSize,blockSize:t.blockSize}}))}}));return e.observe(t),c({inlineSize:t.offsetWidth,blockSize:t.offsetHeight}),function(){e.unobserve(t)}}}),[a]),i},m=function(){return m=Object.assign||function(n){for(var t,e=1,r=arguments.length;e<r;e++)for(var o in t=arguments[e])Object.prototype.hasOwnProperty.call(t,o)&&(n[o]=t[o]);return n},m.apply(this,arguments)};function g(n,t,e,r){return new(e||(e=Promise))((function(o,i){function u(n){try{a(r.next(n))}catch(n){i(n)}}function c(n){try{a(r.throw(n))}catch(n){i(n)}}function a(n){var t;n.done?o(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(u,c)}a((r=r.apply(n,t||[])).next())}))}function S(n,t){var e,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(e)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(u=0)),u;)try{if(e=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=u.trys,(o=o.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(n,u)}catch(n){c=[6,n],r=0}finally{e=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function y(n,t,e){if(e||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 n.concat(r||Array.prototype.slice.call(t))}var b=[{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}],w={exports:{}},E={},z=n;var _="function"==typeof Object.is?Object.is:function(n,t){return n===t&&(0!==n||1/n==1/t)||n!=n&&t!=t},O=z.useState,x=z.useEffect,L=z.useLayoutEffect,R=z.useDebugValue;function T(n){var t=n.getSnapshot;n=n.value;try{var e=t();return!_(n,e)}catch(n){return!0}}var k="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(n,t){return t()}:function(n,t){var e=t(),r=O({inst:{value:e,getSnapshot:t}}),o=r[0].inst,i=r[1];return L((function(){o.value=e,o.getSnapshot=t,T(o)&&i({inst:o})}),[n,e,t]),x((function(){return T(o)&&i({inst:o}),n((function(){T(o)&&i({inst:o})}))}),[n]),R(e),e};E.useSyncExternalStore=void 0!==z.useSyncExternalStore?z.useSyncExternalStore:k;var B={};"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 t=n,e=t.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function r(n){for(var t=arguments.length,r=new Array(t>1?t-1:0),o=1;o<t;o++)r[o-1]=arguments[o];!function(n,t,r){var o=e.ReactDebugCurrentFrame.getStackAddendum();""!==o&&(t+="%s",r=r.concat([o]));var i=r.map((function(n){return String(n)}));i.unshift("Warning: "+t),Function.prototype.apply.call(console[n],console,i)}("error",n,r)}var o="function"==typeof Object.is?Object.is:function(n,t){return n===t&&(0!==n||1/n==1/t)||n!=n&&t!=t},i=t.useState,u=t.useEffect,c=t.useLayoutEffect,a=t.useDebugValue,f=!1,l=!1;function s(n){var t=n.getSnapshot,e=n.value;try{var r=t();return!o(e,r)}catch(n){return!0}}var d=!!("undefined"==typeof window||void 0===window.document||void 0===window.document.createElement)?function(n,t,e){return t()}:function(n,e,d){f||void 0!==t.startTransition&&(f=!0,r("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=e();if(!l){var h=e();o(v,h)||(r("The result of getSnapshot should be cached to avoid an infinite loop"),l=!0)}var p=i({inst:{value:v,getSnapshot:e}}),m=p[0].inst,g=p[1];return c((function(){m.value=v,m.getSnapshot=e,s(m)&&g({inst:m})}),[n,v,e]),u((function(){s(m)&&g({inst:m});return n((function(){s(m)&&g({inst:m})}))}),[n]),a(v),v},v=void 0!==t.useSyncExternalStore?t.useSyncExternalStore:d;B.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?w.exports=E:w.exports=B;var A=function(n,t,e){return function(n,t,e){return!Array.isArray(t)||n.current!==t&&(n.current.length!==t.length||n.current.some((function(n,r){return!e(n,t[r])})))}(n,t,e)&&(n.current=t),n.current},W=function(n,e){void 0===e&&(e=Object.is);var r=t(n);return A(r,n,e)},V=function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=W(n),o=r((function(){return function(n){if("undefined"==typeof window||"function"!=typeof window.matchMedia){var t=Array.from({length:n.length},(function(){return!1}));return[function(){return function(){}},function(){return t}]}var e=n.map((function(n){return window.matchMedia(n)})),r=e.map((function(n){return n.matches}));return[function(n){var t=function(){r=e.map((function(n){return n.matches})),n()};return e.forEach((function(n){"function"!=typeof n.addEventListener?n.addListener(t):n.addEventListener("change",t)})),function(){e.forEach((function(n){"function"!=typeof n.removeEventListener?n.removeListener(t):n.removeEventListener("change",t)}))}},function(){return r}]}(e)}),[e]),i=o[0],u=o[1];return w.exports.useSyncExternalStore(i,u)},C=b.slice(1).map((function(n){return"(min-width: ".concat(n.minViewportWidth,"px)")})),D=function(){var n=V.apply(void 0,C);return r((function(){return n.reduce((function(n,t,e){return t?y(y([],n,!0),[b[e+1].name],!1):n}),[b[0].name])}),[n])},I=function(n){var e=t(n),r=t((function(){for(var n,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(n=e.current).call.apply(n,y([void 0],t,!1))}));return h((function(){e.current=n})),r.current},M=function(n,t){var r=void 0===t?{}:t,o=r.clearTime,i=void 0===o?2e3:o,c=r.onCopySuccess,a=void 0===c?function(){}:c,f=r.onCopyError,l=void 0===f?function(){}:f,s=u(!1),d=s[0],v=s[1],h=I((function(t){return g(void 0,void 0,void 0,(function(){var e;return S(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(n)];case 2:return r.sent(),a(t),v(!0),[3,4];case 3:if((e=r.sent())instanceof Error)return l(e),[2];throw e;case 4:return[2]}}))}))}));return e((function(){if(d){var n=setTimeout((function(){v(!1)}),i);return function(){return clearTimeout(n)}}}),[d,i]),{copy:h,hasCopied:d}},N=function(n){return!!V.apply(void 0,n?[n]:[])[0]},H=function(n){return N("(prefers-color-scheme: ".concat(n||"light",")"))},j=function(n){var t=H("dark");return void 0!==n?n:t},G=function(n,t){var e,r,o,i,c=(void 0===t?{}:t).debounceDelay,a=void 0===c?0:c,f=u({inlineSize:null!==(r=null===(e=n.current)||void 0===e?void 0:e.clientWidth)&&void 0!==r?r:0,blockSize:null!==(i=null===(o=n.current)||void 0===o?void 0:o.clientHeight)&&void 0!==i?i:0}),l=f[0],d=f[1],p=v(d,a);return h((function(){var t=n.current;if(t){var e=new ResizeObserver((function(n){if(0!==n.length){var t=s(n[0]);p((function(n){return n.inlineSize===t.inlineSize&&n.blockSize===t.blockSize?n:{inlineSize:t.inlineSize,blockSize:t.blockSize}}))}}));return e.observe(t),d({inlineSize:t.clientWidth,blockSize:t.clientHeight}),function(){e.unobserve(t)}}}),[p]),l};function K(n,t){var e=n[0],r=n[1];return[e,v(r,t,[])]}function F(n,t,e,r){return K(void 0!==e?c(n,t,e):c(n,t),r)}function J(n,t){return K(u(n),t)}var U=function(n,t){var r=u((function(){return n})),o=r[0],i=r[1];return e((function(){var e=setTimeout((function(){i((function(){return n}))}),t);return function(){clearTimeout(e)}}),[n,t]),o},Y=Symbol("empty"),P="undefined"!=typeof window&&window.document?function(n){var e=t(Y);return i((function(){e.current===Y&&(e.current=n())}),[n]),e}:function(n){return t(n())},q=function(){for(var n=[],e=0;e<arguments.length;e++)n[e]=arguments[e];var r=t(n);return h((function(){r.current=n})),o((function(n){r.current.filter(Boolean).forEach((function(t){"function"!=typeof t?function(n){return"object"==typeof n}(t)&&(t.current=n):t(n)}))}),[])};function Q(n){var r=n[0],i=n[1],u=t(i);e((function(){return function(){u.current=void 0}}),[]);var c=o((function(n){var t=u.current;null==t||t(n)}),[]);return[r,c]}function X(n,t){e((function(){var e=n.current;if(e){var r=function(){n.current&&t()};r();var o=new ResizeObserver(r);o.observe(e),window.addEventListener("resize",r);var i=function(n){for(var t=[],e=n.parentElement;e&&e!==document.documentElement;e=e.parentElement)t.push(e);return t}(e);return i.forEach((function(n){return n.addEventListener("scroll",r,{passive:!0})})),function(){o.disconnect(),window.removeEventListener("resize",r),i.forEach((function(n){return n.removeEventListener("scroll",r)}))}}}),[t,n])}var Z={top:["t","b","r","l"],bottom:["b","t","r","l"],right:["r","l","t","b"],left:["l","r","b","t"]},$={start:["s","e","m"],middle:["m","s","e"],end:["e","s","m"]},nn={t:"top",b:"bottom",l:"left",r:"right",s:"start",e:"end",m:"middle"},tn={style:{position:"fixed",visibility:"hidden"}},en=function(n){var t=n.split("-"),e=t[0],r=t[1];return[e,void 0===r?"middle":r]};function rn(n){var t,e=n.placement,r=n.targetRect,o=n.containerRect,i=n.targetBoundaries,u=n.variantBoundaries,c=n.margin,a=void 0===c?0:c;if(!i)return tn;for(var f=o.top,l=o.left,s=o.bottom,d=o.right,v=en(e),h=v[0],p=v[1],g=Z[h],S=$[p],y=0,b=g;y<b.length;y++){var w=b[y],E=["t","b"].includes(w),z=E?["top","left"]:["left","top"],_=z[0],O=z[1],x=i[w],L=E?[r.height,r.width]:[r.width,r.height],R=L[1],T=E?[s,d]:[d,s],k=T[1],B=E?[f,l]:[l,f],A=B[1];if(!(x<B[0]||x+L[0]>T[0]))for(var W=0,V=S;W<V.length;W++){var C=V[W],D=u["".concat(E?"v":"h").concat(C)];if(!(D<A||D+R>k))return{style:(t={},t[_]="".concat(x,"px"),t[O]="".concat(D,"px"),t.position="fixed",t.zIndex=9999,t.opacity=1,t),placement:"".concat(nn[w],"-").concat(nn[C])}}}var I=g[0],M=["t","b"].includes(I),N=i[I],H=u["".concat(M?"v":"h").concat(S[0])];return{style:m(m({top:"".concat(N,"px"),left:"".concat(H,"px"),position:"fixed"},s<r.height+N&&{bottom:"".concat(a,"px"),overflowY:"auto"}),{zIndex:"9999"}),placement:"".concat(nn[I],"-").concat(nn[S[0]])}}function on(n,r,o){var i=void 0===o?{}:o,c=i.margin,a=void 0===c?8:c,f=i.placement,l=void 0===f?"bottom-start":f,s=i.container,d=void 0===s?document.body:s,h=Q(u(tn)),p=h[0],m=h[1],g=t(d);e((function(){g.current=d}),[d]);var S=v(I((function(){var t=r.current,e=n.current,o=null==t?void 0:t.parentElement;if(t&&e&&o){var i=t.cloneNode(!0);i.style.bottom="",i.id="clone",o.appendChild(i);var u=i.getBoundingClientRect();o.removeChild(i);var c=e.getBoundingClientRect(),f=function(n){var t=n.anchorRect,e=n.targetRect,r=n.margin,o=void 0===r?0:r;return{t:t.top-e.height-o,b:t.bottom+o,r:t.right+o,l:t.left-e.width-o}}({anchorRect:c,targetRect:u,margin:a}),s=function(n){var t=n.anchorRect,e=n.targetRect;return{vm:-e.width/2+(t.left+t.width/2),vs:t.left,ve:t.left+t.width-e.width,hs:t.bottom-t.height,he:t.bottom-e.height,hm:t.bottom-t.height/2-e.height/2}}({anchorRect:c,targetRect:u});m(rn({placement:l,containerRect:d.getBoundingClientRect(),targetBoundaries:f,variantBoundaries:s,targetRect:u,margin:a}))}})),30);return X(r,S),X(n,S),X(g,S),p}var un,cn,an=function(){return N("(prefers-reduced-data: reduce)")},fn=function(){return N("(prefers-reduced-motion: reduce)")},ln=function(n){var r=t();return e((function(){r.current=n})),r.current},sn=function(n){var r=(void 0===n?{}:n).debounceDelay,o=void 0===r?0:r,i=t(null),u=J({borderBoxSize:{inlineSize:void 0,blockSize:void 0},contentBoxSize:{inlineSize:void 0,blockSize:void 0}},o),c=u[0],a=c.borderBoxSize,f=c.contentBoxSize,l=u[1];return e((function(){var n=new ResizeObserver((function(n){var t=n[0];l({contentBoxSize:s(t),borderBoxSize:d(t)})}));return i.current&&n.observe(i.current),function(){n.disconnect()}}),[l]),{ref:i,contentBoxSize:f,borderBoxSize:a}},dn=Symbol("once"),vn=Symbol("evts"),hn=function(){function n(){this[un]=new Map,this[cn]=new WeakMap}return n.prototype.events=function(){return Array.from(this[vn].keys())},n.prototype.has=function(n){return this[vn].has(n)},n.prototype.on=function(n,t){var e,r=this,o=null!==(e=this[vn].get(n))&&void 0!==e?e:[];return o.push(t),this[vn].set(n,o),function(){return r.off(n,t)}},n.prototype.once=function(n,t){var e=this[dn].get(t)||0;return this[dn].set(t,e+1),this.on(n,t)},n.prototype.off=function(n,t){var e,r=this[vn].get(n);if(r){var o=null!==(e=this[dn].get(t))&&void 0!==e?e:0;o>1?this[dn].set(t,o-1):this[dn].delete(t),1!==r.length?r.splice(r.findIndex((function(n){return n===t}))>>>0,1):this[vn].delete(n)}},n.prototype.emit=function(n){for(var t,e=this,r=[],o=1;o<arguments.length;o++)r[o-1]=arguments[o];var i=r[0];(function(n,t,e){if(e||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 n.concat(r||Array.prototype.slice.call(t))})([],null!==(t=this[vn].get(n))&&void 0!==t?t:[],!0).forEach((function(t){t(i),e[dn].get(t)&&e.off(n,t)}))},n}();function pn(n,r){var i=void 0;"undefined"!=typeof window&&(i="function"==typeof n?n():n);var c=function(n){return"fuselage-".concat(r,"-").concat(n)},a=new hn;return function(n,r){var f=t(r);f.current=r;var l=u((function(){if(!i)return r;var t=i.getItem(c(n));return t?JSON.parse(t):r})),s=l[0],d=l[1],v=o((function(t){d((function(e){var r=t instanceof Function?t(e):t;return null==i||i.setItem(c(n),JSON.stringify(r)),a.emit(n,r),r}))}),[n]);return e((function(){var t=function(t){t.key===c(n)&&d(t.newValue?JSON.parse(t.newValue):f.current)},e=function(n){d(n)};return a.on(n,e),window.addEventListener("storage",t),function(){a.off(n,e),window.removeEventListener("storage",t)}}),[n]),[s,v]}}un=vn,cn=dn;var mn=pn((function(){return window.localStorage}),"localStorage"),gn=pn((function(){return window.sessionStorage}),"sessionStorage"),Sn=function(n){var t=u((function(){return"function"==typeof n?!!n():!!n})),e=t[0],r=t[1],o=I((function(n){r((function(){return"boolean"==typeof n?n:"function"==typeof n?n(e):!e}))}));return[e,o]},yn=function(){var n=t(),e=r((function(){return n.current||(n.current=Math.random().toString(36).slice(2)),n.current}),[]);return a(e),e};function bn(n,t){var r=I((function(e){if(n.every((function(n){return e&&n.current&&!n.current.contains(e.target)})))return t(e)}));e((function(){return document.addEventListener("mousedown",r),function(){document.removeEventListener("mousedown",r)}}),[r])}export{rn as getPositionStyle,f as useAutoFocus,p as useBorderBoxSize,D as useBreakpoints,M as useClipboard,G as useContentBoxSize,j as useDarkMode,v as useDebouncedCallback,F as useDebouncedReducer,J as useDebouncedState,K as useDebouncedUpdates,U as useDebouncedValue,h as useIsomorphicLayoutEffect,P as useLazyRef,mn as useLocalStorage,V as useMediaQueries,N as useMediaQuery,q as useMergedRefs,I as useMutableCallback,bn as useOutsideClick,on as usePosition,H as usePrefersColorScheme,an as usePrefersReducedData,fn as usePrefersReducedMotion,ln as usePrevious,sn as useResizeObserver,Q as useSafely,gn as useSessionStorage,W as useStableArray,Sn as useToggle,yn as useUniqueId}; //# sourceMappingURL=index.module.js.map