# react-use-browser-theme

> React hook for retrieving the current browser theme and listening for changes.

Latest version **1.0.6** (published 2020-04-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-use-browser-theme
pnpm add react-use-browser-theme
yarn add react-use-browser-theme
bun add react-use-browser-theme
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-04-06 |
| First published | 2020-03-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=8 |
| Dependencies | 0 |
| Unpacked size | 112.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Emmanouil Zoumpoulakis |
| Maintainers | emzoumpo |

## Links

- npm: https://www.npmjs.com/package/react-use-browser-theme
- Repository: https://github.com/emzoumpo/react-use-browser-theme
- Homepage: https://github.com/emzoumpo/react-use-browser-theme#readme
- Issues: https://github.com/emzoumpo/react-use-browser-theme/issues
- npm.io page: https://npm.io/package/react-use-browser-theme

## Recent versions

- 1.0.6 (latest) — 2020-04-06
- 1.0.5 — 2020-04-06
- 1.0.4 — 2020-04-05
- 1.0.3 — 2020-04-05
- 1.0.2 — 2020-04-05
- 1.0.1 — 2020-03-30

## README

# react-use-browser-theme

> React hook for retrieving the current browser theme and listening for changes.

![npm](https://img.shields.io/npm/v/react-use-browser-theme?style=flat-square) ![npm](https://img.shields.io/npm/dt/react-use-browser-theme?style=flat-square)

## Description

This is a React Hook that will call a callback function provided to it every time the theme changes.
The theme of the browser is returned as `light` or `dark`, `light` being the default if there is no explicit theme setting by the user.

## Example

[here](https://emzoumpo.github.io/react-use-browser-theme/)

## Install

```bash
yarn add react-use-browser-theme
```

or

```bash
npm install --save react-use-browser-theme
```

## Usage

```jsx
import React, { useState, useCallback, useLayoutEffect } from 'react'

import { useBrowserThemeChangeListener } from 'react-use-browser-theme'

const App = () => {
  const [theme, setTheme] = useState('light')

  useLayoutEffect(() => {
    if (matchMedia('(prefers-color-scheme: dark)').matches) {
      setTheme('dark')
    }
  }, [])

  const browserThemeListener = useCallback(
    (e) => setTheme(e.matches ? 'dark' : 'light'),
    []
  )

  useBrowserThemeChangeListener(browserThemeListener)

  return (
    <div
      style={{
        minHeight: '100vh',
        minWidth: '100vw',
        color: `${theme === 'dark' ? 'white' : 'black'}`,
        backgroundColor: `${theme === 'dark' ? 'black' : 'white'}`,
      }}
    >
      Current browser theme: {theme}
    </div>
  )
}

export default App
```

## License

MIT © [emzoumpo](https://github.com/emzoumpo)

---
_Source: https://npm.io/package/react-use-browser-theme · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
