UNPKG

@rocket.chat/fuselage-hooks

Version:

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

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