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

react-mentionable

v0.1.3

Published

A component that allows custom mentions of people or tags

Downloads

34

Readme

React Mentionable

React Mentionable is a react component that allows for inlining mentions or tag labels in a text field. It supports multiple tags triggered by a configured trigger character. onChange will return an object with text and markup.

Getting started

npm i react-mentionable

Usage

React Mentionable is an uncontrolled input, so to clear the input from say a submit button you'll need to create a ref and pass it as the ref prop of the ReactMentionable component that will give you access to the contenteditable div that you can manually clear

The onChange event will return both plain text and markup text, the markup text will provide you the string value whereas the mentions will be formatted as __@[label](value)__. The @ symbol being the trigger used for the given mention. You can then manipulate that formatted mention to be whatever you'd like, a link for example.

The mention suggestions can either be an array of {label, value, any} pairs or an async function that returns a promise that resolves to the same structure.

import ReactMentionable from 'react-mentionable'
...
const fieldRef = useRef()
const [fieldValue, setFieldValue] = useState('')

const apiCall = debounce((resolve: Function) => {
  window.setTimeout(() => {
    resolve([{
      label: 'Albert Einstein',
      value: '/people/albert-einstein'
    }, {
      label: 'Elon Musk',
      value: '/people/elon-musk'
    }]) 
  }, 200)
}, 100)


const fetchSuggestions = async (searchStr: string): Promise<Array<Suggestion>> => {
  return await new Promise((resolve) => {
    apiCall(resolve) 
  })
}

const mentions = [{
  trigger: '@',
  highlightClassName: 'mentionHighlight',
  mentionClassName: 'mention',
  suggestions: [
    { label: 'Elon Musk', value: '/elonmusk' },
    { label: 'Mike Tyson', value: '/miketyson' },
    { label: 'Albert Einstein', value: '/alberteinstein' },
    { label: 'Richard Feynman', value: '/rfeynman ' },
    { label: 'Nikola Tesla', value: '/nikolatesla' }
  ]}, {
  trigger: '#',
  highlightClassName: 'tagHighlight',
  mentionClassName: 'tag',
  suggestions: (searchStr) => fetchSuggestions(searchStr)
}]
<ReactMentionable
  ref={fieldRef}
  autoFocus={true}
  onChange={({ text, __html, markup }) => {
    setFieldValue(markup)
  }}
  defaultValue={''}
  placeHolder='Write away'
  inputClassName='editor-class'
  suggestionsClassName='suggestions'
  mentions={mentions}
/>
<button
  onClick={() => {
    // submit fieldValue and clear field
    fieldRef.current.innerHTML = ''
  }}
>
  Submit
</button>

Why

I really appreciate the work done by react-mentions, however I found their solution didn't work for my use cases. Primarily the inability to color the mention text. In react-mentions you have a textarea field layered atop a div that adds markup for the mention, so while you can add a background or border you can't color the actual text as the textarea text overlaps it. React mentionable uses a contenteditable div whereas you can style the mentions however you'd like. It also doesn't have the same issues with syncing the textarea overlay that react-mentions does.