Example of Insert Emoticons in ASP.NET MVC Rich Text Editor Control
This sample demonstrates how to insert an emoji into the content of a Rich Text Editor. First, click the emoji button in the toolbar. This will open the emoji picker popup. Next, click on the desired emoji in the picker to insert it into the editor.
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "emojiEditor",
Name = "Emoji",
Height = "280px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "Emoticons", Text = "☺", TooltipText = "Insert an emoticon", OnClick = "RteEmoji.open" }
},
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "emojiEditor",
Name = "Emoji",
Height = "280px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "Emoticons", Text = "☺", TooltipText = "Insert an emoticon", OnClick = "RteEmoji.open" }
},
Value = (string)ViewData["RTEValue"]
})
@using (Html.BeginUiDialog(new UiDialogOptions { Id = "emojiDialog", Header = "Emoticons", Width = "340px", ShowCloseIcon = true, IsModal = true }))
{
<div class="rte-emoji" role="group" aria-label="Emoticons">
@foreach (var e in new[] { new[] { "😀", "Grinning" }, new[] { "😂", "Tears of joy" }, new[] { "😊", "Smiling" }, new[] { "😍", "Heart eyes" }, new[] { "🤔", "Thinking" }, new[] { "😎", "Sunglasses" },
new[] { "👍", "Thumbs up" }, new[] { "👏", "Clapping" }, new[] { "🙌", "Raised hands" }, new[] { "🎉", "Party" }, new[] { "🚀", "Rocket" }, new[] { "✅", "Done" },
new[] { "❤️", "Heart" }, new[] { "🔥", "Fire" }, new[] { "⭐", "Star" }, new[] { "☕", "Coffee" }, new[] { "📌", "Pin" }, new[] { "💡", "Idea" } })
{
<button type="button" class="btn btn-outline-secondary btn-sm" data-rte-emoji="@e[0]" aria-label="@e[1]" title="@e[1]">@e[0]</button>
}
</div>
}
<style>
.rte-emoji { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; font-size: 20px; }
</style>
<script>
window.RteEmoji = (function () {
'use strict';
/** The tool's OnClick: opens the list of emoticons (the editor keeps where the caret was). */
function open() {
window.Lumen.get('emojiDialog').show();
}
document.addEventListener('click', function (event) {
var button = event.target.closest('[data-rte-emoji]');
if (!button) { return; }
window.Lumen.get('emojiDialog').hide();
window.Lumen.get('emojiEditor').executeCommand('insertText', button.getAttribute('data-rte-emoji'));
});
return { open: open };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/InsertEmoticons.cshtml; it draws what ViewData holds.
public ActionResult InsertEmoticons()
{
ViewData["RTEValue"] = "<p>Great work on the release, everyone! </p>";
ViewData["Tools"] = new List<string> { "Bold", "Italic", "Underline", "|", "Emoticons", "|", "Undo", "Redo" };
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The Emoji Picker feature allows you to insert an emoji into an editor. The emoji picker has a grid of emojis grouped by categories, such as smileys, animals, food, and travel. You are able to scroll through the emojis and click on one to insert it into the editor.
In this demo, to enable this feature, configure the EmojiPicker toolbar item in the ToolbarSettings property.
You can also insert an emoji by pressing the colon (:) which will open the emoji picker. Choose an emoji from the picker and insert it into the editor. The feature also provides the option to filter emojis based on the typing character.