Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

aweary/react-perimeter

Repository files navigation

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

Usage Example

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

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

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

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

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

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

Installation

yarn add react-perimeter

API

Props

PropertyTypeDefaultDescription
paddingnumber0The buffer around the element that defines the padding of the perimeter
onBreach() => voidundefinedA callback to be invoked when the perimeter is breached
oncebooleanfalseWhether the callback should only be invoked once (for example, when prefetching some data or chunks). If true all event listeners will be removed afteronBreach is called.
mapListenersEventListener => EventListenerundefinedIf 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 themousemove andresize event listeners if you've profiled your application and determined that they are noticeably affecting your performance. You can do so using themapListeners prop, which takes a function that should accept an event listener and return a new function to be used instead.

<PerimetermapListeners={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 usereact-perimeter in multiple places in your application you may want to dedupe the internal event listeners.

react-perimiter integrates withreact-listener-provider to make deduping easy.Simplyyarn add react-listener-provider and wrap your application like this:

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

Any<Perimeter> component you use inside of<ReactListenerProvider> will automatically use the global event listener provided byreact-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 usesreact-loadable andreact-router to preload a route chunk when the cursor gets near a link:

importReactfrom'react'// Assume this is the component returned from `react-loadable`, not the page itselfimportOtherPagefrom'./routes/other-page'importPerimeterfrom'react-perimeter'import{Link}from'react-router'constApp=()=>(<div><h1>Home Page!</h1><p>Here's some content</p><Perimeterpadding={100}onBreach={OtherPage.preload}once={true}><Linkto="other">Other Page</Link></Perimeter></div>)

react-loadable provides an extremely useful staticpreload method that begins fetching the chunk for us. We pass this toonBreach so thatthe preloading begins as soon as the mouse is within100 pixels of theLink component. We also pass in theonce prop to tellreact-perimeterthat 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:

constPreloadLink=({ to, children, preload})=>(<Perimeterpadding={100}onBreach={preload.preload}once={true}><Linkto={to}>{children}</Link></Perimeter>)
<PreloadLinkto="about"preload={AboutPage}/>

About

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

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp