Code Monkey home page Code Monkey logo

is-emoji-supported's Introduction

๐Ÿฆ„ is-emoji-supported

is-emoji-supported is a library allowing you to detect if the running device supports the specified emoji.

No dependency license: MIT lint e2e

As of March 2020, the Unicode Standard includes a total of 3 304 emojis. The latest version introduced 117 new ones and vendors have troubles implementing them. In fact there are no operating system supporting all of them. Therefore there is a need to know if a specified emoji isn't supported by the browser to fallback to an image.


๐Ÿš€ Installation

Install with yarn:

$ yarn add is-emoji-supported

Or install using npm:

$ npm i is-emoji-supported

โณ Running the tests

$ npm test

๐Ÿ“– Examples

Basic usage

The most basic usage is to use the function directly to detect is the current device support the emoji.

import {isEmojiSupported} from 'is-emoji-supported';

if (isEmojiSupported('๐Ÿฆ„')) {
  alert('Houra ๐Ÿฆ„ is supported');
} else {
  alert('No support for unicorn emoji yet');
}

Usage with your own cache handler

This library is doing pixel comparison to determine if an emoji is supported. This check can be slow so there is a memory cache implemented. For some reasons you may want to use your own cache implementation to store the result in either localStorage, IndexedDB or anything else for persistent cache. You only need to match the Map interface.

import {setCacheHandler} from 'is-emoji-supported';

const key = 'emoji-cache';
const cache = JSON.parse(localStorage.getItem(key) || {});

setCacheHandler({
  has: (unicode: string) => unicode in cache,
  get: (unicode: string) => cache[unicode],
  set: (unicode: string, supported: boolean) => {
    cache[unicode] = supported;
    localStorage.setItem(key, JSON.stringify(cache));
  }
});

Fallback to images

In most of the cases, you will want to fallback to images to handle unsupported emojis. The best way for this is to build an object with a fallback to all supported images. You can build your own or use the one given by JoyPixel, Twemoji or others services.

import React from 'react';
import {isEmojiSupported} from 'is-emoji-supported';

const emojiMap = {
  '๐Ÿฆ„': {
    alt: 'unicorn',
    src: '/images/unicorn.png'
  },
  ...
};

export const Emoji = ({ unicode }) => {
  const attrs = emojiMap[unicode];

  return !attrs ? null : isEmojiSupported(unicode) ? (
    <span role="img" aria-label={attrs.alt}>
      {unicode}
    </span>
  ) : (
    <img {...attrs} />
  );
};

is-emoji-supported's People

Contributors

dependabot[bot] avatar guillaumepn avatar muzishell avatar vthibault avatar

Watchers

 avatar  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.