{"id":13802422,"url":"https://github.com/aweary/react-perimeter","last_synced_at":"2025-05-16T07:05:11.749Z","repository":{"id":40002937,"uuid":"85239870","full_name":"aweary/react-perimeter","owner":"aweary","description":"🚧 Create an invisible perimeter around an element and respond when its breached.","archived":false,"fork":false,"pushed_at":"2018-04-30T14:29:31.000Z","size":156,"stargazers_count":1047,"open_issues_count":8,"forks_count":27,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-10T15:53:54.660Z","etag":null,"topics":["mouse-events","perimeter","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aweary.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-16T20:43:12.000Z","updated_at":"2025-04-08T15:56:44.000Z","dependencies_parsed_at":"2022-06-26T07:07:31.649Z","dependency_job_id":null,"html_url":"https://github.com/aweary/react-perimeter","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aweary%2Freact-perimeter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aweary%2Freact-perimeter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aweary%2Freact-perimeter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aweary%2Freact-perimeter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aweary","download_url":"https://codeload.github.com/aweary/react-perimeter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254485056,"owners_count":22078767,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mouse-events","perimeter","react"],"created_at":"2024-08-04T00:01:44.163Z","updated_at":"2025-05-16T07:05:06.683Z","avatar_url":"https://github.com/aweary.png","language":"JavaScript","funding_links":[],"categories":["React","📦 Legacy \u0026 Inactive Projects"],"sub_categories":["React Components"],"readme":"# react-perimeter 🚧\n\nCreate an invisible padding around an element and respond when its breached.\n\n\n## Usage Example\n\n`react-perimeter` exports a single `Perimeter` component that will register a `mousemove` listener and calculate whether the current mouse position is within a padding.\n\nThe padding will be calculated using `getBoundingClientRect` and the `padding` prop, which lets you define \"padding\" for the perimeter.\n\n```jsx\n\u003cPerimeter\n  onBreach={this.prefetch}\n  padding={60}\u003e\n  \u003cbutton onClick={this.fetch}\u003eLoad More\u003c/button\u003e\n\u003c/Perimeter\u003e\n```\n\n`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.\n\n```jsx\n\u003cPerimeter\n  onBreach={this.prefetch}\n  padding={60}\u003e\n  {ref =\u003e (\n    \u003cbutton\n      ref={ref}\n      onClick={this.fetch}\u003eLoad More\u003c/button\u003e\n  )}\n\u003c/Perimeter\u003e\n```\nThe render callback is passed a ref callback which should be passed to the `ref` prop of the element you want to use.\n\n## Installation\n\n```\nyarn add react-perimeter\n```\n\n\n## API\n\n### Props\n\nProperty  \t| \tType\t\t|\tDefault\t\t|\t  Description\n:-----------------------|:-----------------------------|:--------------|:--------------------------------\n`padding` |   `number` | `0` | The buffer around the element that defines the padding of the perimeter\n`onBreach` | `() =\u003e void` | `undefined` | A callback to be invoked when the perimeter is breached\n`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.\n`mapListeners` | `EventListener =\u003e EventListener` | `undefined` | If provided, each event listeners (`resize`, `mousemove`) will be passed in, and the returned function will be used instead.\n\n### Debouncing or Throttling\n\nYou 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.\n\n```jsx\n\u003cPerimeter mapListeners={listener =\u003e debounce(listener, 20)}\u003e\n```\n\nBy 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.\n\n\n### Deduping Event Listeners\n\nIf you use `react-perimeter` in multiple places in your application you may want to dedupe the internal event listeners.\n \n`react-perimiter` integrates with [react-listener-provider](https://github.com/jnsdls/react-listener-provider) to make deduping easy.\n Simply `yarn add react-listener-provider` and wrap your application like this:\n \n ```jsx\n import ReactListenerProvider from 'react-listener-provider';\n \u003cReactListenerProvider\u003e\n    \u003cYourApp\u003e\n        \u003cPerimeter /\u003e\n    \u003c/YourApp\u003e\n \u003c/ReactListenerProvider\u003e\n ```\n \n Any `\u003cPerimeter\u003e` component you use inside of `\u003cReactListenerProvider\u003e` will automatically use the global event listener provided by `react-listener-provider` instead of registering its own.\n\n\n### Prefetching or Preloading\n\n`react-perimeter` shines especially bright when used to prefetch or preload other components. Here is a small example that uses [`react-loadable`](https://github.com/thejameskyle/react-loadable) and [`react-router`](https://github.com/ReactTraining/react-router) to preload a route chunk when the cursor gets near a link:\n\n```jsx\nimport React from 'react'\n// Assume this is the component returned from `react-loadable`, not the page itself\nimport OtherPage from './routes/other-page'\nimport Perimeter from 'react-perimeter'\nimport { Link } from 'react-router'\n\nconst App = () =\u003e (\n  \u003cdiv\u003e\n    \u003ch1\u003eHome Page!\u003c/h1\u003e\n    \u003cp\u003eHere's some content\u003c/p\u003e\n    \u003cPerimeter padding={100} onBreach={OtherPage.preload} once={true} \u003e\n      \u003cLink to=\"other\"\u003eOther Page\u003c/Link\u003e\n    \u003c/Perimeter\u003e\n  \u003c/div\u003e\n)\n\n```\n\n`react-loadable` provides an extremely useful static `preload` method that begins fetching the chunk for us. We pass this to `onBreach` so that\nthe 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`\nthat 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.\n\nWe can go one step further and abstract this out into its own component, `PreloadLink`:\n\n```jsx\nconst PreloadLink = ({ to, children, preload }) =\u003e (\n  \u003cPerimeter padding={100} onBreach={preload.preload} once={true}\u003e\n    \u003cLink to={to}\u003e{children}\u003c/Link\u003e\n  \u003c/Perimeter\u003e\n)\n```\n\n```jsx\n\u003cPreloadLink to=\"about\" preload={AboutPage} /\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faweary%2Freact-perimeter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faweary%2Freact-perimeter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faweary%2Freact-perimeter/lists"}