@thibault.sh/hooks
Version:
A comprehensive collection of React hooks for browser storage, UI interactions, and more
2 lines • 972 B
JavaScript
var react=require('react');function C(e,t){let[d,i]=react.useState(()=>{if(typeof window=="undefined")return t;try{let o=document.cookie.split("; ").find(n=>n.startsWith(e+"="));return o?decodeURIComponent(o.split("=")[1]):t}catch(o){return console.warn(`Error reading cookie "${e}":`,o),t}}),u=react.useCallback((o,n={})=>{if(typeof window!="undefined")try{let{days:r=7,path:p="/",domain:s,secure:f=!1,sameSite:l="Lax"}=n,k=new Date(Date.now()+r*864e5).toUTCString();document.cookie=[`${e}=${encodeURIComponent(o)}`,`expires=${k}`,`path=${p}`,s?`domain=${s}`:"",f?"secure":"",`SameSite=${l}`].filter(Boolean).join("; "),i(o);}catch(r){console.warn(`Error setting cookie "${e}":`,r);}},[e]),a=react.useCallback(()=>{typeof window!="undefined"&&(document.cookie=`${e}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`,i(null));},[e]);return [d,u,a]}exports.useCookieState=C;//# sourceMappingURL=useCookieState.cjs.map
//# sourceMappingURL=useCookieState.cjs.map
;