Skip to content

Detect if the current attached device support the specified emoji.

License

Notifications You must be signed in to change notification settings

JacobNWolf/is-emoji-supported

 
 

Repository files navigation

🦄 @jacobwolf/is-emoji-supported

npm npm

No dependency license: MIT

A lightweight library to detect emoji support in browsers and provide fallbacks.

This is a maintained fork of the original koala-interactive/is-emoji-supported package. It fixes the original package's source map issues and adds improvements in emoji detection and fallback handling.

Features

  • Detect emoji support across different browsers and devices
  • Provide fallback options for unsupported emojis
  • Lightweight with zero dependencies
  • Customizable caching for improved performance

📖 Table of Contents

🚀 Installation

Install with yarn:

$ yarn add @jacobwolf/is-emoji-supported

Install using npm:

$ npm install @jacobwolf/is-emoji-supported

Install using pnpm:

$ pnpm add @jacobwolf/is-emoji-supported

Install using bun:

$ bun add @jacobwolf/is-emoji-supported

🖥️ How to use

Basic usage

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

import { isEmojiSupported } from "@jacobwolf/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 "@jacobwolf/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 '@jacobwolf/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} />
  );
};

⏳ How to test

$ npm test

🤝 How to contribute

This project is a fork of the original koala-interactive/is-emoji-supported package.

  • Fork the project on GitHub

  • Clone your forked repository:

    $ git clone https://github.com/your-username/is-emoji-supported.git
    $ cd is-emoji-supported
    
  • Install dependencies:

    $ npm install
    
  • Create a new branch from main:

    $ git checkout -b contribution/fix/your-github-username
    

    OR

    $ git checkout -b contribution/improvement/your-github-username
    
  • Make your changes and commit them with clear, descriptive messages

  • Add or update tests as necessary

  • Ensure all tests pass:

    $ npm test
    
  • Push your changes to your fork:

    $ git push origin your-branch-name
    
  • Open a pull request on the original repository

License

Original work Copyright (c) 2023 Koala-Interactive.

Modified work Copyright (c) 2024 Jacob Wolf.

This project is MIT licensed.

About

Detect if the current attached device support the specified emoji.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 59.7%
  • JavaScript 40.3%