browser-geolocation

Wraps the Geolocation API as reactive state: the current position, derived coordinate selectors, the permission state, and a status machine. The watch starts on the first subscriber and stops on the last.

Permission & HTTPS
Geolocation requires a secure context (HTTPS or localhost) and a user grant. Subscribing to positionAtom (or a coordinate selector) triggers the browser's permission prompt.

Install

bun add @valdres/browser-geolocation

Live example

Loading demo…

Usage

import { useValue } from "valdres-react"
import { coordsSelector, geolocationStatusAtom } from "@valdres/browser-geolocation"

function Where() {
    const coords = useValue(coordsSelector)
    const status = useValue(geolocationStatusAtom)
    if (!coords) return <span>{status}</span>
    return <span>{coords.latitude.toFixed(3)}, {coords.longitude.toFixed(3)}</span>
}

Exports

ExportKindType
positionAtomatom (read-only)GeolocationSnapshot | null
coordsSelectorselector{ latitude, longitude } | null
accuracySelectorselectornumber | null
altitudeSelector / speedSelector / headingSelectorselectornumber | null
permissionAtomatom (read-only)"granted" | "denied" | "prompt" | "unsupported"
geolocationStatusAtomatom (read-only)"idle" | "pending" | "active" | "error" | "unsupported"
geolocationErrorAtomatom (read-only)GeolocationError | null
geolocationOptionsAtomatom (settable)PositionOptions

Cross-framework

All state is global atoms/selectors, so it works in every framework — only the read primitive differs. Write geolocationOptionsAtom (via your framework's set hook, or store.set) to change accuracy/timeout options; the watch re-subscribes automatically.