GithubHelp home page GithubHelp logo

leonardssh / ifvisible Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rosskevin/ifvisible

0.0 1.0 0.0 43.77 MB

Lightweight way to see if a browser page is visible or the user is interacting.

Home Page: https://rosskevin.github.io/ifvisible/

License: MIT License

JavaScript 6.99% TypeScript 93.01%

ifvisible's Introduction

CI Version MIT

@rosskevin/ifvisible

Lightweight way to see if a browser page is visible or the user is interacting.

Check out the Demo or read below for code examples.

Demo

Installation

From npm

npm install @rosskevin/ifvisible

# or

yarn install @rosskevin/ifvisible

Examples

Instantiation

This library provides a singleton exposed as ifvisible by default in addition to a UMD package, but for more advancecd users, they can import the class directly for a different attachment from the ES package.

// import singleton global bound to the `window`
import { ifvisible } from '@rosskevin/ifvisible'

or for more advanced usage for use cases that a singleton may not be useful, you may instantiate it directly:

// import the object and instantiate it yourself
import { IfVisible } from '@rosskevin/ifvisible'
window.ifvisible = new IfVisible(window, document)

General

// If page is visible right now
if (ifvisible.now()) {
  // Display pop-up
  openPopUp()
}

// You can also check the page status using `now` method
if (!ifvisible.now('hidden')) {
  // Display pop-up if page is not hidden
  openPopUp()
}

// Possible statuses are:
// idle: when user has no interaction
// hidden: page is not visible
// active: page is visible and user is active

Options

ifvisible
  .setIdleDuration(120) // default: 30 - Page will become idle after 120 seconds
  .setThrottleDuration(1000) // default: 500 - DOM event triggers will be throttled to avoid bogging down UI

onEvery

Set intervals that run every X seconds if the page is visible.

// If page is visible run this function on every half seconds
ifvisible.onEvery(0.5, () => {
  // ...
})

on

ifvisible.on('blur', () => {
  // ...
})

ifvisible.on('focus', () => {
  // ...
})

ifvisible.on('idle', () => {
  // ...
})

ifvisible.on('wakeup', () => {
  // ...
})

off:

ifvisible.off('idle', triggeredFunction) // will remove only triggeredFunction from being tiggered on idle
ifvisible.off('idle') // will remove all events triggered on idle

// works with other events: blur | wakeup | focus

Manually trigger status events

// will put page in a idle status
ifvisible.idle()

// will set a callback to listen - same as on('idle', () => void)
ifvisible.idle(() => {
  // ...
})

// works with other events: blur | wakeup | focus

Advanced

ifvisible.detach() // detach from DOM but keep user listeners stored within ifvisible
ifvisible.reattach() // reattach to DOM and start listening again

Browsers

This library is intended to support modern browsers. Legacy IE support (not Edge) was dropped to clean up code. Given Microsoft discontinued IE altogether, moving forward is in the best interest of maintenance. If you need legacy support, look towards the original ifvisible.js

Why fork?

The original ifvisible.js library was:

  • languishing with no updates
  • old build system
  • outdated dev depencies
  • used typescript but was not strongly typed
  • did not publish the typescript types (even though it was written in typescript)
  • burdened with legacy code everywhere
  • only a UMD module available.
  • we wanted to improve the library

In contrast this version:

  • targets modern browsers only
  • is strongly typed
  • publishes UMD and ES bundles (will publish ESM as that comes to pass as well)
  • is fully updated and maintained

NOTE: this fork was detached for the sole purpose of making new pull requests point to this repo instead of the original unmaintained parent.

License

MIT.


ifvisible's People

Contributors

serkanyersen avatar rosskevin avatar explodingcabbage avatar dependabot[bot] avatar mikesigs avatar peters avatar youngzaphod avatar fossabot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.