Haptics
Haptics are tactile cues. They should reinforce meaningful interactions, not replace visual text, screen-reader output, or error messages. Unsupported hosts return errors because many machines have no haptic hardware.
This example provides an explicit feedback-test control. In a save workflow,
request the same feedback only after the save succeeds. The haptic result
must never decide whether an already successful save counts as a success.
Do not vibrate continuously or use haptics as the only signal. Keep patterns short, user-driven, and tied to an action the user understands.
1. Start with the state the screen needs
Add the smallest state fields the screen needs to render honestly. For this flow, useful state is:
#[derive(Debug, Default)]
struct HapticsState {
feedback_pending: bool,
haptic_error: Option<String>,
}
impl GlobalState for HapticsState {}
Keep any haptic error in app state so the UI can report it. Do not imply that feedback played when the host returned an error.
Run this once from the project root:
fission add-capability haptics --project-dir .
That command records the capability in fission.toml and updates generated platform files where Fission can do so safely. It does not replace product-specific review of native manifests, usage text, entitlements, service identifiers, domains, or store policy. The CLI adds Android vibration permission. iOS does not need an Info.plist key for the generated scaffold. Desktop haptics are hardware-dependent and usually require a custom provider for game controllers or specialized devices.
This is the difference between the Fission API, a provider, and packaging configuration:
The Fission API is the typed haptics request your reducer makes; the pattern call is shown below.
The provider is the shell-side Rust implementation that turns that typed request into the correct OS, browser, or hardware call.
Packaging configuration is the manifest, plist, entitlement, service-worker, protocol, domain, or store metadata that lets the platform permit that call in a real installed app.
If the API exists but the provider is missing, the request returns a typed unsupported error. If the provider exists but packaging is missing, the operating system or browser may deny the request before the provider can complete it.
3. Define reducers for success and failure
The haptics request completes after the reducer returns. Handle any provider error in a completion reducer.
#[fission_reducer(HapticFailed)]
fn on_haptic_failed(state: &mut HapticsState, ctx: &mut ReducerContext<HapticsState>) {
state.feedback_pending = false;
state.haptic_error = Some(
ctx.input.capability_error(HAPTIC_NOTIFICATION)
.map(|error| error.message)
.or_else(|| ctx.input.capability_error_message(HAPTIC_NOTIFICATION).map(str::to_owned))
.unwrap_or_else(|| "Haptic feedback unavailable".into()),
);
}
#[fission_reducer(HapticPlayed)]
fn on_haptic_played(state: &mut HapticsState) {
state.feedback_pending = false;
state.haptic_error = None;
}
Unsupported hardware is a normal outcome. Do not make product success depend on a haptic provider.
4. Request the capability from a reducer
Call the capability from the reducer that handles the user's intent. The reducer queues a host effect and returns. The runtime later dispatches either the success reducer or the failure reducer.
#[fission_reducer(PlaySuccessHaptic)]
fn on_play_success_haptic(state: &mut HapticsState, ctx: &mut ReducerContext<HapticsState>) {
if state.feedback_pending { return; }
state.feedback_pending = true;
state.haptic_error = None;
let played = ctx.effects.bind(HapticPlayed, reduce_with!(on_haptic_played));
let failed = ctx.effects.bind(HapticFailed, reduce_with!(on_haptic_failed));
ctx.effects
.haptics()
.notification(HapticNotificationRequest { kind: HapticNotificationKind::Success })
.on_ok(played)
.on_err(failed);
}
The request queues host work and returns. Bind its callbacks first so the
effect builder does not overlap another mutable borrow of ctx.effects.
The error reducer reads the actual capability input, not a default error
placed in a callback's authored payload.
5. Trigger the reducer from the UI
Bind a feedback-test button to that reducer:
fission_component! {
pub PlayHapticFeedbackButton<HapticsState>
view(_button, state, cx) {
let run = cx.bind(PlaySuccessHaptic, reduce!(on_play_success_haptic));
} =>
Column(children: [
Button(
child: Text(cx.tr("feedback.test")),
disabled: state.feedback_pending,
on_press: run,
),
if state.feedback_pending { Spinner(motion: SpinnerMotion::Default, label: cx.tr("feedback.pending")) },
if let Some(error) = &state.haptic_error {
Alert(kind: AlertKind::Warning, title: error.clone())
},
])
}
Dispatch haptics only after the save reducer accepts the result. A failed save should not play success feedback.
6. Provide and test the host behavior
Register a HapticHost with .with_haptic_host(...). Use MemoryHapticHost in tests. Providers should reject long or unsupported patterns rather than blocking the UI or silently doing nothing.
Test that the app still works when haptics are unsupported or a provider fails, and verify that the screen communicates the result without relying on haptics. For custom patterns, test empty and long patterns and intensity bounds.
Android and iOS are the normal targets. Web vibration is limited and browser-dependent. Desktop support depends on hardware such as controllers, touchpads, or specialized devices.
Finished capability flow
A complete Haptics integration has all of these pieces in place:
| |
|---|
| fission.toml declares the capability and generated platform files contain the permission text, manifest entry, entitlement, domain, or protocol metadata the target needs. |
| App state records the in-flight flag, the successful value, and the user-facing error separately. |
| One reducer starts the host request, one handles success, and one handles denial, unsupported hosts, cancellation, or provider errors. |
| The shell registers a real provider for production and a memory provider for tests. |
| At least one test covers success and one covers permission denial or unsupported host behavior. |
Diagnose capability failures
Capability failures should be safe to retry and easy to explain. Start by checking fission.toml, then run fission doctor, then inspect the generated target files. If the provider is missing, the request should return an unsupported error. If the provider exists but the platform configuration is wrong, the OS or browser will usually deny the request before the provider can complete it. Keep those cases distinct in the UI so users know whether they can retry, change settings, connect hardware, or choose another path.