@rocket.chat/fuselage-hooks
Version:
React hooks for Fuselage, Rocket.Chat's design system and UI toolkit
3 lines (2 loc) • 16.6 kB
JavaScript
var e=require("react"),t=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}},n=function(e){return t(e,"contentBoxSize")},r=function(e){return t(e,"borderBoxSize")},o=function(t,n,r){var o=r?e.useMemo((function(){return t}),r):t,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])},i="undefined"!=typeof window?e.useLayoutEffect:e.useEffect,u=function(){return u=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},u.apply(this,arguments)};function c(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 a(e,t){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[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(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,c[0]&&(i=0)),i;)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 i.label++,{value:c[1],done:!1};case 5:i.label++,r=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}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 f(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))}"function"==typeof SuppressedError&&SuppressedError;var s=[{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}],l=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},d=function(t,n){void 0===n&&(n=Object.is);var r=e.useRef(t);return l(r,t,n)},v=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var r=d(t),o=e.useRef(r.map((function(e){return"undefined"!=typeof window&&"function"==typeof window.matchMedia&&matchMedia(e).matches}))),i=e.useCallback((function(e){if("undefined"==typeof window||"function"!=typeof window.matchMedia)return function(){};for(var t=r.map((function(e){return matchMedia(e)})),n=function(){o.current=t.map((function(e){return e.matches})),e()},i=0,u=t;i<u.length;i++){var c=u[i];"function"!=typeof c.addEventListener?c.addListener(n):c.addEventListener("change",n)}return function(){for(var e=0,r=t;e<r.length;e++){var o=r[e];"function"!=typeof o.removeEventListener?o.removeListener(n):o.removeEventListener("change",n)}}}),[r]),u=e.useCallback((function(){return o.current}),[]),c=e.useCallback((function(){return o.current}),[]);return e.useSyncExternalStore(i,u,c)},p=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,f([void 0],t,!1))}));return i((function(){n.current=t})),r.current},h=p,m=function(e){return!!v.apply(void 0,e?[e]:[])[0]},b=function(e){return m("(prefers-color-scheme: ".concat(e||"light",")"))};function g(e,t){var n=e[0],r=e[1];return[n,o(r,t,[])]}function y(t,n){return g(e.useState(t),n)}var S=Symbol("empty"),w="undefined"!=typeof window&&window.document?function(t){var n=e.useRef(S);return e.useLayoutEffect((function(){n.current===S&&(n.current=t())}),[t]),n}:function(t){return e.useRef(t())};function x(t){var n=t[0],r=t[1],o=e.useRef(r);e.useEffect((function(){return o.current=r,function(){o.current=void 0}}),[r]);var i=e.useCallback((function(e){var t=o.current;null==t||t(e)}),[]);return[n,i]}function z(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 R={top:["t","b","r","l"],bottom:["b","t","r","l"],right:["r","l","t","b"],left:["l","r","b","t"]},E={start:["s","e","m"],middle:["m","s","e"],end:["e","s","m"]},k={t:"top",b:"bottom",l:"left",r:"right",s:"start",e:"end",m:"middle"},C={style:{position:"fixed",visibility:"hidden"}};function B(e){var t,n=e.placement,r=e.targetRect,o=e.containerRect,i=e.targetBoundaries,c=e.variantBoundaries,a=e.margin,f=void 0===a?0:a;if(!i)return C;for(var s=o.top,l=o.left,d=o.bottom,v=o.right,p=function(e){var t=e.split("-"),n=t[0],r=t[1];return[n,void 0===r?"middle":r]}(n),h=p[0],m=p[1],b=R[h],g=E[m],y=0,S=b;y<S.length;y++){var w=S[y],x=["t","b"].includes(w),z=x?["top","left"]:["left","top"],B=z[0],M=z[1],W=i[w],L=x?[r.height,r.width]:[r.width,r.height],O=L[1],D=x?[d,v]:[v,d],V=D[1],I=x?[s,l]:[l,s],T=I[1];if(!(W<I[0]||W+L[0]>D[0]))for(var A=0,P=g;A<P.length;A++){var j=P[A],H=c["".concat(x?"v":"h").concat(j)];if(!(H<T||H+O>V))return{style:(t={},t[B]=W,t[M]=H,t.position="fixed",t.zIndex=999999,t.opacity=1,t),placement:"".concat(k[w],"-").concat(k[j])}}}var N=b[0],J=["t","b"].includes(N),q=c["".concat(J?"v":"h").concat(g[0])],F=J?[o.height,r.height]:[o.width,r.width],Q=(F[0]-F[1])/2+(J?s:l);return{style:u({top:Q,left:q,position:"fixed",zIndex:999999},d<r.height+Q&&{bottom:f,overflowY:"auto"}),placement:"".concat(k[N],"-").concat(k[g[0]])}}var M,W;"function"==typeof SuppressedError&&SuppressedError;var L=Symbol("once"),O=Symbol("evts"),D=function(){function e(){this[M]=new Map,this[W]=new WeakMap}return e.prototype.events=function(){return Array.from(this[O].keys())},e.prototype.has=function(e){return this[O].has(e)},e.prototype.on=function(e,t){var n,r=this,o=null!==(n=this[O].get(e))&&void 0!==n?n:[];return o.push(t),this[O].set(e,o),function(){return r.off(e,t)}},e.prototype.once=function(e,t){var n=this[L].get(t)||0;return this[L].set(t,n+1),this.on(e,t)},e.prototype.off=function(e,t){var n,r=this[O].get(e);if(r){var o=null!==(n=this[L].get(t))&&void 0!==n?n:0;o>1?this[L].set(t,o-1):this[L].delete(t),r.splice(r.findIndex((function(e){return e===t}))>>>0,1),0===r.length&&this[O].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){for(var n,r=0,o=t.length;r<o;r++)!n&&r in t||(n||(n=Array.prototype.slice.call(t,0,r)),n[r]=t[r]);return[].concat(n||Array.prototype.slice.call(t))})(0,null!==(t=this[O].get(e))&&void 0!==t?t:[]).forEach((function(t){t(i),n[L].get(t)&&n.off(e,t)}))},e}();M=O,W=L;var V=function(e,t,n){if(!n)return t;var r=n.getItem(e);return r?JSON.parse(r):t};function I(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 D;return function(t,n){var u=e.useRef(n);u.current=n;var c=e.useState((function(){return{current:V(o(t),n,r)}}))[0],a=e.useCallback((function(){return c.current}),[c]),f=e.useCallback((function(){return u.current}),[]),s=e.useCallback((function(e){var n=function(e){e.key===o(t)&&i.emit(t,e.newValue?JSON.parse(e.newValue):u.current)},r=i.on(t,(function(t){c.current=t,e()}));return window.addEventListener("storage",n),function(){r(),window.removeEventListener("storage",n)}}),[t,c]);return[e.useSyncExternalStore(s,a,f),e.useCallback((function(e){var n=e instanceof Function?e(c.current):e;return null==r||r.setItem(o(t),JSON.stringify(n)),i.emit(t,n),n}),[t,c])]}}var T=I((function(){return window.localStorage}),"localStorage"),A=I((function(){return window.sessionStorage}),"sessionStorage");exports.getPositionStyle=B,exports.useAutoFocus=function(t,n){void 0===t&&(t=!0);var r=e.useRef(null),o=e.useRef(n);return o.current=n,e.useEffect((function(){t&&r.current&&r.current.focus(o.current)}),[r,t]),r},exports.useBorderBoxSize=function(t,n){var u=(void 0===n?{}:n).debounceDelay,c=void 0===u?0:u,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=o(s,c);return i((function(){var e=t.current;if(e){var n=new ResizeObserver((function(e){if(0!==e.length){var t=r(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(t){void 0===t&&(t="em");var n=v.apply(void 0,e.useMemo((function(){return s.slice(1).map((function(e){return"(min-width: ".concat("px"===t?"".concat(e.minViewportWidth,"px"):"".concat(e.minViewportWidth/16,"em"),")")}))}),[t]));return e.useMemo((function(){return n.reduce((function(e,t,n){return t?f(f([],e,!0),[s[n+1].name],!1):e}),[s[0].name])}),[n])},exports.useButtonPattern=function(t){var n=e.useCallback((function(e){"Space"!==e.code&&"Enter"!==e.code||(e.preventDefault(),t(e))}),[t]);return{onClick:t,onKeyDown:n,role:"button",tabIndex:0}},exports.useClipboard=function(t,n){var r=void 0===n?{}:n,o=r.clearTime,i=void 0===o?2e3:o,u=r.onCopySuccess,f=void 0===u?function(){}:u,s=r.onCopyError,l=void 0===s?function(){}:s,d=e.useState(!1),v=d[0],h=d[1],m=p((function(e){return c(void 0,void 0,void 0,(function(){var n;return a(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(),f(e),h(!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(){h(!1)}),i);return function(){return clearTimeout(e)}}}),[v,i]),{copy:m,hasCopied:v}},exports.useContentBoxSize=function(t,r){var u,c,a,f,s=(void 0===r?{}:r).debounceDelay,l=void 0===s?0:s,d=e.useState({inlineSize:null!==(c=null===(u=t.current)||void 0===u?void 0:u.clientWidth)&&void 0!==c?c: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=o(p,l);return i((function(){var e=t.current;if(e){var r=new ResizeObserver((function(e){if(0!==e.length){var t=n(e[0]);h((function(e){return e.inlineSize===t.inlineSize&&e.blockSize===t.blockSize?e:{inlineSize:t.inlineSize,blockSize:t.blockSize}}))}}));return r.observe(e),p({inlineSize:e.clientWidth,blockSize:e.clientHeight}),function(){r.unobserve(e)}}}),[h]),v},exports.useDarkMode=function(e){var t=b("dark");return void 0!==e?e:t},exports.useDebouncedCallback=o,exports.useDebouncedReducer=function(t,n,r,o){return g(void 0!==r?e.useReducer(t,n,r):e.useReducer(t,n),o)},exports.useDebouncedState=y,exports.useDebouncedUpdates=g,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.useEffectEvent=p,exports.useElementIsVisible=function(){var t=e.useRef(),n=x(y(!1,100)),r=n[0],o=n[1],i=e.useState((function(){return new IntersectionObserver((function(e){e.forEach((function(e){o(e.isIntersecting)}))}))}))[0];return e.useEffect((function(){return function(){i.disconnect()}}),[i]),[e.useCallback((function(e){null!==e?(t.current=e,i.observe(t.current)):o(!1)}),[i,o]),r]},exports.useIsomorphicLayoutEffect=i,exports.useLazyRef=w,exports.useLocalStorage=T,exports.useMediaQueries=v,exports.useMediaQuery=m,exports.useMergedRefs=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var r=e.useRef(t);return i((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=h,exports.useOutsideClick=function(t,n){var r=p((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 i=void 0===r?{}:r,u=i.margin,c=void 0===u?8:u,a=i.placement,f=void 0===a?"bottom-start":a,s=i.container,l=void 0===s?document.body:s,d=x(e.useState(C)),v=d[0],h=d[1],m=e.useRef(l);e.useEffect((function(){m.current=l}),[l]);var b=o(p((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});h(B({placement:f,containerRect:l.getBoundingClientRect(),targetBoundaries:s,variantBoundaries:d,targetRect:u,margin:c}))}})),30);return z(n,b),z(t,b),z(m,b),v},exports.usePrefersColorScheme=b,exports.usePrefersReducedData=function(){return m("(prefers-reduced-data: reduce)")},exports.usePrefersReducedMotion=function(){return m("(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 o=(void 0===t?{}:t).debounceDelay,i=void 0===o?0:o,u=e.useRef(null),c=y({borderBoxSize:{inlineSize:void 0,blockSize:void 0},contentBoxSize:{inlineSize:void 0,blockSize:void 0}},i),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:n(t),borderBoxSize:r(t)})}));return u.current&&e.observe(u.current),function(){e.disconnect()}}),[l]),{ref:u,contentBoxSize:s,borderBoxSize:f}},exports.useSafeRefCallback=function(t){return e.useMemo((function(){var e;return function(n){"function"==typeof e&&e();var r=t(n);e=r||null}}),[t])},exports.useSafely=x,exports.useSessionStorage=A,exports.useStableArray=d,exports.useToggle=function(t){var n=e.useState((function(){return"function"==typeof t?!!t():!!t})),r=n[0],o=n[1],i=p((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