UNPKG

@thibault.sh/hooks

Version:

A comprehensive collection of React hooks for browser storage, UI interactions, and more

2 lines 958 B
import {useState,useCallback}from'react';function C(e,t){let[d,i]=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=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=useCallback(()=>{typeof window!="undefined"&&(document.cookie=`${e}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`,i(null));},[e]);return [d,u,a]}export{C as useCookieState};//# sourceMappingURL=useCookieState.mjs.map //# sourceMappingURL=useCookieState.mjs.map