Giter Site home page Giter Site logo

fael / react-perimeter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aweary/react-perimeter

1.0 1.0 0.0 156 KB

๐Ÿšง Create an invisible perimeter around an element and respond when its breached.

License: MIT License

JavaScript 100.00%

react-perimeter's Introduction

react-perimeter ๐Ÿšง

Create an invisible padding around an element and respond when its breached.

Usage Example

react-perimeter exports a single Perimeter component that will register a mousemove listener and calculate whether the current mouse position is within a padding.

The padding will be calculated using getBoundingClientRect and the padding prop, which lets you define "padding" for the perimeter.

<Perimeter
  onBreach={this.prefetch}
  padding={60}>
  <button onClick={this.fetch}>Load More</button>
</Perimeter>

Perimeter by default will wrap its children in a span and use that to calculate the boundries. If you want to avoid the wrapping span, or you want the padding to be calculated from another element, you can use a render callback.

<Perimeter
  onBreach={this.prefetch}
  padding={60}>
  {ref => (
    <button
      ref={ref}
      onClick={this.fetch}>Load More</button>
  )}
</Perimeter>

The render callback is passed a ref callback which should be passed to the ref prop of the element you want to use.

Installation

yarn add react-perimeter

API

Props

Property Type Default Description
padding number 0 The buffer around the element that defines the padding of the perimeter
onBreach () => void undefined A callback to be invoked when the perimeter is breached
once boolean false Whether the callback should only be invoked once (for example, when prefetching some data or chunks). If true all event listeners will be removed after onBreach is called.
mapListeners EventListener => EventListener undefined If provided, each event listeners (resize, mousemove) will be passed in, and the returned function will be used instead.

Debouncing or Throttling

You may want to debounce or throttle the mousemove and resize event listeners if you've profiled your application and determined that they are noticeably affecting your performance. You can do so using the mapListeners prop, which takes a function that should accept an event listener and return a new function to be used instead.

<Perimeter mapListeners={listener => debounce(listener, 20)}>

By letting you provide the mapped listener yourself, react-perimeter gives you full control over what debounce/throttle imeplementation you wish to use and its paramaters.

Deduping Event Listeners

If you use react-perimeter in multiple places in your application you may want to dedupe the internal event listeners.

react-perimiter integrates with react-listener-provider to make deduping easy. Simply yarn add react-listener-provider and wrap your application like this:

import ReactListenerProvider from 'react-listener-provider';
<ReactListenerProvider>
   <YourApp>
       <Perimeter />
   </YourApp>
</ReactListenerProvider>

Any <Perimeter> component you use inside of <ReactListenerProvider> will automatically use the global event listener provided by react-listener-provider instead of registering its own.

Prefetching or Preloading

react-perimeter shines especially bright when used to prefetch or preload other components. Here is a small example that uses react-loadable and react-router to preload a route chunk when the cursor gets near a link:

import React from 'react'
// Assume this is the component returned from `react-loadable`, not the page itself
import OtherPage from './routes/other-page'
import Perimeter from 'react-perimeter'
import { Link } from 'react-router'

const App = () => (
  <div>
    <h1>Home Page!</h1>
    <p>Here's some content</p>
    <Perimeter padding={100} onBreach={OtherPage.preload} once={true} >
      <Link to="other">Other Page</Link>
    </Perimeter>
  </div>
)

react-loadable provides an extremely useful static preload method that begins fetching the chunk for us. We pass this to onBreach so that the preloading begins as soon as the mouse is within 100 pixels of the Link component. We also pass in the once prop to tell react-perimeter that we only want to respond to the first breach. This means that, after the preload request has been issued, the listeners will be deregistered, removing any unneeded overhead.

We can go one step further and abstract this out into its own component, PreloadLink:

const PreloadLink = ({ to, children, preload }) => (
  <Perimeter padding={100} onBreach={preload.preload} once={true}>
    <Link to={to}>{children}</Link>
  </Perimeter>
)
<PreloadLink to="about" preload={AboutPage} />

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.