expo-flashlight
v0.1.0
Published
Expo module to control the device torch (flashlight): on/off and brightness.
Maintainers
Readme
expo-flashlight
Expo module to control the device torch (flashlight): turn it on/off and adjust its brightness.
- iOS — continuous brightness via
AVCaptureDevice.setTorchModeOn(level:). - Android — on/off via
CameraManager.setTorchMode; adjustable brightness on Android 13+ (API 33) viaturnOnTorchWithStrengthLevel, with automatic on/off fallback on older devices. - Web — reports the torch as unavailable (control is not supported).
Installation
npx expo install expo-flashlightThis is a native module, so it requires a development build — it does not run in Expo Go.
Usage
import Flashlight from 'expo-flashlight';
if (Flashlight.isAvailable()) {
Flashlight.enable(); // turn on (last/full brightness)
Flashlight.setBrightness(0.5); // 50% brightness (0..1)
Flashlight.toggle(); // flip on/off, returns the new state
if (Flashlight.isOn) {
Flashlight.disable(); // turn off
}
}Reacting to state changes
import Flashlight, { ExpoFlashlightModule } from 'expo-flashlight';
import { useEvent } from 'expo';
function TorchStatus() {
const state = useEvent(ExpoFlashlightModule, 'onTorchStateChanged');
// state?.enabled, state?.brightness
}Or imperatively:
const sub = Flashlight.addListener(({ enabled, brightness }) => {
console.log({ enabled, brightness });
});
// later
sub.remove();API
| Member | Type | Description |
| --- | --- | --- |
| isAvailable() | boolean | Whether the device has a controllable torch. |
| isBrightnessControlAvailable() | boolean | Whether brightness can be adjusted (always on iOS with a torch; Android 13+ only). |
| enable(brightness?) | void | Turn the torch on. Optional 0..1 brightness. |
| disable() | void | Turn the torch off. |
| toggle() | boolean | Toggle the torch; returns the new on/off state. |
| set(on) | void | Set the torch on or off. |
| setBrightness(level) | void | Set brightness 0..1. 0 turns the torch off. |
| getBrightness() | number | Current brightness 0..1 (0 when off). |
| isOn / isOff | boolean (getter) | Whether the torch is currently on/off. |
| isEnabled() | boolean | Method form of isOn. |
| addListener(cb) | EventSubscription | Subscribe to { enabled, brightness } changes. |
Brightness on Android
Below Android 13 the torch is on/off only, so setBrightness(level) turns the
torch on at full brightness for any level > 0. On Android 13+ the normalized
0..1 value is mapped to the device's discrete strength steps. Use
isBrightnessControlAvailable() to detect support.
Development
This package uses Bun as its package manager.
bun install # install dependencies
bun run build # compile src/ -> build/
bun run lint # eslint
bun run test # run the Vitest suite once
bun run test:watchUnit tests live in src/__tests__/ and use Vitest. The
native module is mocked, so the tests cover the JavaScript wrapper logic
(brightness clamping, delegation, getters, event subscription) without a device.
License
MIT © Aziz Becha
