Platform
OS detection and cross-platform Mod key resolution.
OS detection and cross-platform Mod key resolution. Detects whether the user is on macOS, Windows, or Linux and maps the virtual Mod key to the correct platform modifier.
import { detectPlatform, resolveMod, isMac } from '@gentleduck/vim/platform'
import type { Platform } from '@gentleduck/vim/platform'import { detectPlatform, resolveMod, isMac } from '@gentleduck/vim/platform'
import type { Platform } from '@gentleduck/vim/platform'Types
Platform
type Platform = 'mac' | 'windows' | 'linux'type Platform = 'mac' | 'windows' | 'linux'Functions
detectPlatform()
Detects the current operating system by inspecting navigator.userAgent. The result is cached after the first call.
Returns 'linux' when navigator is not available (SSR environments).
function detectPlatform(): Platformfunction detectPlatform(): PlatformExample:
const platform = detectPlatform() // 'mac' | 'windows' | 'linux'const platform = detectPlatform() // 'mac' | 'windows' | 'linux'The result is cached after the first call, so subsequent calls are effectively free.
resolveMod(platform?)
Returns the platform-specific modifier that the virtual Mod key resolves to.
function resolveMod(platform?: Platform): 'meta' | 'ctrl'function resolveMod(platform?: Platform): 'meta' | 'ctrl'| Platform | Returns |
|---|---|
'mac' | 'meta' |
'windows' | 'ctrl' |
'linux' | 'ctrl' |
Example:
resolveMod('mac') // 'meta'
resolveMod('linux') // 'ctrl'
resolveMod() // uses detectPlatform() automaticallyresolveMod('mac') // 'meta'
resolveMod('linux') // 'ctrl'
resolveMod() // uses detectPlatform() automaticallyUse Mod in your key bindings instead of hard-coding Ctrl or Meta. It automatically resolves to the right modifier for the user's OS.
isMac(platform?)
Convenience check for macOS.
function isMac(platform?: Platform): booleanfunction isMac(platform?: Platform): booleanExample:
if (isMac()) {
console.log('Running on macOS')
}if (isMac()) {
console.log('Running on macOS')
}_resetPlatformCache()
Clears the cached platform detection result. Only useful for testing.
function _resetPlatformCache(): voidfunction _resetPlatformCache(): voidThis is an internal API (prefixed with _). Do not use it in production code.