@mmcodemark/fuselage-hooks
Version:
React hooks for Fuselage, Rocket.Chat's design system and UI toolkit
3 lines (2 loc) • 18.3 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(e),r=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}},o=function(e){return r(e,"contentBoxSize")},i=function(e){return r(e,"borderBoxSize")},u=function(t,n,r){var o=e.useMemo((function(){return t}),r),i=e.useRef(),u=e.useRef(),c=e.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=e.useCallback((function(){var e;clearTimeout(u.current),null===(e=i.current)||void 0===e||e.call(i)}),[]),f=e.useCallback((function(){clearTimeout(u.current)}),[]);return e.useEffect((function(){return function(){f()}}),[f]),e.useMemo((function(){return Object.assign(c,{flush:a,cancel:f})}),[c,a,f])},c="undefined"!=typeof window?e.useLayoutEffect:e.useEffect,a=function(){return a=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},a.apply(this,arguments)};function f(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 s(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=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(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 l(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 d=[{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}],v={exports:{}},p={},h=n.default;var m="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},g=h.useState,S=h.useEffect,y=h.useLayoutEffect,b=h.useDebugValue;function w(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!m(e,n)}catch(e){return!0}}var x="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,t){return t()}:function(e,t){var n=t(),r=g({inst:{value:n,getSnapshot:t}}),o=r[0].inst,i=r[1];return y((function(){o.value=n,o.getSnapshot=t,w(o)&&i({inst:o})}),[e,n,t]),S((function(){return w(o)&&i({inst:o}),e((function(){w(o)&&i({inst:o})}))}),[e]),b(n),n};p.useSyncExternalStore=void 0!==h.useSyncExternalStore?h.useSyncExternalStore:x;var E={};"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=n.default,t=e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function r(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,n,d){f||void 0!==e.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=n();if(!s){var p=n();o(v,p)||(r("The result of getSnapshot should be cached to avoid an infinite loop"),s=!0)}var h=i({inst:{value:v,getSnapshot:n}}),m=h[0].inst,g=h[1];return c((function(){m.value=v,m.getSnapshot=n,l(m)&&g({inst:m})}),[t,v,n]),u((function(){l(m)&&g({inst:m});return t((function(){l(m)&&g({inst:m})}))}),[t]),a(v),v},v=void 0!==e.useSyncExternalStore?e.useSyncExternalStore:d;E.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?v.exports=p:v.exports=E;var z=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},R=function(t,n){void 0===n&&(n=Object.is);var r=e.useRef(t);return z(r,t,n)},_=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var r=R(t),o=e.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 v.exports.useSyncExternalStore(i,u)},O=d.slice(1).map((function(e){return"(min-width: ".concat(e.minViewportWidth,"px)")})),L=function(t){var n=e.useRef(t),r=e.useRef((function(){for(var e,t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return(e=n.current).call.apply(e,l([void 0],t,!1))}));return c((function(){n.current=t})),r.current},k=function(e){return!!_.apply(void 0,e?[e]:[])[0]},B=function(e){return k("(prefers-color-scheme: ".concat(e||"light",")"))};function C(e,t){var n=e[0],r=e[1];return[n,u(r,t,[])]}function T(t,n){return C(e.useState(t),n)}var A=Symbol("empty"),D="undefined"!=typeof window&&window.document?function(t){var n=e.useRef(A);return e.useLayoutEffect((function(){n.current===A&&(n.current=t())}),[t]),n}:function(t){return e.useRef(t())};function M(t){var n=t[0],r=t[1],o=e.useRef(r);e.useEffect((function(){return function(){o.current=void 0}}),[]);var i=e.useCallback((function(e){var t=o.current;null==t||t(e)}),[]);return[n,i]}function V(t,n){e.useEffect((function(){var e=t.current;if(e){var r=function(){t.current&&n()};r();var o=new ResizeObserver(r);o.observe(e),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}(e);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,t])}var W={top:["t","b","r","l"],bottom:["b","t","r","l"],right:["r","l","t","b"],left:["l","r","b","t"]},I={start:["s","e","m"],middle:["m","s","e"],end:["e","s","m"]},j={t:"top",b:"bottom",l:"left",r:"right",s:"start",e:"end",m:"middle"},N={style:{position:"fixed",visibility:"hidden"}},H=function(e){var t=e.split("-"),n=t[0],r=t[1];return[n,void 0===r?"middle":r]};function P(e){var t,n=e.placement,r=e.targetRect,o=e.containerRect,i=e.targetBoundaries,u=e.variantBoundaries,c=e.margin,f=void 0===c?0:c;if(!i)return N;for(var s=o.top,l=o.left,d=o.bottom,v=o.right,p=H(n),h=p[0],m=p[1],g=W[h],S=I[m],y=0,b=g;y<b.length;y++){var w=b[y],x=["t","b"].includes(w),E=x?["top","left"]:["left","top"],z=E[0],R=E[1],_=i[w],O=x?[r.height,r.width]:[r.width,r.height],L=O[1],k=x?[d,v]:[v,d],B=k[1],C=x?[s,l]:[l,s],T=C[1];if(!(_<C[0]||_+O[0]>k[0]))for(var A=0,D=S;A<D.length;A++){var M=D[A],V=u["".concat(x?"v":"h").concat(M)];if(!(V<T||V+L>B))return{style:(t={},t[z]="".concat(_,"px"),t[R]="".concat(V,"px"),t.position="fixed",t.zIndex=9999,t.opacity=1,t),placement:"".concat(j[w],"-").concat(j[M])}}}var P=g[0],G=["t","b"].includes(P),K=i[P],F=u["".concat(G?"v":"h").concat(S[0])];return{style:a(a({top:"".concat(K,"px"),left:"".concat(F,"px"),position:"fixed"},d<r.height+K&&{bottom:"".concat(f,"px"),overflowY:"auto"}),{zIndex:"9999"}),placement:"".concat(j[P],"-").concat(j[S[0]])}}var G,K,F=Symbol("once"),U=Symbol("evts"),J=function(){function e(){this[G]=new Map,this[K]=new WeakMap}return e.prototype.events=function(){return Array.from(this[U].keys())},e.prototype.has=function(e){return this[U].has(e)},e.prototype.on=function(e,t){var n,r=this,o=null!==(n=this[U].get(e))&&void 0!==n?n:[];return o.push(t),this[U].set(e,o),function(){return r.off(e,t)}},e.prototype.once=function(e,t){var n=this[F].get(t)||0;return this[F].set(t,n+1),this.on(e,t)},e.prototype.off=function(e,t){var n,r=this[U].get(e);if(r){var o=null!==(n=this[F].get(t))&&void 0!==n?n:0;o>1?this[F].set(t,o-1):this[F].delete(t),1!==r.length?r.splice(r.findIndex((function(e){return e===t}))>>>0,1):this[U].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[U].get(e))&&void 0!==t?t:[],!0).forEach((function(t){t(i),n[F].get(t)&&n.off(e,t)}))},e}();function Y(t,n){var r=void 0;"undefined"!=typeof window&&(r="function"==typeof t?t():t);var o=function(e){return"fuselage-".concat(n,"-").concat(e)},i=new J;return function(t,n){var u=e.useRef(n);u.current=n;var c=e.useState((function(){if(!r)return n;var e=r.getItem(o(t));return e?JSON.parse(e):n})),a=c[0],f=c[1],s=e.useCallback((function(e){f((function(n){var u=e instanceof Function?e(n):e;return null==r||r.setItem(o(t),JSON.stringify(u)),i.emit(t,u),u}))}),[t]);return e.useEffect((function(){var e=function(e){e.key===o(t)&&f(e.newValue?JSON.parse(e.newValue):u.current)},n=function(e){f(e)};return i.on(t,n),window.addEventListener("storage",e),function(){i.off(t,n),window.removeEventListener("storage",e)}}),[t]),[a,s]}}G=U,K=F;var q=Y((function(){return window.localStorage}),"localStorage"),Q=Y((function(){return window.sessionStorage}),"sessionStorage");exports.getPositionStyle=P,exports.useAutoFocus=function(t,n){void 0===t&&(t=!0);var r=e.useRef(null),o=(n||{}).preventScroll;return e.useEffect((function(){t&&r.current&&r.current.focus({preventScroll:o})}),[r,t,o]),r},exports.useBorderBoxSize=function(t,n){var r=(void 0===n?{}:n).debounceDelay,o=void 0===r?0:r,a=e.useState((function(){var e,n,r,o;return{inlineSize:null!==(n=null===(e=t.current)||void 0===e?void 0:e.offsetWidth)&&void 0!==n?n:0,blockSize:null!==(o=null===(r=t.current)||void 0===r?void 0:r.offsetHeight)&&void 0!==o?o:0}})),f=a[0],s=a[1],l=u(s,o);return c((function(){var e=t.current;if(e){var n=new ResizeObserver((function(e){if(0!==e.length){var t=i(e[0]);l((function(e){return e.inlineSize===t.inlineSize&&e.blockSize===t.blockSize?e:{inlineSize:t.inlineSize,blockSize:t.blockSize}}))}}));return n.observe(e),s({inlineSize:e.offsetWidth,blockSize:e.offsetHeight}),function(){n.unobserve(e)}}}),[l]),f},exports.useBreakpoints=function(){var t=_.apply(void 0,O);return e.useMemo((function(){return t.reduce((function(e,t,n){return t?l(l([],e,!0),[d[n+1].name],!1):e}),[d[0].name])}),[t])},exports.useClipboard=function(t,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,l=void 0===a?function(){}:a,d=e.useState(!1),v=d[0],p=d[1],h=L((function(e){return f(void 0,void 0,void 0,(function(){var n;return s(this,(function(r){switch(r.label){case 0:null==e||e.preventDefault(),r.label=1;case 1:return r.trys.push([1,3,,4]),[4,navigator.clipboard.writeText(t)];case 2:return r.sent(),c(e),p(!0),[3,4];case 3:if((n=r.sent())instanceof Error)return l(n),[2];throw n;case 4:return[2]}}))}))}));return e.useEffect((function(){if(v){var e=setTimeout((function(){p(!1)}),i);return function(){return clearTimeout(e)}}}),[v,i]),{copy:h,hasCopied:v}},exports.useContentBoxSize=function(t,n){var r,i,a,f,s=(void 0===n?{}:n).debounceDelay,l=void 0===s?0:s,d=e.useState({inlineSize:null!==(i=null===(r=t.current)||void 0===r?void 0:r.clientWidth)&&void 0!==i?i:0,blockSize:null!==(f=null===(a=t.current)||void 0===a?void 0:a.clientHeight)&&void 0!==f?f:0}),v=d[0],p=d[1],h=u(p,l);return c((function(){var e=t.current;if(e){var n=new ResizeObserver((function(e){if(0!==e.length){var t=o(e[0]);h((function(e){return e.inlineSize===t.inlineSize&&e.blockSize===t.blockSize?e:{inlineSize:t.inlineSize,blockSize:t.blockSize}}))}}));return n.observe(e),p({inlineSize:e.clientWidth,blockSize:e.clientHeight}),function(){n.unobserve(e)}}}),[h]),v},exports.useDarkMode=function(e){var t=B("dark");return void 0!==e?e:t},exports.useDebouncedCallback=u,exports.useDebouncedReducer=function(t,n,r,o){return C(void 0!==r?e.useReducer(t,n,r):e.useReducer(t,n),o)},exports.useDebouncedState=T,exports.useDebouncedUpdates=C,exports.useDebouncedValue=function(t,n){var r=e.useState((function(){return t})),o=r[0],i=r[1];return e.useEffect((function(){var e=setTimeout((function(){i((function(){return t}))}),n);return function(){clearTimeout(e)}}),[t,n]),o},exports.useIsomorphicLayoutEffect=c,exports.useLazyRef=D,exports.useLocalStorage=q,exports.useMediaQueries=_,exports.useMediaQuery=k,exports.useMergedRefs=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var r=e.useRef(t);return c((function(){r.current=t})),e.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)}))}),[])},exports.useMutableCallback=L,exports.useOutsideClick=function(t,n){var r=L((function(e){if(t.every((function(t){return e&&t.current&&!t.current.contains(e.target)})))return n(e)}));e.useEffect((function(){return document.addEventListener("mousedown",r),function(){document.removeEventListener("mousedown",r)}}),[r])},exports.usePosition=function(t,n,r){var o=void 0===r?{}:r,i=o.margin,c=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=M(e.useState(N)),v=d[0],p=d[1],h=e.useRef(l);e.useEffect((function(){h.current=l}),[l]);var m=u(L((function(){var e=n.current,r=t.current,o=null==e?void 0:e.parentElement;if(e&&r&&o){var i=e.cloneNode(!0);i.style.bottom="",i.id="clone",o.appendChild(i);var u=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:u,margin:c}),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:u});p(P({placement:f,containerRect:l.getBoundingClientRect(),targetBoundaries:s,variantBoundaries:d,targetRect:u,margin:c}))}})),30);return V(n,m),V(t,m),V(h,m),v},exports.usePrefersColorScheme=B,exports.usePrefersReducedData=function(){return k("(prefers-reduced-data: reduce)")},exports.usePrefersReducedMotion=function(){return k("(prefers-reduced-motion: reduce)")},exports.usePrevious=function(t){var n=e.useRef();return e.useEffect((function(){n.current=t})),n.current},exports.useResizeObserver=function(t){var n=(void 0===t?{}:t).debounceDelay,r=void 0===n?0:n,u=e.useRef(null),c=T({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 e.useEffect((function(){var e=new ResizeObserver((function(e){var t=e[0];l({contentBoxSize:o(t),borderBoxSize:i(t)})}));return u.current&&e.observe(u.current),function(){e.disconnect()}}),[l]),{ref:u,contentBoxSize:s,borderBoxSize:f}},exports.useSafely=M,exports.useSessionStorage=Q,exports.useStableArray=R,exports.useToggle=function(t){var n=e.useState((function(){return"function"==typeof t?!!t():!!t})),r=n[0],o=n[1],i=L((function(e){o((function(){return"boolean"==typeof e?e:"function"==typeof e?e(r):!r}))}));return[r,i]},exports.useUniqueId=function(){var t=e.useRef(),n=e.useMemo((function(){return t.current||(t.current=Math.random().toString(36).slice(2)),t.current}),[]);return e.useDebugValue(n),n};
//# sourceMappingURL=index.js.map