UNPKG

@guruhotel/aura-hooks

Version:

🪝 Hooks library designed by the Guruhotel team for Aura UI

1 lines 763 B
import{useState as e}from"react";const o="undefined"!=typeof window;export const setCookie=(e,t,n)=>{if(!o)return;const i={days:7,path:"/",...n},c=new Date(Date.now()+864e5*i.days).toUTCString();document.cookie=`${e}=${encodeURIComponent(t)}; expires=${c}; path=${i.path}`};export const getCookie=(e,t="")=>o&&document.cookie.split("; ").reduce(((o,t)=>{const n=t.split("=");return n[0]===e?decodeURIComponent(n[1]):o}),"")||t;export const deleteCookie=e=>{o&&(document.cookie=`${e}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`)};export const useCookie=(t,n)=>{const[i,c]=e((()=>getCookie(t,n)));return{item:i,updateItem:(e,o)=>{c(e),setCookie(t,e,o)},deleteItem:()=>{var e;e=t,o&&(document.cookie=`${e}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;`)}}};