use-os
shadcn-hooks/use-osFreeHook
A hook to detect the current operating system
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-os.jsonSource
1import { useEffect, useState } from 'react'23export type UseOSReturnValue =4 | 'undetermined'5 | 'macos'6 | 'ios'7 | 'windows'8 | 'android'9 | 'linux'10 | 'chromeos'1112export interface UseOsOptions {13 /**14 * Defer browser OS detection until after mount to avoid server/client mismatches.15 * @default true16 */17 getValueInEffect?: boolean18}1920interface NavigatorWithUserAgentData extends Navigator {21 userAgentData?: {22 platform?: string23 }24}2526function getPlatform(): string {27 if (typeof navigator === 'undefined') {28 return ''29 }3031 const nav = navigator as NavigatorWithUserAgentData3233 return (nav.userAgentData?.platform ?? navigator.platform ?? '').toLowerCase()34}3536function getUserAgent(): string {37 if (typeof navigator === 'undefined') {38 return ''39 }4041 return navigator.userAgent.toLowerCase()42}4344/**45 * Detect the current operating system from the browser environment.46 *47 * @param options Hook options.48 * @returns The detected operating system or `undetermined`.49 */50export function getOS(options: UseOsOptions = {}): UseOSReturnValue {51 const { getValueInEffect = true } = options5253 if (getValueInEffect || typeof navigator === 'undefined') {54 return 'undetermined'55 }5657 const platform = getPlatform()58 const userAgent = getUserAgent()59 const maxTouchPoints = navigator.maxTouchPoints ?? 06061 if (userAgent.includes('android')) {62 return 'android'63 }6465 if (66 /iphone|ipad|ipod/.test(userAgent) ||67 (platform === 'macintel' && maxTouchPoints > 1)68 ) {69 return 'ios'70 }7172 if (userAgent.includes('cros')) {73 return 'chromeos'74 }7576 if (platform.includes('win') || userAgent.includes('win')) {77 return 'windows'78 }7980 if (platform.includes('mac') || userAgent.includes('mac')) {81 return 'macos'82 }8384 if (platform.includes('linux') || userAgent.includes('linux')) {85 return 'linux'86 }8788 return 'undetermined'89}9091/**92 * Reactively detect the current operating system from `navigator.userAgent`.93 *94 * @param options Hook options.95 * @returns The detected operating system or `undetermined`.96 */97export function useOs(options: UseOsOptions = {}): UseOSReturnValue {98 const { getValueInEffect = true } = options99 const [os, setOs] = useState<UseOSReturnValue>(() =>100 getOS({ getValueInEffect }),101 )102103 useEffect(() => {104 setOs(getOS({ getValueInEffect: false }))105 }, [getValueInEffect])106107 return os108}
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 |
