react-perimeter

? Create an invisible perimeter around an element and respond when its breached.

Github stars Tracking Chart

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} />

Main metrics

Overview
Name With Owneraweary/react-perimeter
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2017-03-16 20:43:12
Pushed At2018-04-30 14:29:31
Last Commit At2017-09-15 09:22:58
Release Count4
Last Release Namev0.3.1 (Posted on 2017-04-17 00:30:19)
First Release Namev0.1.0 (Posted on 2017-03-16 20:50:41)
用户参与
Stargazers Count1k
Watchers Count8
Fork Count27
Commits Count37
Has Issues Enabled
Issues Count10
Issue Open Count8
Pull Requests Count11
Pull Requests Open Count0
Pull Requests Close Count4
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private