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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@builtview/use-input-trigger

v2.0.3

Published

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

Downloads

73

Readme

React Input Trigger

npm license downloads size

deps peer-deps

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💻 📖 | Adam Goldman💻 | | :---: | :---: | :---: |

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