UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 1.09 kB
{"version":3,"file":"index.cjs","names":[],"sources":["../../../../src/hooks/use-online/index.ts"],"sourcesContent":["\"use client\"\n\nimport { useSyncExternalStore } from \"react\"\n\nfunction subscribe(callback: () => void) {\n window.addEventListener(\"online\", callback)\n window.addEventListener(\"offline\", callback)\n\n return () => {\n window.removeEventListener(\"online\", callback)\n window.removeEventListener(\"offline\", callback)\n }\n}\n\nfunction getSnapshot() {\n return window.navigator.onLine\n}\n\nexport interface UseOnlineProps {}\n\nexport function useOnline(\n getServerSnapshot: () => boolean = () => true,\n): boolean {\n const online = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)\n\n return online\n}\n\nexport type UseOnlineReturn = ReturnType<typeof useOnline>\n"],"mappings":";;;;;;;;AAIA,SAAS,UAAU,UAAsB;AACvC,QAAO,iBAAiB,UAAU,SAAS;AAC3C,QAAO,iBAAiB,WAAW,SAAS;AAE5C,cAAa;AACX,SAAO,oBAAoB,UAAU,SAAS;AAC9C,SAAO,oBAAoB,WAAW,SAAS;;;AAInD,SAAS,cAAc;AACrB,QAAO,OAAO,UAAU;;AAK1B,SAAgB,UACd,0BAAyC,MAChC;AAGT,wCAFoC,WAAW,aAAa,kBAAkB"}