UNPKG

ariakit-react-utils

Version:

Ariakit React utils

2 lines (1 loc) 7.47 kB
!function(e,t){"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).AriakitReactUtils={},e.React)}(this,(function(e,t){"use strict";function n(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var r=n(t);const o="undefined"!=typeof window&&!!window.document?.createElement;function u(e,t,n,r){void 0===r&&(r=window);try{r.document.addEventListener(e,t,n)}catch(e){}const o=[];for(let c=0;c<r.frames?.length;c+=1){const f=r.frames[c];f&&o.push(u(e,t,n,f))}return()=>{try{r.document.removeEventListener(e,t,n)}catch(e){}o.forEach((e=>e()))}}function c(e,t){if(e===t)return!0;if(!e)return!1;if(!t)return!1;if("object"!=typeof e)return!1;if("object"!=typeof t)return!1;const n=Object.keys(e),r=Object.keys(t),{length:o}=n;if(r.length!==o)return!1;for(const r of n)if(e[r]!==t[r])return!1;return!0}function f(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function i(e,t){"function"==typeof e?e(t):e&&(e.current=t)}const s=r.useId,a=r.useDeferredValue,l=r.useInsertionEffect,d=o?t.useLayoutEffect:t.useEffect;function p(e){const[n]=t.useState(e);return n}function y(e){const n=t.useRef();return void 0===n.current&&(n.current=e()),n.current}function m(e){const n=t.useRef(e);return d((()=>{n.current=e})),n}function v(e){const n=t.useRef((()=>{throw new Error("Cannot call an event handler while rendering.")}));return l?l((()=>{n.current=e})):n.current=e,t.useCallback((function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return n.current?.(...t)}),[])}function b(){for(var e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];return t.useMemo((()=>{if(n.some(Boolean))return e=>{n.forEach((t=>{i(t,e)}))}}),n)}function w(e){if("string"==typeof e)return e}const E=Symbol("setNextState");function S(e){return!0===e[E]}function h(e,n,r){void 0===r&&(r=[]);const o=t.useCallback((t=>(e.wrapElement&&(t=e.wrapElement(t)),n(t))),[...r,e.wrapElement]);return{...e,wrapElement:o}}let j=!1,O=0,g=0;function R(e){(function(e){const t=e.movementX||e.screenX-O,n=e.movementY||e.screenY-g;return O=e.screenX,g=e.screenY,t||n||!1})(e)&&(j=!0)}function x(){j=!1}var P,C={},_={};function k(e){const n=(t,n)=>e({ref:n,...t});return t.forwardRef(n)}!function(e){e.exports=function(){if(P)return _;P=1;var e=t,n=Symbol.for("react.element"),r=Symbol.for("react.fragment"),o=Object.prototype.hasOwnProperty,u=e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,c={key:!0,ref:!0,__self:!0,__source:!0};function f(e,t,r){var f,i={},s=null,a=null;for(f in void 0!==r&&(s=""+r),void 0!==t.key&&(s=""+t.key),void 0!==t.ref&&(a=t.ref),t)o.call(t,f)&&!c.hasOwnProperty(f)&&(i[f]=t[f]);if(e&&e.defaultProps)for(f in t=e.defaultProps)void 0===i[f]&&(i[f]=t[f]);return{$$typeof:n,type:e,key:s,ref:a,props:i,_owner:u.current}}return _.Fragment=r,_.jsx=f,_.jsxs=f,_}()}({get exports(){return C},set exports(e){C=e}});const L=Symbol("getState"),I=Symbol("subscribe"),A=Symbol("timestamp"),D=Symbol("initialContext");function N(e){return!!e&&!!e[I]}function F(e,t){void 0===t&&(t=e),Object.defineProperty(e,L,{value:()=>t,writable:!0})}function T(e){return e&&D in e}function U(e){if(!T(e))return;return e[D]}const M=t.createContext(void 0);e.createComponent=k,e.createElement=function(e,t){const{as:n,wrapElement:r,...o}=t;let u;if(n&&"string"!=typeof n)u=C.jsx(n,{...o});else if("function"==typeof t.children){const{children:e,...n}=o;u=t.children(n)}else u=n?C.jsx(n,{...o}):C.jsx(e,{...o});return r?r(u):u},e.createHook=function(e){return function(t){void 0===t&&(t={});const n=e(t),r={};for(const e in n)f(n,e)&&void 0!==n[e]&&(r[e]=n[e]);return r}},e.createMemoComponent=function(e,n){void 0===n&&(n=c);const r=k(e);return t.memo(r,((e,t)=>{const{state:r,...o}=e,{state:u,...c}=t;return u&&N(u)?n(o,c):n(e,t)}))},e.createStoreContext=function(){const e=t.createContext(void 0);return function(e){const n=t.createContext(void 0);Object.defineProperty(e,D,{value:n})}(e),e},e.setRef=i,e.useBooleanEvent=function(e){return v("function"==typeof e?e:()=>e)},e.useControlledState=function(e,n,r){const[o,u]=t.useState(e),c=void 0!==n?n:o,f=m(n),i=m(r),s=m(c),a=t.useCallback((e=>{const t=i.current;if(t)if(S(t))t(e);else{const n=function(e,t){if(function(e){return"function"==typeof e}(e))return e("function"==typeof t?t():t);return e}(e,s.current);s.current=n,t(n)}void 0===f.current&&u(e)}),[]);var l;return S(l=a)||Object.defineProperty(l,E,{value:!0}),[c,a]},e.useDeferredValue=function(e){if(a)return a(e);const[n,r]=t.useState(e);return t.useEffect((()=>{const t=requestAnimationFrame((()=>r(e)));return()=>cancelAnimationFrame(t)}),[e]),n},e.useEvent=v,e.useForceUpdate=function(){return t.useReducer((()=>[]),[])},e.useForkRef=b,e.useId=function(e){if(s){const t=s();return e||t}const[n,r]=t.useState(e);return d((()=>{if(e||n)return;const t=Math.random().toString(36).substr(2,6);r(`id-${t}`)}),[e,n]),e||n},e.useInitialValue=p,e.useIsMouseMoving=function(){return t.useEffect((()=>{u("mousemove",R,!0),u("mousedown",x,!0),u("mouseup",x,!0),u("keydown",x,!0),u("scroll",x,!0)}),[]),v((()=>j))},e.useLazyValue=y,e.useLiveRef=m,e.usePortalRef=function(e,n){void 0===e&&(e=!1);const[r,o]=t.useState(null);return{portalRef:b(o,n),portalNode:r,domReady:!e||r}},e.usePreviousValue=function(e){const[n,r]=t.useState(e);return e!==n&&r(e),n},e.useRefId=function(e,n){const[r,o]=t.useState(void 0);return d((()=>{o(e?.current?.id)}),n),r},e.useSafeLayoutEffect=d,e.useStore=function(e,n){const r=t.useContext(function(e,t){if(!T(e))return M;if(t)return U(e);return e}(e,n)),o=T(e)?r:e,[u,c]=t.useState((()=>function(e){if(!e)return e;const t=e[L];return t?t():e}(o))),f=N(o)&&N(u)?(i=u,(s=o)?i?A in s?A in i&&i[A]>=s[A]?i:s:i:s:i):o;var i,s;const a=function(e){if(e)return e[I]}(o),l=t.useRef(null),p=(y=n,Array.isArray(y)?y:void 0!==y?[y]:[]);var y;const m=!n;return d((()=>{if(a&&c){if(m)return a(c);if(p.length)return a((e=>{const t=l.current;l.current=e;p.some((n=>{if("function"==typeof n){const r=n(e);return"boolean"==typeof r?r||t&&n(t):t?r!==n(t):r}const r=n;return t?.[r]!==e[r]}))&&c(e)}))}}),[a,c,m,...p]),f},e.useStoreProvider=function(e,t){let{state:n,...r}=e;const o=p(n),u=n&&N(n)?o:n;F(u,n);const c=U(t);return h(r,(e=>(u&&c&&(e=C.jsx(c.Provider,{value:u,children:e})),n&&(e=C.jsx(t.Provider,{value:n,children:e})),e)),[u,c,n,t])},e.useStorePublisher=function(e){const n=y((()=>new Set));d((()=>{!function(e){Object.defineProperty(e,A,{value:Date.now(),writable:!0})}(e);for(const t of n)t(e)}),[e]);const r=t.useCallback((e=>(n.add(e),()=>n.delete(e))),[]);return function(e,t){I in e||Object.defineProperty(e,I,{value:t})}(e,r),F(e),function(e){A in e||Object.defineProperty(e,A,{value:Date.now(),writable:!0})}(e),e},e.useTagName=function(e,n){const[r,o]=t.useState((()=>w(n)));return d((()=>{o(e?.current?.tagName.toLowerCase()||w(n))}),[e,n]),r},e.useUpdateEffect=function(e,n){const r=t.useRef(!1);t.useEffect((()=>{if(r.current)return e();r.current=!0}),n),t.useEffect((()=>()=>{r.current=!1}),[])},e.useUpdateLayoutEffect=function(e,n){const r=t.useRef(!1);d((()=>{if(r.current)return e();r.current=!0}),n),d((()=>()=>{r.current=!1}),[])},e.useWrapElement=h}));