GithubHelp home page GithubHelp logo

react-event-injector's Introduction

💉 React-Event-Injector‍


Build status

Declarative React event manager, slim as 1kb. Uses standard addEventListener underneath, and able to overtake current React API Limitations.

Please - don't overuse this library, as long "React" way to attach events is far more performant, and better working with React itself.

API

There is 3 Components, exported from this package

  • EventInjector - to inject events somewhere down the tree
  • PassiveListener - to inject "passive" events, which could be quite useful to make application run smoothly.
  • ActiveListener - to inject non "passive" events, as long some events are passive by default.
  • TargetedInjector - to inject events to the specific target.

All components will add events on mount, remove on unmount, and update changed on update if pure is not set.

Why

  • 😀 to inject passive events. There is no way to inject them in "react-way".
  • 😀 to inject events where you need, without relaying on bubbling or capturing or some react details.
  • ☹️ to get native DOM event, not React.Synthetic.
  • ☹️ to work with DOM Tree, not React.Tree.

Injection API

Children as React Element

  • You may provide a single tag, as a children, or use forwardRef to forward ref to the proper target.
import {EventInjector} from 'react-event-injector';
<EventInjector onClick={event}>
 <div>It will inject onClick on me, please pass a SINGLE and HTML tag inside injector</div>
</EventInjector> 
  • Capture events are also supported
import {EventInjector} from 'react-event-injector';
<EventInjector 
    onClick={event}
    // capture events are also supported
    onKeydownCapture={event} 
    // explicity set passive:false to all events. Better set to true
    settings={{passive:false}}
>
 <div>It will inject onClick on me, please pass a SINGLE and HTML tag inside injector</div>
</EventInjector> 
  • You may nest Injectors one inside another. Injectors is the only way to combine, passive, active, and neutral event listeners.

All injectors implements EventTarget interface, and could be ref-ed by another injectors.

import {PassiveListener, EventInjector} from 'react-event-injector';
<PassiveListener onScroll={event}>
  <EventInjector onClick={event}>
    <EventInjector onKeydownCapture={event}>
      <div>It will inject onClick on me, please pass a SINGLE and HTML tag inside injector</div>
    </EventInjector>
  </EventInjector>  
</PassiveListener>

Children as RenderProp

You may provide a single tag, as a children, or use forwardRef to forward ref to the proper target.

import {EventInjector} from 'react-event-injector';
<EventInjector onClick={event}>
 { setRef => (
 <div ref={setRef}>
   It will inject onClick on me</div>
 }  
</EventInjector> 

EventInjector, ActiveListener and PassiveListener has the same API, and accept only children and any on-event, or on-event-Capture, as any HTML tag does. The difference is default value for settings.

TargetedInjector

  • Inject events to any target provided:
import {TargetedInjector} from 'react-event-injector';
<TargetedInjector 
onClick={event}
target={this.ref}
>  
  • You may use function as a target
import {TargetedInjector} from 'react-event-injector';
<TargetedInjector 
onClick={event}
target={() => document.querySelector('#element-i-need')}
>  

In the case of a function, target would be executed twice - on componentDidMount, and right after it, thus it will be able to inject events to sibling elements, not existing on mount.

Inspiration

Licence

MIT

react-event-injector's People

Contributors

thekashey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mludovici

react-event-injector's Issues

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.