use-document-visibility
shadcn-hooks/use-document-visibilityFreeHook
A hook to track the document visibility state
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-document-visibility.jsonSource
1import { useSyncExternalStore } from 'react'23const DEFAULT_VISIBILITY_STATE: DocumentVisibilityState = 'visible'45function subscribe(onStoreChange: () => void) {6 if (typeof document === 'undefined') {7 return () => {}8 }910 document.addEventListener('visibilitychange', onStoreChange, {11 passive: true,12 })1314 return () => {15 document.removeEventListener('visibilitychange', onStoreChange)16 }17}1819function getSnapshot(): DocumentVisibilityState {20 if (typeof document === 'undefined') {21 return DEFAULT_VISIBILITY_STATE22 }2324 return document.visibilityState25}2627function getServerSnapshot(): DocumentVisibilityState {28 return DEFAULT_VISIBILITY_STATE29}3031/**32 * Reactively track `document.visibilityState`.33 *34 * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState35 * @returns The current document visibility state.36 */37export function useDocumentVisibility(): DocumentVisibilityState {38 return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)39}
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
