All Projects → pgilad → React Page Visibility

pgilad / React Page Visibility

Licence: mit
Declarative, nested, stateful, isomorphic page visibility React component

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Page Visibility

Gitbeaker
🤖 GitLab API NodeJS library with full support of all the Gitlab API services.
Stars: ✭ 755 (+363.19%)
Mutual labels:  api, browser
Yt Player
Simple, robust, blazing-fast YouTube Player API
Stars: ✭ 576 (+253.37%)
Mutual labels:  api, browser
Mtproto Core
Telegram API JS (MTProto) client library for browser and nodejs
Stars: ✭ 242 (+48.47%)
Mutual labels:  api, browser
Marionette
Selenium alternative for Crystal. Browser manipulation without the Java overhead.
Stars: ✭ 119 (-26.99%)
Mutual labels:  api, browser
Apy
Apy is a simple client-side library for making rest api ajax calls.
Stars: ✭ 68 (-58.28%)
Mutual labels:  api, browser
Slack
🎉✨ Slack API client for Node and browsers.
Stars: ✭ 903 (+453.99%)
Mutual labels:  api, browser
Zoonavigator
Web-based ZooKeeper UI / editor / browser
Stars: ✭ 326 (+100%)
Mutual labels:  api, browser
Bus Promise
🚍 Biblioteca que busca informações em tempo real da frota de ônibus da SPTrans na cidade de São Paulo.
Stars: ✭ 49 (-69.94%)
Mutual labels:  api, browser
Slouch
A JS client for CouchDB that does the heavy lifting
Stars: ✭ 116 (-28.83%)
Mutual labels:  api, browser
Brain.js
brain.js is a GPU accelerated library for Neural Networks written in JavaScript.
Stars: ✭ 12,358 (+7481.6%)
Mutual labels:  api, browser
Webservices
CRAN WebTechnologies Task View
Stars: ✭ 160 (-1.84%)
Mutual labels:  api
Finnhub Python
Finnhub Python API Client. Finnhub API provides institutional-grade financial data to investors, fintech startups and investment firms. We support real-time stock price, global fundamentals and alternative data. https://finnhub.io/docs/api
Stars: ✭ 161 (-1.23%)
Mutual labels:  api
Vendure
A headless GraphQL ecommerce framework for the modern web
Stars: ✭ 2,961 (+1716.56%)
Mutual labels:  api
Browsertime
open source browser history page with analytics
Stars: ✭ 162 (-0.61%)
Mutual labels:  browser
Pop
Monorepo of the PoP project, including: a server-side component model in PHP, a GraphQL server, a GraphQL API plugin for WordPress, and a website builder
Stars: ✭ 160 (-1.84%)
Mutual labels:  api
Healthcheck
An simple, easily extensible and concurrent health-check library for Go services
Stars: ✭ 161 (-1.23%)
Mutual labels:  api
Tree Gateway
This is a full featured and free API Gateway
Stars: ✭ 160 (-1.84%)
Mutual labels:  api
Pttdata
Open Source, contain more than 8 million PTT Data. 提供超過 800 萬筆 PTT 文章,每天更新
Stars: ✭ 160 (-1.84%)
Mutual labels:  api
Anpylar
Python client-side web development framework
Stars: ✭ 160 (-1.84%)
Mutual labels:  browser
Branca Spec
Authenticated and encrypted API tokens using modern crypto
Stars: ✭ 163 (+0%)
Mutual labels:  api

React Page Visibility

Declarative, nested, stateful, isomorphic page visibility for React

Build Status

Motivation

Are you polling your Backend on an interval basis? Are you running animations? What do you do if your tab is no longer visible?

See more classic use-cases in MDN Page Visibility API.

Well now you can react (Pun intended) to your app being in the background and invisible by conserving bandwidth and GPU calculations with ease. Introduction React Page Visibility:

  • A React higher order component that wraps the page visibility API
  • Cross-browser support (Yes, even IE and Safari)
  • Safe fallback if browser does not support it
  • Can be used multiple times anywhere in your application without side effects
  • Lets you decide how to handle the page being invisible and turning visible again

Why use a React component and not a helper function?

Because React is cool. 'Nuff said.

But really, why not use a helper function? Because you will then need to addEventListener and removeEventListener in your component lifecycle and that gets tedious.

Also, every time you use it you will need to check if your user's browser supports it and that gets tedious too.

Instead with react-page-visibility everything is taken care of for you.

Installation

$ npm install --save react-page-visibility

Usage

A rotating carousel component that will be passed down a prop of whether to rotate the images or not based on whether page is visible.

Using the usePageVisibility hook

import React from 'react';
import { usePageVisibility } from 'react-page-visibility';

const AppContainer = () => {
    const isVisible = usePageVisibility()

    return <RotatingCarousel rotate={isVisible} />
}

Using onChange callback

import React from 'react';
import PageVisibility from 'react-page-visibility';

class AppContainer extends React.Component {
    state = {
        rotate: true
    };

    handleVisibilityChange = isVisible => {
        this.setState({ rotate: !isVisible });
    }

    render() {
        return (
            <PageVisibility onChange={this.handleVisibilityChange}>
                <RotatingCarousel rotate={this.state.rotate} />
            </PageVisibility>
        );
    }
}

Using children as function callback

import React from 'react';
import PageVisibility from 'react-page-visibility';

const AppContainer = () => {
    return (
        <PageVisibility>
            { isVisible => <RotatingCarousel rotate={isVisible} /> }
        </PageVisibility>
    );
}

API

react-page-visibility is an higher order component, you can pass to it an onChange function:

onChange(handler)

Where handler is the callback to run when the visibilityState of the document changes:

Function handler(<Boolean> isVisible, <String> visibilityState)

  • isVisible is a Boolean indicating whether document is considered visible to the user or not.
  • visibilityState is a String and can be one of visible, hidden, prerender, unloaded (if your browser supports those)

Notice: previous versions had different arguments in the handler

Or you can use function as children pattern, where children is the callback to run when the visibilityState of the document changes.

Function children(<Boolean> isVisible, <String> visibilityState)

See MDN Page Visibility API Properties overview

License

MIT © Gilad Peleg

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].