react-hook-ambient-light
v1.1.3
Published
React hook for Ambient Light Sensor access
Downloads
5
Readme
A React hook to access data from the Ambient Light API.
Installation
Using npm
:
npm install --save react-hook-ambient-light
Using yarn
:
yarn add react-hook-ambient-light
Usage
import React from 'react'
import useAmbientLightSensor from 'react-hook-ambient-light'
const ComponentWithAccelerometer = () => {
const sensor = useAmbientLightSensor()
return !sensor.error ? (
<ul>
<li>X: {sensor.x}</li>
<li>Y: {sensor.y}</li>
<li>Z: {sensor.z}</li>
</ul>
) : (
<p>No ambient light, sorry.</p>
)
}
Using SensorOptions
If you want to use this feature, simply provide useAmbientLightSensor
with a SensorOptions
object:
const sensor = useAmbientLightSensor({
frequency: 60, // cycles per second
})
Notes
Access to data from the Ambient Light API needs user permission.
Caveats
Ambient Light API is available only in secure contexts (only using HTTPS).
Credits
Credit to Bence A. Tóth for his original hook code for Geolocation.
License
LGPL-3.0