Skip to content

JavaScript Adapter

@laravel-rust-mobile/core lets your Inertia pages call native features such as haptics, Face ID, sharing, and notifications, while the same pages keep working in a browser.

Introduction

The @laravel-rust-mobile/core package lets your Inertia pages call native features, such as haptics or Face ID. Inside your iOS or Android app, each call uses the device. In a browser, or during server-side rendering, it does nothing or uses the matching web API, so the very same page works everywhere:

import { mobile } from '@laravel-rust-mobile/core';

await mobile.haptics.success();

if (await mobile.biometrics.authenticate('Unlock your wallet')) {
    router.visit('/wallet');
}

Installation

If you created your mobile app with laravel-mobile init, the package is already in your package.json file and installed. Otherwise, install it with npm:

npm install @laravel-rust-mobile/core

The package has no dependencies of its own. React and Vue are optional peer dependencies, so you only need the framework your app already uses.

Each feature also needs its plugin enabled in mobile.toml. For example, to use haptics and Face ID:

mobile.tomltoml
[plugins]
haptics = true
biometric = true

[permissions]
biometrics = "Unlock your wallet with Face ID."

Using It in Components

In React or Vue components, you may use the useMobile hook:

import { useMobile } from '@laravel-rust-mobile/core/react';

export default function SaveButton() {
    const mobile = useMobile();

    return <button onClick={() => mobile.haptics.success()}>Save</button>;
}

For Vue, import useMobile from @laravel-rust-mobile/core/vue instead.

Available Methods

Method In your app In a browser
mobile.isNative() true false
mobile.platform() 'ios' or 'android' 'web'
mobile.haptics.impact(style?), .success(), .warning(), .error(), .selection(), .vibrate(ms) Haptic feedback. Needs haptics = true. Does nothing.
mobile.biometrics.available() Whether Face ID, Touch ID, or fingerprint unlock is set up. false
mobile.biometrics.authenticate(reason, options?) true once the user confirms, false if they cancel. The options object takes a title, which titles the Android prompt and defaults to “Verify it’s you”. Needs biometric = true and permissions.biometrics. false
mobile.storage.get(key, fallback?), .put(key, value), .has(key), .forget(key) Small JSON values kept on the device between launches, like Laravel’s cache. Not for secrets. Needs store = true. localStorage.
mobile.clipboard.copy(text), .read() The system clipboard. Needs clipboard-manager = true. The browser’s clipboard, or false and null.
mobile.location.current() { latitude, longitude, accuracy }, after asking the user; null if they say no. Needs geolocation = true and permissions.location. The browser’s location, or null.
mobile.scanner.scan(options?), .cancel() The first barcode or QR code the camera reads. It asks for camera access first, and resolves null when the user cancels or refuses. It scans behind the page by default (windowed: true), so the page needs a transparent background and a Cancel button. Needs barcode-scanner = true and permissions.camera. null.
mobile.network.online(), .onChange(callback) Whether the device is online, and a listener for changes that returns a function to stop it. The same.
mobile.share({ title, text, url }) The share sheet, where the WebView supports it. The browser’s share sheet, or false.
mobile.notifications.requestPermission(), .granted(), .send(title, { body }) Local notifications. Needs notification = true and permissions.notifications. Web notifications.
mobile.invoke(command, args?) Calls a command you register with tauri::generate_handler! in apps/mobile/src/lib.rs. sync grants your pages access to it. Throws, so check mobile.isNative() first.

Android’s WebView doesn’t have a share sheet yet, so mobile.share resolves false there. When it does, you may fall back to copying the link.

Branching on the Platform

Most of the time, you can call these methods without checking where your page is running. When the experience genuinely needs to differ, you may branch on mobile.isNative():

if (mobile.isNative()) {
    await mobile.invoke('export_to_files', { id: report.id });
} else {
    window.location.href = `/reports/${report.id}/download`;
}

Calling a Plugin You Haven’t Enabled

If you call a feature whose plugin isn’t enabled, the call throws an error that names the [plugins] line to add to mobile.toml. Add it, run laravel-mobile sync, and rebuild the app.

The network calls use the browser’s own events, so they need no plugin.

Full-Height Sections on Android

On Android, the page shrinks when the keyboard opens, so it stays above the keyboard. 100vh and 100dvh shrink with it. For a section that should keep the full screen height, use 100lvh instead.

Type to search…

↑↓ navigate↵ selectEsc close