react-hook-ambient-light

1.1.3 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i react-hook-ambient-light

Weekly Downloads

1

Version

1.1.3

License

LGPL

Unpacked Size

9.55 kB

Total Files

3

Last publish

Collaborators

  • masao