Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-input-trigger

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-input-trigger

React component for handling character triggers inside textareas and input fields. 🐼

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
637
decreased by-44.46%
Maintainers
2
Weekly downloads
 
Created
Source

React Input Trigger

npm npm npm

React component for handling character triggers inside textareas and input fields. 🐼

Description

Useful for building applications that need Slack-like emoji suggestions (triggered by typing :) or Github-like user mentions (triggered by typing @).

The component provides the following hooks:

  • onStart: whenever the trigger is first activated (eg. when @ is first typed).
  • onType: when something is being typed after it's been triggered.
  • onCancel: when the trigger is canceled.

The hooks pass some meta-data such as the cursor position and/or the text that has been typed since the trigger has been activated.

reactinputtrigger

Demo

A live demo of this component can be found here.

A detailed guide on using this component to build a Github-style user mentions component can be found on CampVanilla.

Usage

Getting Started

  • Install the component
$ npm install react-input-trigger
  • Import the component from the package.
import InputTrigger from 'react-input-trigger';
  • Wrap your existing <textarea /> or <input /> element with <InputTrigger />
<InputTrigger>
  <textarea />
</InputTrigger>

Or get it in the browser directly via unpkg:

<script 
  src="https://unpkg.com/react-input-trigger@latest/build/lib/react-input-trigger.js" 
  type="text/javascript">
</script>

Component Props

<InputTrigger> can take in the following props:

trigger

This prop takes an object that defines the trigger. The object can have the following properties

  • keyCode: This is the character code that will fire the trigger.
  • shiftKey: (Optional) Set this to true if you need the shift key to be pressed along with the keyCode to start the trigger. Ignore this property if it's not required.
  • ctrlKey: (Optional) Set this to true if you need the ctrl key to be pressed along with the keyCode to start the trigger. Ignore this property if it's not required.
  • metaKey: (Optional) Set this to true if you need the cmd key to be pressed along with the keyCode to start the trigger. Ignore this property if it's not required.
<InputTrigger
  trigger={{
    keyCode: 50,
    shiftKey: true,
  }}
>

onStart

This prop takes a function that will fire whenever trigger is activated. The function is passed some meta information about the cursor's position that you can use.

<InputTrigger
  trigger={{
    keyCode: 50,
    shiftKey: true,
  }}
  onStart={(obj) => { console.log(obj); }}
>

The parameter obj contains the following meta information

{
  "hookType": "start",
  "cursor": {
    "selectionStart",
    "selectionEnd",
    "top",
    "left",
    "height"
  }
}

onCancel

This prop takes a function that will fire everytime the user presses backspace and removes the trigger from the input section. The function is passed some meta information about the cursor's position that you can use.

<InputTrigger
  trigger={{
    keyCode: 50,
    shiftKey: true,
  }}
  onCancel={(obj) => { console.log(obj); }}
>

The parameter obj contains the following meta information

{
  "hookType": "cancel",
  "cursor": {
    "selectionStart",
    "selectionEnd",
    "top",
    "left",
    "height"
  }
}

onType

This prop takes a function that will trigger everytime the user continues typing after starting the trigger. The function is passed some meta information about the cursor's position, as well as the text that the user has typed after triggering that you can use.

<InputTrigger
  trigger={{
    keyCode: 50,
    shiftKey: true,
  }}
  onType={(obj) => { console.log(obj); }}
>

The parameter obj contains the following meta information

{
  "hookType": "typing",
  "cursor": {
    "selectionStart",
    "selectionEnd",
    "top",
    "left",
    "height"
  },
  "text"
}

endTrigger

This prop takes a function that returns a function that you need to keep in your parent component. This returned method needs to be called manually by the parent component whenever you are done using the trigger and want to end the trigger.

<InputTrigger
  endTrigger={
    endTriggerHandler => {
      this.endTriggerHandler = endTriggerHandler;

      /*
        Now you can call `this.endTriggerHandler`
        anywhere inside the parent component
        whenever you want to stop the trigger!
      */
    }
  }
>

Contributing

Want to fix something, add a new feature or raise an issue? Please read the contributing guide to get started. :smile:

Contributors

Thanks goes to these wonderful people (emoji key):


Abinav Seelan

💻 📖

Aditi Mohanty

💻 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

FAQs

Package last updated on 17 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc