UNPKG

@lyonph/preact-hooks

Version:

Collection of useful Preact Hooks

56 lines (55 loc) 9.32 kB
var G=Object.defineProperty,J=Object.defineProperties;var K=Object.getOwnPropertyDescriptors;var I=Object.getOwnPropertySymbols;var Z=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable;var M=(e,t,r)=>t in e?G(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,g=(e,t)=>{for(var r in t||(t={}))Z.call(t,r)&&M(e,r,t[r]);if(I)for(var r of I(t))$.call(t,r)&&M(e,r,t[r]);return e},U=(e,t)=>J(e,K(t));import{useEffect as me}from"preact/hooks";import{useRef as te}from"preact/hooks";import{useDebugValue as q,useRef as ee}from"preact/hooks";function a(e,t){return!Object.is(e,t)}function l(e,t=a){let r=ee(e),o=t(r.current,e);return o&&(r.current=e),q(o),o}function v(e,t,r=a){let o=te(e);return l(t,r)&&(o.current=e),o.current}import{useDebugValue as ce}from"preact/hooks";import{useDebugValue as ne}from"preact/hooks";import{useDebugValue as re,useRef as oe}from"preact/hooks";function p(e){let t=oe();return t.current||(t.current={current:e()}),re(t.current),t.current}function c(e){let{current:t}=p(e);return ne(t),t}function m(e){return c(()=>e)}import{useReducer as ue}from"preact/hooks";function S(){return ue(()=>[],[])[1]}function T(e,t,r=a){let o=p(e);return l(t,r)&&(o.current=e()),o}import{useRef as de}from"preact/hooks";import{useEffect as se,useLayoutEffect as ie}from"preact/hooks";var ae=typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.document.createElement!="undefined",u=ae;var fe=u?ie:se,L=fe;function w(){let e=de(!1);return L(()=>(e.current=!0,()=>{e.current=!1}),[]),m(()=>e.current)}function le(e){return typeof e=="function"}function pe(e){return typeof e=="function"}function E(e,t,r=a){let o=T(()=>le(e)?e():e,t,r),n=S(),s=w(),f=m(d=>{if(s()){let{current:y}=o,C=pe(d)?d(y):d;Object.is(y,C)||(o.current=C,n())}});return ce(o.current),[o.current,f]}function V(e,t,r=a){let[o,n]=E(()=>({status:"pending"}),t,r),s=v(e,t,r);return me(()=>{let f=!0;return s().then(d=>{f&&n({status:"success",value:d})},d=>{f&&n({status:"failure",value:d})}),()=>{f=!1}},[s,n]),o}import{useEffect as Re}from"preact/hooks";import{useDebugValue as Se,useRef as be}from"preact/hooks";function h(e,t=a){let r=l(e,t),o=be(0);return r&&(o.current+=1),Se(o.current),o.current}function D(e,t,r=a){let o=h(t,r);Re(e,[o])}import{useLayoutEffect as Te}from"preact/hooks";function P(e,t,r=a){let o=h(t,r);Te(e,[o])}import{useDebugValue as he}from"preact/hooks";function x(e,t,r=a){let o=T(e,t,r).current;return he(o),o}import{useDebugValue as ge}from"preact/hooks";import{useDebugValue as xe,useEffect as ye,useState as Ce}from"preact/hooks";function i({read:e,subscribe:t,shouldUpdate:r=a}){let[o,n]=Ce(()=>({read:e,subscribe:t,shouldUpdate:r,value:e()})),s=o.value;return(o.read!==e||o.subscribe!==t||o.shouldUpdate!==r)&&(s=e(),n({read:e,subscribe:t,shouldUpdate:r,value:s})),xe(s),ye(()=>{let f=!0,d=()=>{if(f){let C=e();n(R=>R.read!==e||R.subscribe!==t||R.shouldUpdate!==r||!r(R.value,C)?R:U(g({},R),{value:C}))}},y=t(d);return d(),()=>{f=!1,y&&y()}},[e,t,r]),s}var ve={read:()=>{if(u&&document.fullscreenElement)return document.fullscreenElement},subscribe:e=>{if(u)return document.addEventListener("fullscreenchange",e),()=>{document.removeEventListener("fullscreenchange",e)}}};function F(){let e=i(ve);return ge(e),e}import{useDebugValue as we}from"preact/hooks";var A=new Map;function Ee(e){let t=A.get(e);if(t)return t;let r=window.matchMedia(e);return A.set(e,r),r}function b(e){let t=x(()=>{if(u)return Ee(e)},e),r=x(()=>({read:()=>!!(t!=null&&t.matches),subscribe:n=>{if(t)return t.addEventListener("change",n),()=>{t.removeEventListener("change",n)}}}),t),o=i(r);return we(o),o}import{useDebugValue as Le}from"preact/hooks";var De={read:()=>u?navigator.onLine:!0,subscribe:e=>{if(u)return window.addEventListener("online",e,!1),window.addEventListener("offline",e,!1),()=>{window.removeEventListener("online",e,!1),window.removeEventListener("offline",e,!1)}}};function O(){let e=i(De);return Le(e),e}import{useDebugValue as Ie}from"preact/hooks";var Me={read:()=>u?document.visibilityState==="visible":!0,subscribe:e=>{if(u)return document.addEventListener("visibilitychange",e,!1),()=>{document.removeEventListener("visibilitychange",e,!1)}}};function z(){let e=i(Me);return Ie(e),e}import{useState as Ue}from"preact/hooks";function Ve(e){return typeof e=="function"}function N(e){let[t,r]=Ue(e),o=m(n=>{r(s=>{let f=Ve(n)?n(s):n;return g(g({},s),f)})});return[t,o]}function j(){return b("(prefers-color-scheme: dark)")}function W(){return b("(prefers-color-scheme: light)")}function k(){return b("(prefers-reduced-motion)")}import{useDebugValue as Pe}from"preact/hooks";var Fe={read:()=>{if(u)return document.readyState},subscribe:e=>{if(u)return document.addEventListener("readystatechange",e,!1),()=>{document.removeEventListener("readystatechange",e,!1)}}};function _(){let e=i(Fe);return Pe(e),e}import{useDebugValue as Ae}from"preact/hooks";function Q(e){let t=c(e),r=S(),o=c(()=>new Proxy(t,{get(n,s,f){return Reflect.get(n,s,f)},set(n,s,f,d){return Object.is(Reflect.get(n,s,d),f)||(Reflect.set(n,s,f,d),r()),!0}}));return Ae(o),o}import{useDebugValue as Oe}from"preact/hooks";function B(e){let t=S(),r=p(e),o=c(()=>({get current(){return r.current},set current(n){Object.is(n,r.current)||(r.current=n,t())}}));return Oe(o),o}import{useDebugValue as ze,useRef as Ne}from"preact/hooks";function H(){let e=Ne(0);return e.current+=1,ze(e.current),e.current}import{useDebugValue as je}from"preact/hooks";var We={read:()=>{if(u)return{x:window.pageXOffset,y:window.pageYOffset}},subscribe:e=>{if(u)return window.addEventListener("scroll",e,{capture:!1,passive:!0}),()=>{window.removeEventListener("scroll",e)}}};function X(){let e=i(We);return je(e),e}import{useDebugValue as ke}from"preact/hooks";var _e={read:()=>{if(u)return{width:window.innerWidth,height:window.innerHeight}},subscribe:e=>{if(u)return window.addEventListener("resize",e,!1),()=>{window.removeEventListener("resize",e,!1)}}};function Y(){let e=i(_e);return ke(e),e}export{a as defaultCompare,V as useAsyncMemo,v as useCallbackCondition,v as useConditionalCallback,D as useConditionalEffect,P as useConditionalLayoutEffect,x as useConditionalMemo,c as useConstant,m as useConstantCallback,l as useDependencyChanged,h as useDependencyVersion,D as useEffectCondition,S as useForceUpdate,T as useFreshLazyRef,E as useFreshState,F as useFullscreenElement,L as useIsomorphicEffect,p as useLazyRef,b as useMediaQuery,x as useMemoCondition,w as useMountedState,O as useOnlineStatus,z as usePageVisibility,N as usePartialState,j as usePrefersDarkTheme,W as usePrefersLightTheme,k as usePrefersReducedMotion,Q as useReactiveObject,B as useReactiveRef,_ as useReadyState,H as useRenderCount,i as useSubscription,X as useWindowScroll,Y as useWindowSize}; /** * @license * MIT License * * Copyright (c) 2021 Lyon Software Technologies, Inc. * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * * @author Lyon Software Technologies, Inc. * @copyright Lyon Software Technologies, Inc. 2021 */ /** * @license * MIT License * * Copyright (c) 2021 Lyon Software Technologies, Inc. * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the 'Software'), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * * @author Lyon Software Technologies, Inc. * @copyright Lyon Software Technologies, Inc. 2021 */