Twemoji

Emoji for everyone

README

Twitter Emoji (Twemoji) Build Status


A simple library that provides standard Unicode emoji support across all platforms.

Twemoji v14.0 adheres to the Unicode 14.0 spec and supports the Emoji 14.0 spec. _We do not support custom emoji._

The Twemoji library offers support for all Unicode-defined emoji which are recommended for general interchange (RGI).

Usage


CDN Support


The folks over at MaxCDN have graciously provided CDN support.

Use the following in the `` tag of your HTML document(s):

  1. ``` html
  2. <script src="https://twemoji.maxcdn.com/v/latest/twemoji.min.js" crossorigin="anonymous"></script>
  3. ```

This guarantees that you will always use the latest version of the library.

If, instead, you'd like to include the latest version explicitly, you can add the following tag:
  1. ``` html
  2. <script src="https://twemoji.maxcdn.com/v/14.0.2/twemoji.min.js" integrity="sha384-32KMvAMS4DUBcQtHG6fzADguo/tpN1Nh6BAJa2QqZc6/i0K+YPQE+bWiqBRAWuFs" crossorigin="anonymous"></script>
  3. ```

Download


If instead you want to download a specific version, please look at the gh-pages branch, where you will find the built assets for both our latest and older versions.

API


Following are all the methods exposed in the twemoji namespace.

twemoji.parse( ... ) V1


This is the main parsing utility and has 3 overloads per parsing type.

Although there are two kinds of parsing supported by this utility, we recommend you use DOM parsing, explained below. Each type of parsing accepts a callback to generate an image source or an options object with parsing info.

The second kind of parsing is string parsing, explained in the legacy documentation here. This is unrecommended because this method does not sanitize the string or otherwise prevent malicious code from being executed; such sanitization is out of scope.

DOM parsing


If the first argument to twemoji.parse is an HTMLElement, generated image tags will replace emoji that are inside #text nodes only without compromising surrounding nodes or listeners, and completely avoiding the usage of innerHTML.

If security is a major concern, this parsing can be considered the safest option but with a slight performance penalty due to DOM operations that are inevitably costly.

  1. ``` js
  2. var div = document.createElement('div');
  3. div.textContent = 'I \u2764\uFE0F emoji!';
  4. document.body.appendChild(div);

  5. twemoji.parse(document.body);

  6. var img = div.querySelector('img');

  7. // note the div is preserved
  8. img.parentNode === div; // true

  9. img.src;        // https://twemoji.maxcdn.com/v/latest/72x72/2764.png
  10. img.alt;        // \u2764\uFE0F
  11. img.className;  // emoji
  12. img.draggable;  // false

  13. ```

All other overloads described for string are available in exactly the same way for DOM parsing.

Object as parameter


Here's the list of properties accepted by the optional object that can be passed to the parse function.

  1. ``` js
  2.   {
  3.     callback: Function,   // default the common replacer
  4.     attributes: Function, // default returns {}
  5.     base: string,         // default MaxCDN
  6.     ext: string,          // default ".png"
  7.     className: string,    // default "emoji"
  8.     size: string|number,  // default "72x72"
  9.     folder: string        // in case it's specified
  10.                           // it replaces .size info, if any
  11.   }
  12. ```

callback


The function to invoke in order to generate image src(s).

By default it is a function like the following one:

  1. ``` js
  2. function imageSourceGenerator(icon, options) {
  3.   return ''.concat(
  4.     options.base, // by default Twitter Inc. CDN
  5.     options.size, // by default "72x72" string
  6.     '/',
  7.     icon,         // the found emoji as code point
  8.     options.ext   // by default ".png"
  9.   );
  10. }
  11. ```

base


The default url is the same as twemoji.base, so if you modify the former, it will reflect as default for all parsed strings or nodes.

ext


The default image extension is the same as twemoji.ext which is ".png".

If you modify the former, it will reflect as default for all parsed strings or nodes.

className


The default class for each generated image is emoji. It is possible to specify a different one through this property.

size

The default asset size is the same as twemoji.size which is "72x72".

If you modify the former, it will reflect as default for all parsed strings or nodes.

folder


In case you don't want to specify a size for the image. It is possible to choose a folder, as in the case of SVG emoji.

  1. ``` js
  2. twemoji.parse(genericNode, {
  3.   folder: 'svg',
  4.   ext: '.svg'
  5. });
  6. ```

This will generate urls such https://twemoji.maxcdn.com/svg/2764.svg instead of using a specific size based image.

Utilities


Basic utilities / helpers to convert code points to JavaScript surrogates and vice versa.

twemoji.convert.fromCodePoint()


For a given HEX codepoint, returns UTF-16 surrogate pairs.

  1. ``` js
  2. twemoji.convert.fromCodePoint('1f1e8');
  3. // "\ud83c\udde8"
  4. ```

twemoji.convert.toCodePoint()


For given UTF-16 surrogate pairs, returns the equivalent HEX codepoint.

  1. ``` js
  2. twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
  3. // "1f1e8-1f1f3"

  4. twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
  5. // "1f1e8~1f1f3"
  6. ```

Tips


Inline Styles


If you'd like to size the emoji according to the surrounding text, you can add the following CSS to your stylesheet:

  1. ```css
  2. img.emoji {
  3.    height: 1em;
  4.    width: 1em;
  5.    margin: 0 .05em 0 .1em;
  6.    vertical-align: -0.1em;
  7. }
  8. ```

This will make sure emoji derive their width and height from the font-size of the text they're shown with. It also adds just a little bit of space before and after each emoji, and pulls them upwards a little bit for better optical alignment.

UTF-8 Character Set


To properly support emoji, the document character set must be set to UTF-8. This can be done by including the following meta tag in the document ``

  1. ``` html
  2. <meta charset="utf-8">
  3. ```

Exclude Characters (V1)


To exclude certain characters from being replaced by twemoji.js, call twemoji.parse() with a callback, returning false for the specific unicode icon. For example:

  1. ``` js
  2. twemoji.parse(document.body, {
  3.     callback: function(icon, options, variant) {
  4.         switch ( icon ) {
  5.             case 'a9':      // © copyright
  6.             case 'ae':      // ® registered trademark
  7.             case '2122':    // ™ trademark
  8.                 return false;
  9.         }
  10.         return ''.concat(options.base, options.size, '/', icon, options.ext);
  11.     }
  12. });
  13. ```

Legacy API (V1)


If you're still using our V1 API, you can read our legacy documentation here.

Contributing


The contributing documentation can be found here.

Attribution Requirements


As an open source project, attribution is critical from a legal, practical and motivational perspective in our opinion. The graphics are licensed under the CC-BY 4.0 which has a pretty good guide on best practices for attribution.

However, we consider the guide a bit onerous and as a project, will accept a mention in a project README or an 'About' section or footer on a website. In mobile applications, a common place would be in the Settings/About section (for example, see the mobile Twitter application Settings->About->Legal section). We would consider a mention in the HTML/JS source sufficient also.

Community Projects


Twemoji Cheatsheet by @ShahriarKh: An easy-to-use cheatsheet for exploring, copying and downloading emojis!
Twemoji Amazing by @SebastianAigner: Use Twemoji using CSS classes (like Font Awesome).
Twemoji Ruby by @JollyGoodCode: Use Twemoji in Ruby.
Twemoji Utils by @gustavwilliam: Utilities for finding and downloading Twemoji source files.
Twemoji for Pencil by @Nathanielnw: Use Twemoji in Pencil.
FrwTwemoji - Twemoji in dotnet by @FrenchW: Use Twemoji in any dotnet project (C#, asp.net ...).
Emojiawesome - Twemoji for Yellow by @datenstrom: Use Twemoji on your website.
EmojiPanel for Twitter by @danielbovey: Insert Twemoji into your tweets on twitter.com.
Twitter Color Emoji font by @bderickson: Use Twemoji as your system default font on Linux & OS X.
Emojica by @xoudini: An iOS framework allowing you to replace all standard emoji in strings with Twemoji.
gwt-twemoji by @nbartels: Use Twemoji in GWT
JavaFXEmojiTextFlow by @pavlobu: A JavaFX library allowing you to replace all standard emoji in extended EmojiTextFlow with Twemoji.
Vue Twemoji Picker by @kevinfaguiar: A fast plug-n-play Twemoji Picker (+textarea for Twemoji rendering) for Vue.
[Unmaintained] Twemoji Awesome by @ellekasai: Use Twemoji using CSS classes (like Font Awesome).
EmojiOnRoku by @KasperGam: Use Twemoji on Roku!
LaTeX Twemoji by @rossel.jost: Use Twemoji in LaTeX.
PHP Twemoji by @Astrotomic: Use twemoji within your PHP website project's by replacing standard Emoji with twemoji urls.

Committers and Contributors


Justine De Caires (Twitter)
Jason Sofonia (Twitter)
Bryan Haggerty (ex-Twitter)
Nathan Downs (ex-Twitter)
Tom Wuttke (ex-Twitter)
Andrea Giammarchi (ex-Twitter)
Joen Asmussen (WordPress)
Marcus Kazmierczak (WordPress)

The goal of this project is to simply provide emoji for everyone. We definitely welcome improvements and fixes, but we may not merge every pull request suggested by the community due to the simple nature of the project.

The rules for contributing are available in the CONTRIBUTING.md file.

Thank you to all of our contributors.

License


Copyright 2019 Twitter, Inc and other contributors

Code licensed under the MIT License:

Graphics licensed under CC-BY 4.0: