Categories: CSSScriptWeb Design

Replace Emojis with Crisp SVG Images with Nomoji JS Library

Nomoji is a JavaScript library that replaces native text emojis with high-quality SVG images.

It uses Google’s Noto Emoji library to ensure consistent rendering across all platforms and devices. You just pass a string to the library, and it returns HTML with embedded SVG emoji elements instead of native emoji characters.

Table of Contents

Toggle

Features:

  • HTML Sanitization: Built-in protection against XSS attacks by automatically escaping dangerous HTML characters in processed text.
  • Customizable Path Prefix: Configure the SVG directory location to match your project structure and deployment setup.
  • CSS Styling Control: Apply custom styles to all emoji images through a single .emoji class selector.
  • Minimal Dependencies: Pure JavaScript implementation with no external runtime dependencies required.
  • Comprehensive Coverage: Includes 3700+ SVG files covering the complete Unicode emoji specification, including country flags and complex emoji sequences.

How To Use It:

1. Install nomoji and import it into your project.

# NPM
$ npm install nomoji
import nomoji from 'nomoji';

2. The nomoji package includes a folder named svg containing all the emoji images. You need to copy this folder from node_modules/nomoji/ and place it in a publicly accessible directory on your web server.

For example, you might place it in your public or static assets folder.

3. Call the nomoji function with a text string containing emojis:

The function returns an HTML string with emoji characters replaced by img tags. Each emoji is converted to its unicode hex codepoint, which maps to the corresponding SVG filename.

const result = nomoji("Hello world! 😃 This is a test. 💻");
Hello world! <img draggable="false" class="emoji" src="svg/1f603.svg"> This is a test. <img draggable="false" class="emoji" src="svg/1f4bb.svg"></p>

4. The nomoji function accepts two optional parameters for more control.

  • prefix (string, default: ''): Defines a path prefix for the SVG image sources. This is useful if you placed the svg folder in a subdirectory.
  • disableSanitation (boolean, default: false): By default, nomoji sanitizes characters like &lt; and &gt; to prevent them from being rendered as HTML. Set this to true only if you trust the input source and need to render HTML mixed with emojis.
nomoji(text, [prefix], [disableSanitation])

5. The default styles make emojis match your text’s font size. You can override these properties to adjust sizing, spacing, or add effects like filters or transforms.

.emoji {
  width: 1em;
  height: 1em;
  vertical-align: -0.1em;
  margin: 0 0.05em;
}

The post Replace Emojis with Crisp SVG Images with Nomoji JS Library appeared first on CSS Script.

rssfeeds-admin

Share
Published by
rssfeeds-admin

Recent Posts

The $23 Lisen 10,000mAh MagSafe Power Bank Can Charge Both Your iPhone and Apple Watch

Looking for a dual-purpose power bank that can charge both your iPhone and your Apple…

45 minutes ago

The Punisher: One Last Kill ‘CGI’ Shot Was Mocked for Looking Like a GTA Cutscene, but It Turns Out It Was Done for Real

The Punisher: One Last Kill has been the subject of online mockery following a wonky-looking…

45 minutes ago

Warhammer 40,000: Mechanicus II Finally Has a Release Date, Timed to Launch Alongside the Warhammer Skulls Reveal Show

Warhammer 40,000: Mechanicus II launches on May 21 — alongside the Warhammer Skulls video game…

2 hours ago

Star Fox, Zelda: Ocarina of Time Switch 2 Leaks Are a ‘Worst-Case Scenario’ for Nintendo, Former Staff Say

Former Nintendo staff have branded the recent leak of upcoming Switch 2 game details as…

2 hours ago

Today’s Best Deals: Final Fantasy I-VI Collection, Shell Oil Gift Card, and Yakuza Kiwami 1+2

Want a few new video games? Well, there are some incredible deals on Switch and…

2 hours ago

Microsoft Teams Flaw Allows Hackers to Launch Spoofing Attacks

A newly disclosed security vulnerability in Microsoft Teams could allow attackers to spoof local devices,…

2 hours ago

This website uses cookies.