withspectrum/slate-mentions

Name: slate-mentions

Owner: Spectrum

Description: Add support for mentions to your Slate editor.

Created: 2017-05-06 18:49:32.0

Updated: 2018-02-04 10:46:35.0

Pushed: 2017-11-17 19:23:31.0

Homepage: null

Size: 125

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

slate-mentions

WE'VE SWITCHED TO DRAFT.JS, SO THIS REPO IS NO LONGER MAINTAINED! If you want to develop this further please fork the repo and start working it. When you've made substantial improvements ping me (@mxstbr) on Twitter and I'd be happy to link to your fork from here.

If this is your first time in this repo, this project was never more than a proof of concept/alpha version. It doesn't work well, I'd not recommend using it.

Click if you want to see the old documentation

Add support for mentions to your Slate editor.

Installation
install --save slate-mentions
Usage
TL;DR:
rt { Editor } from 'slate';
rt MentionsPlugin from 'slate-mentions';

t mentions = MentionsPlugin({
ntion: (props) => <span {...props.attributes}>{props.children}</span>,
ggestions: (props) => (
<div>
  {props.suggestions.map((suggestion, index) => (
    <span
      style={{
        background: index === props.selected ? 'red' : 'grey',
      }}
    >
      {suggestion}
    </span>
  ))}
</div>



t suggestions = ['max', 'brian', 'bryn'];

teSuggestions = (text) => {
is.setState({
suggestions: suggestions.filter(suggestion => suggestion.indexOf(text) > -1),
;


tor
ugins={[mentions]}
ggestions={suggestions}
MentionSearch={this.updateSuggestions}

The plugin has two required options:

  • Mention: The component to render a mention mark in the editor. This gets two props (attributes and children), and props.attributes must be attached to the DOM node. (just like any other Slate mark)
  • Suggestions: The component to render the suggestions as a list. This is already in a portal that's positioned next to the mention. This component gets three props: suggestions (the list of suggestions), selected (the index of the currently selected mention via keyboard shortcuts) and mention. (the search text)

After passing these two options to the plugin and adding it to the plugins array you have to pass two special props to the Slate Editor component:

  • suggestions: A list of suggestions to show in the Suggestions. Has to be an array.
  • onMentionSearch: A function that filters/gets the suggestions based on the input from the user.

onMentionSearch can be asynchronous if you need to fetch data from a server. To add a loading state set suggestions to null temporarily:

teSuggestions = (text) => {
 Tell slate-mentions you're currently loading suggestions
is.setState({
suggestions: null
;
 Fetch the suggestions
tch(`api.com/v1/people?filter=${text}`)
.then(possibleMentions => {
  // Set the state
  this.setState({
    suggestions: possibleMentions,
  })
})


License

Licensed under the MIT License, Copyright ©? 2017 Maximilian Stoiber. See LICENSE.md for more information.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.