Skip to content
Lucent
SEARCH LUCENT

Search guides, APIs, and examples.

GitHub

Very early and experimental. The language, the generated native code and every package API change without notice. Do not use Lucent in production.

Check the OS version

available("ios", 17) and available("android", 31) guard newer APIs. On Android, the compiler requires it.

haptic.lucent.ts
import { PLATFORM } from "lucent:platform";
import {
UIImpactFeedbackGenerator,
UIImpactFeedbackGenerator_FeedbackStyle as Style,
} from "lucent:ios/UIKit";
import { Vibrator, VibratorManager } from "lucent:android/android.os";
import { appContext, available } from "lucent:android";
import { available as availableOnIOS } from "lucent:ios";
import { main } from "lucent:thread";
export async function tap(): Promise<void> {
if (PLATFORM === "ios") {
await main(() => {
const style = availableOnIOS("ios", 13) ? Style.soft : Style.light;
new UIImpactFeedbackGenerator(style).impactOccurred();
});
} else {
const context = appContext();
const vibrator = available("android", 31)
? context.getSystemService(VibratorManager)?.defaultVibrator
: context.getSystemService(Vibrator);
vibrator?.vibrate(20n);
}
}
  • available("android", api) from lucent:android is Build.VERSION.SDK_INT >= api. available("ios", major, minor?) from lucent:ios is Swift's #available.
  • On Android, a class or member newer than API 24 compiles only behind such a check (LUCENT3007). Build_VERSION.SDK_INT >= 31 works too, and so does an early return on the opposite check.
  • On iOS, the compiler doesn't check versions yet: guard new APIs yourself. Each SDK member's doc comment says the version it needs.
  • vibrate(20n) passes a bigint: the milliseconds are a Java long, and every native 64-bit integer is a bigint in Lucent.