GithubHelp home page GithubHelp logo

avinesh2101 / react-easy-emoji Goto Github PK

View Code? Open in Web Editor NEW

This project forked from appfigures/react-easy-emoji

0.0 0.0 0.0 200 KB

Super minimal emoji rendering utility for react

License: MIT License

JavaScript 100.00%

react-easy-emoji's Introduction

React Easy Emoji ๐ŸŽ‰

react-easy-emoji MIT license on NPM react-easy-emoji on NPM Build Status react-easy-emoji total downloads on NPM react-easy-emoji monthly downloads on NPM

Super minimal emoji rendering utility for React, with built in support for the wonderful twemoji image set.

We use Easy Emoji at Appfigures for rendering millions of review cards.

Example

Basic usage

import emoji from 'react-easy-emoji'

emoji('Emojis make me ๐Ÿ˜€')
// output: ['Emojis make me ', <img src="//twemoji.maxcdn.com/2/72x72/1f604.png" ... />]

Rendering emojis in a component

import emoji from 'react-easy-emoji'

class Page extends React.Component {
	render () {
		return <p>{ emoji('Emojis make me ๐Ÿ˜€') }</p>
	}
}

Features

  • It's tiny (on purpose).
  • Simple functional API with minimal surface area and full customization hooks.
  • Lean implementation that follows the React wayโ„ข all the way down (no DOM manipulation).
  • Renders emoji from Twitter's Twemoji cdn out of the box.
  • Currently in use on high-traffic production pages.

What it doesn't do

We've consciously left out some extra features in order to keep this library as minimal as possible:

  • Doesn't parse emoji names like :smile:.
  • Doesn't parse emoticons like :).
  • Doesn't provide built-in support for emoji assets other than Twemoji, but provides hooks to use custom sets.

Installation

npm install --save react-easy-emoji

React version support

React 0.14.x, 15.x, 16.x

Common tasks

Using svg Twemoji assets instead of the default png

React Easy Emoji uses png assets from the Twemoji CDN by default. To use svg assets instead you can pass a few extra options:

function svgEmoji (input) {
	return emoji(input, {
		baseUrl: 'https://twemoji.maxcdn.com/2/svg/',
		ext: '.svg',
		size: ''
	})
}

// example output: https://twemoji.maxcdn.com/2/svg/1f600.svg

Using EmojiOne cdn assets instead of Twemoji

While there's only built-in support for Twemoji assets, using assets from other libraries is extremely easy:

function emojiOne(input) {
	return emoji(input, {
		baseUrl: '//cdnjs.cloudflare.com/ajax/libs/emojione/2.2.5/assets/png/',
		size: ''
	})
}

Rendering a custom React element instead of <img>

For full control over how emojis get rendered just provide a function as the second parameter:

function customEmoji (input) {
	return emoji(input, (code, string, key) => (
		<MyEmoji code={ code } alt={ string } key={ key } />
	))
}

API Reference

emoji(string|array[, options|renderFn]) => array

This is the only function in this library. It allows for several different levels of customization

Parameters

input: string | array

The object to be emojified. Can be a simple string or an array which mixes react elements and strings. When an array is processed, all react elements are simply skipped but still get included in the output.

Array input example

emoji(['simple string', <br />, 'string with emoji ๐Ÿป'])
// Output: ['simple string', <br />, 'string with emoji ', <img src="//twemoji.maxcdn.com/..." />]

Notice how the first two items in the array remain exactly the same, while the third gets split up.

options : object | renderFn : func

For basic customization pass in an options object to specify how image urls are constructed. The following options are available:

  • baseUrl : string - Location of emoji images. Defaults to //twemoji.maxcdn.com/2/
  • size : string - Size of emojis images to use. Notice that this is the size of the base images, not the actual size the emoji will be rendered on the page. Defaults to '72x72'. For valid sizes to use with the Twemoji image set, please visit the Twemoji project page.
  • ext : string - File extension to be appended to the end of the url. Defaults to .png.
  • protocol : string - The url protocol of the image (either http: or https:). Defaults to the protocol of the current page to prevent mismatched protocol errors. To create a url with no protocol (ie. //twemoji.maxcdn.com/...) pass an empty string ('').
  • props : object - Additional props to merge into the <img> jsx element. Defaults to null. Note that user provided props are applied last and may override any previous ones.

For complete control over the output emoji, pass a function instead of an options object, with the following signature:

(code, string, offset) => element : ReactElement

Params

  • code : string - The unicode value for the emoji. This is usually used to construct the source url.
  • string : string - The original string version of the emoji. This is usually used as the alt attribute to make copy and paste behave as expected.
  • offset : number - The zero-based position of the emoji in the original string. This is safe to use as the key attribute on the returned jsx element.

Return

This function must return a valid React element. Since the output will be placed in an array it must have a key attribute. For more information on why a key attribute is required see the React docs

Example:

emoji(string, (code, string, offset) => {
	return <MyEmoji code={ code } alt={ string } key={ offset } />
})

Inspiration

While building Appfigures Review Cards we needed a simple emoji rendering library for React. We found several but none of them checked all of our boxes. The idealist in us wanted a library that doesn't pack any unnecessary features, doesn't manipulate the DOM, and has a functional API that does things the React wayโ„ข. So with a little inspiration from this thread we decided to roll our own.

Other React emoji libraries

react-easy-emoji's People

Contributors

oztune avatar advaith1 avatar millar avatar gnestor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.