The documentation currently represents the 2.0.0-rc2 release.
For troubleshooting please checkout theFAQ

Sticker

Supported Environment

  • Desktop: Yes
  • Mobile: Yes
  • Screen-reader: Yes

Getting Started

npm install [email protected] --save
npm install [email protected] --save
gettingStarted.js
// It is important to import the Editor which accepts plugins.
import Editor from 'draft-js-plugins-editor'; // eslint-disable-line import/no-unresolved
import createStickerPlugin from 'draft-js-sticker-plugin'; // eslint-disable-line import/no-unresolved
import React from 'react';
import { fromJS } from 'immutable';

// Creates an Instance. Passing in an Immutable.js List of stickers as an
// argument.
const stickers = fromJS({
  data: {
    'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b': {
      id: 'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b',
      url: '../images/unicorn-4.png',
    },
    'adec3f13-823c-47c3-b4d1-be4f68dd9d6d': {
      id: 'adec3f13-823c-47c3-b4d1-be4f68dd9d6d',
      url: '../images/unicorn-1.png',
    },
  },
});

const stickerPlugin = createStickerPlugin({ stickers });

// The Editor accepts an array of plugins. In this case, only the stickerPlugin
// is passed in, although it is possible to pass in multiple plugins.
const MyEditor = ({ editorState, onChange }) => (
  <Editor
    editorState={editorState}
    onChange={onChange}
    plugins={[stickerPlugin]}
  />
);

export default MyEditor;

Importing the default styles

The plugin ships with a default styling available at this location in the installed package:  node_modules/draft-js-sticker-plugin/lib/plugin.css

Webpack Usage

  • 1. Install Webpack loaders:  npm i style-loader css-loader --save-dev
  • 2. Add the below section to Webpack config (if your config already has a loaders array, simply add the below loader object to your existing list.
    module.exports = {
      module: {
        loaders: [
          {
            test: /plugin\.css$/,
            loaders: [
              'style', 'css',
            ],
          },
        ],
      },
    };
    
  • 3. Add the below import line to your component to tell Webpack to inject the style to your component.
    import 'draft-js-sticker-plugin/lib/plugin.css'; // eslint-disable-line import/no-unresolved
    
  • 4. Restart Webpack.

Browserify Usage

Please help, by submiting a Pull Request to the documentation.

Configuration Parameters

themeObject of CSS classes with the following keys.
sticker: CSS class for sticker.
stickerImage: CSS class for sticker image tag.
stickerRemoveButton: CSS class for sticker remove button.
select: CSS class for sticker select.
selectPopover: CSS class for sticker select popup.
selectClosedPopover: CSS class for sticker select close button.
selectBottomGradient: CSS class for sticker select bottom gradient.
selectButton: CSS class for button to open sticker select.
selectPressedButton: CSS class for pressed state of button to open sticker select.
selectStickerList: CSS class for sticker select list.
selectSticker: CSS class for sticker select.
selectStickerImage: CSS class for sticker select image.
stickersImmutable.js List of stickers.
selectButtonContentContent of button which opens select sticker drop-down. (Default content is ☺)
attachRemoveButtonFlag to attach or detach a remove button to stickers. (Default value is true)

Simple Example


SimpleStickerEditor.js
import React, { Component } from 'react';
import { EditorState } from 'draft-js';
import Editor from 'draft-js-plugins-editor'; // eslint-disable-line import/no-unresolved
import createStickerPlugin from 'draft-js-sticker-plugin'; // eslint-disable-line import/no-unresolved
import editorStyles from './editorStyles.css';
import stickers from './stickers';

const stickerPlugin = createStickerPlugin({ stickers });
const plugins = [stickerPlugin];
const StickerSelect = stickerPlugin.StickerSelect;

export default class SimpleMentionEditor extends Component {

  state = {
    editorState: EditorState.createEmpty(),
  };

  onChange = (editorState) => {
    this.setState({
      editorState,
    });
  };

  focus = () => {
    this.editor.focus();
  };

  render() {
    return (
      <div>
        <div className={editorStyles.editor} onClick={this.focus}>
          <Editor
            editorState={this.state.editorState}
            onChange={this.onChange}
            plugins={plugins}
            ref={(element) => { this.editor = element; }}
          />
        </div>
        <div className={editorStyles.options}>
          <StickerSelect editor={this} />
        </div>
      </div>
    );
  }
}
stickers.js
import { fromJS } from 'immutable';

const stickers = fromJS({
  data: {
    'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b': {
      id: 'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b',
      url: '../images/unicorn-4.png',
    },
    'adec3f13-823c-47c3-b4d1-be4f68dd9d6d': {
      id: 'adec3f13-823c-47c3-b4d1-be4f68dd9d6d',
      url: '../images/unicorn-1.png',
    },
    'e14b5a20-1025-4952-b731-41cd4b118ba0': {
      id: 'e14b5a20-1025-4952-b731-41cd4b118ba0',
      url: '../images/unicorn-6.png',
    },
    '659a0dbf-5f85-4f32-999d-eb9ba6b0f417': {
      id: '659a0dbf-5f85-4f32-999d-eb9ba6b0f417',
      url: '../images/unicorn-2.png',
    },
    'fab0ae95-ae95-4775-b484-72c290437602': {
      id: 'fab0ae95-ae95-4775-b484-72c290437602',
      url: '../images/unicorn-5.png',
    },
    '71853190-8acd-4d3b-b4fd-63f7b0648daa': {
      id: '71853190-8acd-4d3b-b4fd-63f7b0648daa',
      url: '../images/unicorn-3.png',
    },
  },
});

export default stickers;
editorStyles.css
.editor {
  box-sizing: border-box;
  border: 1px solid #ddd;
  cursor: text;
  padding: 16px;
  border-radius: 2px;
  margin-bottom: 2em;
  box-shadow: inset 0px 1px 8px -3px #ABABAB;
  background: #fefefe;
}

.editor :global(.public-DraftEditor-content) {
  min-height: 140px;
}

.options {
  margin-bottom: 20px;
}

Themed Sticker Example


CustomStickerEditor.js
import React, { Component } from 'react';
import { EditorState } from 'draft-js';
import Editor from 'draft-js-plugins-editor'; // eslint-disable-line import/no-unresolved
import createStickerPlugin from 'draft-js-sticker-plugin'; // eslint-disable-line import/no-unresolved
import editorStyles from './editorStyles.css';
import stickers from './stickers';

const stickerPlugin = createStickerPlugin({ stickers });
const plugins = [stickerPlugin];
const StickerSelect = stickerPlugin.StickerSelect;

export default class CustomStickerEditor extends Component {

  state = {
    editorState: EditorState.createEmpty(),
  };

  onChange = (editorState) => {
    this.setState({
      editorState,
    });
  };

  focus = () => {
    this.editor.focus();
  };

  render() {
    return (
      <div>
        <div className={editorStyles.editor} onClick={this.focus}>
          <Editor
            editorState={this.state.editorState}
            onChange={this.onChange}
            plugins={plugins}
            ref={(element) => { this.editor = element; }}
          />
        </div>
        <div className={editorStyles.options}>
          <StickerSelect editor={this} />
        </div>
      </div>
    );
  }
}
stickers.js
import { fromJS } from 'immutable';

const stickers = fromJS({
  data: {
    'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b': {
      id: 'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b',
      url: '../images/unicorn-4.png',
    },
    'adec3f13-823c-47c3-b4d1-be4f68dd9d6d': {
      id: 'adec3f13-823c-47c3-b4d1-be4f68dd9d6d',
      url: '../images/unicorn-1.png',
    },
    'e14b5a20-1025-4952-b731-41cd4b118ba0': {
      id: 'e14b5a20-1025-4952-b731-41cd4b118ba0',
      url: '../images/unicorn-6.png',
    },
    '659a0dbf-5f85-4f32-999d-eb9ba6b0f417': {
      id: '659a0dbf-5f85-4f32-999d-eb9ba6b0f417',
      url: '../images/unicorn-2.png',
    },
    'fab0ae95-ae95-4775-b484-72c290437602': {
      id: 'fab0ae95-ae95-4775-b484-72c290437602',
      url: '../images/unicorn-5.png',
    },
    '71853190-8acd-4d3b-b4fd-63f7b0648daa': {
      id: '71853190-8acd-4d3b-b4fd-63f7b0648daa',
      url: '../images/unicorn-3.png',
    },
  },
});

export default stickers;
editorStyles.css
.editor {
  box-sizing: border-box;
  border: 1px solid #ddd;
  cursor: text;
  padding: 16px;
  border-radius: 2px;
  margin-bottom: 2em;
  box-shadow: inset 0px 1px 8px -3px #ABABAB;
  background: #fefefe;
}

.editor :global(.public-DraftEditor-content) {
  min-height: 140px;
}

.options {
  margin-bottom: 20px;
}