npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-warpgate

v0.1.2

Published

Higher order component to warp imperative method calls down the tree

Readme

React Warpgate

npm Build Status

This tiny decorator wraps a component tree and warps the imperative method calls down the chain. It uses the React's ref callback feature.

To use this package you would need to be fairly familiar with React itself and its ref feature.

Installation

You can install this package with the following command:

npm install react-warpgate

Examples

These examples demonstrate how you can use this library:

Simple Usage

You can pass warpgate a single method name and it will warp that.

import React from 'react';
import ReactDOM from 'react-dom';
import warpgate from 'react-warpgate';

// "target" is the default function prop warpgate passes down.
const TextBox = ({target, style}) => <input type="text" ref={target} style={style} />;

// There is no way to access the input nested inside the div and call focus on it.
const CuteTextBox = (props) => <div><TextBox {...props} style={{color: 'pink'}}/></div>;

// warpgate to rescue!
const WarpedCuteTextBox = warpgate('focus')(CuteTextBox);

// Now you can do this:
const instance = ReactDOM(<WarpedCuteTextBox />, document.getElementById('container'));
instance.focus();

Please note that, calling these methods on unmounted elements is an error and will throw.

Manual Binding

In case you wish to bind target without having to use ref you can manually bind the target to your component.

import React from 'react';
import ReactDOM from 'react-dom';
import warpgate from 'react-warpgate';

class ManuallyBoundComponent extends React.Component<any, any> {
  componentDidMount() {
    this.props.target(this);
  }

  componentWillUnmount() {
    this.props.target(null);
  }

  focus() {
    // Some loigc to manage focus.
    this.refs.input.focus();
  }

  render() {
    return <input type="text" ref="input" />;
  }
}

const WarpedManuallyBoundComponent = warpgate('focus')(CuteTextBox);

const instance = ReactDOM(<WarpedManuallyBoundComponent />, document.getElementById('container'));
// Calls ManuallyBoundComponent's focus.
instance.focus();

Make sure you call target with null in componentWillUnmount or you'll have memory leak.

Multiple Methods

You can use warpgate to warp more than one function.

import React from 'react';
import ReactDOM from 'react-dom';
import warpgate from 'react-warpgate';

const TextBox = ({target, style}) => <input type="text" ref={target} style={style} />;
const CuteTextBox = (props) => <div><TextBox {...props} style={{color: 'pink'}}/></div>;

const WarpedCuteTextBox = warpgate(['focus', 'blur', 'click', 'select'])(CuteTextBox);

const instance = ReactDOM(<WarpedCuteTextBox />, document.getElementById('container'));
instance.focus();
instance.blur();
instance.click();
instance.select();

Multiple Targets

You can use warpgate to warp more than one function, or for more than one target.

import React from 'react';
import ReactDOM from 'react-dom';
import warpgate from 'react-warpgate';

const TextBox = ({target, style}) => <input type="text" ref={target} style={style} />;

class MyComponent extends React.Component {
  sayHello() {
    alert('hello');
  }

  render() {
    return this.props.children;
  }
}

const CuteTextBox = (props) => (
  <MyComponent ref={props.myTarget}>
    <TextBox {...props} style={{color: 'pink'}}/>
  </MyComponent>
);

const WarpedCuteTextBox = warpgate({
  target: ['focus', 'blur', 'click', 'select'],
  myTarget: 'sayHello',
})(CuteTextBox);

const instance = ReactDOM(<WarpedCuteTextBox />, document.getElementById('container'));
// Called on the input element.
instance.focus();
instance.blur();
instance.click();
instance.select();
// Called on MyComponent.
instance.sayHello();

Alias

To warp common methods from multiple targets it is possible to alias methods. This can also be useful to provide compatibility when renaming methods since many aliases can point to the same method. To alias a method simple import alias and put alias(method: string, alias: string) wherever you would normally put a method name.

import React from 'react';
import ReactDOM from 'react-dom';
import warpgate, {alias} from 'react-warpgate';

class MultiTargetComponent extends React.Component {
  sayHello() {
    alert('hello');
  }

  render() {
    return (
      <div>
        <input ref={this.props.target1} type="text"/>
        <input ref={this.props.target2} type="text"/>
      </div> 
    );
  }
}

const SomeHOC = (props) => <MultiTargetComponent {...props}/>;

const WarpedSomeHOC = warpgate({
  target1: [
    'focus',
    'blur',
    alias('click', 'clickFirst'),
    alias('click', 'clickMe'),
    'select',
  ],
  target2: alias('focus', 'focusSecond'),
})(SomeHOC);

// Also possible: 
// warpgate(alias('click', 'tap'))
// warpgate(['focus', alias('select', 'selectMe')])

const instance = ReactDOM(<WarpedSomeHOC />, document.getElementById('container'));

// All called on the first input.
instance.focus();
instance.blur();
// Both will call click on the first input.
instance.clickFirst();
instance.clickMe();
// Called on the second input.
instance.focusSecond();

Typings

The typescript type definitions are also available and are installed via npm.

License

This project is licensed under the MIT license.