{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-cookie",
  "title": "UseCookie",
  "description": "Synchronizes a value with cookies.",
  "files": [
    {
      "path": "registry/hooks/use-cookie.tsx",
      "content": "import { useCallback, useMemo } from 'react';\n\nexport type CookieOptions = {\n  path?: string;\n  expires?: Date | string;\n  maxAge?: number;\n  domain?: string;\n  secure?: boolean;\n  sameSite?: 'strict' | 'lax' | 'none';\n};\n\n/**\n * Type-safe React hook for managing cookies.\n * Provides methods to get, set, and remove cookies.\n *\n * @param key The cookie key\n * @param initialValue Optional initial value for the cookie\n * @returns [value, setCookie, removeCookie]\n */\nexport function useCookie<T = string>(\n  key: string,\n  initialValue?: T,\n): [T | undefined, (value: T, options?: CookieOptions) => void, () => void] {\n  // Helper to check if running in browser\n  const isBrowser = typeof document !== 'undefined';\n\n  // Parse cookie string to object\n  const getCookies = useCallback((): Record<string, string> => {\n    if (!isBrowser) return {};\n    return document.cookie.split('; ').reduce(\n      (acc, cookie) => {\n        const [k, ...v] = cookie.split('=');\n        acc[decodeURIComponent(k)] = decodeURIComponent(v.join('='));\n        return acc;\n      },\n      {} as Record<string, string>,\n    );\n  }, [isBrowser]);\n\n  // Set cookie\n  const setCookie = useCallback(\n    (val: T | undefined, options: CookieOptions = {}) => {\n      if (!isBrowser) return;\n      let encodedValue: string;\n      if (typeof val === 'string') {\n        encodedValue = encodeURIComponent(val);\n      } else {\n        encodedValue = encodeURIComponent(JSON.stringify(val));\n      }\n      let cookieStr = `${encodeURIComponent(key)}=${encodedValue}`;\n      if (options.path) cookieStr += `; path=${options.path}`;\n      if (options.expires)\n        cookieStr += `; expires=${options.expires instanceof Date ? options.expires.toUTCString() : options.expires}`;\n      if (options.maxAge) cookieStr += `; max-age=${options.maxAge}`;\n      if (options.domain) cookieStr += `; domain=${options.domain}`;\n      if (options.secure) cookieStr += '; secure';\n      if (options.sameSite) cookieStr += `; samesite=${options.sameSite}`;\n      document.cookie = cookieStr;\n    },\n    [isBrowser, key],\n  );\n\n  // Remove cookie\n  const removeCookie = useCallback(() => {\n    setCookie(undefined, { path: '/', expires: new Date(0) });\n  }, [setCookie]);\n\n  // Get cookie value (and create if missing)\n  const value = useMemo(() => {\n    const cookies = getCookies();\n    if (!(key in cookies)) {\n      if (initialValue !== undefined) {\n        setCookie(initialValue);\n        return initialValue;\n      } else {\n        setCookie(undefined);\n        return undefined;\n      }\n    }\n    try {\n      return JSON.parse(cookies[key]) as T;\n    } catch {\n      return cookies[key] as T;\n    }\n  }, [getCookies, key, initialValue, setCookie]);\n\n  return [value, setCookie, removeCookie];\n}\n",
      "type": "registry:hook",
      "target": "hooks/guarahooks/use-cookie.tsx"
    }
  ],
  "categories": [
    "utilities"
  ],
  "type": "registry:hook"
}