# toggle-fullscreen

> Promise based Fullscreen API for cross-browser.

Latest version **0.4.2** (published 2019-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install toggle-fullscreen
pnpm add toggle-fullscreen
yarn add toggle-fullscreen
bun add toggle-fullscreen
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.2 |
| Published | 2019-03-10 |
| First published | 2017-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 19.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Masashi Hirano |
| Maintainers | shisama |
| Keywords | toggle, toggle fullscreen, fullscreen, fullscreen api, switch fullscreen, fullscreen promise, promise |

## Links

- npm: https://www.npmjs.com/package/toggle-fullscreen
- Repository: https://github.com/shisama/toggle-fullscreen
- Homepage: https://github.com/shisama/toggle-fullscreen#readme
- Issues: https://github.com/shisama/toggle-fullscreen/issues
- npm.io page: https://npm.io/package/toggle-fullscreen

## Dependencies (1)

- [es6-promise](https://npm.io/package/es6-promise.md) ^4.1.1

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 0.4.2 (latest) — 2019-03-10
- 0.4.1 — 2019-03-10
- 0.4.0 — 2019-03-10
- 0.3.3 — 2017-09-10
- 0.3.2 — 2017-09-10
- 0.3.1 — 2017-08-29
- 0.3.0 — 2017-08-29
- 0.2.3 — 2017-08-26
- 0.2.2 — 2017-08-26
- 0.2.1 — 2017-08-24
- 0.2.0 — 2017-08-24
- 0.1.4 — 2017-08-23
- 0.1.3 — 2017-08-10
- 0.1.2 — 2017-08-10
- 0.1.1 — 2017-08-10
- … 1 more at https://npm.io/package/toggle-fullscreen/versions

## README

toggle-fullscreen
===
[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) based [Fullscreen API](https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API) for cross-browser.

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/shisama/toggle-fullscreen/blob/master/LICENSE)
[![npm](https://img.shields.io/npm/dt/toggle-fullscreen.svg)](https://www.npmjs.com/package/toggle-fullscreen)

```js
async function() {
  const element = document.querySelector('some element');
  await toggleFullscreen(element);
  await otherFunc();
}
```

## Install
```
npm install --save toggle-fullscreen
```

## API
### toggleFullscreen(target) ⇒ <code>Promise<></code>
Requests Fullscreen API.
Request to exit fullscreen mode if target is already fullscreen.
 
| Param  | Type                | Description  |
| ------ | ------------------- | ------------ |
| target  | <code>Element</code> | target element to change fullscreen|

Usage:
```js
var target = document.querySelector('#target');
toggleFullscreen(target);
```
For jQuery:
```js
$('#target').on('click', event => {
	toggleFullscreen(event.target);
});
```

### fullscreenChange(callback) ⇒ <code>Promise<></code>
Add a listener for when the browser switches in and out of fullscreen. 
 
| Param  | Type                | Description  |
| ------ | ------------------- | ------------ |
| callback  | <code>function</code> | function to be called when the browser switches in and out of fullscreen|

Usage:
```js
fullscreenChange(function() {
  console.log('switch fullscreen');
});
```
### isFullscreen() ⇒ <code>Boolean</code>
Check whether fullscreen is active.
Usage:
```js
if (isFullscreen()) {
  console.log('fullscreen is active');
} else {
  console.log('fullscreen is not active');
} 
```

## Example
```jsx harmony
import toggleFullscreen, {
  fullscreenChange,
  isFullscreen,
} from 'toggle-fullscreen';

onChangeFullScreen = () => {
  const element = document.getElementById('something');
  toggleFullscreen(element)
    .then(() => {
      return fullscreenChange(() => {
        const isFullScreen = isFullscreen();
        if (isFullScreen) {
          // any process in fullscreen mode
          // e.g.document.addEventListener('keydown', keydownFunction);
        } else {
          // any process in non-fullscreen mode
          // e.g.document.removeEventListener('keydown', keydownFunction);
        }
      });
    })
    .then(() => {
      console.log('successed!');
    })
    .catch(() => {
      console.log('failed!');
    });
};
```

Or use Promise.all()

```jsx harmony
import toggleFullscreen, {
  fullscreenChange,
  isFullscreen,
} from 'toggle-fullscreen';

onChangeFullScreen = () => {
  // target element
  const element = document.getElementById('something');
  // callback function when fullscreen change is detected.
  const callback = () => {
    const isFullScreen = isFullscreen();
    this.setState({isFullScreen: isFullScreen});
    if (isFullScreen) {
      document.addEventListener('keydown', this.keydownEvent);
      element.style.width = '70%';
    } else {
      document.removeEventListener('keydown', this.keydownEvent);
      element.style.width = '100%';
    }
  };
  // execute toggle-fullscreen and add listener when fullscreen change detected asynchronously
  Promise.all([toggleFullscreen(element), fullscreenChange(callback)]);
};
```

For async/await:
```jsx harmony
import toggleFullscreen, {
  fullscreenChange,
  isFullscreen,
} from 'toggle-fullscreen';

onChangeFullScreen = async () => {
  const element = document.getElementById('something');
  await toggleFullscreen(element);
  await fullscreenChange(() => {
    const isFullScreen = isFullscreen();
    if (isFullScreen) {
      // any process in fullscreen mode
      // e.g.document.addEventListener('keydown', keydownFunction);
    } else {
      // any process in non-fullscreen mode
      // e.g.document.removeEventListener('keydown', keydownFunction);
    }
  });
  console.log('successed!');
};
```

For callback (use if something is wrong with Promise) :
```js
const toggleFullscreen = require('toggle-fullscreen');
onChangeFullScreen = function() {
  const element = document.getElementById('something');
  toggleFullscreen(element, function(isFullScreen) {
    if (isFullScreen) {
      // any process in fullscreen mode
      // e.g.document.addEventListener('keydown', this.keydownEvent);
    } else {
      // any process in non-fullscreen mode
      // e.g.document.removeEventListener('keydown', this.keydownEvent);
    }
  });
};
```
## Support
- Chrome@latest
- Firefox@latest
- Safari@latest
- MSEdge@latest
- IE11

## License
This project is licensed under the terms of the
[MIT license](https://github.com/shisama/toggle-fullscreen/blob/master/LICENSE)

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