-
-
Notifications
You must be signed in to change notification settings - Fork 1
2. Methods
Emoji-Fallback.js provides several methods for handling emoji rendering. Below are detailed descriptions of each method available in the library.
Checks if the browser supports native emoji rendering.
-
boolean: Returns
true
if the browser supports emojis natively; otherwise, returnsfalse
.
const isSupported = emojiSupported();
console.log(`Native emoji support: ${isSupported}`);
Replaces Unicode emojis with image emojis using Twemoji if native emoji support is not available.
-
element:
Element | DocumentFragment
(optional, default:document.body
)
The DOM element to parse for emojis. This can be a specific element or the entire body. -
cdn:
string
(optional, default:https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/
)
The CDN URL for Twemoji assets. This should point to where the Twemoji images are hosted. -
className:
string
(optional, default:emoji
)
The CSS class name to apply to the replaced emoji images, allowing for custom styling.
- Promise: Resolves when the parsing and replacement are complete.
- Error: If the provided element, CDN URL, or class name is invalid.
await parseEmoji(document.getElementById('emoji-container'),
'https://your-cdn-url/',
'emoji-class');
Checks if emojis are supported and falls back to Twemoji if they are not.
-
element:
Element | DocumentFragment
(optional, default:document.body
)
The DOM element to parse for emojis. -
cdn:
string
(optional, default:https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/
)
The CDN URL for Twemoji assets. -
className:
string
(optional, default:emoji
)
The CSS class name to apply to the replaced emoji images.
- Promise: Resolves when the fallback to Twemoji is complete.
- Error: If there is an error during the fallback process.
await emojiFallback(document.getElementById('emoji-container'),
'https://your-cdn-url/',
'emoji-class');
Parses a string containing Unicode emojis and replaces them with Twemoji image emojis.
-
str:
string
The string containing the emojis to be parsed. -
cdn:
string
(optional, default:https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/
)
The CDN URL for Twemoji assets. This should point to where the Twemoji images are hosted. -
className:
string
(optional, default:emoji
)
The CSS class name to apply to the replaced emoji images.
- string: The string with emojis replaced by Twemoji image URLs.
const updatedString = parseEmojiString("Hello 🌍!", 'https://your-cdn-url/', 'emoji-class');
console.log(updatedString); // Outputs: "Hello <img class="emoji" src="https://your-cdn-url/emoji_url.png" alt="🌍" />!"