UNPKG

@lyonph/preact-hooks

Version:

Collection of useful Preact Hooks

56 lines (55 loc) 9.77 kB
"use strict";var D=Object.defineProperty,ye=Object.defineProperties,Ce=Object.getOwnPropertyDescriptor,ge=Object.getOwnPropertyDescriptors,ve=Object.getOwnPropertyNames,Z=Object.getOwnPropertySymbols;var q=Object.prototype.hasOwnProperty,we=Object.prototype.propertyIsEnumerable;var $=(e,t,r)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,w=(e,t)=>{for(var r in t||(t={}))q.call(t,r)&&$(e,r,t[r]);if(Z)for(var r of Z(t))we.call(t,r)&&$(e,r,t[r]);return e},ee=(e,t)=>ye(e,ge(t));var Ee=(e,t)=>{for(var r in t)D(e,r,{get:t[r],enumerable:!0})},Le=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of ve(t))!q.call(e,n)&&n!==r&&D(e,n,{get:()=>t[n],enumerable:!(o=Ce(t,n))||o.enumerable});return e};var De=e=>Le(D({},"__esModule",{value:!0}),e);var ke={};Ee(ke,{defaultCompare:()=>a,useAsyncMemo:()=>O,useCallbackCondition:()=>y,useConditionalCallback:()=>y,useConditionalEffect:()=>F,useConditionalLayoutEffect:()=>z,useConditionalMemo:()=>x,useConstant:()=>c,useConstantCallback:()=>m,useDependencyChanged:()=>l,useDependencyVersion:()=>h,useEffectCondition:()=>F,useForceUpdate:()=>S,useFreshLazyRef:()=>T,useFreshState:()=>L,useFullscreenElement:()=>N,useIsomorphicEffect:()=>V,useLazyRef:()=>p,useMediaQuery:()=>b,useMemoCondition:()=>x,useMountedState:()=>E,useOnlineStatus:()=>j,usePageVisibility:()=>W,usePartialState:()=>k,usePrefersDarkTheme:()=>_,usePrefersLightTheme:()=>Q,usePrefersReducedMotion:()=>B,useReactiveObject:()=>X,useReactiveRef:()=>Y,useReadyState:()=>H,useRenderCount:()=>G,useSubscription:()=>i,useWindowScroll:()=>J,useWindowSize:()=>K});module.exports=De(ke);var ae=require("preact/hooks");var te=require("preact/hooks");var I=require("preact/hooks");function a(e,t){return!Object.is(e,t)}function l(e,t=a){let r=(0,I.useRef)(e),o=t(r.current,e);return o&&(r.current=e),(0,I.useDebugValue)(o),o}function y(e,t,r=a){let o=(0,te.useRef)(e);return l(t,r)&&(o.current=e),o.current}var ue=require("preact/hooks");var re=require("preact/hooks");var M=require("preact/hooks");function p(e){let t=(0,M.useRef)();return t.current||(t.current={current:e()}),(0,M.useDebugValue)(t.current),t.current}function c(e){let{current:t}=p(e);return(0,re.useDebugValue)(t),t}function m(e){return c(()=>e)}var oe=require("preact/hooks");function S(){return(0,oe.useReducer)(()=>[],[])[1]}function T(e,t,r=a){let o=p(e);return l(t,r)&&(o.current=e()),o}var ne=require("preact/hooks");var U=require("preact/hooks");var Ie=typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.document.createElement!="undefined",u=Ie;var Me=u?U.useLayoutEffect:U.useEffect,V=Me;function E(){let e=(0,ne.useRef)(!1);return V(()=>(e.current=!0,()=>{e.current=!1}),[]),m(()=>e.current)}function Ue(e){return typeof e=="function"}function Ve(e){return typeof e=="function"}function L(e,t,r=a){let o=T(()=>Ue(e)?e():e,t,r),n=S(),s=E(),f=m(d=>{if(s()){let{current:g}=o,v=Ve(d)?d(g):d;Object.is(g,v)||(o.current=v,n())}});return(0,ue.useDebugValue)(o.current),[o.current,f]}function O(e,t,r=a){let[o,n]=L(()=>({status:"pending"}),t,r),s=y(e,t,r);return(0,ae.useEffect)(()=>{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}var se=require("preact/hooks");var P=require("preact/hooks");function h(e,t=a){let r=l(e,t),o=(0,P.useRef)(0);return r&&(o.current+=1),(0,P.useDebugValue)(o.current),o.current}function F(e,t,r=a){let o=h(t,r);(0,se.useEffect)(e,[o])}var ie=require("preact/hooks");function z(e,t,r=a){let o=h(t,r);(0,ie.useLayoutEffect)(e,[o])}var fe=require("preact/hooks");function x(e,t,r=a){let o=T(e,t,r).current;return(0,fe.useDebugValue)(o),o}var de=require("preact/hooks");var C=require("preact/hooks");function i({read:e,subscribe:t,shouldUpdate:r=a}){let[o,n]=(0,C.useState)(()=>({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})),(0,C.useDebugValue)(s),(0,C.useEffect)(()=>{let f=!0,d=()=>{if(f){let v=e();n(R=>R.read!==e||R.subscribe!==t||R.shouldUpdate!==r||!r(R.value,v)?R:ee(w({},R),{value:v}))}},g=t(d);return d(),()=>{f=!1,g&&g()}},[e,t,r]),s}var Pe={read:()=>{if(u&&document.fullscreenElement)return document.fullscreenElement},subscribe:e=>{if(u)return document.addEventListener("fullscreenchange",e),()=>{document.removeEventListener("fullscreenchange",e)}}};function N(){let e=i(Pe);return(0,de.useDebugValue)(e),e}var le=require("preact/hooks");var ce=new Map;function Fe(e){let t=ce.get(e);if(t)return t;let r=window.matchMedia(e);return ce.set(e,r),r}function b(e){let t=x(()=>{if(u)return Fe(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(0,le.useDebugValue)(o),o}var pe=require("preact/hooks");var Ae={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 j(){let e=i(Ae);return(0,pe.useDebugValue)(e),e}var me=require("preact/hooks");var Oe={read:()=>u?document.visibilityState==="visible":!0,subscribe:e=>{if(u)return document.addEventListener("visibilitychange",e,!1),()=>{document.removeEventListener("visibilitychange",e,!1)}}};function W(){let e=i(Oe);return(0,me.useDebugValue)(e),e}var Se=require("preact/hooks");function ze(e){return typeof e=="function"}function k(e){let[t,r]=(0,Se.useState)(e),o=m(n=>{r(s=>{let f=ze(n)?n(s):n;return w(w({},s),f)})});return[t,o]}function _(){return b("(prefers-color-scheme: dark)")}function Q(){return b("(prefers-color-scheme: light)")}function B(){return b("(prefers-reduced-motion)")}var be=require("preact/hooks");var Ne={read:()=>{if(u)return document.readyState},subscribe:e=>{if(u)return document.addEventListener("readystatechange",e,!1),()=>{document.removeEventListener("readystatechange",e,!1)}}};function H(){let e=i(Ne);return(0,be.useDebugValue)(e),e}var Re=require("preact/hooks");function X(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(0,Re.useDebugValue)(o),o}var Te=require("preact/hooks");function Y(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(0,Te.useDebugValue)(o),o}var A=require("preact/hooks");function G(){let e=(0,A.useRef)(0);return e.current+=1,(0,A.useDebugValue)(e.current),e.current}var he=require("preact/hooks");var je={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 J(){let e=i(je);return(0,he.useDebugValue)(e),e}var xe=require("preact/hooks");var We={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 K(){let e=i(We);return(0,xe.useDebugValue)(e),e} /** * @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 */