ServiceUpdated in v15Optional

WrPlatform

SSR-safe browser checks, user-preference signals and haptics.

Installation

Import the class. This entry point has no stylesheet.

import { WrHaptics, WrPlatform } from 'ngwr/platform';

Usage

private readonly platform = inject(WrPlatform);

if (this.platform.isBrowser) {
  localStorage.setItem('theme', 'dark');
}

protected readonly dark = this.platform.prefersDark; // Signal<boolean>
protected readonly reduce = this.platform.prefersReducedMotion; // Signal<boolean>
isBrowser: false
prefersDark: false
prefersReducedMotion: false
Reflects your OS / browser preferences.

API

NameDescriptionTypeDefault
isBrowser / isServerSynchronous platform check.boolean—
userAgentnavigator.userAgent or null on the server.string | null—
prefersDarkSignal mirroring (prefers-color-scheme: dark).Signal<boolean>—
prefersReducedMotionSignal mirroring (prefers-reduced-motion: reduce).Signal<boolean>—

WrHaptics

Vibration presets for touch devices; a no-op on the server, in iOS Safari and wherever navigator.vibrate is missing.

private readonly haptics = inject(WrHaptics);

this.haptics.warning(); // before a destructive action
this.haptics.selection(); // on a switch or segmented change
this.haptics.supported; // false on the server and in iOS Safari
Impact lightImpact mediumImpact heavySelectionSuccessWarningError
supported: false means the API exists, not that the device can vibrate.
NameDescriptionTypeDefault
supportedWhether the Vibration API exists. false on iOS Safari.boolean—
vibrate(pattern)Vibrate for ms, or a [buzz, pause, …] pattern. 0 or [] cancels.(number | readonly number[]) => boolean—
impact(strength?)A tap: light, medium or heavy.('light'|'medium'|'heavy') => boolean'medium'
selection()A subtle tick for a discrete change.() => boolean—
success() / warning() / error()Pulses for a done, risky or failed action.() => boolean—
stop()Cancel any in-progress vibration.() => void—

See also