Nuxt UI Pro v1.0 is out with 50+ Vue components.

twemoji
twemoji

Twemoji module for Nuxt. Render emojis as SVG elements or PNG images.

nuxt-twemoji

nuxt-twemoji

npm version npm downloads License Tests Nuxt Modules

Twemoji module for Nuxt. Render emojis as SVG elements or PNG images

Index

Features

  • Nuxt 3 ready
  • Emoji 15.0 support
  • Render emoji by character
  • Render emoji by codepoint
  • Render emoji by definition object
  • SVG rendering by default
  • PNG image render option
  • Twitter Emoji assets from the original ex-Twitter authors (jdecked/twemoji) fork repository
  • Assets from the jsDelivr CDN
  • Multiples ways of use

Quick Setup

  1. Add nuxt-twemoji dependency to your project
# Using pnpm
pnpm add -D nuxt-twemoji

# Using yarn
yarn add --dev nuxt-twemoji

# Using npm
npm install --save-dev nuxt-twemoji
  1. Add nuxt-twemoji to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    'nuxt-twemoji'
  ]
})

That's it! You can now use nuxt-twemoji in your Nuxt app โœจ

Components

You can make use of any of these available components according to your Nuxt app needs.

NameDescriptionSSR
TwemojiRenders Twemojis by emoji characters, codepoint or definition.โœ”๏ธ
TwemojifyParses a text and replaces all emoji characters with Twemoji svg elements or png images.โœ”๏ธ
TwemojiParseWrap elements with the component and it will parse all emoji characters found with Twemoji svg or png image elements.โŒ

Usage (Twemoji)

  1. Find emojis from the recommended unicode emoji list: https://unicode.org/emoji/charts-15.0/full-emoji-list.html
  2. In the project, use the component <Twemoji emoji="" />, where emoji is the emoji character or codepoint.
  3. If you employ SSR (Server Side Rendering) in your Nuxt application, this module will inject the emoji <svg> or <img> element into the output code during your project's build/generate process. Alternatively, if SSR is not used, the emojis will dynamically render during client runtime.

Twemoji properties

PropertyRequiredDefaultType
emojiYesstring or EmojiDefinition
sizeNo1emstring
pngNofalseboolean

Rendering

Use the emoji property to render an emoji by character.

<!-- Render SVG element -->
<Twemoji emoji="๐Ÿš€" />

<!-- Resize -->
<Twemoji emoji="๐Ÿš€" size="2em" />

<!-- Render PNG <img> element -->
<Twemoji emoji="๐Ÿš€" png />

Use the emoji property to render an emoji by codepoint.

<Twemoji emoji="1F60A" />

or

<Twemoji emoji="U+1F60A" />

Use the emoji property to render an emoji by definition.

<script setup>
import { twSmilingFaceWithSmilingEyes } from 'nuxt-twemoji/emojis'
</script>

<template>
  <Twemoji :emoji="twSmilingFaceWithSmilingEyes" />
</template>

The emojis file has been generated using the self made generateEmojis.js script, which fetches emojis data from the Unicode public Emoji 15.0 file available at https://unicode.org/Public/emoji/15.0/emoji-test.txt

Definitions

The EmojiDefinition type represents objects that have these specific three string properties:

  • code represents the code associated with the emoji.
  • emoji represents the actual emoji.
  • name represents the name of the emoji.
type EmojiDefinition = {
  code: string,
  emoji: string,
  name: string
}

More examples

Check out the ๐Ÿ€ Online playground for more examples.

Twemoji Default CSS

Both the <svg> and <img> tags will have the .twemoji class assigned to them. These are the default styles, but you can add your own styles by using the class name.

.twemoji {
  display: inline-block;
  vertical-align: middle;
}

Usage (Twemojify)

This component parses a string text and replaces all emoji characters with Twemoji svg elements or png images.

  1. In the project, use the component <Twemojify text="" />, where text is a string.
  2. If you employ SSR (Server Side Rendering) in your Nuxt application, this module will inject the emoji <svg> or <img> element into the output code during your project's build/generate process. Alternatively, if SSR is not used, the emojis will dynamically render during client runtime.

Twemojify properties

PropertyRequiredDefaultType
textYesstring
pngNofalseboolean

Parser

This component uses the @twemoji/parser package by jdecked for identifying emoji entities within a string.

Use the text property to parse all the emoji characters inside a string

<!-- Replaces โค๏ธ and ๐Ÿš€ with SVG elements -->
<Twemojify text="I โค๏ธ Nuxt ๐Ÿš€" />

<!-- Replaces โค๏ธ and ๐Ÿš€ with PNG images -->
<Twemojify text="I โค๏ธ Nuxt ๐Ÿš€" png />

Twemojify Default CSS

Both the <svg> and <img> tags will have the .twemojify class assigned to them. These are the default styles, but you can add your own styles by using the class name.

These style rules make sure that parsed emojis will have the same size as the wrapper element.

.twemojify {
  height: 1em;
  width: 1em;
  margin: 0 .05em 0 .1em;
  vertical-align: -0.1em;
}

Usage (TwemojiParse)

This component will parse all emoji characters found.

  1. Wrap elements inside the <TwemojiParse> </TwemojiParse> component
  2. If you want to parse emojis with png images, use the png property with the component, otherwise it will parse svg images by default.

Note: This component has a drawback as it does not support SSR (Server-Side Rendering) because it is executed during the mounted Vue lifecycle, functioning solely on the client-side.

TwemojiParse properties

PropertyRequiredDefaultType
pngNofalseboolean

DOM parser

This component uses the @twemoji/api package by jdecked and its DOM parser api.

<!-- Replaces ๐Ÿš€ with SVG image -->
<TwemojiParse>
  <p>Nuxt Twemoji ๐Ÿš€</p>
</TwemojiParse>

<!-- Replaces ๐Ÿš€ with PNG image -->
<TwemojiParse png>
  <p>Nuxt Twemoji ๐Ÿš€</p>
</TwemojiParse>

<!-- You can wrap any amount of emojis inside the component -->
<TwemojiParse>
  <p>Nuxt Twemoji ๐Ÿš€</p>
  <div>
    <p>I โค๏ธ Nuxt ๐Ÿš€</p>
  </div>
</TwemojiParse>

TwemojiParse Default CSS

The <img> tags will have the .twemojiParse class assigned to them. These are the default styles, but you can add your own styles by using the class name.

These style rules make sure that parsed emojis will have the same size as the wrapper element.

img.twemojiParse {
  height: 1em;
  width: 1em;
  margin: 0 .05em 0 .1em;
  vertical-align: -0.1em;
}

Credits

Development

# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Release new version
npm run release